Skip to main content

Overview

This guide covers all possible errors you may encounter when using the SundayPyjamas AI Suite API, along with best practices for handling them gracefully in your applications.
All API errors follow a consistent JSON format with human-readable error messages and appropriate HTTP status codes.

Error Response Format

Standard Error Format

All API errors return a consistent JSON structure:

Enhanced Error Format

Some errors may include additional fields for better debugging:

HTTP Status Codes

400 Bad Request

Invalid request format or parameters.
Cause: Request body doesn’t include a messages arraySolution: Ensure your request includes a valid messages array
Cause: Message missing required content field or empty contentSolution: Ensure all messages have valid role and content fields
Cause: Malformed JSON or invalid Content-TypeSolution: Ensure proper JSON formatting and Content-Type header

401 Unauthorized

Authentication issues with your API key.
Common Causes:
  • API key doesn’t exist or has been deleted
  • API key format is incorrect
  • API key has been deactivated
Solutions:
  • Verify your API key is correct and active
  • Check the key format: spj_ai_[64-character-string]
  • Generate a new API key if needed
Cause: Missing or malformed Authorization headerSolution: Include proper Bearer token authorization

402 Payment Required

Returned by credit-metered endpoints — Agents, Artifacts, Image, and Apps chat — when your workspace’s credit balance is insufficient. This is separate from the legacy token-limit model used by the base Chat API; see Rate Limits for how the two relate.
Cause: Workspace credit balance is too low to cover the request (checked up front, and again mid-stream for long-running generations).Solution: Top up credits from workspace billing settings, or catch code: "INSUFFICIENT_CREDITS" and prompt the user to do so.

403 Forbidden

Permission or limit issues.
Cause: Workspace has exceeded monthly token quotaSolutions:
  • Wait for monthly reset
  • Upgrade subscription plan
  • Optimize prompts to use fewer tokens
Cause: User doesn’t have required role (owner/admin) for API key managementSolution: Contact workspace owner to grant appropriate permissions

404 Not Found

Resource doesn’t exist.
Cause: Attempting to delete or access a non-existent API keySolution: Verify the API key ID is correct
Cause: Invalid API endpoint URLSolution: Check the API documentation for correct endpoints

429 Too Many Requests

Rate limiting applied.
Cause: Making requests too quickly Solution: Implement exponential backoff and retry logic

500 Internal Server Error

Server-side issues.
Causes:
  • AI model temporarily unavailable
  • Server overload
  • Temporary service disruption
Solution: Implement retry logic with exponential backoff
Cause: AI service configuration issuesSolution: Retry the request; contact support if persistent

502 Bad Gateway

Cause: Upstream AI service is down Solution: Retry with exponential backoff

503 Service Unavailable

Cause: Scheduled maintenance or high load Solution: Wait and retry; check status page

Error Handling Patterns

Basic Error Handling

Comprehensive Error Handling

Error Recovery Strategies

Graceful Degradation

Circuit Breaker Pattern

Retry with Jitter

Debugging Tips

Enable Detailed Logging

Test Error Scenarios

Error Monitoring

Best Practices Summary

Always Handle Errors

Implement comprehensive error handling for all API calls

Use Exponential Backoff

Retry with increasing delays for transient errors

Graceful Degradation

Provide fallback responses when the API is unavailable

Monitor Error Patterns

Track error frequencies to identify and fix issues

Validate Inputs

Validate requests before sending to avoid 400 errors

Secure API Keys

Protect API keys and handle auth errors appropriately

Log for Debugging

Implement detailed logging for troubleshooting

Circuit Breaker

Use circuit breakers to prevent cascade failures

Next Steps

Authentication

Learn about API key management and security

Rate Limits

Understand usage limits and optimization

Chat API

Complete API documentation with examples

Code Examples

See robust implementations with error handling