Retry Transient Model Calls
Providers fail now and then with rate limits and 503s. Let's make an agent route retry those, give a flaky tool its own retry loop, and turn retry off where failing fast matters:
import { agent } from "@b4run/sdk"
export default agent({
model: "gpt-5-mini",
retry: { maxAttempts: 5, baseDelay: 500 },
systemPrompt: "Summarize the document the user provides in three bullet points.",
})NotesCopy link to section: Notes
- Retry is per route. Each
agent()declares its ownretry, so different routes can have different policies. - Each model call retries on its own.
maxAttemptsis the attempts per model call, not per run. A failure in a later model call of the tool loop sends only that call again; tools that already ran don't run again. - Only temporary failures retry. Server errors (
5xx), network errors, timeouts and rate limits are retried. A quota429, an invalid API key, a missing model and a malformed request fail immediately. baseDelaypaces capacity rate limits. A429with noRetry-Afterwaitsmin(baseDelay * 2^n + jitter, 10s)before the next attempt. A429whoseRetry-Afteris 60 seconds or less is waited out by LangChain; one over 60 seconds isn't retried, and the run fails at once. LangChain paces the other retries with its own backoff, starting at 1 to 2 seconds.maxAttempts: 1disables retry. If you omitretry, the default is3.- A response that fails partway isn't retried. Retries happen before the model's response starts, so no token is ever sent twice. Once part of a response has streamed, an error comes through the stream.
- Tool retry lives in the tool. Agent retry covers model and provider calls. For a tool, write the loop yourself, as
fetch-doc.tsdoes above.