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.
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.
AgentState
in LangGraphLangGraph is built around a stateful, step-based computation graph. Each step (node) in the graph receives and returns an updated AgentState
.
AgentState = {
"messages": [...], # Chat history
"tools": [...], # Available tools
"intermediate_steps": [...], # Memory of actions taken
"current_goal": "Do X", # Optional: current user goal
}
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.
Tracks conversation or workflow context.
Stores tool usage history or agent reasoning.
Passes updated data across steps.
Allows conditional routing (e.g., if AgentState contains tool result → go to summarizer
).
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 |
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.