Lesson 4: State Management and Passing Context
Learning goals:
- Distinguish workflow state from agent context
- Master three state-management patterns
- Understand checkpointing and recovery
Prerequisites: Lesson 3: Decomposing a Complex Task into a Workflow | Next: Lesson 5 >>
Why state management is the heart of a workflow
You design a perfect workflow: 10 steps, clean dependencies. On step 8, the server restarts. The workflow crashes.
Rerun it? Then the work from the first 7 steps — maybe 30 minutes of it — is thrown away.
That's the price of having no state management.
State management solves three problems:1
- Passing data between steps: how does step 3 get the results of steps 1 and 2?
- Progress tracking: how far along is the workflow? How much is left?
- Failure recovery: after a crash, resume from where it stopped instead of starting over.
Without state management, an agent can only pass information through conversation history. Conversation history overflows, gets lost, and gets forgotten by the agent.
With state management, the workflow has a clear "memory": persistent, queryable, recoverable.2
State vs. context vs. memory
These three words are easy to mix up, so let's pin them down first:1
State
- All the information about the current task: which step you're on, the result of each step, what to do next
- It's a snapshot: everything the workflow knows at this moment
- Stored in: script variables, a database, files
Context
- The information passed into a single agent call
- It's input: what this agent needs to know to do its job
- Selectively pulled from state: not all state goes to the agent, only the relevant part
Memory
- Lessons learned from the past: what was done before, what problems came up, what the solutions were
- It's history: long-term knowledge across tasks and sessions
- Out of scope for this lesson (long-term memory is its own hard topic)
An example:
The key principle: state is global, context is local.3
State pattern 1: script variables (in-memory state)
When to use it: short workflows (< 10 minutes) that don't need to cross processes or machines.
Upside: simple, fast, no external dependencies.
Downside: state is lost when the process crashes, with no way to recover.
Basic pattern
Where does the state live? In the function's local variables (processed, results, errors).
What if the process crashes? All state is lost, and you start over from the beginning.
Better: a structured state object
Why it helps: the state has a clear structure, it's easy to pass to other functions, and it's easy to serialize (if you need to persist it).
State pattern 2: checkpointing
When to use it: medium-length workflows (10-60 minutes) where you need to save progress after expensive operations.
Upside: after a crash, you can resume from the most recent checkpoint and avoid redoing work.
Downside: you have to design checkpoint locations and recovery logic.1
Choosing checkpoint locations
Checkpoint strategies:
- Periodic checkpoints: save every N tasks or every M minutes
- Phase checkpoints: save after each major phase completes (e.g. "analysis phase done")
- Before critical operations: save before an irreversible operation (e.g. a deploy, a delete)
State pattern 3: external storage (persistent state)
When to use it: long-running workflows (> 1 hour), work that needs to coordinate across machines, or work that needs human approval.
Upside: state is persistent; a process crash or machine restart doesn't matter, and pause/resume is supported.
Downside: it needs an external dependency (a database, Redis) and adds complexity.4
Basic implementation
The key pattern: a state machine2
The workflow's phases are the states of a state machine:
Every phase transition is saved to external storage, which is what lets the workflow resume from any phase.
Best practices for passing context
Principle 1: pass only what's needed
Why? The bigger the context, the easier it is for the agent to get distracted; reasoning quality drops and cost rises.3
Principle 2: structure the context
Why? Structured context is easier for the agent to understand, and easier for you to debug.
Principle 3: accumulating context vs. resetting context
Accumulating context: each step's result is added to the context, so it keeps growing.
Resetting context: each step clears the context and keeps only what's needed.
Which to pick: use resetting context most of the time to avoid context explosion. Use accumulating context only when later steps genuinely need every earlier result (like a final summary step).5
Observability of state
A good workflow should be able to answer these questions:
- Which phase is it in right now?
- How much is done? How much is left?
- How many errors has it hit?
- When is it expected to finish?
Implementing progress tracking
Next lesson: Lesson 5: Error Handling and Retry Strategies — learn how to make a workflow recover gracefully on failure instead of crashing outright