mirror of
https://github.com/open-webui/open-webui.git
synced 2026-07-18 10:48:00 -05:00
issue: Incorrect Token Usage Calculation using Function Calling in Native Mode #4913
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @ChenxingLi on GitHub (Apr 20, 2025).
Check Existing Issues
Installation Method
Docker
Open WebUI Version
v0.6.5
Ollama Version (if applicable)
Not related
Operating System
Ubuntu 22.04
Browser (if applicable)
Not related
Confirmation
README.md.I have not included additional browser or server logs, as I believe the provided network packet records offer more relevant insights into this issue than logs. That said, if you think additional log details might be useful, please don’t hesitate to let me know, and I would be glad to share them.
Principle
Native function calling involves multiple calls to the Completion API:
Expected Behavior
"include_usage": truein the request payload.usagedata returned by every API call should be captured and aggregated correctly.Filtertool functions, bothoutletandstreaminterface inputs should reflect the full token usage, including all API calls, ensuring accurate reporting and cost calculation across the entire workflow.Actual Behavior
Open WebUI does not correctly track or calculate token usage during native function calling:
"include_usage": true.usagedata is not returned for these calls, causing the token usage from subsequent calls to be omitted.outletfunction of theFilterTool functions only reflects the token usage from the first API call, while the stream function inputs lack any token usage data entirely.This behavior results in incomplete and incorrect token cost calculations for workflows involving native function calling.
Steps to Reproduce
Set Up LLM Model
gpt-4oorclaude-3.7).Set Up a Tool Server
get_current_timefrom the tutorial).Enable Usage Capabilities for the Model
Configure Conversation Settings
Function Callingmode as Native.Ask a Function-Calling-Related Question
Capture Network Traffic
/chat/completionscalls made to the OpenAI API. The second call(s) will not include the expected token usage calculation ("include_usage": true).Check Filter Tool Logs
Filterfunction that logs thestreamandoutlethandler outputs.outletfunction input only includes token usage from the first API call.streamfunction input contains no token usage data for the second API call.Logs & Screenshots
The screenshots below show the HTTP packets exchanged between OpenWebUI and the OpenAI API server during the aforementioned steps. It can be observed that the second communication fails to correctly request token usage.
Additional Information
I am not using the official native API but rather a third-party API. Therefore, I am unsure whether the official native API can still return token usage during the second call, even if
include_usageis not requested. However, after testing multiple OpenAI-compatible APIs, I found that none of them return token usage ifusageis not explicitly requested.@rgaricano commented on GitHub (Apr 23, 2025):
On CommVersion Outlet filters are not triggered on API calls.
https://github.com/open-webui/open-webui/discussions/8722#discussioncomment-12193783