Add opt-in token usage tracking for LLM queries#15
Open
patrickvossler18 wants to merge 1 commit intomainfrom
Open
Add opt-in token usage tracking for LLM queries#15patrickvossler18 wants to merge 1 commit intomainfrom
patrickvossler18 wants to merge 1 commit intomainfrom
Conversation
Introduce track_usage parameter on LLMApi that prints and logs token counts (input/output) for each query. Uses a LangChain callback handler to capture usage metadata, supports verbosity-aware output (detailed breakdowns at high verbosity), and reports zero tokens for cache hits. Key implementation details: - UsageCallbackHandler captures usage from on_llm_end callback - Shared parse_usage_metadata() used by both single and batch paths - Comma-formatted output, consistent format across single/batch calls - Thread-safety documented (not safe for concurrent get_output calls) Also fixes pre-existing TypeError in batch test mocks where MagicMock responses lacked explicit usage_metadata=None.
Collaborator
venkatesh-sivaraman
left a comment
There was a problem hiding this comment.
Overall looks good, I added a couple minor suggestions.
| if cached: | ||
| result["cached_tokens"] = cached | ||
| if reasoning: | ||
| result["reasoning_tokens"] = reasoning |
Collaborator
There was a problem hiding this comment.
Should total_tokens include the count of reasoning_tokens?
| msg += f" {suffix}" | ||
| return msg | ||
|
|
||
| def _report_usage(self, usage: dict, cached: bool = False): |
Collaborator
There was a problem hiding this comment.
It would be great if there was also a way to print/return the total usage after a bunch of LLM calls, rather than just at the time of each call.
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.
Adds opt-in token usage tracking to LLMApi. When enabled, token counts are printed and logged after each query.
Example output