Skip to content

Error Handling

All errors from the gateway follow a consistent envelope compatible with the OpenAI client libraries:

{
"error": {
"message": "Invalid authentication credentials.",
"type": "authentication_error",
"code": "invalid_api_key"
}
}
FieldTypeDescription
error.messagestringHuman-readable description of what went wrong
error.typestringMachine-readable error category (see below)
error.codestring | nullOptional fine-grained error code within the type
TypeHTTP StatusDescription
invalid_request_error400The request body is malformed, missing required fields, or contains invalid values
authentication_error401The Bearer token is missing, invalid, or has been revoked
rate_limit_error429The request was rejected due to IP or provider rate limits
provider_error502The upstream provider returned an error or an unexpected response
not_found_error404The requested endpoint or resource does not exist
service_unavailable503No healthy provider is available to serve the requested model

Every response — including errors — includes the header:

x-gateway-request-id: req_01j9xkz3p4q5r6s7t8u9v0w1

Include this ID when reporting issues. It uniquely identifies the request in the gateway’s logs and can be cross-referenced with Request Logs.

{
"error": {
"message": "Invalid authentication credentials.",
"type": "authentication_error",
"code": "invalid_api_key"
}
}

Fix: Ensure your Authorization header is set to Bearer <your-token> and the token has not been revoked.

{
"error": {
"message": "Model 'gpt-99' is not supported by this gateway.",
"type": "not_found_error",
"code": null
}
}

Fix: Check the Health endpoint for the list of currently configured models.

{
"error": {
"message": "The upstream provider returned an error: upstream connect error or disconnect/reset before headers.",
"type": "provider_error",
"code": null
}
}

Fix: Retry with exponential backoff. The gateway will have already attempted its configured retry count before surfacing this error. Check Health for provider status.

{
"error": {
"message": "No healthy provider available for model 'llama-3.3-70b-versatile'. Please try again shortly.",
"type": "service_unavailable",
"code": null
}
}

Fix: Wait for providers to exit cooldown (typically seconds to minutes) and retry.

Terminal window
RESPONSE=$(curl -s -w "\n%{http_code}" \
https://your-gateway.workers.dev/v1/chat/completions \
-H "Authorization: Bearer <your-token>" \
-H "Content-Type: application/json" \
-d '{"model":"gpt-4o","messages":[{"role":"user","content":"Hello"}]}')
HTTP_STATUS=$(echo "$RESPONSE" | tail -n1)
BODY=$(echo "$RESPONSE" | head -n -1)
if [ "$HTTP_STATUS" -ne 200 ]; then
echo "Error $HTTP_STATUS: $(echo "$BODY" | jq -r '.error.message')"
fi