Content

What Is a Retry Policy, and Why Does Your AI Agent Need One?

Not every failed API call should be retried, and not every retry should happen the same way. Here's what a retry policy actually decides, in plain English, and how Swytchcode configures one per API.

EngineeringNov 4, 2026

Key takeaways

  • -A retry policy is the set of rules deciding whether a failed call gets retried at all, how long to wait between attempts, and when to stop — "retry on failure" by itself isn't a policy.
  • -Some errors should never be retried (a 400 with a malformed request will fail the same way every time) and some should always back off instead of retrying immediately (a 429 rate limit).
  • -Swytchcode's default retry policy caps attempts at 3, starts the wait at 500ms and backs off up to 30 seconds, and only retries specific error codes — 429, 503, and 504 — while treating others as final.
  • -The policy is set per integration in that integration's manifest, not as one fixed setting for every API an agent calls, because a slow analytics API and a payment API shouldn't be retried the same way.
  • -A retry policy decides whether to try again. Idempotency decides whether trying again is safe. An agent needs both, and they're configured separately.

An agent calls an API and gets back an error. What should happen next? "Try again" sounds like the obvious answer, but it's the wrong answer more often than people assume. A malformed request fails for the same reason every single time you send it — retrying it just burns time arriving at the same error. A rate-limit response is the opposite problem: retrying immediately usually makes it worse, not better, because you're hitting the same limit again before it's had a chance to reset.

A retry policy is the answer to all of that, written down as rules instead of left to whatever an agent happens to do in the moment.

"Retry on failure" isn't a policy

A real retry policy has to answer at least four separate questions: which errors are even worth retrying, how many times to try before giving up, how long to wait between each attempt, and whether that wait should grow as the failures continue. Skip any one of those and you get a predictable failure mode — an agent that retries a permanently broken request forever, or one that hammers a rate-limited endpoint faster than the rate limit can clear.

The split that matters most is between errors that are temporary and errors that are permanent. A 503 usually means the server is overloaded right now and might be fine in a few seconds — worth retrying. A 404 means the thing you asked for doesn't exist — it will still not exist on the next attempt, and the one after that. Treating those two the same way is where a lot of retry logic quietly goes wrong.

How Swytchcode's default retry policy is built

Swytchcode's execution policy caps retries at 3 attempts by default, and backs off between them — starting at a 500ms wait and growing up to a 30-second ceiling, rather than hammering the endpoint at a fixed interval. Only specific error codes are treated as worth retrying: 429 (rate limited), 503 (service unavailable), and 504 (gateway timeout). Everything in the 400 and 401/403/404/422 range is treated as final — the call failed for a reason that won't change by asking again, so it's surfaced immediately instead of silently retried into the same wall.

A 401 gets its own handling, because "unauthorized" can mean two different things: a credential that's permanently invalid, or a token that simply expired and needs refreshing. The default behavior is to fail rather than guess, with the option to switch an integration to refresh-and-retry once you know that provider's tokens actually expire and refresh cleanly.

Why this is set per API, not once for everything

A single global retry policy sounds simpler, but it's wrong for most of the APIs an agent actually touches. A read against an analytics API can usually afford a generous retry budget — nothing bad happens if it takes four tries and twenty seconds. A write against a payments API often can't, especially once you factor in whether that specific write is idempotent. So the policy lives in each integration's own configuration, not as one setting applied uniformly to every API Swytchcode connects to.

Seeing an integration's retry policy

swy get <integration>
swy info <integration>.<method>

swy info on a specific method shows the retry behavior that applies to that exact call — not a generic statement about the whole integration. That's the number worth reading before you assume an agent will "just handle it" if a call fails midway through a task.

A retry policy isn't the same thing as idempotency

These two get treated as interchangeable constantly, and they're not. A retry policy decides whether to try a failed call again, and on what schedule. Idempotency decides whether trying it again is actually safe — whether a second attempt at a write produces a second result or just returns the first one. You can have a perfectly sensible retry policy wrapped around a call that isn't idempotent, and the result is a textbook duplicate: a second charge, a second email, a second record, all because the retry logic did exactly what it was told to do.

That's why Swytchcode checks both before it retries a write — not just whether the error code is one worth retrying, but whether an idempotency key is actually attached to that call. A retry policy without that check is a policy for making the same mistake faster.

FAQ

Can I build an agent with Swytchcode that uses a different retry policy for different APIs?

Yes — that's the default shape of it, not a special case. Each integration carries its own policy in its manifest, so a read-heavy analytics API and a payments API can have genuinely different retry behavior without you writing separate retry logic for each one.

Can I add a policy that holds a call for human approval instead of retrying it automatically?

Yes. A retry policy governs what happens on failure; a separate policy layer can require a human to approve a call before it runs at all, which is a stronger control than any retry setting for the calls that genuinely warrant one.

Does Swytchcode retry a write the same way it retries a read?

No. A read is retried on the retryable error codes regardless, because reading the same thing twice doesn't change anything. A write only gets retried if it also carries an idempotency key — otherwise the retry is skipped and the failure is surfaced, since retrying an unsafe write is worse than stopping.

What happens after the retry policy's attempts run out?

The call fails and that failure is logged and surfaced rather than silently swallowed. An agent that's exhausted its retries has hit something that backoff alone won't fix — at that point it needs to be visible, not quietly retried a fourth or fifth time.

More content