Some text some message..
Back AgentState in LangGraph 21 May, 2025

Referring to how state is managed and passed between nodes (steps) in a LangGraph, which is a framework built on top of LangChain for orchestrating agent-based workflows.


🧠 What is AgentState in LangGraph?

In LangGraph, AgentState is the structured data object (often a dictionary or a Pydantic model) that represents the current context of an agent's execution. It acts as the memory or context passed between nodes in a graph-based flow.


🔁 Role of AgentState in LangGraph

LangGraph is built around a stateful, step-based computation graph. Each step (node) in the graph receives and returns an updated AgentState.

👇 Typical structure:

AgentState = {
    "messages": [...],         # Chat history
    "tools": [...],            # Available tools
    "intermediate_steps": [...],  # Memory of actions taken
    "current_goal": "Do X",    # Optional: current user goal
}

📌 Example: A Simple LangGraph Agent

from langgraph.graph import StateGraph
from typing import TypedDict, List
from langchain.schema import BaseMessage

# Step 1: Define the AgentState
class AgentState(TypedDict):
    messages: List[BaseMessage]
    intermediate_steps: List[str]

# Step 2: Define node functions
def agent_node(state: AgentState) -> AgentState:
    # Generate a new message
    new_msg = "Hello, how can I help you?"
    return {
        "messages": state["messages"] + [new_msg],
        "intermediate_steps": state["intermediate_steps"] + ["agent_node"]
    }

def tool_node(state: AgentState) -> AgentState:
    # Imagine tool was invoked
    tool_output = "Tool result"
    return {
        "messages": state["messages"] + [tool_output],
        "intermediate_steps": state["intermediate_steps"] + ["tool_node"]
    }

# Step 3: Build the graph
graph = StateGraph(AgentState)
graph.add_node("agent", agent_node)
graph.add_node("tool", tool_node)
graph.set_entry_point("agent")
graph.add_edge("agent", "tool")
graph.add_edge("tool", "agent")
workflow = graph.compile()

Here, AgentState is the context object that flows through the graph.


🧩 Why is AgentState Important?

  1. Tracks conversation or workflow context.

  2. Stores tool usage history or agent reasoning.

  3. Passes updated data across steps.

  4. Allows conditional routing (e.g., if AgentState contains tool result → go to summarizer).


✅ Common Fields in AgentState:

Field Description
messages List of chat messages (LLM input/output)
intermediate_steps Tool calls or decisions made
input Original user input
final_output Agent's completed response or result
tools List of tool names available
next_action What the agent plans to do next

🧠 Summary

In LangGraph, AgentState is the shared memory/context of the agent, used to pass and update data between graph steps, enabling intelligent, stateful workflows with language models and tools.