-
Notifications
You must be signed in to change notification settings - Fork 64
fix: capture LangChain token usage fallbacks #640
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?
Changes from all commits
d6b29d6
208d173
5972b28
fcc0e9c
1a52f29
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Capture LangChain chat token usage from generation metadata and LLM output fallbacks. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -616,7 +616,14 @@ def resolve_response_model_and_id( | |
| return response_model, response_id | ||
|
|
||
|
|
||
| def extract_token_details(usage_metadata: dict[str, Any]) -> dict[str, int]: | ||
| def _get_positive_int(values: Mapping[str, Any], key: str) -> int | None: | ||
| value = values.get(key) | ||
| if isinstance(value, int) and not isinstance(value, bool) and value > 0: | ||
| return value | ||
| return None | ||
|
|
||
|
|
||
| def extract_token_details(usage_metadata: Mapping[str, Any]) -> dict[str, int]: | ||
| """Extract cache, reasoning, and modality token break-downs from LangChain usage metadata.""" | ||
|
|
||
| token_details: dict[str, int] = {} | ||
|
|
@@ -633,21 +640,22 @@ def extract_token_details(usage_metadata: dict[str, Any]) -> dict[str, int]: | |
| else {} | ||
| ) | ||
|
|
||
| def _get_positive_int(d: dict[str, Any], key: str) -> int | None: | ||
| val = d.get(key) | ||
| if isinstance(val, int) and not isinstance(val, bool) and val > 0: | ||
| return val | ||
| return None | ||
|
|
||
| cache_write = _get_positive_int(input_details, "cache_write") | ||
| if cache_write is None: | ||
| cache_write = _get_positive_int(input_details, "cache_creation") | ||
| if cache_write is None: | ||
| cache_write = _get_positive_int( | ||
| usage_metadata, "cache_creation_input_tokens" | ||
| ) | ||
| if cache_write is not None: | ||
| token_details["cache_write_input_tokens"] = cache_write | ||
|
|
||
| if ( | ||
| cache_read := _get_positive_int(input_details, "cache_read") | ||
| ) is not None: | ||
| cache_read = _get_positive_int(input_details, "cache_read") | ||
| if cache_read is None: | ||
| cache_read = _get_positive_int( | ||
| usage_metadata, "cache_read_input_tokens" | ||
| ) | ||
| if cache_read is not None: | ||
| token_details["cache_read_input_tokens"] = cache_read | ||
|
|
||
| if ( | ||
|
|
@@ -672,3 +680,41 @@ def _get_positive_int(d: dict[str, Any], key: str) -> int | None: | |
| token_details["audio_output_tokens"] = audio_out | ||
|
|
||
| return token_details | ||
|
|
||
|
|
||
| def extract_usage_tokens( | ||
| usage_metadata: Mapping[str, Any], | ||
| ) -> tuple[ | ||
| int | None, | ||
| int | None, | ||
| ]: | ||
| """Extract input and output token counts from LangChain usage mappings.""" | ||
|
|
||
| input_tokens = _first_int_value( | ||
| usage_metadata.get("input_tokens"), | ||
| usage_metadata.get("prompt_tokens"), | ||
|
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. Could we also handle |
||
| usage_metadata.get("prompt_token_count"), | ||
| ) | ||
| if input_tokens is not None: | ||
| input_tokens += sum( | ||
| cache_tokens | ||
| for key in ( | ||
| "cache_creation_input_tokens", | ||
| "cache_read_input_tokens", | ||
| ) | ||
| if (cache_tokens := _get_positive_int(usage_metadata, key)) | ||
| is not None | ||
| ) | ||
| output_tokens = _first_int_value( | ||
| usage_metadata.get("output_tokens"), | ||
| usage_metadata.get("completion_tokens"), | ||
| usage_metadata.get("candidates_token_count"), | ||
| ) | ||
| return input_tokens, output_tokens | ||
|
|
||
|
|
||
| def _first_int_value(*values: Any) -> int | None: | ||
| for value in values: | ||
| if isinstance(value, int) and not isinstance(value, bool): | ||
| return value | ||
| return None | ||
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.
The
usagefallback can contain Anthropic’s raw usage shape, whereinput_tokensexcludescache_creation_input_tokensandcache_read_input_tokens. Treating it like normalized LangChain metadata undercountsgen_ai.usage.input_tokensand drops the cache breakdown. Can we normalize those fields and add a test using the real Anthropic payload shape?