Errors and Debugging
For errors, OpenRouter returns a JSON response with the following shape:
The HTTP Response will have the same status code as error.code, forming a request error if:
- Your original request is invalid
- Your API key/account is out of credits
Otherwise, the returned HTTP response status will be 200 and any error occurred while the LLM is producing the output will be emitted in the response body or as an SSE data event.
Example code for printing errors in JavaScript:
Error Codes
- 400: Bad Request (invalid or missing params, CORS)
- 401: Invalid credentials (OAuth session expired, disabled/invalid API key)
- 402: Your account or API key has insufficient credits. Add more credits and retry the request.
- 403: Your chosen model requires moderation and your input was flagged
- 408: Your request timed out
- 429: You are being rate limited
- 502: Your chosen model is down or we received an invalid response from it
- 503: There is no available model provider that meets your routing requirements
Moderation Errors
If your input was flagged, the error.metadata will contain information about the issue. The shape of the metadata is as follows:
Provider Errors
If the model provider encounters an error, the error.metadata will contain information about the issue. The shape of the metadata is as follows:
When No Content is Generated
Occasionally, the model may not generate any content. This typically occurs when:
- The model is warming up from a cold start
- The system is scaling up to handle more requests
Warm-up times usually range from a few seconds to a few minutes, depending on the model and provider.
If you encounter persistent no-content issues, consider implementing a simple retry mechanism or trying again with a different provider or model that has more recent activity.
Additionally, be aware that in some cases, you may still be charged for the prompt processing cost by the upstream provider, even if no content is generated.
Streaming Error Formats
When using streaming mode (stream: true), errors are handled differently depending on when they occur:
Pre-Stream Errors
Errors that occur before any tokens are sent follow the standard error format above, with appropriate HTTP status codes.
Mid-Stream Errors
Errors that occur after streaming has begun are sent as Server-Sent Events (SSE) with a unified structure that includes both the error details and a completion choice:
Example SSE data:
Key characteristics:
- The error appears at the top level alongside standard response fields
- A
choicesarray is included withfinish_reason: "error"to properly terminate the stream - The HTTP status remains 200 OK since headers were already sent
- The stream is terminated after this event
OpenAI Responses API Error Events
The OpenAI Responses API (/api/alpha/responses) uses specific event types for streaming errors:
Error Event Types
-
response.failed- Official failure event -
response.error- Error during response generation -
error- Plain error event (undocumented but sent by OpenAI)
Error Code Transformations
The Responses API transforms certain error codes into successful completions with specific finish reasons:
This allows for graceful handling of limit-based errors without treating them as failures.
API-Specific Error Handling
Different OpenRouter API endpoints handle errors in distinct ways:
OpenAI Chat Completions API (/api/v1/chat/completions)
- No tokens sent: Returns standalone
ErrorResponse - Some tokens sent: Embeds error information within the
choicesarray of the final response - Streaming: Errors sent as SSE events with top-level error field
OpenAI Responses API (/api/alpha/responses)
- Error transformations: Certain errors become successful responses with appropriate finish reasons
- Streaming events: Uses typed events (
response.failed,response.error,error) - Graceful degradation: Handles provider-specific errors with fallback behavior
Error Response Type Definitions
Debugging
OpenRouter provides a debug option that allows you to inspect the exact request body that was sent to the upstream provider. This is useful for understanding how OpenRouter transforms your request parameters to work with different providers.
Debug Option Shape
The debug option is an object with the following shape:
Usage
To enable debug output, include the debug parameter in your request:
Debug Response Format
When debug.echo_upstream_body is set to true, OpenRouter will send a debug chunk as the first chunk in the streaming response. This chunk will have an empty choices array and include a debug field containing the transformed request body:
Important Notes
Streaming Chat Completions Only
The debug option only works with streaming mode (stream: true) for the Chat Completions API. Non-streaming requests and Responses API requests will ignore the debug parameter.
Not for Production
The debug flag should not be used in production environments. It is intended for development and debugging purposes only, as it may potentially return sensitive information included in the request that was not intended to be visible elsewhere.
Use Cases
The debug output is particularly useful for:
-
Understanding Parameter Transformations: See how OpenRouter maps your parameters to provider-specific formats (e.g., how
max_tokensis set, howtemperatureis handled). -
Verifying Message Formatting: Check how OpenRouter combines and formats your messages for different providers (e.g., how system messages are concatenated, how user messages are merged).
-
Checking Applied Defaults: See what default values OpenRouter applies when parameters are not specified in your request.
-
Debugging Provider Fallbacks: When using provider fallbacks, a debug chunk will be sent for each attempted provider, allowing you to see which providers were tried and what parameters were sent to each.
Privacy and Redaction
OpenRouter will make a best effort to automatically redact potentially sensitive or noisy data from debug output. Remember that the debug option is not intended for production.