📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials AI Agents and Automation LangGraph Stateful Agents

LangGraph Stateful Agents

6 min read Quiz at the end
LangGraph builds stateful agent loops as directed graphs — cycles, conditions, and human-in-the-loop.

LangGraph Stateful Agent Workflows

from langgraph.graph import StateGraph, END
from langgraph.prebuilt import ToolNode
from typing import TypedDict, Annotated
import operator

class AgentState(TypedDict):
    messages: Annotated[list, operator.add]
    iteration: int

llm_with_tools = llm.bind_tools(tools)

def call_agent(state: AgentState) -> AgentState:
    messages = state["messages"]
    response = llm_with_tools.invoke(messages)
    return {"messages": [response], "iteration": state["iteration"]+1}

def should_continue(state: AgentState) -> str:
    last_msg = state["messages"][-1]
    if hasattr(last_msg, "tool_calls") and last_msg.tool_calls:
        return "tools"
    return END

tool_node = ToolNode(tools)

graph = StateGraph(AgentState)
graph.add_node("agent", call_agent)
graph.add_node("tools", tool_node)
graph.set_entry_point("agent")
graph.add_conditional_edges("agent", should_continue,
    {"tools": "tools", END: END})
graph.add_edge("tools", "agent")  # loop back

app = graph.compile()
result = app.invoke({"messages":[HumanMessage("Analyse sales trends")],
                     "iteration":0})
Topic Quiz · 1 questions

Test your understanding before moving on

1. What does LangGraph add compared to basic LangChain agents?
💡 LangGraph models agent workflows as directed graphs — enabling loops, branches, and state persistence.