You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jun 5, 2025. It is now read-only.
Repository navigation
This repository was archived by the owner on Jun 5, 2025. It is now read-only.
See if there's a way the LLM providers list in their response the tokens they have used. At a first glance it looks to be possible at least for OpeanAI
Use our own tokenizer. We could tokenize ourselves the request and response and calculate that way the number of used tokens. The big drawback with this is that the tokens we calculate with the tokenizer may not match the tokens used by the LLM. But at least it would be an approximation
I have been playing around with the APIs. It's possible for all providers. All of them include the token usage automatically if the request is non-streaming. For streaming we need to explicitly request for it, except for Anthropic, which already includes it at the first chunk.
Anthropic
The token usage comes separated in 2 chunks. One at the beginning and another one at the end.
We need to request explicitly the token usage when the request is set to streaming, which is most of the time from clients. Note the stream_options field in the following example request
Not sure where OpenRouter is getting their info. However we can do an API call to get the prices they use: curl "https://openrouter.ai/api/v1/models". Reference link
Activity
lukehinds commentedon Jan 6, 2025
this will require #454 to land first, so let's keep in backlog for now.
aponcedeleonch commentedon Jan 24, 2025
At an initial investigation the used tokens are not listed neither in the request nor the response from the LLM.
Request
{ "messages": [...], "model": "gpt-4o", "temperature": 0.1, "top_p": 1, "max_tokens": 4096, "n": 1, "stream": true }max_tokens: The maximum number of tokens that can be generated in the chat completion. ReferenceResponse
There are 2 alternatives:
aponcedeleonch commentedon Jan 24, 2025
I have been playing around with the APIs. It's possible for all providers. All of them include the token usage automatically if the request is non-streaming. For streaming we need to explicitly request for it, except for Anthropic, which already includes it at the first chunk.
Anthropic
The token usage comes separated in 2 chunks. One at the beginning and another one at the end.
OpenAI, Ollama, VLLM
We need to request explicitly the token usage when the request is set to streaming, which is most of the time from clients. Note the
stream_optionsfield in the following example requestResponse with token usage at the last chunk. It comes after the chunk with
finish_reason: "stop".{ "id": "chatcmpl-4933d74a8f8b4a82a855439eeab1ae3d", "object": "chat.completion.chunk", "created": 1737723773, "model": "unsloth/Qwen2.5-Coder-32B-Instruct", "choices": [], "usage": { "prompt_tokens": 32, "total_tokens": 42, "completion_tokens": 10 } }aponcedeleonch commentedon Jan 27, 2025
Costs
curl "https://openrouter.ai/api/v1/models". Reference linkInclude the token usage for every conversation and workspace
Include the token usage for every conversation and workspace
Include the token usage for every conversation and workspace
Include the token usage for every conversation and workspace
Include the token usage for every conversation and workspace (#788)
Include the token usage for every conversation and workspace (#788)