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 own retry. Different routes can have different policies.
  • Transient errors only. Rate limits (429), server errors (500/502/503), network timeouts, and OpenAI overloaded/server_error are 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). Lowering baseDelay makes the first retry faster.
  • maxAttempts: 1 disables retry. The default is 3 if retry is omitted entirely.
  • Streaming routes only retry before the first token or event. Once content has streamed, the response is committed.

Related