State and Checkpoints
Persisting agent state so long jobs can pause, resume, and recover after failures.
In plain words
The system saves the agent's progress after each step so it can continue from there if something breaks or if it has to wait for a person.
Think of it like…
Saving a video game at each level: if the game crashes you reload the last save, not the start. But if reloading re-awards a one-time bonus, you've created a bug, which is why each action must be safe to repeat.
Where you’ve seen it
A data-migration agent processes 200 customer accounts, each needing several tool calls. State (the list of completed account ids, current account, step counter, messages) is checkpointed to Postgres after every account. A node crashes at account 137; the job restarts, loads the latest checkpoint, and continues at account 137, not 1. Each write tool takes an idempotency key (migrate-{account_id}), so the partially completed account 137 isn't migrated twice. Total re-work after the crash: one account instead of 136.
Words you’ll hear
- Checkpoint
- A durable snapshot of run state at a step boundary.
- Thread / run id
- The key that identifies a run's state for resume and inspection.
- Idempotency key
- A unique token ensuring repeating an operation has the same effect as doing it once.
- Durable execution
- Running workflows so progress survives process failures (Temporal, LangGraph with a checkpointer).
Visual diagram
Theory & deep dive
What to save and when
Save enough to reconstruct the run: conversation messages, structured working state (plan, completed ids, counters), pending tool calls, and metadata (thread id, step number, timestamps, state schema version, model and prompt versions). Don't store secrets in state; keep references to credentials and fetch them at run time. Large artefacts (documents, tool outputs) belong in object storage, referenced by key, to keep checkpoints small.
Checkpoint at natural boundaries: after each node or tool batch, before and after risky side effects, and when pausing for a human. Frameworks such as LangGraph do this automatically per step with a checkpointer; with a hand-written loop, write the state before waiting, and after each tool result. The same mechanism supports time travel: load an earlier checkpoint, optionally edit state, and re-run to debug or branch.
{"thread_id": "migrate-2026-11", "step": 137, "state_version": 3, "state": {"done": [1, 2, …, 136], "current": 137, "messages": […]}, "updated_at": "2026-11-20T10:42:07Z"}
Resuming safely: idempotency, versions and cleanup
When a run resumes after a crash, the last step may have partly executed: the tool call went out but the result wasn't saved. Design side effects so repeating them is harmless: pass idempotency keys to payment, email and database-write APIs, make writes upserts, check "already done?" before acting, and record intent before and completion after. Distinguish at-least-once delivery (steps may repeat; make them idempotent) from exactly-once (hard; usually emulated with idempotency).
Handle change over time. When you alter the state schema or the graph, persisted threads may no longer match: include a state version, write migrations or run old threads to completion on old code, and avoid renaming fields casually. Set retention and cleanup (TTL for finished threads, deletion for privacy requests), cap checkpoint size, and secure the store (contains user data). Add recovery behaviour for failures that aren't crashes: retry with backoff for transient errors, mark a step as failed after N attempts and route to a human, and keep dead-letter records for inspection. Test it: kill the process mid-run in tests and assert that resume produces the right final state with no duplicates.
payments.refund(order_id, amount, idempotency_key=f"refund-{run_id}-{step}") means a retry after a crash returns the original refund instead of creating a second one.
How it works, step by step
Step 1 of 41. Decide the state and boundaries
List what must be saved to continue, and choose checkpoint points such as after each tool batch.
- Small
- state; big data by reference
When to use it & trade-offs
What it is
Agent state is everything needed to continue a run: the messages, plan, intermediate results, step counters and pending actions. A checkpoint is a saved snapshot of that state, written after each step to durable storage (Postgres, Redis, a database table) under a run or thread id. With checkpoints, a long job can pause (waiting for approval or an external event), resume after a crash or deploy from the last saved step instead of starting over, and be inspected, replayed or forked for debugging. Because resumed steps may re-run, side effects must be idempotent.
Why it matters: Agent runs can take minutes or hours, call dozens of tools and cost real money. Without persistence, a timeout at step 18 of 20 wastes everything, a deploy kills in-flight jobs, and human approvals can't span hours. Checkpointing turns fragile in-memory loops into reliable workflows, but it brings new correctness issues: duplicated side effects, schema versioning and stale state.
When to reach for it
- Long-running agents and batch jobs
- Workflows waiting on human approval
- Debugging via replay and time travel
When to avoid or be careful
- Non-idempotent write tools in resumable runs
- Storing secrets or huge blobs inside checkpoints
- Changing state schemas with live persisted threads and no migration
Ways to survive failures
Strengths: Resume, replay and inspect; fits agent frameworks.
Watch out: Needs idempotent steps and schema versioning.
Strengths: Strong retry and durability semantics.
Watch out: Heavier infrastructure and learning curve.
Strengths: Simplest.
Watch out: Wastes work and money; unsafe with side effects.
Interview signals & numbers to know
When this topic comes up
- “The job died at step 18 of 20”
- “Approval takes hours; how does the agent wait?”
- “We double-charged after a retry”
- Idempotency keys
- Time travel debugging
Benchmarks & rules of thumb
- 137 of 200
- crash point in the migration example
- 1 vs 136
- accounts redone with vs without checkpoints
- at-least-once
- typical delivery guarantee to design for
Common mistakes & follow-ups
Common interview mistakes
- Resuming a run that re-sends an email because the send wasn't idempotent.
- Putting API keys into persisted state.
- Deploying a renamed state field and breaking every paused thread.
Likely interviewer follow-ups
- How would you make an agent resumable after a crash?
- Why must tools be idempotent in checkpointed workflows?
- How do you handle state schema changes?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply State and Checkpoints with generative AI interview Q&A and hands-on practice.