Coco
Build agents

Streaming

Stream an agent's node updates as each step finishes, and the model's tokens as they arrive, with stream_mode.

Stream node updates and model tokens.

Compiled agents and StateGraph graphs take a stream_mode. "updates" yields each node's update as the node finishes, and "messages" yields the model's tokens as they arrive.

Prerequisites: The framework and the agent from the Quickstart.


Stream node updates

inputs = {"messages": [{"role": "user", "content": "Check status of ORD-102"}]}

# Stream step-by-step node updates (model calls and tool executions)
for step in agent.stream(inputs, stream_mode="updates"):
    for node_name, update in step.items():
        print(f"--- Node: {node_name} ---")
        if "messages" in update:
            last_msg = update["messages"][-1]
            print(f"{last_msg.type}: {last_msg.content or getattr(last_msg, 'tool_calls', '')}")

It prints a model step with the tool call, a tools step with the tool's result, and a final model step with the answer.

Stream model tokens

inputs = {"messages": [{"role": "user", "content": "Check status of ORD-102"}]}

# Stream LLM tokens as they arrive
for message_chunk, metadata in agent.stream(inputs, stream_mode="messages"):
    if message_chunk.type == "AIMessageChunk" and message_chunk.content:
        print(message_chunk.content, end="", flush=True)
print()

Good to know: "messages" also yields each tool's result as a message. The type check keeps only the model's tokens; drop it to print the tool results too.

Stream modes

ModeYields
"values"The full state after each step.
"updates"Each node's update as the node finishes.
"messages"Model tokens, with metadata, as they arrive.
"custom"Data a node writes through StreamWriter.
"checkpoints", "tasks", "debug"Checkpoint, task, and debug events.

Pass a list, such as stream_mode=["updates", "messages"], to stream several at once. astream is the async version.


On this page