Learn AI · Guides
Idempotency and Retries for LLM Workflows
How to retry failed LLM and tool calls without double-charging customers, sending duplicate emails, or corrupting agent state.
Key takeaways
- 1Classify every step as read-only or mutating before writing retry logic.
- 2Idempotency keys on mutating tools prevent duplicate charges, emails, and tickets.
- 3LLM timeouts are ambiguous — the model may have started; use request IDs and dedupe tables.
- 4LangGraph checkpoints let you resume mid-agent without re-running expensive steps.
- 5Log retry count and final outcome in observability — retry storms are a smell.
Read-only vs mutating steps
| Step type | Retry policy |
|---|---|
| LLM classify / extract | Retry freely with backoff |
| Vector search | Retry freely |
| HTTP GET | Retry with caution on large payloads |
| Send email | Idempotency key required |
| Charge card | Idempotency key required |
| Create ticket | Dedupe on business key (e.g. source event ID) |
| Delete record | Do not auto-retry — require human |
Idempotency key pattern
def charge_refund(order_id: str, idempotency_key: str, amount: cents):
existing = db.get_idempotent_result(idempotency_key)
if existing:
return existing
result = payment_api.refund(order_id, amount, idempotency_key)
db.store_idempotent_result(idempotency_key, result)
return result
Pass the same key from the agent tool call. Store in your DB, not just the payment provider.
LLM timeout ambiguity
When an HTTP call times out, the model may still have completed. Mitigations:
- Request ID — pass provider request ID; query status if supported
- Dedupe table — hash
(user_id, task_id, step)to stored output - Checkpoint before expensive calls — LangGraph or custom state
Retry backoff
attempt 1: immediate
attempt 2: 2s + jitter
attempt 3: 4s + jitter
attempt 4: 8s + jitter
cap at 60s, max 5 attempts on 429
Route to fallback model after 2 failures on primary.
Agent checkpointing
LangGraph persists state at node boundaries. On failure, resume from last checkpoint instead of re-running retrieval and prior tool calls.
For n8n/Zapier: use execution IDs and step-level error workflows; avoid blind full-rerun on partial success.
Observability
Log in Langfuse:
retry_countfailure_reason(timeout, 429, validation, tool error)idempotency_keyon mutating toolscheckpoint_idon agent resume
Retry storms often mean rate limits too tight or prompts too large — fix root cause, not just retry count.
Common misconceptions
The wrong-but-common takes worth correcting.
Myth
Retrying the whole agent run is always safe.
Reality
If step 3 sent an email and step 5 failed, a full retry sends twice. Checkpoint or make step 3 idempotent.
Myth
LLM outputs are deterministic enough to dedupe by prompt hash.
Reality
Temperature above zero means different outputs. Dedupe on business keys, not prompt text.
Real-world use cases
Support ticket classification
Read-only LLM step — safe to retry on timeout.
Open
Lead enrichment batch
Write results with row ID dedupe — reruns skip completed rows.
Open
Agent with payment tool
Idempotency key required on charge_refund tool.
Frequently asked questions
Related on AIKnowHub
Concept
Function Calling Explained
Function calling is how LLMs trigger your code. Here's how it actually works under the hood, and how to design tools that the model uses correctly.
Concept
Guardrails & Output Validation
LLMs don't come with guarantees. Guardrails and output validation are how you enforce safety, structure, and policy on probabilistic outputs before they reach users.
Concept
LLM Observability in Production
You can't debug what you can't see. LLM observability means tracing every prompt, token, latency spike, and failure — here's the production stack.
Directory
LangGraph
Stateful graph framework for building reliable multi-step AI agents with cycles, checkpoints, and human-in-the-loop.
Workflow
Build Support Ticket Triage + Draft Reply
An automation that classifies incoming tickets, routes them, pulls account context, and drafts replies your agents review before sending.
Tool Guide
n8n Guide
The definitive guide to n8n for AI workflows — self-host vs cloud, agent nodes, LLM chaining, code escape hatches, and when to pick it over Zapier or Make.