AI Hints
Home›LangChain Tutorial›Prompts, Streaming, Memory
LangChain · Lesson 47 of 120

Streaming Agent Steps

Watching it work.

⏱ 2 min read▶ Video at 51:53🎓 Free
Watch this lesson in the LangChain courseWatch on YouTube ↗Full LangChain course

Agents can take several steps, and several seconds. Streaming lets you show progress, like, checking stock, as it happens, instead of a long silence.

Streaming Agent Steps
From the video: Streaming Agent Steps
  • Several steps
  • Several seconds
  • Show progress

Stream mode updates

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_agent
from langchain.tools import tool

@tool
def 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.

Stream modes

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.

ModeYou get
updateswhat each step added
valuesfull state after each step
messagestokens as they're written
['updates', 'messages']several at once

Summary

Streaming an agent shows each step as it happens. Use updates for progress, and messages for live text. Next, giving agents a memory.

Key takeaways
  • agent.stream(...)
  • stream_mode='updates'
  • 'messages' for tokens
Next up: Agent Memory — threads and checkpoints