Lesson 3 of 6 · 30 min

Idempotent Agent Runs: Retries, Timeouts and Long Jobs

Design runs that are safe to retry and do not hold connections open.

XDQ Tutor · AI guide

Hello. In this lesson I will take you through 5 ideas, one at a time.

  1. Why retries happen
  2. A simple idempotency store
  3. Timeouts and long work
  4. Failing well
  5. Common retry mistakes

Why it matters: Design runs that are safe to retry and do not hold connections open. After each idea I will ask you one quick question.

Why retries happen

The short version: Platforms retry when they do not get a success response in time.

Platforms retry when they do not get a success response in time. Stripe retries live events for up to three days with exponential backoff, and the Standard Webhooks specification suggests a multi-day schedule with jitter. If your agent charges, writes or sends something on each call, a retry can do it twice.

Make every run idempotent: the same run ID always produces the same stored result and side effects happen once.

Quick check

What makes a run idempotent?

A simple idempotency store

The short version: Before doing work, insert the run ID into a table with a unique constraint and a status of running.

Before doing work, insert the run ID into a table with a unique constraint and a status of running. If the insert fails because the ID exists, return the stored result or a still-running status instead of starting again. When the run finishes, store the output against the ID.

Expire old IDs after a period longer than the sender's retry window.

typescript · illustrative
// illustrative
const row = await db.insertOrGet("runs", { id: runId, status: "running" });
if (!row.inserted) return row.status === "done" ? row.output : { status: "running" };
const output = await runAgent(input);
await db.update("runs", runId, { status: "done", output });
return output;
Quick check

What should an endpoint do with a job longer than the timeout?

Timeouts and long work

The short version: Standard Webhooks suggests request timeouts of roughly 15 to 30 seconds.

Standard Webhooks suggests request timeouts of roughly 15 to 30 seconds. Many agent runs take longer. For those, acknowledge quickly with an accepted status and the run ID, do the work in a queue, and let the platform collect the result or receive a callback.

Keep responses small; the same specification suggests payloads under about 20 KB, with large files linked rather than embedded.

Quick check

Which failure should not be retried?

Failing well

The short version: Distinguish failures the platform should retry, such as a temporary model error, from ones it should not, such as invalid input.

Distinguish failures the platform should retry, such as a temporary model error, from ones it should not, such as invalid input. Return a clear status and message for each. Refund or do not charge for runs that fail on your side.

Log run ID, duration, model cost and outcome for every run. These numbers drive pricing in a later lesson.

Common retry mistakes

The short version: The classic mistake is doing the side effect before recording the run.

The classic mistake is doing the side effect before recording the run. If the process crashes after sending an email but before saving the run ID, the retry sends the email again. Record the run as started first, inside the same transaction as any charge, then do the work. The second mistake is using the timestamp or a hash of the body as the idempotency key. Retries may carry a new timestamp, and two different runs can have identical bodies; use the run or message ID the sender provides.

The third mistake is treating every error as retryable. Returning a server error for invalid input makes the platform retry for days, wasting both sides' resources and money. Return a client error for bad input and a server error only for temporary problems. Finally, watch for slow retries piling up after an outage, and make sure your queue can drain them without hitting model rate limits.

Key takeaways
  • Assume every request may be retried
  • Use the run ID as an idempotency key with a unique constraint
  • Acknowledge long jobs quickly and work in a queue
  • Separate retryable from non-retryable failures
  • Log cost and duration per run
Try it

Add an idempotency table to your endpoint and test that sending the same run ID three times produces one result and one side effect. Add an accepted path for runs longer than 15 seconds.

That is the lesson.
Mark it complete to track your progress on this device.
← PreviousVerify Signed Requests With HMAC and TimestampsNext lesson →Validate Agent Output and Defend Against Prompt Injection