logfire
logfire copied to clipboard
Token usage on LLM integrations
Description
We should be able to estimate the token usage, and show it to the user on our UI.
Should have same JSON path for all providers so queries are easy.
Should have a key for openai, or anthropic etc. to make filtering easy.
OpenAI recently added the ability to include usage data in streaming responses:
class ChatCompletionChunk(BaseModel):
...
usage: Optional[CompletionUsage] = None
"""
An optional field that will only be present when you set
`stream_options: {"include_usage": true}` in your request. When present, it
contains a null value except for the last chunk which contains the token usage
statistics for the entire request.
"""
It looks like anthropic might have always had it. So we shouldn't need to estimate anything, just use the data when it's present and document the fact.
Given Alex comment, do we still need this? I'm not sure the state of art of token usage. @samuelcolvin
Do we need to display better on the UI?
We need to use the semantic conventions, particularly for token usage so that it shows in the UI: https://pydanticlogfire.slack.com/archives/C06EDRBSAH3/p1747173927230509
Partial fix: https://github.com/pydantic/logfire/pull/1076