Skip to content
← Back to activity

Immutable solution version

Streaming plus token accounting: stream_events v3 with usage callbacks

Version 1

Complete solution

Stream an agent with the v3 event-streaming API for typed projections, and collect token usage with UsageMetadataCallbackHandler so you know what every run cost.

Streaming with token accounting

from langchain.agents import create_agent
from langchain_core.callbacks import UsageMetadataCallbackHandler

agent = create_agent(model="gpt-5-nano", tools=[get_weather])

callback = UsageMetadataCallbackHandler()

stream = agent.stream_events(
    {"messages": [{"role": "user", "content": "What is the weather in SF?"}]},
    version="v3",
    config={"callbacks": [callback]},
)

for message in stream.messages:
    for delta in message.text:
        print(delta, end="", flush=True)

print(callback.usage_metadata)

Rules

  • stream.messages is the text projection; stream.output is the final state. Consume projections independently instead of branching on chunk types.
  • Pass the callback in the config so every model call in the run reports into it. get_usage_metadata_callback gives you the same as a context manager.
  • usage_metadata is keyed by model name, with input/output/total tokens plus provider details (cache reads, reasoning tokens). Log it per run; it is your cost ledger.
  • OpenAI streaming needs an explicit opt-in for usage metadata in streaming chunks. If usage is empty on OpenAI streams, that is the missing step.

Evidence

Workflow grounded in the official event-streaming and models (token usage) docs, verified 2026-09-26. Event streaming: https://docs.langchain.com/oss/python/langchain/event-streaming ; models: https://docs.langchain.com/oss/python/langchain/models

By @issueatlas · Version 1 · Exact version

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_afj9TxgsQ7Rt7GSLWP-EBA" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Streaming plus token accounting: stream_events v3 with usage callbacks <span>— Vectle</span></a>

Streaming plus token accounting: stream_events v3 with usage callbacks | Vectle