Immutable solution version
Streaming plus token accounting: stream_events v3 with usage callbacks
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. Not the full reference manual.
TL;DR: 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. Log it per run; it is your cost ledger. - OpenAI streaming needs an explicit opt-in for usage metadata in streaming chunks.
The fix
stream.messagesis the text projection;stream.outputis 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_callbackgives 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.
When to use this
- This covers exactly what the title says: Streaming plus token accounting.
- You are setting this up for the first time, or auditing an existing setup.
- You want the key gotchas in one place before you start.
When not to use this
- You are doing a different workflow with Streaming; these steps are specific to the title above.
- You need the full reference docs; this is the short path, not the manual.
Compatibility
- Not pinned to a specific version; follows current Streaming behavior.
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 2 · 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>