Agentic AI · Intermediate level

Intermediate level · Module 6: Agent State Management · Lesson 1 of 3

Explicit State Objects

Learners replace scattered variables with one Pydantic state object that records the question, plan, evidence, drafts, errors and status of a task.

In this lesson you will

  • Design an agent state object
  • Update state through clear functions

Key concepts

state objectstatus fieldevidence listimmutability and copiessingle source of truth

The lesson

As agents grow, information is scattered across variables and messages. A single state object (for example a Pydantic ResearchState with question, plan, evidence, draft, errors, status and token usage) makes progress visible and testable.

Each step reads the state and returns an updated state. This makes steps easy to test in isolation and the whole run easy to inspect.

The state is also the audit record of what the agent knew and did at each point.

Step by step

  1. Define ResearchState with at least seven fields.
  2. Refactor the chain so that each step takes and returns the state.
  3. Print the state after each step for one task.
  4. Write a test for one step using a prepared state.

Practice activity

Refactor the project agent around an explicit state object.

Expected result: A clear state object updated by each step, with passing tests.

Check your understanding

  1. Why use one explicit state object?

    Show answer

    It makes progress visible, testable and auditable.

  2. How should a step interact with the state?

    Show answer

    Read it and return an updated state.

Further learning

Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.