Skip to main content
AI Agents & Automation

⏱ About 20 min20 XP

State Across Steps

An agent does not complete its task in a single call. It loops — plan, act, observe, update, repeat — until the goal is reached or a stopping condition triggers. Each iteration of that loop produces output: a tool call result, a partial answer, a decision that constrains future decisions. This output is the agent's state, and if state is not tracked and carried forward explicitly, every new iteration starts blind.

State management in agent systems is analogous to state management in any long-running program. A database migration script tracks which migrations have already run. A download manager tracks which file segments are complete. An agent tracks which subtasks are done, what information has been gathered, what decisions have been committed to, and what the current plan is. The difference is that an agent's state must be legible to a language model — it has to be expressible in text, because the model reads it as part of its prompt.

State Must Be Model-Legible

Unlike state in a traditional program (which can be any data structure a compiler understands), agent state must ultimately be expressible in natural language or structured text so the model can read it and reason over it. JSON, YAML, and structured text are common formats precisely because they are both machine-parseable and human-readable — and therefore model-legible.

What Agent State Contains

Agent state is not a monolith — it is a collection of distinct information types that the agent must track simultaneously. Identifying these types explicitly helps in designing a state representation. Task status tracks which subtasks in the agent's plan are pending, in progress, or complete. For a research agent writing a report, this might be a list of sections with a status field for each. Accumulated results holds the outputs of completed steps — the retrieved documents, the summarized paragraphs, the code that was successfully generated. These are the building blocks the agent uses in later steps. Active constraints records decisions that have already been made and must not be contradicted. If the agent decided in step 2 to use Python 3.11, that constraint must be visible in steps 5 and beyond to prevent the model from switching to a different version. Plan state captures the current plan and any revisions made as new information arrived. Plans change — a well-designed state object tracks not just the original plan but the history of revisions and the reasons for them.

Match each piece of agent state to the category it belongs to.

Terms

Section 3 of the report: COMPLETE. Section 4: IN PROGRESS.
The three retrieved papers that support the introduction argument
Decision: use metric units throughout — do not switch to imperial
Original plan: 5 sections. Revised plan after step 3: 6 sections (add methodology).
Error log: API call to weather service failed at step 2 with a timeout

Definitions

Active constraints
Accumulated results
Plan state
Task status
Error history

Drag terms onto their definitions, or click a term then click a definition to match.

How State Is Stored and Injected

State can be stored in memory (a Python dictionary in the running process), in a database (persisted across crashes and restarts), or in the context window itself (the model's scratchpad). In practice, production agents use a combination. In-process state — a plain Python dictionary or object — is fast and simple but lost if the process crashes. It is appropriate for ephemeral agents completing short tasks in a single session. Database-persisted state — written to Postgres or a similar store after each step — is durable. It survives crashes and allows the agent to be paused and resumed, or even distributed across multiple machines. LangGraph, a popular agent orchestration library, stores its graph state in a checkpoint store using this pattern. Context-window state — the model's own scratchpad, sometimes called a scratchpad or reasoning trace — is the text the model produces before its final answer in chain-of-thought prompting. This is not persisted automatically; the application must capture and replay it.

The injection step is equally important. At the start of each loop iteration, the agent assembles the current prompt. This includes the original task, the current state (formatted as readable text or structured JSON), any fresh tool outputs from the previous step, and the model's next instruction. The formatting of state matters: a wall of raw JSON is technically correct but may cause the model to attend poorly. Well-labeled, human-readable state representations — with clear headings, concise labels, and explicit status markers — produce better model behavior.

Formatting State for the Model

State injected into the prompt should be formatted for readability, not just correctness. Use headings, labels, and short summaries rather than raw data dumps. A state block that reads 'COMPLETED STEPS: [Step 1: Retrieved 5 papers. Step 2: Summarized papers 1-3.]' is far more useful to the model than an equivalent but opaque JSON blob.

Agent state must be to the model, meaning it must be expressed in text the model can read. State is typically stored as or structured text. At the start of each loop iteration, the agent the current state into the prompt so the model can reason over it. Decisions that must not be changed are called .

A code-writing agent is refactoring a codebase. After step 3, it decides all new functions must include type annotations. In step 7, the model writes a new function without type annotations. What state management failure caused this?

Which storage approach for agent state is best for an agent that must survive a server crash and resume exactly where it left off?

State Object Design

  1. You are building an AI agent that monitors a company's social media accounts, identifies negative comments, and drafts polite response suggestions for a human reviewer to approve.
  2. The agent runs a loop: (1) fetch new comments, (2) classify each as positive/neutral/negative, (3) for negative comments, draft a response, (4) log everything for the human reviewer.
  3. Step 1: Design the agent's state object. Write it out as a JSON structure with all the fields the agent needs to track. Include at minimum: what comments have been processed, what drafts are waiting for review, any active constraints (e.g., the company's required tone), and the current loop status.
  4. Step 2: Write the state as it would appear after the agent processes 3 comments: one positive, one neutral, one negative (draft written, awaiting review).
  5. Step 3: Describe exactly how this state object gets included in the model's prompt at step 3 of the loop. Write out the first 6-8 lines of that prompt including the injected state.
  6. Step 4: What information in your state object would you drop after 30 days to prevent the state from growing indefinitely?