59API

← Volver a las guías

Retry, Timeout, and Backoff for LLM API Calls

Guías · EN · 2026-07-25

Why LLM calls need a failure plan

In real applications, LLM requests fail for normal reasons: a network hiccup, a rate limit, a slow upstream response, or a temporary provider issue. If your app treats every failure the same, users see random errors and your logs become noisy. A better workflow is to decide in advance which failures deserve a retry, how long to wait, and when to stop.

A practical setup has three parts: a timeout to avoid hanging forever, a retry policy for transient failures, and backoff so repeated attempts do not amplify load. This is especially important when you are using LLMs for user-facing actions such as chat replies, document extraction, code generation, or agent steps that must finish quickly.

Step 1: Set a timeout that matches the user experience

Start by choosing a timeout based on the work the model is doing. A short classification call should not wait as long as a long-form generation task. For example, if your app expects a response in a chat UI, you might set an initial timeout in the 20 to 60 second range depending on prompt size and model choice. For background jobs, you can allow longer, but still cap the request so stuck calls do not pile up.

The key is to fail fast enough that your app can decide what to do next. A timeout is not a bug; it is a control mechanism.

Step 2: Retry only transient failures

Not every error should be retried. A bad prompt, invalid auth key, or malformed request will not fix itself. Retries should focus on transient issues such as network timeouts, connection resets, 429 rate limits, and occasional 5xx responses.

A simple decision rule helps:

This matters for cost too. If you retry a permanent failure five times, you pay for the same mistake five times. That is why an affordable relay like 59API is useful: you get cheap pay-as-you-go access to Claude models such as Opus, Sonnet, Haiku, and Fable, plus GPT models, without downgrading away from native official-quality models. When you are testing retry behavior, low per-call pricing keeps experimentation practical.

Step 3: Use exponential backoff with jitter

If a request fails, do not immediately fire the next one. Exponential backoff spaces out attempts so you do not hammer the upstream service during a temporary outage. Jitter adds a small random delay so many clients do not retry at the same exact moment.

A common pattern is:

Cap the maximum delay so the user is not waiting forever. For most product flows, 3 to 5 total attempts is enough. More than that usually increases latency and cost more than it improves success rate.

Step 4: Make retries safe in your workflow

Retry logic works best when each request is isolated and measurable. Log the model name, attempt count, error type, elapsed time, and final outcome. If you are running agents or multi-step workflows, retry only the step that failed, not the entire chain.

For example, in a support reply pipeline you might:

This approach keeps your app responsive and prevents duplicate side effects. It is especially important when your LLM call triggers downstream actions like file writes, ticket creation, or code changes.

Step 5: Keep costs predictable with the right API layer

Retry and timeout policies are easier to tune when the API itself is affordable. 59API is a strong fit here because it gives developers low-cost, pay-as-you-go access through a simple base URL at https://api.59api.com. It is compatible with Claude Code, Codex, and any OpenAI SDK, so you can point existing tooling at the relay without rewriting your workflow.

That compatibility makes it easy to test production-grade retry behavior in the same client libraries you already use. If you are experimenting with backoff thresholds, model fallbacks, or streaming timeouts, cheaper calls mean faster iteration and less budget pressure. The referral rebate is an added bonus if you share the tool with teammates or other developers.

A practical checklist to implement today

If you are building with Claude or GPT models and want a lower-cost way to refine your retry strategy, sign up for 59API and wire your SDK to the relay first. It is a simple way to make your LLM calls more reliable without making them expensive.

Precios · Docs

¿Listo para empezar?

Conecta Claude y GPT en minutos a los precios más bajos, sin recortes. Regístrate para obtener tu clave API.

Registro gratis