Lesson 3 of 9 · Components
State
Agent state explained: history, task state, notes and long-term memory. Keep state explicit and serialisable, save checkpoints and resume agents in Python.
- 5 min read
- Intermediate
- Reviewed
- Distinguish the kinds of state an agent keeps
- Keep task state explicit in a simple data structure
- Save checkpoints and resume an agent where it stopped
- Decide what state belongs where
What an agent knows
| Kind of state | Holds | Lives |
|---|---|---|
| Conversation | Messages, tool calls and results | In the prompt sent to the model |
| Task state | Goal, plan, steps done, status | In your program (and saved checkpoints) |
| Working notes | Intermediate results, facts found | Task state, or a scratchpad in the prompt |
| Long-term memory | Facts that outlast one task: preferences, past outcomes | A database or vector store |
A common mistake is to keep everything only in the conversation, hoping the model will track progress. It's more reliable to keep task state in code: a plan, a list of completed steps and a status, which the program reads and the prompt can summarise.
Checkpoints: surviving crashes and pauses
Agents run for minutes or hours, wait for people to approve actions, and call services that fail. If the state is saved after each step, the agent can resume instead of starting over.
import json
from dataclasses import asdict, dataclass, field
@dataclass
class TaskState:
goal: str
plan: list
done: list = field(default_factory=list)
notes: dict = field(default_factory=dict)
status: str = "running"
def save(state): # a checkpoint: plain JSON you can store anywhere
return json.dumps(asdict(state))
def load(text):
return TaskState(**json.loads(text))
def do_step(step, state):
results = {"collect invoices": "14 invoices", "total by supplier": "3 suppliers, 9,840 EUR",
"draft summary email": "draft saved"}
state.notes[step] = results[step]
state.done.append(step)
state = TaskState(goal="Monthly supplier report",
plan=["collect invoices", "total by supplier", "draft summary email"])
checkpoint = None
for step in state.plan:
if step == "draft summary email" and checkpoint and "crash" not in state.notes:
state.notes["crash"] = True
print("!! the process crashed before:", step)
break
do_step(step, state)
checkpoint = save(state)
print("done:", step, "| checkpoint saved")
# ---- Later, a new process resumes from the last checkpoint ----
resumed = load(checkpoint)
remaining = [s for s in resumed.plan if s not in resumed.done]
print("\nResumed. Already done:", resumed.done)
for step in remaining:
do_step(step, resumed)
print("done:", step)
resumed.status = "finished"
print("Notes:", resumed.notes)The state is a small dataclass, saved as JSON after each completed step. When the process crashed, the new process loaded the last checkpoint, saw that two steps were done, and ran only the remaining one. Nothing was repeated, which matters when a step sends an email or makes a payment.
Common mistakes
Resume an agent from a saved checkpoint: work out which steps are left.
import json
checkpoint = '{"goal": "Plan a team offsite", "plan": ["pick dates", "book venue", "order food", "send invites"], "done": ["pick dates", "book venue"]}'
# The agent restarted. Load the checkpoint and print the steps still to do, in order:
# Still to do: [...]
Why save an agent's state as a checkpoint after each completed step?
Summary
- Agent state covers the conversation, task state, working notes and long-term memory.
- Keep task state explicit in code: goal, plan, completed steps, status.
- Save checkpoints to resume after crashes or human approvals.
- Make state serialisable and versioned, and protect side effects with idempotency.
Next: writing prompts for agents.
Finished reading? Tick it off to track your progress.