A complete tutorial for LangGraph.js - from basics to building production agents.
- Introduction
- Core Concepts
- Installation & Setup
- Tutorial Files
- Building Your First Graph
- State Management
- Conditional Routing
- Tool Calling
- Memory & Persistence
- Building Agents
- Human-in-the-Loop
- Integration with Apex CLI
LangGraph is a library for building stateful, multi-actor applications with LLMs. It extends LangChain with:
- Graph-based orchestration - Define workflows as nodes and edges
- Built-in state management - Automatic state passing between nodes
- Persistence - Save and resume conversations
- Human-in-the-loop - Pause for user input
- Streaming - Real-time responses
| Use Case | Why LangGraph? |
|---|---|
| Chatbots with memory | Built-in state persistence |
| Multi-step agents | Graph-based planning |
| Tool-using AI | Native tool node support |
| Complex workflows | Conditional branching |
| Human oversight | Interrupt points |
LangChain (Chains):
Input → Step1 → Step2 → Step3 → Output
(linear, no loops, no conditions)
LangGraph (Graphs):
Input → Node1 → [condition] → Node2 → Node3
↓ ↑
Node4 ────────────┘
(loops, conditions, parallel, stateful)
State is the data that flows through your graph. Every node receives and can modify the state.
// State is defined using Annotation.Root
const MyState = Annotation.Root({
messages: Annotation({ reducer: (a, b) => [...a, ...b], default: () => [] }),
count: Annotation({ reducer: (_, b) => b, default: () => 0 }),
});Nodes are functions that transform state. They receive state and return state updates.
// A node is just an async function
async function myNode(state) {
// Do something with state
return { count: state.count + 1 }; // Return updates
}Edges connect nodes and define the flow of your graph.
graph.addEdge("node1", "node2"); // Always go node1 → node2
graph.addConditionalEdges("node1", routingFn, { ... }); // ConditionalGraph is the container that holds nodes and edges together.
const graph = new StateGraph(MyState);
graph.addNode("name", nodeFunction);
graph.addEdge(START, "name");
graph.addEdge("name", END);
const app = graph.compile();npm install @langchain/core @langchain/google-genai @langchain/langgraphGOOGLE_API_KEY=your-gemini-api-key// Core LangGraph imports
import { StateGraph, START, END, Annotation } from "@langchain/langgraph";
// LangChain message types
import { HumanMessage, AIMessage, SystemMessage } from "@langchain/core/messages";
// LLM provider
import { ChatGoogleGenerativeAI } from "@langchain/google-genai";
// For tool definitions
import { DynamicStructuredTool } from "@langchain/core/tools";
import { z } from "zod";This tutorial includes the following files:
| File | Description |
|---|---|
01-hello-world.js |
Simplest possible LangGraph |
02-state-basics.js |
Understanding state and reducers |
03-llm-node.js |
Adding LLM to your graph |
04-conditional-edges.js |
Routing and branching |
05-tool-calling.js |
Giving tools to LLM |
06-react-agent.js |
ReAct pattern agent |
07-memory-checkpointer.js |
Persistence and memory |
08-human-in-loop.js |
Pausing for human input |
09-full-agent.js |
Complete production agent |
See: 01-hello-world.js
The simplest LangGraph that just passes data through nodes.
See: 02-state-basics.js
Understanding how state flows and how reducers work.
See: 04-conditional-edges.js
Making decisions in your graph based on state.
See: 05-tool-calling.js and 06-react-agent.js
How to give tools to your LLM.
See: 07-memory-checkpointer.js
Saving and resuming conversations.
See: 09-full-agent.js
Complete agent implementation.
See: 08-human-in-loop.js
Pausing for human approval.
After completing this tutorial, you can integrate LangGraph into Apex CLI by:
- Copy the patterns from tutorial files
- Create
lib/langgraph/directory - Build chat, tools, and agent commands
- Use memory for conversation persistence
See the docs/implement_* folders for full integration guides.