Module 2 Lesson 2: Agent State
·Agentic AI

Module 2 Lesson 2: Agent State

The snapshot of intelligence. Managing variables, message history, and flags within a running agent.

Agent State: The "Brain" Snapshot

In a traditional web app, "State" might be your login session or your shopping cart. In Agentic AI, State is the complete list of everything the agent knows about the current task. If an agent crashes and resets its state, it loses all progress and logic.

1. What's inside "State"?

A typical agent state contains:

  • Messages: The literal history of the chat (User, Assistant, and Tool messages).
  • Internal Variables: Logic flags like has_searched: True or retry_count: 2.
  • Knowledge Snippets: Information gathered from tools during the loop.
  • Goal: The original prompt that must be satisfied.

2. Stateless vs. Stateful Agents

  • Stateless: The agent is a "pure function." It takes a request and returns a response. If it needs to loop, it has to be fed the entire history every single time. Most low-level LLM APIs (like GPT-4) are stateless.
  • Stateful: The agent lives inside a system (like LangGraph) that manages the memory for you. The agent can "pause" and "resume" because its state is saved in a database (Checkpoints).

3. Why State Management is Hard

If your state gets too large, the "Context Window" of the LLM will fill up.

  • The Problem: You can't just keep adding everything to history.
  • The Solution: State Trimming. You must decide which parts of the history are still relevant and which can be deleted or summarized to make room for new "Thoughts."

4. Visualizing State Flow

graph LR
    S1[State v1: Start] --> Step1[Reasoning]
    Step1 --> Action[Tool Call]
    Action --> S2[State v2: Tool Result Added]
    S2 --> Step2[Next Reasoning]
    Step2 --> S3[State v3: Final Answer]

5. Code Example: A Python State Dictionary

In many frameworks, the state is just a Typed Dictionary. As the agent moves through its graph, it "mutates" this dictionary.

from typing import TypedDict, List

class AgentState(TypedDict):
    # The history of the conversation
    messages: List[str]
    # A custom variable to track if we found the answer
    is_ready_to_reply: bool
    # A list of sources found so we can cite them later
    collected_sources: List[str]

# Initial state
current_state = {
    "messages": ["Who won the Superbowl?"],
    "is_ready_to_reply": False,
    "collected_sources": []
}

# The agent loop will then update 'collected_sources' and 'is_ready_to_reply'

Key Takeaways

  • State is the persistent memory of an agent run.
  • Message history is the most common form of state.
  • Checkpointing allows agents to survive crashes and long-running tasks.
  • Trimming state is essential for staying within the LLM's context limits.
  • Frameworks like LangGraph exist primarily to make state management easier.

Subscribe to our newsletter

Get the latest posts delivered right to your inbox.

Subscribe on LinkedIn