AI Hints
Home›LangGraph Tutorial›Agents by Hand
LangGraph · Lesson 35 of 120

Watching the Agent

Step by step.

⏱ 2 min read▶ Video at 36:48🎓 Free
Watch this lesson in the LangGraph courseWatch on YouTube ↗Full LangGraph course

Invoke only shows the final state. To understand an agent, or to show progress to a customer, we want to see each step. Stream, with the updates mode, gives us one event per node.

Watching the Agent
From the video: Watching the Agent
  • invoke shows the end
  • See each step
  • stream updates

Streaming the steps

The agent graph is the one we just built, now imported from a helper file. Stream mode updates yields a dictionary for each node that runs. We print the node's name, and a short description of its new message. Run it. We see the agent ask for tools, the tools reply, and the agent answer.

watch_agent.pypython
from agent_graph import graph

q = {"messages": [{"role": "user", "content": "How much are Dune and Circe together?"}]}
for update in graph.stream(q, stream_mode="updates"):
    for node, data in update.items():
        msg = data["messages"][-1]
        if msg.type == "ai" and msg.tool_calls:
            print(f"{node}: wants {[c['name'] + str(c['args']) for c in msg.tool_calls]}")
        else:
            print(f"{node}: {msg.type} -> {msg.text.strip()[:70]}")
Output
agent: wants ["get_price{'title': 'Dune'}", "get_price{'title': 'Circe'}"]
tools: tool -> 10.99
agent: ai -> Dune costs £9.99 and Circe costs £10.99, so together they are £20.98.
Also uses shop_tools.py
from langchain.tools import tool

BOOKS = {
    "The Hobbit": {"stock": 7, "price": 8.99, "author": "J.R.R. Tolkien"},
    "Dune": {"stock": 3, "price": 9.99, "author": "Frank Herbert"},
    "Circe": {"stock": 0, "price": 10.99, "author": "Madeline Miller"},
}

@tool
def check_stock(title: str) -> int:
    """Copies of a book in stock at Riverside Books."""
    return BOOKS.get(title, {}).get("stock", 0)

@tool
def get_price(title: str) -> float:
    """Price of a book in pounds."""
    return BOOKS.get(title, {}).get("price", 0.0)

@tool
def get_author(title: str) -> str:
    """Author of a book."""
    return BOOKS.get(title, {}).get("author", "unknown")
Also uses agent_graph.py
from langchain.chat_models import init_chat_model
from langgraph.graph import MessagesState, StateGraph, START
from langgraph.prebuilt import ToolNode, tools_condition
from shop_tools import check_stock, get_price, get_author

tools = [check_stock, get_price, get_author]
model = init_chat_model("google_genai:gemini-flash-lite-latest", temperature=0).bind_tools(tools)

def agent(s: MessagesState):
    return {"messages": [model.invoke(s["messages"])]}

b = StateGraph(MessagesState)
b.add_node(agent); b.add_node("tools", ToolNode(tools))
b.add_edge(START, "agent")
b.add_conditional_edges("agent", tools_condition)
b.add_edge("tools", "agent")
graph = b.compile()

Using the steps

Step updates are great for debugging. You see every decision. In an app, you can show progress, like checking stock, while the customer waits. Later, we'll stream individual tokens too.

keyvalue
Debuggingsee each decision
Appsshow progress
Modeupdates
Laterstream tokens

Summary

Stream with updates mode shows each node's update as it happens. Use it to debug, and to show progress. Next, comparing your own graph with create agent.

Key takeaways
  • graph.stream(...)
  • stream_mode='updates'
  • One event per node
Next up: Your Graph vs create_agent — when to use which