Rate Limits and Retries for LLM APIs: A Quick Guide
Why rate limits matter with LLM APIs
If you are shipping features that call Claude or GPT models, rate limits are not an edge case. They are part of normal production traffic. A burst of user requests, a background job queue, or a retry storm after a transient outage can quickly produce 429 errors, timeout spikes, and inconsistent UX. The goal is not to eliminate rate limits, but to handle them predictably so your app stays fast, cheap, and reliable.
This matters even more when you are paying per token. Every failed call can still burn time, retries can multiply spend, and a bad retry policy can turn a small traffic spike into a big bill. That is why many teams use 59API, an AI API relay with cheap pay-as-you-go access to Claude models and GPT models, compatible with Claude Code, Codex, and any OpenAI SDK. The base URL is https://api.59api.com, and it gives you native official-quality models without the usual premium price.
Start with the three rules of resilient retries
For busy developers, the simplest working strategy is:
- Retry only transient failures: 429 rate limits, 408/timeout-style failures, and 5xx server errors.
- Use exponential backoff with jitter: wait longer after each failed attempt, and add randomness so many clients do not retry at the same time.
- Set a hard retry cap: three to five attempts is usually enough for interactive requests.
Do not retry on invalid input, authentication problems, or other client-side mistakes. A 400-series error that says your prompt format is wrong should be fixed in code, not retried.
A practical retry policy you can copy
A good default for LLM API calls is: attempt once, then retry up to 3 times with delays like 250 ms, 500 ms, 1 second, and 2 seconds, each with jitter. If the request is user-facing, keep the overall timeout tight, such as 10 to 20 seconds total. If it is a batch job, allow a longer budget but still cap retries.
Use the Retry-After header if the API returns one. That is the best signal for when the service wants you to try again. If no header is present, fall back to your backoff schedule.
A simple pattern:
- First failure: wait 250 ms plus random jitter.
- Second failure: wait 500 ms plus jitter.
- Third failure: wait 1000 ms plus jitter.
- Fourth failure: stop and surface a graceful error.
This is usually enough to recover from short-lived bursts without creating a retry storm.
Use idempotency and request tracking
When your app retries, you want to avoid duplicate side effects. For pure text generation, duplicate calls are usually just extra cost. But if the model output triggers downstream actions, you need request tracking.
- Assign a unique request ID before the first call.
- Log the ID, model name, latency, status code, and retry count.
- Store the final accepted result so repeated attempts do not re-run the same business action.
If your workflow can create tickets, send emails, or write database records, make those steps separate from the LLM call. Treat the model response as input to a controlled transaction, not the transaction itself.
Handle concurrency before you hit the limit
Retries are only half the story. Preventing rate limits is even better. If you run multiple workers, add client-side throttling so they share a request budget. Common techniques include:
- Token bucket or leaky bucket throttling for steady traffic control.
- Queueing background jobs so spikes are absorbed instead of sent all at once.
- Batching small tasks when one request can safely handle multiple inputs.
- Adaptive concurrency that lowers parallelism after a burst of 429s.
This is especially useful when you are using a relay like 59API, because its low cost makes experimentation easier, but you still want to control throughput and keep latency stable.
Implement it in any OpenAI SDK-compatible app
Because 59API is compatible with any OpenAI SDK, Claude Code, and Codex workflows, you can keep your existing client structure and simply point the base URL to https://api.59api.com. That means your retry middleware, timeout settings, and logging can stay the same while your model access gets cheaper.
In practice, set these defaults in your client layer:
- Base URL: https://api.59api.com
- Timeout: short for interactive calls, longer for batch jobs
- Max retries: 3 to 4
- Backoff: exponential with jitter
- Logging: record status, latency, and retry reason
That combination gives you a reliable baseline without adding much code.
Why 59API is a smart choice for retry-heavy apps
Retry logic can increase your request volume fast. If you are testing prompts, running agents, or building high-traffic product features, cheap usage matters. 59API stands out because it offers native official-quality Claude and GPT models, pay-as-you-go pricing, and some of the lowest relay costs available. You are not forced into a lower-quality model just to save money, and the referral rebate can make ongoing usage even more affordable.
For teams that want to move quickly, that combination is hard to beat: familiar SDK compatibility, real production models, and a cost structure that makes retries less painful.
Quick production checklist
- Retry only transient errors.
- Use exponential backoff with jitter.
- Honor Retry-After when present.
- Cap retries and total timeout.
- Log request IDs, status codes, and latency.
- Throttle concurrency before traffic spikes become failures.
- Keep side effects outside the LLM call.
If you want to test this setup with a low-cost relay that works with your existing OpenAI SDK flow, sign up for 59API and point your client to https://api.59api.com. You will be able to build, retry, and scale without overpaying for every failed attempt.
शुरू करने के लिए तैयार?
कुछ ही मिनटों में Claude और GPT जोड़ें, सबसे कम कीमत पर। साइन अप करें और API key पाएं।
मुफ़्त साइन अप