Skip to main content
When you build an agent with LangGraph, you will first break it apart into discrete steps called nodes. Then, you will describe the different decisions and transitions from each of your nodes. Finally, you connect nodes together through a shared state that each node can read from and write to. In this walkthrough, we’ll guide you through the thought process of building a customer support email agent with LangGraph.

Start with the process you want to automate

Imagine that you need to build an AI agent that handles customer support emails. Your product team has given you these requirements:
To implement an agent in LangGraph, you will usually follow the same five steps.

Step 1: Map out your workflow as discrete steps

Start by identifying the distinct steps in your process. Each step will become a node (a function that does one specific thing). Then, sketch how these steps connect to each other. The arrows in this diagram show possible paths, but the actual decision of which path to take happens inside each node. Now that we’ve identified the components in our workflow, let’s understand what each node needs to do:
  • Read Email: Extract and parse the email content
  • Classify Intent: Use an LLM to categorize urgency and topic, then route to appropriate action
  • Doc Search: Query your knowledge base for relevant information
  • Bug Track: Create or update issue in tracking system
  • Draft Reply: Generate an appropriate response
  • Human Review: Escalate to human agent for approval or handling
  • Send Reply: Dispatch the email response
Notice that some nodes make decisions about where to go next (Classify Intent, Draft Reply, Human Review), while others always proceed to the same next step (Read Email always goes to Classify Intent, Doc Search always goes to Draft Reply).

Step 2: Identify what each step needs to do

For each node in your graph, determine what type of operation it represents and what context it needs to work properly.

LLM steps

Use when you need to understand, analyze, generate text, or make reasoning decisions

Data steps

Use when you need to retrieve information from external sources

Action steps

Use when you need to perform external actions

User input steps

Use when you need human intervention

LLM steps

When a step needs to understand, analyze, generate text, or make reasoning decisions:
  • Static context (prompt): Classification categories, urgency definitions, response format
  • Dynamic context (from state): Email content, sender information
  • Desired outcome: Structured classification that determines routing
  • Static context (prompt): Tone guidelines, company policies, response templates
  • Dynamic context (from state): Classification results, search results, customer history
  • Desired outcome: Professional email response ready for review

Data steps

When a step needs to retrieve information from external sources:
  • Parameters: Query built from intent and topic
  • Retry strategy: Yes, with exponential backoff for transient failures
  • Caching: Could cache common queries to reduce API calls
  • Parameters: Customer email or ID from state
  • Retry strategy: Yes, but with fallback to basic info if unavailable
  • Caching: Yes, with time-to-live to balance freshness and performance

Action steps

When a step needs to perform an external action:
  • When to execute node: After approval (human or automated)
  • Retry strategy: Yes, with exponential backoff for network issues
  • Should not cache: Each send is a unique action
  • When to execute node: Always when intent is “bug”
  • Retry strategy: Yes, critical to not lose bug reports
  • Returns: Ticket ID to include in response

User input steps

When a step needs human intervention:
  • Context for decision: Original email, draft response, urgency, classification
  • Expected input format: Approval boolean plus optional edited response
  • When triggered: High urgency, complex issues, or quality concerns

Step 3: Design your state

State is the shared memory accessible to all nodes in your agent. Think of it as the notebook your agent uses to keep track of everything it learns and decides as it works through the process.

What belongs in state?

Ask yourself these questions about each piece of data:

Include in state

Does it need to persist across steps? If yes, it goes in state.

Don't store

Can you derive it from other data? If yes, compute it when needed instead of storing it in state.
For our email agent, we need to track:
  • The original email and sender info (can’t reconstruct these later)
  • Classification results (needed by multiple later/downstream nodes)
  • Search results and customer data (expensive to re-fetch)
  • The draft response (needs to persist through review)
  • Execution metadata (for debugging and recovery)

Keep state raw, format prompts on-demand

A key principle: your state should store raw data, not formatted text. Format prompts inside nodes when you need them.
This separation means:
  • Different nodes can format the same data differently for their needs
  • You can change prompt templates without modifying your state schema
  • Debugging is clearer—you see exactly what data each node received
  • Your agent can evolve without breaking existing state
Let’s define our state:
Notice that the state contains only raw data—no prompt templates, no formatted strings, no instructions. The classification output is stored as a single dictionary, straight from the LLM.

Step 4: Build your nodes

Now we implement each step as a function. A node in LangGraph is just a Python function that takes the current state and returns updates to it.

Handle errors appropriately

Different errors need different handling strategies:
Add a retry policy to automatically retry network issues and rate limits.Combine with timeout= to cap each attempt. See Fault tolerance for the full lifecycle.

Implementing our email agent nodes

We’ll implement each node as a simple function. Remember: nodes take state, do work, and return updates.

Step 5: Wire it together

Now we connect our nodes into a working graph. Since our nodes handle their own routing decisions, we only need a few essential edges. To enable human-in-the-loop with interrupt(), we need to compile with a checkpointer to save state between runs:

Graph compilation code

The graph structure is minimal because routing happens inside nodes through Command objects. Each node declares where it can go using type hints like Command[Literal["node1", "node2"]], making the flow explicit and traceable.

Try out your agent

Let’s run our agent with an urgent billing issue that needs human review:
The graph pauses when it hits interrupt(), saves everything to the checkpointer, and waits. It can resume days later, picking up exactly where it left off. The thread_id ensures all state for this conversation is preserved together.

Summary and next steps

Key Insights

Building this email agent has shown us the LangGraph way of thinking:

Break into discrete steps

Each node does one thing well. This decomposition enables streaming progress updates, durable execution that can pause and resume, and clear debugging since you can inspect state between steps.

State is shared memory

Store raw data, not formatted text. This lets different nodes use the same information in different ways.

Nodes are functions

They take state, do work, and return updates. When they need to make routing decisions, they specify both the state updates and the next destination.

Errors are part of the flow

Transient failures get retries, LLM-recoverable errors loop back with context, user-fixable problems pause for input, and unexpected errors bubble up for debugging.

Human input is first-class

The interrupt() function pauses execution indefinitely, saves all state, and resumes exactly where it left off when you provide input. When combined with other operations in a node, it must come first.

Graph structure emerges naturally

You define the essential connections, and your nodes handle their own routing logic. This keeps control flow explicit and traceable - you can always understand what your agent will do next by looking at the current node.

Advanced considerations

This section explores the trade-offs in node granularity design. Most applications can skip this and use the patterns shown above.
You might wonder: why not combine Read Email and Classify Intent into one node?Or why separate Doc Search from Draft Reply?The answer involves trade-offs between resilience and observability.The resilience consideration: LangGraph’s persistence layer creates checkpoints at node boundaries. When a workflow resumes after an interruption or failure, it starts from the beginning of the node where execution stopped. Smaller nodes mean more frequent checkpoints, which means less work to repeat if something goes wrong. If you combine multiple operations into one large node, a failure near the end means re-executing everything from the start of that node.Why we chose this breakdown for the email agent:
  • Isolation of external services: Doc Search and Bug Track are separate nodes because they call external APIs. If the search service is slow or fails, we want to isolate that from the LLM calls. We can add retry policies to these specific nodes without affecting others.
  • Intermediate visibility: Having Classify Intent as its own node lets us inspect what the LLM decided before taking action. This is valuable for debugging and monitoring—you can see exactly when and why the agent routes to human review.
  • Different failure modes: LLM calls, database lookups, and email sending have different retry strategies. Separate nodes let you configure these independently.
  • Reusability and testing: Smaller nodes are easier to test in isolation and reuse in other workflows.
A different valid approach: You could combine Read Email and Classify Intent into a single node. You’d lose the ability to inspect the raw email before classification and would repeat both operations on any failure in that node. For most applications, the observability and debugging benefits of separate nodes are worth the trade-off.Application-level concerns: The caching discussion in Step 2 (whether to cache search results) is an application-level decision, not a LangGraph framework feature. You implement caching within your node functions based on your specific requirements—LangGraph doesn’t prescribe this.Performance considerations: More nodes doesn’t mean slower execution. LangGraph writes checkpoints in the background by default (async durability mode), so your graph continues running without waiting for checkpoints to complete. This means you get frequent checkpoints with minimal performance impact. You can adjust this behavior if needed—use "exit" mode to checkpoint only at completion, or "sync" mode to block execution until each checkpoint is written.

Where to go from here

This was an introduction to thinking about building agents with LangGraph. You can extend this foundation with:

Human-in-the-loop patterns

Learn how to add tool approval before execution, batch approval, and other patterns

Subgraphs

Create subgraphs for complex multi-step operations

Streaming

Add streaming to show real-time progress to users

Observability

Add observability with LangSmith for debugging and monitoring

Tool Integration

Integrate more tools for web search, database queries, and API calls

Retry Logic

Implement retry logic with exponential backoff for failed operations