Debugging 401, 402, 429 and 5xx API Failures
Start with one reproducible API request
When an AI API request fails, avoid changing your model, key, prompt, and SDK settings at the same time. First, create the smallest possible request that uses one known model, a short prompt, and the exact environment where the failure occurs. Record the HTTP status, response body, response headers, timestamp, model name, and any request ID returned by the provider.
For 59API, confirm that your application is pointed at https://api.59api.com and that your SDK is using the correct integration format. 59API is compatible with Claude Code, Codex, and OpenAI SDK workflows, but an OpenAI-style request sent to an Anthropic-style route, or the reverse, can produce confusing authentication or validation symptoms. Copy the current model ID from your account documentation rather than guessing it.
- Test with a fresh API key stored in an environment variable.
- Verify the deployed service uses the same environment variable name as local development.
- Log the status code and request ID, but never log the full API key or sensitive prompt content.
- Check whether the error occurs for every model or only one selected model.
Fix 401 errors: authentication and configuration
A 401 Unauthorized response means the server could not authenticate the request. The most common cause is a missing or malformed Authorization header. Ensure your client sends the expected Bearer token format, with no copied quotation marks, hidden newline characters, or whitespace around the key. A key that works locally but fails in production is often missing from the container, serverless function, CI secret store, or deployment environment.
Also check that you did not accidentally use a key from another provider, a revoked key, or a test variable that contains an empty value. If the response is immediate and no tokens are used, authentication likely failed before model inference began. Rotate compromised keys instead of trying to repair them, and update the secret in every deployment target.
Resolve 402 errors: balance, pricing, and limits
A 402 Payment Required response commonly indicates that the account cannot cover the requested work. Unlike 401, it is usually not a header problem. Read the response message carefully, then review your available balance, billing status, selected model, and requested output length. A high maximum token setting or a premium model can make an otherwise affordable request exceed an account limit.
59API is useful here because it offers low-cost, pay-as-you-go access to native official-quality Claude and GPT models without a model downgrade. Still, production systems should estimate cost before sending unusually large jobs. Set a sensible output cap, track spend by environment, and alert the team before the balance reaches zero. If a 402 appears unexpectedly, save the request ID and compare the requested model and token limits with your billing settings.
Handle 429 errors without making the outage worse
A 429 Too Many Requests response means your application has exceeded a temporary request, concurrency, quota, or safety limit. Do not immediately retry every failed request in parallel. That pattern creates a retry storm and keeps the service rate-limited longer.
- Honor the Retry-After header when it is present.
- Use exponential backoff with random jitter, such as progressively longer waits with a small random variation.
- Limit concurrent requests with a queue or semaphore.
- Deduplicate user actions so one button click cannot launch several identical generations.
- Separate interactive traffic from batch jobs and give user-facing requests priority.
Determine whether the 429 is temporary or tied to a recurring account quota. Temporary spikes benefit from backoff; predictable batch workloads need scheduling and concurrency controls. Track 429 rates per model and endpoint so you can identify whether a new release, background worker, or traffic surge caused the pressure.
Triage 5xx errors: retry safely and collect evidence
Status codes such as 500, 502, 503, and 504 generally indicate a server, gateway, upstream model, or network problem. These are often transient, but not every request should be replayed blindly. Retry short-lived 5xx failures a limited number of times with exponential backoff and a total deadline. If your platform supports idempotency keys, use them for operations that must not execute twice.
For generation requests, preserve a request fingerprint and check your application history before manually replaying a timed-out request. A timeout can happen after the upstream system has already started processing. If an error persists, capture the timestamp, model, status code, request ID, and a redacted payload summary for support. Do not include secrets or private user text in tickets.
Make API failures observable in production
Turn each response into structured telemetry: status family, model, latency, retry count, deployment version, and request ID. Create separate alerts for rising 401 rates, low-balance 402 events, sustained 429 rates, and elevated 5xx percentages. This classification prevents billing issues from being treated like infrastructure incidents.
Finally, add a user-friendly fallback message instead of exposing raw provider errors. A reliable integration plus predictable pay-as-you-go pricing makes 59API a strong option for production AI features, and its referral rebate can further reduce ongoing costs. If you are building or migrating an AI workflow, sign up for 59API and test your smallest production request before scaling traffic.
Prêt à commencer ?
Connectez Claude et GPT en quelques minutes aux prix les plus bas, sans bridage. Inscrivez-vous pour votre clé API.
Inscription gratuite