NorthGradient
Start reading
Building Production Agents with LangGraph Browse lessons

Building Production Agents with LangGraph · Building Production Agents with LangGraph · 5 min read

Tools and graphs

The loop from the last lesson needs two things in code: a way for the agent to take actions (tools), and a structure that controls what runs when (a graph).

Tools let an agent act. A graph keeps it acting in a clear, repeatable order.

Tools

A tool is a normal Python function the model is allowed to call. You mark it with a decorator and write a docstring. The model reads that docstring to decide when to use the tool.

from langchain_core.tools import tool

@tool
def get_weather(city: str) -> dict:
    """Get the current temperature and conditions for a city."""
    return fetch_weather_api(city)

The docstring is not just a comment. It is how the model knows what the tool does. A vague docstring leads to a tool the agent uses at the wrong time. A clear one leads to a tool it uses correctly.

Keep each tool to one job. A tool that does several things hides its failures. If get_all_data() breaks, you cannot tell whether the weather, news, or calendar part failed. Three small tools give you three clear failure points instead.

Graphs

A graph describes your agent’s workflow as steps and transitions. A node is a function that takes the state and returns an updated state. An edge says which node runs next.

LangGraph is the library we use to build these. Here is the smallest version:

from langgraph.graph import StateGraph, END
from typing import TypedDict

class State(TypedDict):
    task: str
    result: str

def process(state: State) -> State:
    answer = call_llm(state["task"])
    return {**state, "result": answer}   # return updated state

graph = StateGraph(State)
graph.add_node("process", process)
graph.set_entry_point("process")
graph.add_edge("process", END)

app = graph.compile()
result = app.invoke({"task": "Summarize climate change.", "result": ""})

Notice that process returns {**state, "result": answer}, a new state, instead of changing the old one in place. Every node works this way: read the state, return an updated copy. That is what makes a node easy to test on its own. Pass in a dictionary, check what comes back.

Why a graph instead of if/else

You could write the same logic as a chain of if/else statements, but it gets hard to follow past a few branches. A graph gives you two things that plain code does not:

  • You can see it. A graph can be drawn. If you cannot draw your agent on a whiteboard, you do not understand its flow well enough to code it yet.
  • You can replay it. LangGraph can save the state at each node. If the agent crashes, it can pick up from the last saved point instead of starting over.

The next lesson looks at state up close: what goes in it, and why keeping data and logic apart is the most important decision you will make.