-
Notifications
You must be signed in to change notification settings - Fork 8.3k
feat: token usage tracking in responses api #11302
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
Important Review skippedAuto incremental reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the WalkthroughThis pull request introduces per-response usage metrics (input_tokens, output_tokens, total_tokens) throughout the codebase. A new Changes
Sequence DiagramsequenceDiagram
participant Client
participant OpenAIAPI as OpenAI API
participant Handler as OpenAI Response Handler
participant Schema as Message Properties
participant Response as OpenAI Response Object
Client->>OpenAIAPI: Send prompt (streaming or non-streaming)
OpenAIAPI->>Handler: Return response with usage metadata
alt Streaming Path
loop For each chunk
OpenAIAPI-->>Handler: Stream chunk with usage_metadata
Handler->>Schema: Extract usage (input, output, total tokens)
Handler->>Handler: Accumulate usage_data
end
Handler->>Handler: Message reaches "complete" state
Handler->>Response: Emit completion event with usage_data
Response-->>Client: Send response.completed with usage
else Non-Streaming Path
Handler->>Schema: Extract usage from result outputs
Handler->>Handler: Construct usage_data dict
Handler->>Response: Populate OpenAIResponsesResponse with usage field
Response-->>Client: Send response with usage_data
end
Response-->>Client: Final response includes usage metrics
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes 🚥 Pre-merge checks | ✅ 5 | ❌ 2❌ Failed checks (1 error, 1 warning)
✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #11302 +/- ##
==========================================
- Coverage 34.25% 32.67% -1.58%
==========================================
Files 1409 1409
Lines 66892 66969 +77
Branches 9860 9881 +21
==========================================
- Hits 22912 21884 -1028
- Misses 42787 43889 +1102
- Partials 1193 1196 +3
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
edwinjosechittilappilly
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tested in LF. LGTM
Token Usage Tracking Implementation
- New Usage class with input_tokens, output_tokens, total_tokens fields
- Added usage field to Properties class
- New extract_usage() method to extract token usage from AIMessage response_metadata
- Supports OpenAI format (token_usage.prompt_tokens/completion_tokens/total_tokens)
- Supports Anthropic format (usage.input_tokens/output_tokens)
- Modified _get_chat_result() to set usage on returned Message for non-streaming
- Modified _stream_message() to return usage data from final chunk
- Modified send_message() to set usage on message properties after streaming
- Non-streaming: Extract usage from component_output.results Message properties
- Streaming: Capture usage from add_message event with state=complete
- Added response.completed event with usage in streaming responses
- Added programmatic model configuration for LanguageModelComponent
- Set API key directly in template (workaround for global variable lookup issue in tests)
- Added usage validation assertions
Summary by CodeRabbit
Release Notes
✏️ Tip: You can customize this high-level summary in your review settings.