feat(llm): add token usage tracking and statistics for LLM providers#54
Closed
einsiedler0408 wants to merge 1 commit intomainfrom
Closed
feat(llm): add token usage tracking and statistics for LLM providers#54einsiedler0408 wants to merge 1 commit intomainfrom
einsiedler0408 wants to merge 1 commit intomainfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR adds comprehensive token usage tracking and statistics across all LLM provider clients to enable better monitoring and optimization of token consumption.
Key changes:
- Added cumulative usage tracking fields to the base LLM client class
- Implemented automatic usage extraction and accumulation for all responses
- Updated provider clients to use unified configuration access patterns and support usage extraction
Reviewed Changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| src/llm/provider_client_base.py | Added token usage tracking fields, accumulation logic, and utility methods |
| src/core/orchestrator.py | Integrated usage tracking with session management and logging |
| src/llm/providers/claude_anthropic_client.py | Updated config access and added Anthropic-specific usage extraction |
| src/llm/providers/gpt_openai_response_client.py | Updated config access and added OpenAI Response API usage extraction |
| src/llm/providers/gpt_openai_client.py | Updated config access pattern |
| src/llm/providers/qwen_sglang_client.py | Updated config access pattern |
| src/llm/providers/deepseek_newapi_client.py | Updated config access pattern |
| src/llm/providers/claude_newapi_client.py | Updated config access pattern |
| src/llm/providers/claude_openrouter_client.py | Added usage tracking request parameter |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
|
|
||
| # Update token count | ||
| self._update_token_usage(response.get("usage", None)) | ||
| # self._update_token_usage(response.get("usage", None)) |
There was a problem hiding this comment.
This commented-out code should be removed rather than left as a comment. The functionality has been replaced by the new usage tracking system in the base class.
Suggested change
| # self._update_token_usage(response.get("usage", None)) |
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.
Describe this PR
feat(llm): add token usage tracking and statistics for LLM providers
This PR introduces comprehensive token usage tracking capabilities across all LLM provider clients to enable better monitoring and optimization of token consumption.
Key Features Added:
Cumulative Usage Tracking: Added token usage fields to
LLMProviderClientBaseto track:Automatic Usage Collection: Enhanced the base method to automatically extract and accumulate usage statistics from LLM responses using a standardized interface
Provider-Specific Support: Updated all LLM provider clients to properly support usage extraction:
claude_anthropic_client.pyclaude_newapi_client.pyclaude_openrouter_client.pydeepseek_newapi_client.pygpt_openai_client.pygpt_openai_response_client.pyqwen_sglang_client.pyOrchestrator Integration: Enhanced the orchestrator to:
Utility Methods: Added helper methods for: