Unable to get usage while using streaming function

Here is my code snippet

client = Client(api_key=config["LANGSMITH_API_KEY"])
chain = client.pull_prompt(langsmith_prompt_name, include_model=True)
callback = UsageMetadataCallbackHandler()
  response_stream = chain.stream(
      input, stream_usage=True, config={"callbacks": [callback]}
  )

  for chunk in response_stream:
      print(f"chunkx: {chunk}")
      if hasattr(chunk, "content"):
          yield chunk.content

This is last chunk

content='' additional_kwargs={} response_metadata={'finish_reason': 'stop', 'model_name': 'gpt-4.1-2025-04-14', 'system_fingerprint': 'fp_51e1070cf2', 'service_tier': 'default'} id='run--c1d9dd50-3ae6-4f87-9a9a-0f62f4246239'

Versions
langchain-openai: 0.3.28
langchain-core: 0.3.70
langchain: 0.3.26
langsmith: 0.4.8
openai: 1.97.0

Hi @Maya. Are you facing this issue all the time or is it something intermittent?

The issue is that stream_usage=True sends usage metadata in a separate chunk that doesn’t have a content attribute, so your hasattr(chunk, "content") filter is skipping it. Usage data appears in chunks with usage_metadata instead.

Modify your code to capture usage:

for chunk in response_stream:
    if hasattr(chunk, "content"):
        yield chunk.content
    elif hasattr(chunk, "usage_metadata"):
        print(f"Usage: {chunk.usage_metadata}")
        # Store usage data as needed

The UsageMetadataCallbackHandler should also capture this data, so check callback.total_tokens after streaming completes. The usage chunk typically comes at the end of the stream.

1 Like

All the time.

I tried that, i printed all chunks
```
for chunk in response_stream:

    print("chunk", chunk)

```
But none of chunk has cost or usage_metadata