Getting Started with LangGraph
Before starting, you need Python 3.9 or later and an API key from an LLM provider (OpenAI, Anthropic, or another supported provider). LangGraph works with any LLM that LangChain supports, so use whichever provider you are most comfortable with.
Install LangGraph and Dependencies
Install the core packages using pip. You need langgraph for the orchestration framework, langchain for the LLM integration layer, and your chosen provider's package for model access. For example, if using OpenAI, install langchain-openai. If using Anthropic, install langchain-anthropic.
The installation is straightforward: pip install langgraph langchain langchain-openai (substituting your preferred provider). This installs the core framework, the integration layer, and the model provider library. Set your API key as an environment variable so the provider library can authenticate requests.
Verify the installation by importing langgraph in a Python shell. If the import succeeds without errors, you are ready to proceed.
Define Your State Schema
Every LangGraph application starts with a state definition. Create a TypedDict class that declares all the fields your agent needs to track during execution. For a basic conversational agent, you need at minimum a messages field that stores the conversation history.
The messages field should be annotated with the add_messages reducer from langgraph. This reducer appends new messages to the existing list rather than replacing it, which is essential for maintaining conversation history across multiple agent turns. Without this reducer, each node would overwrite the previous conversation, losing all context.
Your state class might look conceptually like: a TypedDict called AgentState with a single field, messages, annotated with the add_messages reducer and typed as a list of messages. As your agent grows more complex, you can add additional fields for tool results, user preferences, intermediate calculations, or any other data your workflow needs to track.
Create Your Agent Nodes
Nodes are Python functions that receive the current state and return partial state updates. For a conversational agent, you need at least two nodes: one that calls the LLM and one that executes tools.
The LLM node takes the current state, extracts the messages, sends them to your chosen model, and returns the model's response as a new message in the state. If the model requests a tool call, the response will contain tool call information that the next node can process.
The tool node receives tool call requests from the LLM's response, executes the requested tools, and returns the tool results as messages. LangGraph's ToolNode utility handles this automatically for tools defined using LangChain's tool interface, but you can also write custom tool execution logic.
Each node function follows the same pattern: receive state as input, perform computation, return a dictionary of state updates. The framework handles merging the updates into the shared state using the reducer functions you defined in your schema.
Build and Connect the Graph
Create a StateGraph instance using your state class. Add your nodes with descriptive names. Then connect them with edges that define the execution flow.
Set the entry point to your LLM node, so incoming messages go to the model first. Add a conditional edge from the LLM node that checks whether the model requested a tool call. If tools were requested, route to the tool node. If no tools were requested (meaning the model produced a final response), route to the END sentinel to complete execution.
Add a regular edge from the tool node back to the LLM node, creating a cycle. This loop lets the agent call a tool, observe the result, and then decide whether to call another tool or produce a final response. The cycle continues until the LLM produces a response without any tool calls.
This simple graph, with an LLM node, a tool node, a conditional edge, and a cycle, is the foundation of most LangGraph agents. More complex agents add additional nodes and edges, but the pattern remains the same.
Compile with a Checkpointer
Compile your graph by calling the compile method and passing a checkpointer instance. For development, use MemorySaver, which stores checkpoints in memory with no external infrastructure needed.
Compiling with a checkpointer activates state persistence. After each node completes, the full state is saved as a checkpoint. This enables conversation memory across multiple invocations (the agent remembers previous messages), fault tolerance (failed steps can be retried from the last checkpoint), and time-travel debugging (you can inspect any historical state).
For production deployments, you would swap MemorySaver for PostgresSaver or another durable backend. The graph code stays the same, only the checkpointer changes.
Run Your Agent
Invoke the compiled graph by calling its invoke method with an input dictionary containing the user's message and a config dictionary specifying a thread ID. The thread ID groups related checkpoints together, enabling multi-turn conversations that persist across invocations.
The agent processes the message through the graph: the LLM receives the conversation history, generates a response (possibly with tool calls), tools execute if needed, and the cycle repeats until the LLM produces a final answer. The output includes the complete updated state with all messages.
To continue the conversation, invoke the graph again with the same thread ID and a new user message. The checkpointer loads the previous state, so the agent has full context of the prior exchange. This is how LangGraph maintains stateful conversations without the developer managing context manually.
Next Steps
With a basic agent running, there are several directions to explore. Add more tools to expand what your agent can do, such as web search, database queries, or file operations. Add interrupt gates for human-in-the-loop approval at critical decision points. Structure your graph with subgraphs to organize complex multi-step workflows. Open the agent in LangGraph Studio for visual debugging and time-travel through execution history. And when you are ready for production, swap MemorySaver for PostgresSaver and deploy using LangGraph Cloud or your own infrastructure.
A basic LangGraph agent requires a state schema with a messages reducer, an LLM node, a tool node, conditional routing between them, and a checkpointer for state persistence. This foundation supports everything from simple chatbots to complex multi-agent production systems.