Skip to content

Langchain and Langgraph don't report cached/reasoning token usage #6799

Description

@sentry-junior

The Langchain and Langgraph integrations only record input_tokens, output_tokens, and total_tokens on spans. They don't populate gen_ai.usage.input_tokens.cached or gen_ai.usage.output_tokens.reasoning, even though SPANDATA already defines both and the OpenAI, OpenAI Agents, and Google GenAI integrations already set them from equivalent provider fields (e.g. prompt_tokens_details.cached_tokens, completion_tokens_details.reasoning_tokens).

Without these, cost calculations that rely on cache discounts and reasoning-token pricing are inaccurate for Langchain/Langgraph-instrumented apps.

  • sentry_sdk/integrations/langchain.py: _extract_tokens, _extract_tokens_from_generations, and _record_token_usage only read prompt_tokens/input_tokens, completion_tokens/output_tokens, and total_tokens from the token usage dict.
  • sentry_sdk/integrations/langgraph.py: token accumulation loop only reads prompt_tokens, completion_tokens, total_tokens from response_metadata["token_usage"].

Both should also extract cached input tokens and reasoning output tokens where the underlying LLM provider surfaces them (e.g. via usage_metadata.input_token_details.cache_read / output_token_details.reasoning in LangChain's newer usage metadata shape) and set SPANDATA.GEN_AI_USAGE_INPUT_TOKENS_CACHED / SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS_REASONING accordingly.

Related: #5455 (same gap for the LiteLLM integration).

Requested by Connor via Junior.

--

View Junior Session in Sentry

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions