From e3e2adbbe9f97c90b927b94d0a80544408b676c1 Mon Sep 17 00:00:00 2001 From: Yash Singh Date: Sun, 1 Nov 2026 10:11:12 +0530 Subject: [PATCH] fix(otel): emit gen_ai.usage.reasoning.output_tokens on v2 LLM spans --- litellm/integrations/otel/mappers/genai.py | 1 + litellm/integrations/otel/model/semconv.py | 1 + .../otel/test_otel_v2_components.py | 17 +++++++++++++++++ 3 files changed, 19 insertions(+) diff --git a/litellm/integrations/otel/mappers/genai.py b/litellm/integrations/otel/mappers/genai.py index 4e07d090edd..e374c7256c4 100644 --- a/litellm/integrations/otel/mappers/genai.py +++ b/litellm/integrations/otel/mappers/genai.py @@ -67,6 +67,7 @@ class GenAIMapper: GenAI.USAGE_OUTPUT_TOKENS: lambda d: d.usage.output_tokens, GenAI.USAGE_CACHE_CREATION_INPUT_TOKENS: lambda d: d.usage.cache_creation_input_tokens, GenAI.USAGE_CACHE_READ_INPUT_TOKENS: lambda d: d.usage.cache_read_input_tokens, + GenAI.USAGE_REASONING_OUTPUT_TOKENS: lambda d: d.usage.reasoning_tokens, Error.TYPE: lambda d: d.error.error_type if d.error else None, Server.ADDRESS: lambda d: d.server.address if d.server else None, Server.PORT: lambda d: d.server.port if d.server else None, diff --git a/litellm/integrations/otel/model/semconv.py b/litellm/integrations/otel/model/semconv.py index 8d186a2715a..e1fa406eddb 100644 --- a/litellm/integrations/otel/model/semconv.py +++ b/litellm/integrations/otel/model/semconv.py @@ -113,6 +113,7 @@ class GenAI: USAGE_OUTPUT_TOKENS: Final = "gen_ai.usage.output_tokens" USAGE_CACHE_CREATION_INPUT_TOKENS: Final = "gen_ai.usage.cache_creation.input_tokens" USAGE_CACHE_READ_INPUT_TOKENS: Final = "gen_ai.usage.cache_read.input_tokens" + USAGE_REASONING_OUTPUT_TOKENS: Final = "gen_ai.usage.reasoning.output_tokens" # content (opt-in, gated by capture mode) INPUT_MESSAGES: Final = "gen_ai.input.messages" OUTPUT_MESSAGES: Final = "gen_ai.output.messages" diff --git a/tests/unit/integrations/otel/test_otel_v2_components.py b/tests/unit/integrations/otel/test_otel_v2_components.py index aad06d33edb..e4308c0da72 100644 --- a/tests/unit/integrations/otel/test_otel_v2_components.py +++ b/tests/unit/integrations/otel/test_otel_v2_components.py @@ -270,6 +270,23 @@ def test_genai_mapper_cache_token_attrs(): assert GenAI.USAGE_CACHE_READ_INPUT_TOKENS not in uncached +def test_genai_mapper_reasoning_token_attr(): + reasoning = replace( + _full_llm_call(), + usage=LLMUsage(input_tokens=10, output_tokens=50, total_tokens=60, reasoning_tokens=40), + ) + assert GenAIMapper().map(reasoning)[GenAI.USAGE_REASONING_OUTPUT_TOKENS] == 40 + + explicit_zero = replace( + _full_llm_call(), + usage=LLMUsage(input_tokens=10, output_tokens=5, total_tokens=15, reasoning_tokens=0), + ) + assert GenAIMapper().map(explicit_zero)[GenAI.USAGE_REASONING_OUTPUT_TOKENS] == 0 + + # No reported reasoning count keeps the key off the span. + assert GenAI.USAGE_REASONING_OUTPUT_TOKENS not in GenAIMapper().map(_full_llm_call()) + + def test_genai_mapper_stamps_input_output_messages(): data = LLMCallSpanData( operation=GenAIOperation.CHAT,