A streaming chat demo can pass while its token-accounting check remains untested. Before using a LangChain stream to populate a cost dashboard, make text delivery and usage delivery separate acceptance criteria.
The distinction matters when moving ChatOpenAI to a custom endpoint. LangChain’s integration documentation lists base URL resolution in this order:
- Explicit
base_urloropenai_api_baseconstructor argument. OPENAI_API_BASE, read by LangChain at initialization.OPENAI_BASE_URL, read by the underlying SDK.
For that third entry, LangChain explicitly says it also inspects the variable when deciding whether to default-enable stream_usage: when set, the default is left off because many non-OpenAI endpoints do not support streaming token usage. This is not proof that your endpoint lacks support. It is a reason not to infer usage behavior from a successful text stream—or from a different environment’s defaults.
Requesting usage is not demonstrating support
LangChain documents stream_usage=True, at initialization or invocation, as the control for recovering token counts while streaming. Treat this as a setting to test on the exact route, not a capability guarantee for every compatible endpoint.
For a concrete example, the CometAPI LangChain tutorial sets an explicit base_url, enables streaming=True, and prints chunk.content inside a .stream() loop. That demonstrates what the tutorial asks the client to display; the example does not inspect returned usage metadata. It therefore cannot establish that a cost collector will receive token counts. Nor does the environment-variable caveat above, by itself, establish the default behavior of that explicit-constructor configuration.
A small acceptance matrix
I would use this procedure before relying on streamed counts for a dashboard. It is a proposed test, not a report of live endpoint results.
Record the exact model identifier, endpoint, langchain-openai and SDK versions, base URL configuration method, and whether usage was explicitly requested. Keep credentials out of the test record. Then separate these cases:
| Case | What to inspect | Accounting treatment |
|---|---|---|
| Stream consumed to completion, with usage requested | Text delivery, normal completion, and actual usage metadata exposed by the adapter | Mark usage observed only when the expected counts are present |
| Text arrives but usage is absent | Whether usage was requested and whether the selected route returns it | Retain unknown usage; do not replace it with zero |
| Request rejects the usage option | Exact sanitized error and installed configuration | Mark this configuration unsuitable for stream-based metering until resolved |
| Stream is interrupted or cancelled | Which usage fields, if any, were received before termination | Keep interruption status separate; do not assume received counts cover the full request |
Inspect the whole stream, including chunks without displayable text, rather than instrumenting only the UI’s text-printing branch. Repeat the check after changing the model route or client version. A passing test should be scoped to that configuration, not promoted to a gateway-wide guarantee.
Keep the denominator honest
For completed requests, report usage coverage = requests with usable usage metadata / completed requests, alongside the sum of observed token counts. Track interrupted requests separately. These are proposed dashboard definitions, not provider billing rules.
If missing usage is silently converted to zero, an average over all requests understates consumption whenever those unobserved requests consumed tokens. A lower displayed token total can therefore reflect worse observability rather than a cheaper model.
Do not turn that partial total into a savings claim. Reconcile billing separately against an authoritative usage or billing record where available; stream metadata alone does not establish the final charge. The practical decision is simple: accept a route for text streaming after the text check, but accept it for stream-based metering only after the usage check also passes.