We call stream instead of invoke, with stream mode set to updates. Each update tells us which part of the agent just ran, the model or the tools, and what it produced. Run it. We see the model asking for a tool, the tool answering, and the model giving its final reply, in order.
stream_agent.pypython
from langchain.agents import create_agentfrom langchain.tools import tool@tooldef check_stock(title: str) -> int: """Return how many copies of a book Riverside Books has in stock.""" return {"Dune": 3}.get(title, 0)agent = create_agent("google_genai:gemini-flash-lite-latest", tools=[check_stock])question = {"messages": [{"role": "user", "content": "Is Dune in stock? One sentence."}]}for update in agent.stream(question, stream_mode="updates"): for step, data in update.items(): last = data["messages"][-1] print(f"[{step}] {type(last).__name__}: {last.text or last.tool_calls[0]['name']}")
Output
[model] AIMessage: check_stock
[tools] ToolMessage: 3
[model] AIMessage: Riverside Books currently has 3 copies of Dune in stock.
There are a few stream modes. Updates gives what each step added. Values gives the full state after every step. Messages streams the model's text token by token, for chat interfaces. And you can ask for several modes at once.