Is there any way to get token usage count after calling the streaming api of open ai to get response #1769
Replies: 1 comment
-
You’re correct — the OpenAI streaming API does return token usage, but only in the final chunk of the stream. That’s why you don’t see it until the very end. When you call with {
"usage": {
"prompt_tokens": 123,
"completion_tokens": 456,
"total_tokens": 579
}
} The issue is that when you’re using instructor with That’s why you couldn’t find a way to access it in your current snippet. If you need token usage, you have two main paths:
At the moment, there’s no “magic” way to do So if token counts are critical for you (e.g. for logging, billing, or token-budgeting), the safest approach is to handle the raw stream yourself and then validate the output with instructor afterwards. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I couldn't find any way to get token usage after calling the streaming api. OpenAI returns the last chunk with the usage data but couldn't find any way to get them using instructor. Here's my code snippet
Here client is
instructor.from_openai(AsyncOpenAI())
Beta Was this translation helpful? Give feedback.
All reactions