BerriAI / litellm

Call all LLM APIs using the OpenAI format. Use Bedrock, Azure, OpenAI, Cohere, Anthropic, Ollama, Sagemaker, HuggingFace, Replicate (100+ LLMs)
https://docs.litellm.ai/docs/
Other
10.3k stars 1.15k forks source link

[Bug]: langfuse has wrong token counts #4496

Closed Manouchehri closed 1 day ago

Manouchehri commented 6 days ago

What happened?

curl -v "${OPENAI_API_BASE}/chat/completions" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -d '{
    "model": "gemini-1.5-pro-001",
    "max_tokens": 1000,
    "stream": true,
    "temperature": 0.0,
    "messages": [
      {
        "role": "user",
        "content": "Tell me the funniest joke you know."
      },
      {
        "role": "assistant",
        "content": "Why did the chicken cross the road?\nYou will not guess this one I bet\n"
      },
      {
        "role": "user",
        "content": "I do not know, why?"
      },
      {
        "role": "assistant",
        "content": "uhhhh\n\n\nhmmmm.....\nthinking....\n"
      },
      {
        "role": "user",
        "content": "\nI am waiting...\n\n...\n"
      }
    ],
    "stream_options": {
      "include_usage": true
    }
  }'
data: {"id":"chatcmpl-eae25cb3-1b59-43d9-b7bc-0fe2f3ea4e03","choices":[{"index":0,"delta":{"content":"To","role":"assistant"}}],"created":1719865807,"model":"gemini-1.5-pro-001","object":"chat.completion.chunk"}

data: {"id":"chatcmpl-eae25cb3-1b59-43d9-b7bc-0fe2f3ea4e03","choices":[{"index":0,"delta":{"content":" get to the other....  Wait for it...  \n\n*Side of the"}}],"created":1719865808,"model":"gemini-1.5-pro-001","object":"chat.completion.chunk"}

data: {"id":"chatcmpl-eae25cb3-1b59-43d9-b7bc-0fe2f3ea4e03","choices":[{"index":0,"delta":{"content":"ā€”* \n\nTo prove to the opossum it **could** be done"}}],"created":1719865808,"model":"gemini-1.5-pro-001","object":"chat.completion.chunk"}

data: {"id":"chatcmpl-eae25cb3-1b59-43d9-b7bc-0fe2f3ea4e03","choices":[{"index":0,"delta":{"content":"!  Opossums just play dead, you know!  \n\nšŸ˜‚  Get it?  \n\nLet me know if you want to hear another joke!"}}],"created":1719865809,"model":"gemini-1.5-pro-001","object":"chat.completion.chunk"}

data: {"id":"chatcmpl-eae25cb3-1b59-43d9-b7bc-0fe2f3ea4e03","choices":[{"index":0,"delta":{"content":" šŸ˜„ \n"}}],"created":1719865809,"model":"gemini-1.5-pro-001","object":"chat.completion.chunk"}

data: {"id":"chatcmpl-eae25cb3-1b59-43d9-b7bc-0fe2f3ea4e03","choices":[{"index":0,"delta":{"content":""}}],"created":1719865809,"model":"gemini-1.5-pro-001","object":"chat.completion.chunk","usage":{"prompt_tokens":50,"completion_tokens":68,"total_tokens":118}}

data: {"id":"chatcmpl-eae25cb3-1b59-43d9-b7bc-0fe2f3ea4e03","choices":[{"finish_reason":"stop","index":0,"delta":{}}],"created":1719865809,"model":"gemini-1.5-pro-001","object":"chat.completion.chunk"}

See this: {"prompt_tokens":50,"completion_tokens":68,"total_tokens":118}

Yet langfuse shows:

image

Relevant log output

No response

Twitter / LinkedIn details

https://twitter.com/DaveManouchehri

themrzmaster commented 3 days ago

Any clue on that? It's an important issue

krrishdholakia commented 1 day ago

Tested on logfire, i don't see this behaviour there:

Screenshot 2024-07-06 at 2 21 26 PM Screenshot 2024-07-06 at 2 21 35 PM
krrishdholakia commented 1 day ago

seeing the correct count on langfuse

Screenshot 2024-07-06 at 2 22 57 PM
krrishdholakia commented 1 day ago

investigating if we're returning the token counts given from the provider

krrishdholakia commented 1 day ago

rerunning with your curl @Manouchehri

krrishdholakia commented 1 day ago

works with langfuse

Screenshot 2024-07-06 at 2 52 01 PM Screenshot 2024-07-06 at 2 51 57 PM