API: Always return usage for non-streaming completions - #487
Open
blaakstone wants to merge 1 commit into
Open
blaakstone wants to merge 1 commit into
blaakstone wants to merge 1 commit into
Conversation
The non-streaming paths of /v1/chat/completions and /v1/completions gated usage on stream_options.include_usage, which only exists for streaming requests, so non-streaming responses always had usage=null. Regression from 1794791, where the rework reused the streaming condition for both paths. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Is your pull request related to a problem? Please describe.
Non-streaming
/v1/chat/completionsand/v1/completionsalways return"usage": null.Both non-streaming paths do
stream_optionsonly exists for streaming requests, so this is always falsy there and the usage stats get dropped. Looks like a side effect of 1794791 ("Rework tool calls and OAI chat completions"): before it, the non-streaming response builtusage=UsageStats(...)unconditionally, and the rework reused the streaming condition for both paths.Why should this feature be added?
OpenAI returns
usageon every non-streaming response. Clients that don't stream currently get no token counts, and proxies that read them (llama-swap's activity view here) show zeros.Examples
Same greedy request, streaming with
include_usagevs non-streaming, before and after:nullnull(prompt / completion tokens)
On the chat endpoint I also checked completion_tokens against
/v1/token/encodeon the output (108 vs 107, the extra one is the stop token), prompt_tokens against the encoded/v1/apply-templateprompt (25 = 25), and thatn=2aggregates (214).Additional context
Streaming behaviour is unchanged.
#478 has a test,
test_chat_response_carries_timings_without_usage, that assertsusage is Nonewheninclude_usageisn't set, so it pins the current behaviour and would need adjusting alongside this.🤖 Generated with Claude Code