How to count tokens when aborting stream? #33017
-
In our app we have a stop button that triggers a an What happens backend? We use Azure OpenAI btw. Is the token usage on Azure counted as the full response or just up until cancellation? How can we count tokens reliably without We use Node.js. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
When you hit Stop in a streaming Azure OpenAI call, you’re only billed for the prompt tokens + the completion tokens that were actually streamed before cancel. You’re not billed for the full hypothetical answer. The reason 👉 How to fix:
Libraries: gpt-tokenizer or @dqbd/tiktoken.
Optional: if you let the stream finish, add Let me know if this helps. |
Beta Was this translation helpful? Give feedback.
Ensure it using the same tokenizers
OpenAI documents the mapping and provides an official tokenizer and cookbook examples. (https://platform.openai.com/tokenizer)
How to be certain?