Retry flaky tools
You want an agent route to retry transient execution failures automatically. Here's how.
The code
import { agent } from "@dawn-ai/sdk"
export default agent({
model: "gpt-4o-mini",
retry: { maxAttempts: 5, baseDelay: 500 },
systemPrompt: "Summarize the document the user provides in three bullet points.",
})Notes
- Per-route, not global. Each
agent()declares its ownretry. Different routes can have different policies. - Transient errors only. Rate limits (
429), server errors (500/502/503), network timeouts, and OpenAIoverloaded/server_errorare retried. Invalid API keys, missing models, and schema validation errors fail immediately. - Exponential backoff with jitter, capped at 10s.
delay = min(baseDelay * 2^n + jitter, 10s). LoweringbaseDelaymakes the first retry faster. maxAttempts: 1disables retry. The default is3ifretryis omitted entirely.- Streaming routes only retry before the first token or event. Once content has streamed, the response is committed.