LangChain, LangGraph, and Agentic AI Patterns
Introduction
The world of AI is rapidly evolving, and with the rise of large language models (LLMs), the need for robust, flexible, and controllable agentic systems has never been greater. Two of the most exciting frameworks in this space are LangChain and LangGraph. These libraries empower developers to build advanced, agentic AI applications that can reason, plan, use tools, collaborate, and even reflect on their own actions.
In this post, I’ll take you on a comprehensive journey through LangChain and LangGraph, exploring their architectures, the agentic AI patterns they enable, and how to use them in practice. We’ll cover everything from the basics to advanced multi-agent workflows, with plenty of code, diagrams, best practices, and real-world use cases. Whether you’re building a knowledge assistant, a data analysis pipeline, or a fully autonomous agentic system, this guide will serve as your definitive technical reference.
LangChain Overview
What is LangChain?
LangChain is a powerful open-source framework designed to make it easy to build applications with LLMs. It provides abstractions for chains, agents, tools, memory, and more, allowing you to compose complex workflows that leverage the reasoning and generative capabilities of modern language models.
Key Concepts:
- Chains: Sequences of calls (to LLMs, APIs, or other functions) that form a deterministic workflow.
- Agents: Systems that use an LLM to decide their own control flow, enabling dynamic, tool-using, and autonomous behaviors.
- Tools: External functions or APIs that agents can call (e.g., web search, calculators, databases).
- Memory: Mechanisms for storing and recalling information across interactions (short-term, long-term, or custom).
LangChain is available in both Python and TypeScript/JavaScript, and is widely used for building chatbots, RAG systems, tool-using agents, and more.
Chains vs. Agents
- Chains are like scripts: they execute a fixed sequence of steps every time.
- Agents are like autonomous workers: they use an LLM to decide what to do next, which tool to use, or when to stop.
LangChain’s agent abstractions make it easy to build systems that can reason, use tools, and adapt to new tasks.
LangGraph Overview
What is LangGraph?
LangGraph is a next-generation framework for building graph-based agentic workflows. While LangChain introduced the world to chains and agents, LangGraph takes things further by letting you define your application as a graph of nodes and edges, where each node is a function (often an LLM call or tool) and edges define the control flow.
Why LangGraph?
- Controllability: Explicitly define the flow of your application as a graph.
- Persistence: Built-in support for checkpointing, state management, and time travel.
- Streaming: First-class support for streaming outputs and events.
- Debugging: Visualize and debug your agentic workflows with LangGraph Studio.
- Modularity: Compose complex systems from reusable subgraphs and nodes.
LangGraph is especially powerful for building advanced agentic systems: multi-agent collaboration, self-reflective RAG, planning and execution, human-in-the-loop, and more.
Core Concepts
- State: A shared data structure representing the current snapshot of your application (e.g., messages, memory, documents).
- Nodes: Functions that perform work (LLM calls, tool invocations, grading, etc.).
- Edges: Define which node(s) to execute next, based on the current state (can be conditional or fixed).
- Reducers: Functions that specify how updates from nodes are applied to the state.
Example: Minimal LangGraph Workflow (TypeScript)
1import { StateGraph, Annotation, START, END } from "@langchain/langgraph";23const State = Annotation.Root({4 input: Annotation<string>,5 output: Annotation<string>,6});78const nodeA = async (state: typeof State.State) => {9 return { output: `Hello, ${state.input}!` };10};1112const graph = new StateGraph(State)13 .addNode("nodeA", nodeA)14 .addEdge(START, "nodeA")15 .addEdge("nodeA", END)16 .compile();1718await graph.invoke({ input: "World" }); // { output: "Hello, World!" }
LangGraph’s explicit graph structure makes it easy to build, debug, and extend complex agentic workflows.
Agentic AI Patterns
What is an Agent in AI?
An agent is a system that uses an LLM to decide the control flow of an application. Unlike a chain (which always runs the same steps), an agent can choose which tools to use, when to stop, and how to adapt to new situations. This autonomy enables more flexible, powerful, and intelligent applications.
Why Agentic Patterns?
- Dynamic Control Flow: Agents can make decisions, branch, and adapt.
- Tool Use: Agents can call external APIs, search engines, databases, and more.
- Collaboration: Multiple agents can work together, each specializing in different tasks.
- Reflection: Agents can grade, critique, and improve their own outputs.
- Human-in-the-Loop: Agents can pause for human feedback or approval.
Common Agentic Patterns
1. ReAct (Reasoning and Acting)
- The agent alternates between reasoning (LLM) and acting (tool use).
- Example: “To answer this, I need to search the web. [calls search tool] Now, based on the results…”
2. Plan-and-Execute
- The agent first plans a sequence of steps, then executes them (possibly using tools).
- Example: “Plan: 1) Find the winner of the 2023 Australian Open. 2) Find their hometown.”
3. Tool-Using Agents
- The agent decides which tool to use at each step (search, calculator, database, etc.).
4. Router Agents
- The agent routes tasks to specialized sub-agents or workflows.
5. Multi-Agent Collaboration
- Multiple agents (or agent types) work together, often in a supervisor/worker or divide-and-conquer pattern.
6. Reflection and Self-Correction (CRAG, Self-RAG)
- The agent grades or critiques its own outputs, possibly re-running steps if needed.
7. Human-in-the-Loop
- The agent pauses for human input, approval, or correction at key steps.
Diagram: Agentic Patterns
1[User] -> [Agent] -> [Tool/LLM] -> [Reflection/Grader] -> [Output]2 |-> [Sub-Agent/Worker]3 |-> [Human-in-the-Loop]
Practical Usage: LangChain and LangGraph in Practice
Let’s dive into how to use these frameworks to build real-world agentic AI systems. I’ll walk through several patterns, with code and explanations.
1. Retrieval-Augmented Generation (RAG) with LangGraph
RAG is a foundational pattern for knowledge assistants: retrieve relevant documents, then generate an answer grounded in those documents.
Step-by-Step: Building a RAG Agent in LangGraph
- Load Documents: Use loaders (e.g., CheerioWebBaseLoader) to fetch and parse web pages.
- Split Documents: Use text splitters to chunk documents for retrieval.
- Vector Store: Embed and index chunks for similarity search.
- Retriever: Query the vector store for relevant chunks.
- Agent State: Track messages, retrieved docs, and conversation history.
- Nodes: Define functions for retrieval, grading, rewriting, and generation.
- Edges: Control flow: retrieve -> grade -> generate or rewrite -> …
Example: Minimal RAG Graph (TypeScript)
1import { CheerioWebBaseLoader } from "@langchain/community/document_loaders/web/cheerio";2import { RecursiveCharacterTextSplitter } from "@langchain/textsplitters";3import { MemoryVectorStore } from "langchain/vectorstores/memory";4import { OpenAIEmbeddings } from "@langchain/openai";5import { StateGraph, Annotation, START, END } from "@langchain/langgraph";67// 1. Load and split documents8const urls = ["https://lilianweng.github.io/posts/2023-06-23-agent/"];9const docs = await Promise.all(urls.map(url => new CheerioWebBaseLoader(url).load()));10const docsList = docs.flat();11const textSplitter = new RecursiveCharacterTextSplitter({ chunkSize: 500, chunkOverlap: 50 });12const docSplits = await textSplitter.splitDocuments(docsList);1314// 2. Create vector store and retriever15const vectorStore = await MemoryVectorStore.fromDocuments(docSplits, new OpenAIEmbeddings());16const retriever = vectorStore.asRetriever();1718// 3. Define state and nodes19const GraphState = Annotation.Root({20 question: Annotation<string>(),21 documents: Annotation<any[]>({ reducer: (x, y) => y ?? x ?? [] }),22 answer: Annotation<string>(),23});2425const retrieve = async (state) => ({ documents: await retriever.invoke(state.question) });26const generate = async (state) => ({ answer: `Answer based on: ${state.documents.map(d => d.pageContent).join(" ")}` });2728// 4. Build the graph29const workflow = new StateGraph(GraphState)30 .addNode("retrieve", retrieve)31 .addNode("generate", generate)32 .addEdge(START, "retrieve")33 .addEdge("retrieve", "generate")34 .addEdge("generate", END)35 .compile();3637const result = await workflow.invoke({ question: "What are agentic patterns?" });38console.log(result.answer);
Advanced: Corrective RAG (CRAG), Self-RAG
- CRAG: Grade retrieved docs for relevance; if not relevant, rewrite query or use web search.
- Self-RAG: Reflect on the answer; if not grounded or useful, re-run steps.
Key Nodes:
gradeDocuments: LLM grades each doc for relevance.transformQuery: LLM rewrites the query for better retrieval.webSearch: Fallback to web search if retrieval fails.generateGenerationVDocumentsGrade: Grade if answer is grounded in docs.generateGenerationVQuestionGrade: Grade if answer is useful for the question.
Edge Logic: Use conditional edges to decide next steps based on grades.
2. Multi-Agent Collaboration
Pattern: Divide-and-conquer with specialized agents (e.g., Researcher + Chart Generator).
Example: Researcher + Chart Generator
- Researcher Agent: Uses web search to gather data.
- Chart Generator Agent: Uses D3.js (via a tool) to generate charts from data.
- State: Tracks messages and sender.
- Edge Logic: Route between agents based on task completion.
Code Sketch:
1import { StateGraph, Annotation, START, END } from "@langchain/langgraph";2// ... (define agents, tools, and nodes as in the context)34const AgentState = Annotation.Root({5 messages: Annotation<any[]>({ reducer: (x, y) => x.concat(y) }),6 sender: Annotation<string>({ reducer: (x, y) => y ?? x ?? "user" }),7});89// Define nodes for Researcher, ChartGenerator, and tool calls10// ...1112const workflow = new StateGraph(AgentState)13 .addNode("Researcher", researchNode)14 .addNode("ChartGenerator", chartNode)15 .addNode("call_tool", toolNode)16 // ... (add conditional edges for routing)17 .addEdge(START, "Researcher")18 .compile();
Advanced: Hierarchical Agent Teams
- Compose subgraphs for teams (e.g., research team, writing team).
- Use a supervisor agent to delegate tasks.
- Enables scalable, modular multi-agent systems.
3. Planning and Reasoning Patterns
Pattern: ReWOO (plan, execute, solve)
- Planner Node: LLM generates a plan (sequence of tool calls).
- Tool Executor Node: Executes each step, with variable substitution.
- Solver Node: LLM generates the final answer based on evidence.
Code Sketch:
1// ... (define state, planner, tool executor, solver as in context)2const workflow = new StateGraph(GraphState)3 .addNode("plan", getPlan)4 .addNode("tool", toolExecution)5 .addNode("solve", solve)6 .addEdge(START, "plan")7 .addEdge("plan", "tool")8 .addConditionalEdges("tool", routeFn)9 .addEdge("solve", END)10 .compile();
4. Human-in-the-Loop and Evaluation
Pattern: Interrupts, breakpoints, simulation
- Simulated User: LLM acts as a user for chatbot evaluation.
- Chatbot Node: LLM acts as the bot.
- Edge Logic: Continue or end based on conversation length or special tokens.
Code Sketch:
1// ... (define simulatedUserNode, chatBotNode, shouldContinue)2const workflow = new StateGraph(MessagesAnnotation)3 .addNode('user', simulatedUserNode)4 .addNode('chatbot', chatBotNode)5 .addEdge('chatbot', 'user')6 .addConditionalEdges('user', shouldContinue, { [END]: END, continue: 'chatbot' })7 .addEdge(START, 'chatbot')8 .compile();
Advanced Concepts and Best Practices
Memory in Agentic Systems
- Short-term memory: In-context (conversation history)
- Long-term memory: External vector stores, databases
- Sensory memory: Embeddings for raw inputs (text, images)
State Management and Reducers
- Use reducers to control how state updates are applied (e.g., append, overwrite, merge).
- For message histories, use
messagesStateReducerfor robust handling.
Persistence, Checkpointing, and Time Travel
- Use built-in checkpointers to save and resume graph state.
- Enables human-in-the-loop, debugging, and fault-tolerance.
Streaming, Debugging, and Visualization
- Stream outputs and events for responsive UX.
- Use LangGraph Studio for graph visualization and step-by-step debugging.
Subgraphs and Modularity
- Compose complex systems from reusable subgraphs.
- Enables hierarchical agent teams and scalable architectures.
Performance Considerations
- Minimize unnecessary LLM/tool calls.
- Use conditional edges to avoid redundant steps.
- Profile and monitor token usage and latency.
Troubleshooting
- Use graph visualization to debug control flow.
- Add logging to nodes for step-by-step tracing.
- Handle edge cases (e.g., empty retrieval, tool failures) with fallback logic.
Real-World Use Cases
Knowledge Assistants
- RAG, CRAG, Self-RAG for document Q&A, research, and summarization.
Data Analysis and Visualization
- Multi-agent workflows for data gathering, analysis, and chart generation.
Automated Research and Report Generation
- Planning, tool use, and multi-agent collaboration for end-to-end research pipelines.
Customer Support Bots
- Simulation and evaluation with human-in-the-loop for robust, user-friendly bots.
Workflow Automation
- Planning, execution, and tool use for automating business processes and decision-making.
Conclusion
LangChain and LangGraph represent the cutting edge of agentic AI development. LangChain makes it easy to build chains and agents, while LangGraph empowers you to design, debug, and deploy complex, controllable agentic workflows as graphs. By mastering these frameworks and the agentic patterns they enable, you can build AI systems that are not only powerful and flexible, but also reliable, debuggable, and ready for real-world deployment.
When to use which?
- Use LangChain for simple chains, basic agents, and rapid prototyping.
- Use LangGraph for advanced agentic systems: multi-agent, self-reflective, human-in-the-loop, or production-grade workflows.
The future of AI is agentic, and with LangChain and LangGraph, you have the tools to build it. Happy hacking!