Agent Mentor Learn
Designing Agent Workflows: From One-Off Conversations to Multi-Step Automation · Lesson 4 of 6

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

  1. Passing data between steps: how does step 3 get the results of steps 1 and 2?
  2. Progress tracking: how far along is the workflow? How much is left?
  3. 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:

init → processing → awaiting_approval → approved → finalizing → completed                     rejected → cancelled

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

Footnotes

  1. MachineLearningMastery: 5 Architectural Patterns for Persistent Memory and State in AI Agents — https://machinelearningmastery.com/5-architectural-patterns-for-persistent-memory-and-state-in-ai-agents/ 2 3

  2. MindStudio: Workflow State vs. Session State — https://www.mindstudio.ai/blog/workflow-state-vs-session-state-ai-agents 2

  3. Chrono Innovation: Architecture for Scalable Agentic AI Workflows — https://www.chronoinnovation.com/resources/agentic-ai-workflows-architecture/ 2

  4. Appamass: State Management Patterns for Reliable AI Agent Workflows — https://appamass.com/en/blog/state-management-patterns-for-reliable-ai-agent-workflows-5yemlru6ui6cacast3l5

  5. Ranjan Kumar: Building Agents That Remember — https://ranjankumar.in/building-agents-that-remember-state-management-in-multi-agent-ai-systems

Exercises

01

For the three workflows below, choose the right state-management pattern (script variables, checkpointing, external storage) and explain why:

Level 1: Choose a state-management pattern

Workflow A: batch-compress 20 images, 5 seconds each, 100 seconds total

Workflow B: train a machine learning model, 50 epochs at 10 minutes each, 500 minutes total (8 hours)

Workflow C: review 100 PRs, each needing human approval before merge, and the whole process may run for several days

Done criteria · checked locally
02

Design the state object for a "multi-service deployment" workflow. The workflow needs to: (1) build Docker images for 5 services (2) push them to an image registry (3) deploy to a test environment one by one (4) run integration tests (5) if tests pass, deploy to production.

Level 2: Design a state structure

Requirements:

  • Design a JSON object that represents the workflow state
  • Include: current phase, per-service status, error info, timestamps
  • Explain where checkpoints should be saved
Done criteria · checked locally