Sub topic 2
What state has to survive
This chapter assumes you can read basic Python, JSON, and Markdown. It also assumes you know how an LLM agent calls tools in a loop, and that processes and files are familiar to you.
The problem: memory is not durable
A long-running task outlives something. The context window fills up, the process crashes, the session ends, or a different agent takes over. Anything the agent holds only in its context or in process memory disappears at that point.
State is everything the task needs to know to continue correctly. Your job is to decide which parts of it must be stored outside the agent. Storing everything is noisy and expensive. Storing too little forces the next run to guess.
Five kinds of state worth keeping
Ask of each piece of information: if it vanished now, could the task still continue correctly? If not, it must survive.
| Kind of state | Example | What goes wrong without it |
|---|---|---|
| Goal and constraints | "All pages build without warnings. Keep old URLs." | The next run optimizes for the wrong thing |
| Progress | Steps done, steps remaining | Finished work is redone or skipped |
| Decisions and reasons | "Use redirects, not renames, because of external links" | A later run reverses the decision |
| Side effects already made | Files uploaded, emails sent, rows inserted | Actions are repeated and create duplicates |
| Next action and open problems | "Convert shortcodes in docs/guides/" | The resumer wastes time rediscovering where it was |
Progress is the obvious one. Side effects are the one people forget. A restarted job cannot tell whether an upload happened unless something recorded it.
What does not need to survive
Some state can be rebuilt cheaply, so storing it only creates a second copy that can go stale.
- Data you can recompute or re-read, such as a file listing or a database row count.
- Intermediate reasoning that led to a decision. Keep the decision and the reason, not the whole chain of thought.
- Raw tool output that is already in a file or log somewhere else. Store a pointer to it instead.
A useful rule: store facts that only this task knows, and re-derive facts the world already knows.
A minimal state shape
Here is one way to hold the five kinds of state in a single structure. Later chapters cover how to write it to disk safely.
state = {
"goal": "All pages under docs/ build without warnings",
"constraints": ["Keep old URLs"],
"completed": ["inventory", "front_matter"],
"decisions": [
{"what": "Use redirects", "why": "External sites link to old URLs"}
],
"side_effects": [
{"action": "upload", "target": "2023-01", "done": True}
],
"next_action": "Convert shortcodes in docs/guides/",
"open_problems": ["Gallery shortcode has no equivalent"],
}Every field answers a question the next run will ask. If a field answers none, remove it.
Common mistakes
- Keeping state only in the conversation. Once the context is trimmed or the session ends, it is gone.
- Saving results but not decisions. The next run sees what was done but not why, and may undo it.
- Saving derived values as truth. A saved row count that is a week old is a stale guess. Treat it as a hint and re-check it.
- Vague next action. "Continue the migration" forces the resumer to investigate. "Convert shortcodes in docs/guides/ starting at setup.md" does not.
Exercise
A job sends a weekly report email, then crashes before saving any progress. Which kind of state is missing, and why is it the most dangerous to lose?
Show answer
The record of side effects. On restart the job cannot tell that the email was already sent, so it may send a duplicate. Lost progress usually only wastes work, but an unrecorded side effect can repeat an action that cannot be undone.