Durable and Asynchronous Agent Work › Retries, backoff, and retry budgets
Language AI HandbookAll chapters The worker loop does not call this retry helper. The standalone helper retries only exceptions explicitly included in its retriable tuple, which defaults to TransientError.
- Retry a transient failure only when it is safe to repeat the operation. A permanent failure ends the attempt. If the effect’s outcome is unknown, first reconcile it or use the receiver’s valid idempotency contract.
- Cap the nominal exponential delay before multiplying it by jitter.
- Bound both the number of attempts and overall waiting time in the service policy.
The budget ensures a task whose tool keeps returning 503 doesn't consume unbounded compute. The same reasoning underlies circuit breakers in serving (LLM Inference Serving). Here's a tiny policy object.
PythonIn [28]Expand
retry_rng = random.Random(11)
def backoff(n: int, base: float = 0.1, cap: float = 5.0) -> float:
return min(cap, base * (2**n)) * retry_rng.uniform(0.5, 1.5)
class TransientError(Exception):
pass
def with_retries(
fn, max_attempts: int = 3, retriable=(TransientError,), *, sleep=time.sleep
):
if max_attempts < 1:
raise ValueError("max_attempts must be positive")
for attempt in range(max_attempts):
try:
return fn()
except retriable:
if attempt + 1 == max_attempts:
raise
sleep(backoff(attempt))The helper counts calls including the first. The separate durable budget counts claim attempts. Claiming checks the persisted number while holding the database write transaction and fails an exhausted task without giving it another lease.
PythonIn [29]Expand
tid3 = submit("draft_report", {"topic": "budget demo"})
with tx() as con:
con.execute(
"UPDATE tasks SET attempts = ? WHERE id=?", (MAX_TASK_ATTEMPTS, tid3)
)
claimed = claim_task("A")
budget_events = read_events(tid3)
assert claimed is None
assert status_of(tid3)["attempts"] == MAX_TASK_ATTEMPTS
assert budget_events[-1]["payload"]["reason"] == "attempt_budget"ConsoleOut [30]Expand
claimed or skipped: None status: failed
The demo injects a task whose prior attempt count is already at the limit. It remains at that count, logs the reason, and never reaches a worker.
The reason code identifies the rejected task for inspection.
There is a subtlety in where the budget is enforced that is easy to miss. If the budget were checked inside run_task, a poison task would still be claimed, still have its lease written, still renew once, before the check fired, and in some designs still execute one more failing step. By checking at claim time and immediately transitioning to failed, we spend essentially nothing on a task we've already decided to abandon. We also leave a clean task_failed event explaining why, which is what an on-call engineer needs to triage. A retry budget is only useful if exhausting it produces a diagnosable outcome, not a silent black hole.
Comments
Add to the conversation
A question, a correction, or an example from your own work.
No comments yet. Be the first to share your thoughts!