0Pricing
AI Agents · Lesson

Persisting Graph State (Checkpoints)

Save state to Postgres or SQLite so a long-running agent can be paused, resumed, and audited.

Persisting Graph State (Checkpoints) is a free AI Agents lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the AI Agents learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Persist State?

Long-running agents need to:

  • Pause and resume hours later
  • Recover from crashes mid-execution
  • Support human-in-the-loop (wait for approval)
  • Replay a run for debugging

LangGraph checkpointers persist the state at every node boundary.

In-Memory Checkpointer

For dev/testing:

from langgraph.checkpoint.memory import MemorySaver

checkpointer = MemorySaver()
app = graph.compile(checkpointer=checkpointer)

Postgres Checkpointer

For production:

from langgraph.checkpoint.postgres import PostgresSaver

checkpointer = PostgresSaver.from_conn_string('postgresql://...')
await checkpointer.setup()   # create tables
app = graph.compile(checkpointer=checkpointer)

Sqlite Checkpointer

Local persistence without a DB server:

from langgraph.checkpoint.sqlite import SqliteSaver
import sqlite3

conn = sqlite3.connect('agents.db', check_same_thread=False)
checkpointer = SqliteSaver(conn)

Thread IDs

Every run has a thread_id — like a session ID. Multiple users get separate thread_ids; resuming a run means using the same id:

config = {'configurable': {'thread_id': 'user-42-conversation-1'}}
result = app.invoke({'messages': [...]}, config)

Resuming a Run

Call invoke with the same thread_id and no input — the agent continues from where it stopped:

# Some hours later:
app.invoke(None, config)

Inspecting State

Read the current state of a thread:

snapshot = app.get_state(config)
print(snapshot.values)         # current state dict
print(snapshot.next)           # which node is next
print(snapshot.created_at)

State History

Walk back through every checkpoint:

for snapshot in app.get_state_history(config):
    print(snapshot.created_at, snapshot.values)

Forking a Run

You can update state at any past checkpoint and re-run from there — useful for "what if" debugging:

app.update_state(config, {'plan': 'a different plan'})
app.invoke(None, config)   # continue with the new plan

Human-in-the-Loop with interrupt_before

Pause execution before a sensitive node so a human can review:

app = graph.compile(
    checkpointer=checkpointer,
    interrupt_before=['send_email']
)

result = app.invoke({'messages': [...]}, config)
# Agent paused. Human inspects state.
# When approved:
app.invoke(None, config)

Resuming After Editing

The human can edit state before resuming:

app.update_state(config, {'email_draft': 'final version'})
app.invoke(None, config)

Cost-Tracking Across Checkpoints

Persisted state can include token/cost counters that survive crashes:

class State(TypedDict):
    cost_usd: Annotated[float, lambda a, b: a + b]

# Every LLM-calling node adds to it.

Cleanup

Old threads accumulate. Periodically delete completed ones to control storage:

await checkpointer.delete(thread_id='old-thread')

Resuming Threads

How do you resume an agent run after a crash?

Recap

Use SqliteSaver for local, PostgresSaver for production. Thread IDs separate runs. Combine with interrupt_before for human-in-the-loop workflows.

Frequently asked questions

Is the “Persisting Graph State (Checkpoints)” lesson free?

Yes — the full text of “Persisting Graph State (Checkpoints)” is free to read here on the web, and the AI Agents course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the AI Agents course, upgrade to CoddyKit PRO.

What will I learn in “Persisting Graph State (Checkpoints)”?

Save state to Postgres or SQLite so a long-running agent can be paused, resumed, and audited. You practise AI Agents with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start AI Agents?

No prior experience is required. AI Agents on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Persisting Graph State (Checkpoints)” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this AI Agents lesson?

Yes. Every AI Agents lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Why Graphs Beat Flat Chains
  2. Nodes, Edges and State
  3. Conditional Routing and Branching
  4. Persisting Graph State (Checkpoints)
← Back to AI Agents