logfire icon indicating copy to clipboard operation
logfire copied to clipboard

Token usage on LLM integrations

Open Kludex opened this issue 1 year ago • 5 comments

Description

We should be able to estimate the token usage, and show it to the user on our UI.

Kludex avatar Jul 15 '24 15:07 Kludex

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.

samuelcolvin avatar Jul 15 '24 15:07 samuelcolvin

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.

alexmojaki avatar Jul 15 '24 16:07 alexmojaki

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?

Kludex avatar Dec 24 '24 11:12 Kludex

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

alexmojaki avatar May 14 '25 10:05 alexmojaki

Partial fix: https://github.com/pydantic/logfire/pull/1076

alexmojaki avatar May 14 '25 13:05 alexmojaki