feat(langgraph): Usage attributes on invocation spans #5211
Bugbot Review
Bugbot Analysis Progress (3m 51s elapsed)
✅ Gathered PR context (2s)
✅ Analyzed code changes (1s)
✅ Completed bug detection — 2 potential bugs found (3m 43s)
✅ Validation and filtering completed (1s)
✅ Posted analysis results — 2 bugs reported (4s)
✅ Analysis completed successfully (0s)
Final Result: Bugbot completed review and found 2 potential issues
Request ID: serverGenReqId_fd5c2625-3c9e-4602-841c-ec2bdb263302
Details
Bug: Usage data miscounted when PII collection disabled
When should_send_default_pii() is false or include_prompts is false, input_messages remains None. This causes _get_new_messages in _set_response_attributes to return all output messages instead of just the new ones. Since LangGraph state accumulates messages, usage data will include tokens from all messages in the response rather than only the new messages added during this invocation. The input messages need to be parsed unconditionally (at least for _get_new_messages) to correctly calculate usage data regardless of PII settings.
sentry_sdk/integrations/langgraph.py#L184-L204
sentry-python/sentry_sdk/integrations/langgraph.py
Lines 184 to 204 in 4f3fab3
sentry_sdk/integrations/langgraph.py#L240-L260
sentry-python/sentry_sdk/integrations/langgraph.py
Lines 240 to 260 in 4f3fab3
Bug: Usage data miscounted when PII collection disabled
When should_send_default_pii() is false or include_prompts is false, input_messages remains None. This causes _get_new_messages in _set_response_attributes to return all output messages instead of just the new ones. Since LangGraph state accumulates messages, usage data will include tokens from all messages in the response rather than only the new messages added during this invocation. The input messages need to be parsed unconditionally (at least for _get_new_messages) to correctly calculate usage data regardless of PII settings.
sentry_sdk/integrations/langgraph.py#L184-L204
sentry-python/sentry_sdk/integrations/langgraph.py
Lines 184 to 204 in 4f3fab3
sentry_sdk/integrations/langgraph.py#L240-L260
sentry-python/sentry_sdk/integrations/langgraph.py
Lines 240 to 260 in 4f3fab3