-
Notifications
You must be signed in to change notification settings - Fork 11
feat(cost): per-modality token breakdown in Python SDK (F3 Track C) #596
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
Open
john-weiler
wants to merge
6
commits into
main
Choose a base branch
from
feat/f3-multimodal-cost-sdk-python
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
aefcde2
feat(cost): per-modality token breakdown in Python SDK (F3 Track C)
john-weiler bee7b89
fixes
john-weiler 2bd1a2a
fix: TraceBuilder.add_llm_span() modality params, LoggedMessage tool …
john-weiler 17464f5
feat: add image_output_tokens throughout the full modality stack
john-weiler 697487c
test: add coverage for image_output_tokens, provider scope, and multi…
john-weiler 4961ec6
chore: add modality token fields to openapi.yaml and regenerate resou…
john-weiler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -385,6 +385,10 @@ def on_llm_end(self, run_id: UUID, llm_response: Any, status_code: int = 200) -> | |
| num_output_tokens=usage.get("completion_tokens"), | ||
| total_tokens=usage.get("total_tokens"), | ||
| status_code=status_code, | ||
| image_input_tokens=usage.get("image_input_tokens"), | ||
| audio_input_tokens=usage.get("audio_input_tokens"), | ||
| audio_output_tokens=usage.get("audio_output_tokens"), | ||
| image_output_tokens=usage.get("image_output_tokens"), | ||
| ) | ||
|
|
||
| def _is_retriever_tool(self, tool: Any) -> bool: | ||
|
|
@@ -498,19 +502,66 @@ def _extract_tools(self, llm_request: Any) -> list[dict[str, Any]] | None: | |
| return None | ||
|
|
||
| def _extract_usage_metadata(self, llm_response: Any) -> dict[str, Any]: | ||
| """Extract token usage metrics from LLM response.""" | ||
| """Extract token usage metrics from LLM response. | ||
|
|
||
| Also extracts per-modality breakdown (image/audio) from | ||
| ``usage_metadata.prompt_tokens_details`` and ``candidates_tokens_details`` | ||
| when the native Gemini SDK returns them (list of ModalityTokenCount objects). | ||
| """ | ||
| if not llm_response: | ||
| return {} | ||
| usage = getattr(llm_response, "usage_metadata", None) | ||
| if not usage: | ||
| return {} | ||
| return { | ||
|
|
||
| result: dict[str, Any] = { | ||
| "prompt_tokens": getattr(usage, "prompt_token_count", None) or getattr(usage, "input_token_count", None), | ||
| "completion_tokens": getattr(usage, "candidates_token_count", None) | ||
| or getattr(usage, "output_token_count", None), | ||
| "total_tokens": getattr(usage, "total_token_count", None), | ||
| } | ||
|
|
||
| # Per-modality breakdown — only present on native Gemini SDK responses. | ||
| prompt_details = getattr(usage, "prompt_tokens_details", None) | ||
| candidates_details = getattr(usage, "candidates_tokens_details", None) | ||
| if prompt_details or candidates_details: | ||
| image_in = 0 | ||
| audio_in = 0 | ||
| audio_out = 0 | ||
| image_out = 0 | ||
| has_prompt = bool(prompt_details) | ||
| has_candidates = bool(candidates_details) | ||
| for entry in prompt_details or []: | ||
| modality_attr = getattr(entry, "modality", None) | ||
|
Comment on lines
523
to
+535
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Want Baz to fix this for you? Activate Fixer |
||
| modality = getattr(modality_attr, "value", modality_attr) | ||
| if not isinstance(modality, str): | ||
| continue | ||
| count = getattr(entry, "token_count", None) or 0 | ||
| upper = modality.upper() | ||
| if upper == "IMAGE": | ||
| image_in += count | ||
| elif upper == "AUDIO": | ||
| audio_in += count | ||
| for entry in candidates_details or []: | ||
| modality_attr = getattr(entry, "modality", None) | ||
| modality = getattr(modality_attr, "value", modality_attr) | ||
| if not isinstance(modality, str): | ||
| continue | ||
| count = getattr(entry, "token_count", None) or 0 | ||
| upper = modality.upper() | ||
| if upper == "AUDIO": | ||
| audio_out += count | ||
| elif upper == "IMAGE": | ||
| image_out += count | ||
| if has_prompt: | ||
| result["image_input_tokens"] = image_in | ||
| result["audio_input_tokens"] = audio_in | ||
| if has_candidates: | ||
| result["audio_output_tokens"] = audio_out | ||
| result["image_output_tokens"] = image_out | ||
|
|
||
| return result | ||
|
|
||
| def _extract_final_output(self, invocation_context: Any) -> str: | ||
| if hasattr(invocation_context, "session"): | ||
| session = invocation_context.session | ||
|
|
||
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
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
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
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.