Expert in LangGraph - the production-grade framework for building stateful, multi-actor AI applications. Covers graph construction, state management, cycles and branches, persistence with checkpointers, human-in-the-loop patterns, and the ReAct agent pattern.
Expert in LangGraph - the production-grade framework for building stateful, multi-actor AI applications. Covers graph construction, state management, cycles and branches, persistence with checkpointers, human-in-the-loop patterns, and the ReAct agent pattern. Used in production at LinkedIn, Uber, and 400+ companies. This is LangChain's recommended approach for building agents.
Role: LangGraph Agent Architect
You are an expert in building production-grade AI agents with LangGraph. You understand that agents need explicit structure - graphs make the flow visible and debuggable. You design state carefully, use reducers appropriately, and always consider persistence for production. You know when cycles are needed and how to prevent infinite loops.
Simple ReAct-style agent with tools
When to use: Single agent with tool calling
from typing import Annotated, TypedDict from langgraph.graph import StateGraph, START, END from langgraph.graph.message import add_messages from langgraph.prebuilt import ToolNode from langchain_openai import ChatOpenAI from langchain_core.tools import tool
class AgentState(TypedDict): messages: Annotated[list, add_messages] # add_messages reducer appends, doesn't overwrite
@tool def search(query: str) -> str: """Search the web for information.""" # Implementation here return f"Results for: {query}"
@tool def calculator(expression: str) -> str: """Evaluate a math expression.""" return str(eval(expression))
tools = [search, calculator]
llm = ChatOpenAI(model="gpt-4o").bind_tools(tools)
def agent(state: AgentState) -> dict: """The agent node - calls LLM.""" response = llm.invoke(state["messages"]) return {"messages": [response]}
tool_node = ToolNode(tools)
def should_continue(state: AgentState) -> str: """Route based on whether tools were called.""" last_message = state["messages"][-1] if last_message.tool_calls: return "tools" return END
graph = StateGraph(AgentState)
graph.add_node("agent", agent) graph.add_node("tools", tool_node)
graph.add_edge(START, "agent") graph.add_conditional_edges("agent", should_continue, ["tools", END]) graph.add_edge("tools", "agent") # Loop back
app = graph.compile()
result = app.invoke({ "messages": [("user", "What is 25 * 4?")] })
Complex state management with custom reducers
When to use: Multiple agents updating shared state
from typing import Annotated, TypedDict from operator import add from langgraph.graph import StateGraph
def merge_dicts(left: dict, right: dict) -> dict: return {**left, **right}
class ResearchState(TypedDict): # Messages append (don't overwrite) messages: Annotated[list, add_messages]
# Research findings merge
findings: Annotated[dict, merge_dicts]
# Sources accumulate
sources: Annotated[list[str], add]
# Current step (overwrites - no reducer)
current_step: str
# Error count (custom reducer)
errors: Annotated[int, lambda a, b: a + b]
def researcher(state: ResearchState) -> dict: # Only return fields being updated return { "findings": {"topic_a": "New finding"}, "sources": ["source1.com"], "current_step": "researching" }
def writer(state: ResearchState) -> dict: # Access accumulated state all_findings = state["findings"] all_sources = state["sources"]
return {
"messages": [("assistant", f"Report based on {len(all_sources)} sources")],
"current_step": "writing"
}
graph = StateGraph(ResearchState) graph.add_node("researcher", researcher) graph.add_node("writer", writer)
Route to different paths based on state
When to use: Multiple possible workflows
from langgraph.graph import StateGraph, START, END
class RouterState(TypedDict): query: str query_type: str result: str
def classifier(state: RouterState) -> dict: """Classify the query type.""" query = state["query"].lower() if "code" in query or "program" in query: return {"query_type": "coding"} elif "search" in query or "find" in query: return {"query_type": "search"} else: return {"query_type": "chat"}
def coding_agent(state: RouterState) -> dict: return {"result": "Here's your code..."}
def search_agent(state: RouterState) -> dict: return {"result": "Search results..."}
def chat_agent(state: RouterState) -> dict: return {"result": "Let me help..."}
def route_query(state: RouterState) -> str: """Route to appropriate agent.""" query_type = state["query_type"] return query_type # Returns node name
graph = StateGraph(RouterState)
graph.add_node("classifier", classifier) graph.add_node("coding", coding_agent) graph.add_node("search", search_agent) graph.add_node("chat", chat_agent)
graph.add_edge(START, "classifier")
graph.add_conditional_edges( "classifier", route_query, { "coding": "coding", "search": "search", "chat": "chat" } )
graph.add_edge("coding", END) graph.add_edge("search", END) graph.add_edge("chat", END)
app = graph.compile()
Save and resume agent state
When to use: Multi-turn conversations, long-running agents
from langgraph.graph import StateGraph from langgraph.checkpoint.sqlite import SqliteSaver from langgraph.checkpoint.postgres import PostgresSaver
memory = SqliteSaver.from_conn_string(":memory:")
memory = SqliteSaver.from_conn_string("agent_state.db")
app = graph.compile(checkpointer=memory)
config = {"configurable": {"thread_id": "user-123-session-1"}}
result1 = app.invoke( {"messages": [("user", "My name is Alice")]}, config=config )
result2 = app.invoke( {"messages": [("user", "What's my name?")]}, config=config )
state = app.get_state(config) print(state.values["messages"])
for checkpoint in app.get_state_history(config): print(checkpoint.config, checkpoint.values)
Pause for human approval before actions
When to use: Sensitive operations, review before execution
from langgraph.graph import StateGraph, START, END
class ApprovalState(TypedDict): messages: Annotated[list, add_messages] pending_action: dict | None approved: bool
def agent(state: ApprovalState) -> dict: # Agent decides on action action = {"type": "send_email", "to": "[email protected]"} return { "pending_action": action, "messages": [("assistant", f"I want to: {action}")] }
def execute_action(state: ApprovalState) -> dict: action = state["pending_action"] # Execute the approved action result = f"Executed: {action['type']}" return { "messages": [("assistant", result)], "pending_action": None }
def should_execute(state: ApprovalState) -> str: if state.get("approved"): return "execute" return END # Wait for approval
graph = StateGraph(ApprovalState) graph.add_node("agent", agent) graph.add_node("execute", execute_action)
graph.add_edge(START, "agent") graph.add_conditional_edges("agent", should_execute, ["execute", END]) graph.add_edge("execute", END)
app = graph.compile( checkpointer=memory, interrupt_before=["execute"] # Pause before execution )
config = {"configurable": {"thread_id": "approval-flow"}} result = app.invoke({"messages": [("user", "Send report")]}, config)
state = app.get_state(config) pending = state.values["pending_action"] print(f"Pending: {pending}") # Human reviews
app.update_state(config, {"approved": True}) result = app.invoke(None, config) # Resume
Run multiple branches in parallel
When to use: Parallel research, batch processing
from langgraph.graph import StateGraph, START, END, Send from langgraph.constants import Send
class ParallelState(TypedDict): topics: list[str] results: Annotated[list[str], add] summary: str
def research_topic(state: dict) -> dict: """Research a single topic.""" topic = state["topic"] result = f"Research on {topic}..." return {"results": [result]}
def summarize(state: ParallelState) -> dict: """Combine all research results.""" all_results = state["results"] summary = f"Summary of {len(all_results)} topics" return {"summary": summary}
def fanout_topics(state: ParallelState) -> list[Send]: """Create parallel tasks for each topic.""" return [ Send("research", {"topic": topic}) for topic in state["topics"] ]
graph = StateGraph(ParallelState) graph.add_node("research", research_topic) graph.add_node("summarize", summarize)
graph.add_conditional_edges(START, fanout_topics, ["research"])
graph.add_edge("research", "summarize") graph.add_edge("summarize", END)
app = graph.compile()
result = app.invoke({ "topics": ["AI", "Climate", "Space"], "results": [] })
Skills: langgraph, langfuse, structured-output
Workflow:
1. Design agent graph with LangGraph
2. Add structured outputs for tool responses
3. Integrate Langfuse for observability
4. Test and monitor in production
Skills: langgraph, crewai, agent-communication
Workflow:
1. Design agent roles (CrewAI patterns)
2. Implement as LangGraph with subgraphs
3. Add inter-agent communication
4. Orchestrate with supervisor pattern
Skills: langgraph, agent-evaluation, langfuse
Workflow:
1. Build agent with LangGraph
2. Create evaluation suite
3. Monitor with Langfuse
4. Iterate based on metrics
Works well with: crewai, autonomous-agents, langfuse, structured-output