LangGraph fundamentals

LangGraph is a library for building stateful, multi-actor applications. It extends LangChain with graph-based workflow orchestration, perfect for our multi-agent system.

Stategraph: nodes, edges, and state flow

How LangGraph organizes agents into a directed graph with shared state.

text2sql_agent.py
python
from langgraph.graph import StateGraph, END

# Create a new StateGraph with our state type
workflow = StateGraph(AgentState)

# Add nodes (each node is a function that takes state and returns state)
workflow.add_node("guardrails_agent", guardrails_agent)
workflow.add_node("sql_agent", sql_agent)
workflow.add_node("execute_sql", execute_sql)
workflow.add_node("analysis_agent", analysis_agent)
workflow.add_node("error_agent", error_agent)

# Set the entry point
workflow.set_entry_point("guardrails_agent")

# Add edges between nodes
workflow.add_edge("sql_agent", "execute_sql")

# Compile the graph
graph = workflow.compile()

The basic structure of a LangGraph StateGraph.

Good catch! AgentState is a TypedDict we will define shortly. For now, just know it is a typed dictionary that holds all the data flowing through the graph, like the user question, SQL query, results, and errors. Every node reads from and writes to this shared state.

Compiling validates the graph structure, checks that all edges point to real nodes, and returns an executable object. Think of it like compiling code: the graph definition is the source, and the compiled graph is the runnable program.

Flashcards: Flashcards

Loading practice…

Matching exercise: Match LangGraph concepts

Loading practice…

Quiz: Quiz

Loading practice…

You now understand the LangGraph building blocks: StateGraph, nodes, edges, and compilation. Next, we will design the AgentState that all our agents share.