Hands-On 7 min read Updated Sep 7, 2026

Build Your First LangGraph Agent

The clearest way to understand LangGraph is to build the smallest useful thing with it: an agent that can decide, on its own, whether it needs to call a tool before it answers you. That's the classic "agent loop," and once you've built one, every larger LangGraph project is just this same pattern with more nodes.

What we're building

A small assistant with access to one tool - say, a weather lookup. When you ask it something that needs the tool, it should call the tool, look at the result, and then answer. When you ask it something that doesn't need the tool, it should just answer directly. That branching decision is exactly what a conditional edge is for.

has tool_calls tool result no tool_calls START Agent calls the model Tool runs the lookup END

The Agent node re-runs after every tool call, until the model decides it has enough to answer.

Step 1: define the state

The state for a chat-style agent is almost always built around a running list of messages - the conversation so far, including tool calls and their results. LangGraph's prebuilt add_messages reducer handles appending new messages to that list automatically, so you don't have to write that merge logic yourself.

from typing import Annotated, TypedDict
from langgraph.graph.message import add_messages

class AgentState(TypedDict):
    messages: Annotated[list, add_messages]

Step 2: write the agent node

The agent node's job is simple: take the message history, call the model (with the tool made available to it), and return whatever the model responded with - which might be a normal answer, or might be a request to call the tool.

def agent_node(state: AgentState):
    response = model_with_tools.invoke(state["messages"])
    return {"messages": [response]}

Step 3: write the tool node

You don't need to hand-write this one - LangGraph ships a prebuilt ToolNode that looks at the model's most recent tool-call request, actually runs the matching tool, and appends the result back into the message list in the format the model expects.

from langgraph.prebuilt import ToolNode

tool_node = ToolNode([get_weather])

Step 4: wire the conditional loop

This is the part that makes it an agent rather than a one-shot call: after the agent node runs, check whether the model's response included a tool call. If it did, route to the tool node and then straight back to the agent so it can see the result. If it didn't, the model was ready to answer, so route to END.

from langgraph.graph import StateGraph, START, END
from langgraph.prebuilt import tools_condition

graph = StateGraph(AgentState)
graph.add_node("agent", agent_node)
graph.add_node("tools", tool_node)

graph.add_edge(START, "agent")
graph.add_conditional_edges("agent", tools_condition)   # -> "tools" or END
graph.add_edge("tools", "agent")

app = graph.compile()

tools_condition is another small prebuilt helper - it just checks the agent's last message for a tool call and routes accordingly, so you don't have to write that check by hand for the common case.

Step 5: run it

result = app.invoke({"messages": [("user", "What's the weather in Mumbai right now?")]})
print(result["messages"][-1].content)

Ask it something the tool can't help with - "summarize this in one sentence" - and it skips the tool node entirely and answers directly, because the conditional edge only routes to the tool node when the model actually asked for it.

Try it yourself: add a second tool and watch tools_condition keep working unchanged - it only cares whether the model's last message contains any tool call, not which one.

What you just built, generalized

This four-node shape - agent, tool, a conditional edge deciding between them, and a loop back - is the backbone of the large majority of real LangGraph agents. Bigger systems add more tools, more nodes for different kinds of work, and the reliability features covered next: making sure this loop can survive a crash, and knowing when to pause it for a human.

Share this guide

Was this guide helpful?

Thanks for the feedback!

Want more hands-on AI builds like this?

APA Mastery runs live, practical sessions on working with modern AI tools - not just theory.

See What's On →
← PreviousThe Building Blocks: State, Nodes, Edges & Graphs