-
Notifications
You must be signed in to change notification settings - Fork 639
feat(langchain): report cached and reasoning token usage in Langchain/Langgraph #6800
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: master
Are you sure you want to change the base?
Changes from all commits
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 |
|---|---|---|
|
|
@@ -432,6 +432,8 @@ def _set_usage_data(span: "sentry_sdk.tracing.Span", messages: "Any") -> None: | |
| input_tokens = 0 | ||
| output_tokens = 0 | ||
| total_tokens = 0 | ||
| cached_tokens = 0 | ||
| reasoning_tokens = 0 | ||
|
|
||
| for message in messages: | ||
| response_metadata = message.get("response_metadata") | ||
|
|
@@ -446,6 +448,12 @@ def _set_usage_data(span: "sentry_sdk.tracing.Span", messages: "Any") -> None: | |
| output_tokens += int(token_usage.get("completion_tokens", 0)) | ||
| total_tokens += int(token_usage.get("total_tokens", 0)) | ||
|
|
||
| input_details = token_usage.get("prompt_tokens_details") or {} | ||
| cached_tokens += int(input_details.get("cached_tokens") or 0) | ||
|
|
||
| output_details = token_usage.get("completion_tokens_details") or {} | ||
| reasoning_tokens += int(output_details.get("reasoning_tokens") or 0) | ||
|
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. Langgraph misses LangChain token shapesMedium Severity
Reviewed by Cursor Bugbot for commit cb26c51. Configure here. |
||
|
|
||
| set_on_span = ( | ||
| span.set_attribute if isinstance(span, StreamedSpan) else span.set_data | ||
| ) | ||
|
|
@@ -462,6 +470,12 @@ def _set_usage_data(span: "sentry_sdk.tracing.Span", messages: "Any") -> None: | |
| total_tokens, | ||
| ) | ||
|
|
||
| if cached_tokens > 0: | ||
| set_on_span(SPANDATA.GEN_AI_USAGE_INPUT_TOKENS_CACHED, cached_tokens) | ||
|
|
||
| if reasoning_tokens > 0: | ||
| set_on_span(SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS_REASONING, reasoning_tokens) | ||
|
|
||
|
|
||
| def _set_response_model_name(span: "sentry_sdk.tracing.Span", messages: "Any") -> None: | ||
| if len(messages) == 0: | ||
|
|
||


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.
Bug: The use of the
oroperator incorrectly handles legitimate0values for token counts, causing them to be evaluated asNoneand dropped from span data.Severity: MEDIUM
Suggested Fix
Refactor the logic to avoid using the
oroperator for fallback when0is a valid value. Instead, explicitly check if the first value isNonebefore attempting to retrieve the second value. This ensures that a0from the primary key is preserved.Prompt for AI Agent
Also affects:
sentry_sdk/integrations/langchain.py:761~763sentry_sdk/integrations/langchain.py:737~738sentry_sdk/integrations/langchain.py:742~743Did we get this right? 👍 / 👎 to inform future reviews.