17 — LangGraph Fundamentals¶
Understand the foundations of LangGraph and how graph-based orchestration can be used to build stateful, controllable, and production-oriented AI applications.
📖 Overview¶
Modern AI applications increasingly require more than a single LLM call.
A production system may need to:
Receive Request
↓
Understand Task
↓
Retrieve Information
↓
Call Tools
↓
Evaluate Result
↓
Retry / Branch
↓
Continue
↓
Return Response
Traditional sequential pipelines can become difficult to manage when applications require:
State
Branching
Loops
Conditional Execution
Human Approval
Tool Calling
Retries
Persistence
Long-Running Execution
LangGraph introduces a graph-based execution model for building these kinds of stateful AI workflows and agent systems.
A useful mental model is:
┌──────────────┐
│ START │
└──────┬───────┘
↓
┌──────────────┐
│ Analyze │
└──────┬───────┘
↓
┌──────────────┐
│ Retrieve │
└──────┬───────┘
↓
┌──────────────┐
│ Agent │
└──────┬───────┘
↓
┌────────────┐
│ Decide │
└───┬────┬───┘
│ │
Yes │ │ No
↓ ↓
Tool Retry
│ │
└─┬──┘
↓
┌──────────────┐
│ Finish │
└──────────────┘
The important idea is:
🎯 Learning Objectives¶
After completing this chapter, you will be able to:
- Understand the purpose of LangGraph
- Understand graph-based AI orchestration
- Understand nodes and edges
- Understand graph state
- Understand START and END
- Build a basic LangGraph
- Understand sequential execution
- Understand conditional routing
- Understand loops
- Understand state transitions
- Understand reducers
- Understand checkpoints conceptually
- Understand persistence requirements
- Understand how LangGraph differs from simple chains
- Understand how LangGraph differs from agents
- Design basic production-oriented graphs
- Identify when LangGraph is appropriate
1. What Is LangGraph?¶
LangGraph is a graph-based orchestration framework for building stateful AI applications.
Instead of representing execution only as:
you can represent more complex execution as:
This makes branching and cycles first-class architectural concepts.
2. Why Graph-Based Orchestration?¶
AI applications frequently contain decisions.
For example:
A graph provides an explicit representation of this execution structure.
3. Core LangGraph Concepts¶
The basic building blocks are:
Conceptually:
4. Graph Model¶
A graph can be represented as:
Example:
5. Nodes¶
A node represents a unit of computation.
Examples:
Conceptually:
The node receives state, performs work, and returns state updates.
6. Node Responsibilities¶
A good node should have a focused responsibility.
Good:
Avoid:
A graph becomes easier to understand when each node has a clear responsibility.
7. Edges¶
Edges define how execution moves between nodes.
Example:
The edges are:
8. Conditional Edges¶
Not every transition is deterministic.
Example:
A routing function determines which path to take.
9. Graph State¶
State represents information shared across graph execution.
Example:
As the graph executes:
10. State as the Source of Context¶
A useful mental model is:
Therefore:
11. State Schema¶
A state schema defines the information managed by the graph.
Conceptually:
The exact implementation can vary depending on the LangGraph version and state-management approach.
12. Basic Graph Example¶
A simple graph can look conceptually like:
from typing import TypedDict
from langgraph.graph import StateGraph, START, END
class State(TypedDict):
message: str
def process(state: State):
return {
"message": state["message"] + " processed"
}
builder = StateGraph(State)
builder.add_node(
"process",
process
)
builder.add_edge(
START,
"process"
)
builder.add_edge(
"process",
END
)
graph = builder.compile()
Execution:
The exact APIs should be verified against the LangGraph version used by the project.
13. Basic Graph Architecture¶
flowchart LR
A[START] --> B[Process]
B --> C[END]
This is the smallest useful mental model for a graph.
14. Sequential Graph¶
A sequential graph is:
Example:
builder.add_edge(
START,
"retrieve"
)
builder.add_edge(
"retrieve",
"generate"
)
builder.add_edge(
"generate",
END
)
15. Sequential Graph Diagram¶
flowchart TD
A[START] --> B[Retrieve]
B --> C[Generate]
C --> D[END]
This is similar to a traditional pipeline.
The real power appears when we introduce:
16. Conditional Routing¶
Example:
Conceptually:
17. Conditional Graph¶
flowchart TD
A[Retrieve] --> B{Evidence Available?}
B -->|Yes| C[Generate]
B -->|No| D[Fallback]
C --> E[END]
D --> E
This makes decision logic explicit.
18. Loops¶
AI systems often require repeated execution.
Example:
The graph contains a cycle.
19. Loop Architecture¶
flowchart TD
A[Generate] --> B[Validate]
B --> C{Valid?}
C -->|Yes| D[END]
C -->|No| A
This is particularly useful for:
20. Bounded Loops¶
Loops should not normally run forever.
Use:
Example:
Conceptually:
21. Why State Matters in Loops¶
A loop may need:
Example:
State allows the graph to remember execution progress.
22. Graph State Evolution¶
flowchart LR
A["State<br/>query"] --> B["Retrieve<br/>+ documents"]
B --> C["Generate<br/>+ answer"]
C --> D["Validate<br/>+ feedback"]
D --> E["Next State"]
Each node contributes updates to the graph state.
23. State Updates¶
A node may return:
Another node:
Another:
The graph combines these updates according to its state semantics.
24. Reducers¶
Some state fields may receive multiple updates.
For example:
A reducer determines how updates are combined.
25. Reducer Concept¶
Suppose:
Another node produces:
A reducer may combine them into:
rather than replacing the existing value.
26. State Ownership¶
Be careful about which node owns which state fields.
Example:
This makes state transitions easier to reason about.
27. State Design Principle¶
Avoid:
containing everything.
Prefer:
This reduces accidental coupling between nodes.
28. START and END¶
LangGraph graphs commonly use special entry and termination points.
Conceptually:
Example:
29. Graph Compilation¶
A graph is typically built and then compiled.
Conceptually:
30. Graph Construction¶
builder = StateGraph(State)
builder.add_node(
"retrieve",
retrieve
)
builder.add_node(
"generate",
generate
)
builder.add_edge(
START,
"retrieve"
)
builder.add_edge(
"retrieve",
"generate"
)
builder.add_edge(
"generate",
END
)
graph = builder.compile()
31. Graph Execution¶
After compilation:
Conceptually:
32. Streaming¶
For long-running AI applications, users may benefit from incremental updates.
Conceptually:
can expose execution progress rather than waiting for the entire graph to complete.
This is useful for:
33. Human-in-the-Loop¶
Graphs can represent human approval points.
Example:
34. Human Approval Architecture¶
flowchart TD
A[Agent] --> B[Prepare Action]
B --> C[Human Review]
C --> D{Approved?}
D -->|Yes| E[Execute]
D -->|No| F[Reject]
E --> G[END]
F --> G
This is useful for high-risk operations.
35. Persistence¶
State may need to survive beyond a single execution.
For example:
This requires persistence of execution state.
36. Checkpointing¶
Checkpointing provides a way to persist graph execution state at defined points.
Conceptually:
If execution stops:
37. Checkpoint Architecture¶
flowchart LR
A[Node A] --> B[Checkpoint]
B --> C[Node B]
C --> D[Checkpoint]
D --> E[Node C]
E --> F[END]
Checkpointing is especially important for:
38. Thread / Conversation Context¶
Stateful AI applications may need separate execution contexts.
Conceptually:
and:
State must not leak between unrelated users or conversations.
39. Thread Isolation¶
flowchart TD
A[Graph Runtime] --> B[Thread A]
A --> C[Thread B]
A --> D[Thread C]
B --> E[State A]
C --> F[State B]
D --> G[State C]
Production systems must carefully control identity and state isolation.
40. LangGraph for RAG¶
A basic RAG graph:
But production RAG may be:
41. RAG Graph¶
flowchart TD
A[START] --> B[Validate Query]
B --> C[Retrieve]
C --> D{Evidence?}
D -->|No| E[Fallback]
D -->|Yes| F[Generate]
F --> G[Validate]
G --> H[END]
E --> H
42. LangGraph for Agents¶
A common agent graph is:
This creates an explicit agent loop.
43. Agent Graph¶
flowchart TD
A[START] --> B[LLM]
B --> C{Tool Required?}
C -->|No| D[END]
C -->|Yes| E[Tool]
E --> B
This graph is simple, but production systems need:
44. LangGraph + RAG + Tools¶
A more realistic architecture:
flowchart TB
A[User] --> B[Agent Node]
B --> C{Capability}
C -->|Knowledge| D[RAG]
C -->|Customer Data| E[Customer API]
C -->|Transaction| F[Transaction Tool]
D --> G[Agent Node]
E --> G
F --> G
G --> H{Complete?}
H -->|Yes| I[END]
H -->|No| C
The graph explicitly represents the control flow.
45. Deterministic vs Dynamic Graphs¶
A graph can contain:
and:
For example:
This combination is powerful.
46. Control Plane vs Decision Plane¶
A useful architecture distinction:
while:
Example:
The graph can constrain what the model is allowed to do.
47. Guardrails¶
Guardrails can be implemented around graph nodes.
Example:
48. Guardrail Architecture¶
flowchart TD
A[Input] --> B[Input Guardrail]
B --> C[Agent]
C --> D[Tool Authorization]
D --> E[Tool]
E --> F[Result Validation]
F --> G[Output Guardrail]
G --> H[Response]
49. Error Handling¶
Graph execution may encounter:
The graph should define appropriate handling paths.
50. Error Routing¶
flowchart TD
A[Node] --> B{Success?}
B -->|Yes| C[Next Node]
B -->|No| D{Failure Type}
D -->|Retryable| E[Retry]
D -->|Validation| F[Correction]
D -->|Fatal| G[Fail]
E --> A
F --> C
51. Retry Loops¶
A bounded retry pattern:
or:
52. Maximum Iterations¶
Any graph containing loops should have an explicit safety mechanism.
Example:
Otherwise:
could continue indefinitely.
53. Timeouts¶
Production graphs should define time limits for:
Conceptually:
54. Idempotency¶
Graphs can retry or resume execution.
Side-effecting operations should therefore use:
Example:
A retry must not create:
55. Graph Observability¶
A production graph should expose:
56. Graph Trace¶
Example:
Execution: exec-1001
START
↓
validate_request 12ms
↓
retrieve 145ms
↓
agent 820ms
├── search 120ms
└── customer_api 210ms
↓
validate 35ms
↓
END
This provides an execution-level view of the system.
57. Graph Metrics¶
Useful metrics include:
Graph Success Rate
Graph Failure Rate
Node Latency
P95 Execution Time
Loop Count
Retry Count
Tool Calls
LLM Calls
Token Usage
Cost
58. Node-Level Metrics¶
Track:
This helps identify bottlenecks.
59. Graph Versioning¶
Graphs should be versioned when behavior changes.
Example:
Track:
60. State Schema Evolution¶
Changing state structures can be dangerous.
Example:
Production systems should consider:
61. Graph Deployment¶
A production deployment can be:
62. Testing Graphs¶
Test:
Example:
63. Graph Path Testing¶
Suppose:
You should test all important paths:
rather than testing only the happy path.
64. Failure Testing¶
Simulate:
LLM Timeout
Retriever Failure
Tool Timeout
Invalid Tool Result
Unauthorized Tool
Empty Retrieval
Loop Limit
State Corruption
Checkpoint Failure
Verify expected recovery behavior.
65. Human-in-the-Loop Testing¶
Test:
Human workflows require careful state handling.
66. Security¶
A LangGraph application should enforce:
Authentication
Authorization
Tenant Isolation
Tool Authorization
Secret Management
Data Privacy
Input Validation
Output Validation
Audit
Graph structure alone is not a security boundary.
67. Tool Security¶
Never assume:
Instead:
68. Multi-Tenant Graphs¶
A production system may run:
and:
Isolation must be explicit.
69. Graph + Enterprise Services¶
A production architecture may look like:
flowchart TB
A[API Gateway] --> B[Application]
B --> C[LangGraph Runtime]
C --> D[Agent Node]
D --> E[Tool Gateway]
E --> F[Customer Service]
E --> G[Payment Service]
E --> H[Ticket Service]
C --> I[RAG Service]
I --> J[(Vector Store)]
C --> K[LLM Gateway]
K --> L[Model Provider]
C --> M[State Store]
C --> N[Observability]
70. LangGraph vs Traditional Chain¶
Chain¶
Graph¶
A graph is more appropriate when execution contains:
71. LangGraph vs Workflow¶
A traditional workflow might be:
A graph-based workflow can express:
and:
The graph makes transitions and cycles explicit.
72. LangGraph vs Agent¶
An agent is a behavioral pattern:
LangGraph can be used to implement that behavior explicitly:
Therefore:
LangGraph is an orchestration mechanism that can be used to build agents and other stateful workflows.
73. LangGraph + LlamaIndex¶
These frameworks can serve different roles.
For example:
and:
Architecture:
This separation can be useful when orchestration and data/retrieval concerns have different requirements.
74. Framework Boundary¶
A capability-oriented architecture can look like:
and:
This prevents the entire application from becoming framework-specific.
75. Production Architecture Principle¶
Use:
rather than:
The application still needs:
76. When LangGraph Is Useful¶
LangGraph is particularly useful when the application requires:
Stateful Execution
Conditional Branching
Loops
Human Approval
Long-Running Tasks
Agent Orchestration
Tool Calling
Explicit Control Flow
77. When LangGraph May Be Excessive¶
For:
a graph may add unnecessary complexity.
Prefer the simplest architecture that satisfies the requirements.
78. Common Anti-Patterns¶
Anti-Pattern 1 — Everything Is a Node¶
Avoid creating nodes for trivial operations:
Use meaningful application boundaries.
79. Anti-Pattern 2 — Giant State¶
Avoid:
Prefer:
80. Anti-Pattern 3 — Infinite Agent Loops¶
Avoid:
Use:
81. Anti-Pattern 4 — Business Rules Only in LLM¶
Avoid:
Prefer:
82. Anti-Pattern 5 — Direct Tool Access¶
Avoid:
Prefer:
83. Anti-Pattern 6 — No State Isolation¶
Avoid:
Prefer:
84. Anti-Pattern 7 — No Graph Testing¶
Avoid testing only:
Test:
85. Production Graph Checklist¶
Architecture¶
- [ ] Clear graph boundaries
- [ ] Small nodes
- [ ] Explicit edges
- [ ] Minimal state
- [ ] Bounded loops
- [ ] Clear termination
Reliability¶
- [ ] Timeouts
- [ ] Retry policies
- [ ] Backoff
- [ ] Fallback
- [ ] Idempotency
- [ ] Recovery
Security¶
- [ ] Authentication
- [ ] Authorization
- [ ] Tenant isolation
- [ ] Tool authorization
- [ ] Secret management
- [ ] Data privacy
- [ ] Audit
AI¶
- [ ] Prompt versioning
- [ ] Model versioning
- [ ] Tool evaluation
- [ ] RAG evaluation
- [ ] Agent evaluation
Operations¶
- [ ] Tracing
- [ ] Metrics
- [ ] Structured logs
- [ ] Cost tracking
- [ ] Alerts
- [ ] Versioning
86. Key Takeaways¶
- LangGraph provides graph-based orchestration for stateful AI applications.
- Nodes represent units of computation.
- Edges define execution transitions.
- Conditional edges enable explicit routing.
- Graph state maintains execution context.
- Reducers can define how concurrent state updates are combined.
- Loops enable iterative reasoning and correction.
- Loops should always be bounded.
- Checkpointing can support persistence and recovery.
- Human approval can be modeled as an explicit graph stage.
- LangGraph can implement agent loops explicitly.
- LangGraph can also implement deterministic workflows.
- RAG can be represented as a graph.
- Tools can be integrated as graph nodes.
- Graph structure can constrain agent behavior.
- Tool authorization must remain outside model decisions.
- Production graphs require observability.
- Graphs should be versioned.
- State schemas should evolve carefully.
- Multi-tenant state must be isolated.
- LangGraph and LlamaIndex can complement each other.
- LlamaIndex can provide retrieval and data capabilities while LangGraph handles orchestration.
- LangGraph is not a replacement for enterprise security, infrastructure, or governance.
- Simple tasks should not automatically be turned into complex graphs.
- The goal is controlled, observable, reliable orchestration.
📝 Quick Revision Notes¶
LangGraph¶
Node¶
Conditional Graph¶
Agent Loop¶
Production Agent Loop¶
LangGraph + LlamaIndex¶
Reliable Graph¶
❓ Interview Questions¶
Beginner¶
- What is LangGraph?
- Why is graph-based orchestration useful for AI applications?
- What is a node?
- What is an edge?
- What is graph state?
- What are START and END?
- What is a conditional edge?
- Why are loops useful in AI systems?
- What is a checkpoint?
- How is LangGraph different from a simple chain?
Intermediate¶
- How would you build a basic LangGraph?
- How would you implement conditional routing?
- How would you implement an agent loop?
- How would you prevent infinite loops?
- How would you implement retries?
- How would you design graph state?
- What are reducers?
- How would you implement human approval?
- How would you persist graph execution state?
- How would you test graph branches?
- How would you integrate RAG into LangGraph?
- How would you integrate tools?
- How would you monitor graph execution?
- How would you handle graph errors?
Advanced¶
- Design a production LangGraph agent architecture.
- How would you implement durable execution?
- How would you recover a graph after infrastructure failure?
- How would you design multi-tenant graph state?
- How would you prevent unauthorized tool execution?
- How would you combine LangGraph and LlamaIndex?
- How would you version graph state?
- How would you migrate checkpoint data after a state-schema change?
- How would you design a bounded autonomous agent?
- How would you implement graph-level cost controls?
- How would you trace a graph containing RAG, agents, and tools?
- How would you design high-availability graph execution?
- How would you test every important execution path?
- How would you prevent graph nodes from becoming tightly coupled?
- How would you design a framework-independent orchestration layer?
- When would you choose LangGraph over a simpler workflow implementation?
🛠️ Practical Exercise¶
Build a simple customer-support graph.
Requirements:
1. Receive customer query
2. Validate query
3. Classify request
4. Retrieve knowledge
5. Generate answer
6. Validate answer
7. Return response
Architecture:
flowchart TD
A[START] --> B[Validate]
B --> C[Classify]
C --> D[Retrieve]
D --> E[Generate]
E --> F[Validate]
F --> G{Valid?}
G -->|Yes| H[END]
G -->|No| E
Add:
🧪 Agent Exercise¶
Build:
Tools:
The graph must enforce:
🚀 Production Exercise¶
Extend the graph with:
Authentication
Authorization
Tenant Isolation
RAG
Tool Gateway
Human Approval
Persistence
Observability
Audit
Cost Tracking
Architecture:
flowchart TB
A[User] --> B[API Gateway]
B --> C[Authentication]
C --> D[Authorization]
D --> E[LangGraph Runtime]
E --> F[Agent Node]
F --> G[RAG Node]
F --> H[Tool Gateway]
H --> I[Enterprise APIs]
G --> J[(Vector Store)]
E --> K[Human Approval]
E --> L[State / Checkpoint Store]
E --> M[Observability]
E --> N[Audit]
📊 Evaluation Exercise¶
Create at least:
Cover:
Normal Requests
Empty Retrieval
Tool Failure
LLM Failure
Authorization Failure
Human Rejection
Retry
Loop Limit
Timeout
State Recovery
Measure:
Task Completion
Correct Routing
Tool Selection
Tool Argument Accuracy
Graph Success Rate
P95 Latency
Token Usage
Cost
🏢 Enterprise Architecture Challenge¶
Design a LangGraph-based platform supporting:
500 Tenants
100+ Tools
Multiple LLM Providers
RAG
Human Approval
Long-Running Tasks
Multiple Agent Types
Required:
API Gateway
Identity
Authorization
Graph Runtime
State Store
Checkpointing
RAG Layer
Tool Gateway
LLM Gateway
Observability
Audit
Evaluation
Cost Controls
🧠 Final Architecture Challenge¶
Design:
flowchart TB
U[Users] --> G[API Gateway]
G --> I[Identity]
I --> A[Authorization]
A --> APP[Enterprise AI Application]
APP --> LG[LangGraph Runtime]
LG --> N1[Validation Node]
N1 --> N2[Agent Node]
N2 --> R[RAG Node]
N2 --> T[Tool Gateway]
R --> LI[LlamaIndex]
LI --> VS[(Vector Store)]
T --> ES[Enterprise Services]
N2 --> D{Decision}
D -->|Continue| N2
D -->|Approval| H[Human Approval]
H --> D
D -->|Complete| END[END]
LG --> S[(Checkpoint / State Store)]
LG --> O[Observability]
LG --> AU[Audit]
LG --> EVAL[Evaluation]
Design the system for:
📚 References & Further Reading¶
Recommended areas for further study:
- LangGraph Graph Architecture
- LangGraph State
- LangGraph Nodes
- LangGraph Edges
- Conditional Routing
- LangGraph Persistence
- Checkpointing
- Human-in-the-Loop
- LangGraph Streaming
- LangGraph Agent Patterns
- LangGraph Tool Calling
- LangGraph RAG
- LangGraph Evaluation
- LangGraph Production Deployment
- Stateful AI Systems
- Graph-Based Orchestration
- Durable Execution
- Agent Architecture
- Enterprise Workflow Architecture
LangGraph evolves rapidly. Before implementing production systems, verify the current graph APIs, state semantics, reducers, checkpointing behavior, streaming interfaces, persistence mechanisms, agent patterns, and deployment guidance against the official documentation for the exact LangGraph version used by your project.
🧭 Chapter Navigation¶
⬅️ Previous: 16. LlamaIndex Limitations and Trade-offs
📚 Part VIII Index: AI Engineering Frameworks & Tooling
➡️ Next: 18. Graph Based Agent Architecture
Enterprise AI Engineering Handbook
Building Production-Grade Enterprise AI Systems — One Chapter at a Time.