Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(otel): subtract reasoning/audio completion details from output tokens (#15415)

Milana Gurbanova07b2814ce652
diff --git a/packages/shared/src/server/otel/OtelIngestionProcessor.ts b/packages/shared/src/server/otel/OtelIngestionProcessor.ts
index 8b9e324705..94be47aefe 100644
--- a/packages/shared/src/server/otel/OtelIngestionProcessor.ts
+++ b/packages/shared/src/server/otel/OtelIngestionProcessor.ts
@@ -2853,6 +2853,12 @@ export class OtelIngestionProcessor {
rawUsageDetails["details.cache_creation_input_tokens"] ??
rawUsageDetails["prompt_details.cache_write"] ??
rawUsageDetails["input_cache_creation"];
+ // Reasoning/audio details are included in the emitted output token count
+ // and are therefore subtracted from output below to avoid double counting.
+ const outputReasoningTokens =
+ rawUsageDetails["reasoning.output_tokens"] ??
+ rawUsageDetails["completion_details.reasoning"];
+ const outputAudioTokens = rawUsageDetails["completion_details.audio"];
const normalizedUsageDetails = Object.entries(rawUsageDetails).reduce(
(acc: Record<string, number>, [key, value]) => {
@@ -2878,6 +2884,9 @@ export class OtelIngestionProcessor {
"details.cache_creation_input_tokens",
"prompt_details.cache_write",
"input_cache_creation",
+ "reasoning.output_tokens",
+ "completion_details.reasoning",
+ "completion_details.audio",
].includes(key)
) {
return acc;
@@ -2901,7 +2910,10 @@ export class OtelIngestionProcessor {
}
if (outputTokens !== undefined) {
- normalizedUsageDetails.output = outputTokens;
+ normalizedUsageDetails.output = Math.max(
+ outputTokens - (outputReasoningTokens ?? 0) - (outputAudioTokens ?? 0),
+ 0,
+ );
}
if (totalTokens !== undefined) {
@@ -2916,6 +2928,14 @@ export class OtelIngestionProcessor {
normalizedUsageDetails.input_cache_creation = cacheCreationTokens;
}
+ if (outputReasoningTokens !== undefined) {
+ normalizedUsageDetails.output_reasoning_tokens = outputReasoningTokens;
+ }
+
+ if (outputAudioTokens !== undefined) {
+ normalizedUsageDetails.output_audio_tokens = outputAudioTokens;
+ }
+
return normalizedUsageDetails;
}
diff --git a/web/src/__tests__/server/api/otel/otelMapping.servertest.ts b/web/src/__tests__/server/api/otel/otelMapping.servertest.ts
index a2c2f1b57e..7f1bd6739b 100644
--- a/web/src/__tests__/server/api/otel/otelMapping.servertest.ts
+++ b/web/src/__tests__/server/api/otel/otelMapping.servertest.ts
@@ -7646,6 +7646,329 @@ describe("OTel Resource Span Mapping", () => {
expect(observationEvent?.body.usageDetails.input_cache_creation).toBe(10);
expect(observationEvent?.body.usageDetails.output_audio_tokens).toBe(5);
});
+
+ it("should extract OpenInference llm.token_count.completion_details.reasoning/audio into output detail buckets without double counting", async () => {
+ // OpenInference emits reasoning/audio completion details under
+ // llm.token_count.completion_details.*. The completion count includes
+ // these details (mirroring OpenAI's completion_tokens_details), so they
+ // must be subtracted from output — otherwise the UI, which sums all
+ // output* buckets, double-counts them
+ // (github.com/langfuse/langfuse/issues/12057).
+ const traceId = "abcdef1234567890abcdef1234567894";
+
+ const openinferenceCompletionDetailsSpan = {
+ resource: {
+ attributes: [
+ {
+ key: "service.name",
+ value: { stringValue: "test-service" },
+ },
+ ],
+ },
+ scopeSpans: [
+ {
+ scope: {
+ name: "openinference.instrumentation.openai",
+ version: "0.1.0",
+ },
+ spans: [
+ {
+ traceId: Buffer.from(traceId, "hex"),
+ spanId: Buffer.from("1234567890abcde3", "hex"),
+ name: "openinference-completion-details",
+ kind: 1,
+ startTimeUnixNano: {
+ low: 1000000,
+ high: 406528574,
+ unsigned: true,
+ },
+ endTimeUnixNano: {
+ low: 2000000,
+ high: 406528574,
+ unsigned: true,
+ },
+ attributes: [
+ {
+ key: "llm.token_count.prompt",
+ value: { intValue: { low: 100, high: 0, unsigned: false } },
+ },
+ {
+ key: "llm.token_count.completion",
+ value: { intValue: { low: 50, high: 0, unsigned: false } },
+ },
+ {
+ key: "llm.token_count.completion_details.reasoning",
+ value: { intValue: { low: 30, high: 0, unsigned: false } },
+ },
+ {
+ key: "llm.token_count.completion_details.audio",
+ value: { intValue: { low: 5, high: 0, unsigned: false } },
+ },
+ ],
+ status: {},
+ },
+ ],
+ },
+ ],
+ };
+
+ const events = await convertOtelSpanToIngestionEvent(
+ openinferenceCompletionDetailsSpan,
+ new Set(),
+ );
+ const observationEvent = events.find(
+ (e) => e.type === "generation-create" || e.type === "span-create",
+ );
+
+ expect(observationEvent).toBeDefined();
+ const usageDetails = observationEvent?.body.usageDetails as Record<
+ string,
+ number
+ >;
+ expect(usageDetails.input).toBe(100);
+ // output must be the remainder: 50 - 30 - 5 = 15
+ expect(usageDetails.output).toBe(15);
+ expect(usageDetails.output_reasoning_tokens).toBe(30);
+ expect(usageDetails.output_audio_tokens).toBe(5);
+ // No double count: the sum of all output* buckets must equal the
+ // reported completion token count.
+ const outputSum = Object.entries(usageDetails)
+ .filter(([key]) => key.startsWith("output"))
+ .reduce((acc, [, value]) => acc + value, 0);
+ expect(outputSum).toBe(50);
+ // The raw OpenInference keys must NOT be passed through
+ expect(usageDetails["completion_details.reasoning"]).toBeUndefined();
+ expect(usageDetails["completion_details.audio"]).toBeUndefined();
+ });
+
+ it("should subtract gen_ai.usage.reasoning.output_tokens from inclusive output tokens (Google ADK)", async () => {
+ // Google ADK reports gen_ai.usage.output_tokens as candidates + thoughts,
+ // i.e. inclusive of reasoning. The reasoning share arrives separately as
+ // gen_ai.usage.reasoning.output_tokens and must be subtracted from
+ // output, otherwise the UI (summing all output* buckets) shows more
+ // output tokens than the model produced.
+ const traceId = "abcdef1234567890abcdef1234567895";
+
+ const adkReasoningSpan = {
+ resource: {
+ attributes: [
+ {
+ key: "service.name",
+ value: { stringValue: "test-service" },
+ },
+ ],
+ },
+ scopeSpans: [
+ {
+ scope: {
+ name: "gcp.vertex.agent",
+ version: "1.0.0",
+ },
+ spans: [
+ {
+ traceId: Buffer.from(traceId, "hex"),
+ spanId: Buffer.from("1234567890abcde4", "hex"),
+ name: "adk-reasoning-usage",
+ kind: 1,
+ startTimeUnixNano: {
+ low: 1000000,
+ high: 406528574,
+ unsigned: true,
+ },
+ endTimeUnixNano: {
+ low: 2000000,
+ high: 406528574,
+ unsigned: true,
+ },
+ attributes: [
+ {
+ key: "gen_ai.usage.input_tokens",
+ value: { intValue: { low: 100, high: 0, unsigned: false } },
+ },
+ {
+ key: "gen_ai.usage.output_tokens",
+ value: { intValue: { low: 60, high: 0, unsigned: false } },
+ },
+ {
+ key: "gen_ai.usage.total_tokens",
+ value: { intValue: { low: 160, high: 0, unsigned: false } },
+ },
+ {
+ key: "gen_ai.usage.reasoning.output_tokens",
+ value: { intValue: { low: 25, high: 0, unsigned: false } },
+ },
+ ],
+ status: {},
+ },
+ ],
+ },
+ ],
+ };
+
+ const events = await convertOtelSpanToIngestionEvent(
+ adkReasoningSpan,
+ new Set(),
+ );
+ const observationEvent = events.find(
+ (e) => e.type === "generation-create" || e.type === "span-create",
+ );
+
+ expect(observationEvent).toBeDefined();
+ const usageDetails = observationEvent?.body.usageDetails as Record<
+ string,
+ number
+ >;
+ expect(usageDetails.input).toBe(100);
+ // output must be the non-reasoning remainder: 60 - 25 = 35
+ expect(usageDetails.output).toBe(35);
+ expect(usageDetails.output_reasoning_tokens).toBe(25);
+ expect(usageDetails.total).toBe(160);
+ // No double count: the sum of all output* buckets must equal the
+ // reported (inclusive) output token count.
+ const outputSum = Object.entries(usageDetails)
+ .filter(([key]) => key.startsWith("output"))
+ .reduce((acc, [, value]) => acc + value, 0);
+ expect(outputSum).toBe(60);
+ // The raw ADK key must NOT be passed through
+ expect(usageDetails["reasoning.output_tokens"]).toBeUndefined();
+ });
+
+ it("should clamp output at 0 when reasoning details exceed the emitted output count", async () => {
+ // Reasoning > output must floor output at 0 (negatives are dropped downstream); breaks the output-sum invariant by design.
+ const traceId = "abcdef1234567890abcdef1234567896";
+
+ const clampSpan = {
+ resource: {
+ attributes: [
+ {
+ key: "service.name",
+ value: { stringValue: "test-service" },
+ },
+ ],
+ },
+ scopeSpans: [
+ {
+ scope: {
+ name: "openinference.instrumentation.openai",
+ version: "0.1.0",
+ },
+ spans: [
+ {
+ traceId: Buffer.from(traceId, "hex"),
+ spanId: Buffer.from("1234567890abcde5", "hex"),
+ name: "openinference-reasoning-exceeds-output",
+ kind: 1,
+ startTimeUnixNano: {
+ low: 1000000,
+ high: 406528574,
+ unsigned: true,
+ },
+ endTimeUnixNano: {
+ low: 2000000,
+ high: 406528574,
+ unsigned: true,
+ },
+ attributes: [
+ {
+ key: "llm.token_count.completion",
+ value: { intValue: { low: 50, high: 0, unsigned: false } },
+ },
+ {
+ key: "llm.token_count.completion_details.reasoning",
+ value: { intValue: { low: 60, high: 0, unsigned: false } },
+ },
+ ],
+ status: {},
+ },
+ ],
+ },
+ ],
+ };
+
+ const events = await convertOtelSpanToIngestionEvent(
+ clampSpan,
+ new Set(),
+ );
+ const observationEvent = events.find(
+ (e) => e.type === "generation-create" || e.type === "span-create",
+ );
+
+ expect(observationEvent).toBeDefined();
+ const usageDetails = observationEvent?.body.usageDetails as Record<
+ string,
+ number
+ >;
+ // 50 - 60 must clamp to 0, never go negative
+ expect(usageDetails.output).toBe(0);
+ expect(usageDetails.output_reasoning_tokens).toBe(60);
+ expect(usageDetails["completion_details.reasoning"]).toBeUndefined();
+ });
+
+ it("should keep output absent when only a reasoning detail is emitted", async () => {
+ // A lone reasoning detail must not synthesize an output bucket: output stays absent (not 0).
+ const traceId = "abcdef1234567890abcdef1234567897";
+
+ const reasoningOnlySpan = {
+ resource: {
+ attributes: [
+ {
+ key: "service.name",
+ value: { stringValue: "test-service" },
+ },
+ ],
+ },
+ scopeSpans: [
+ {
+ scope: {
+ name: "gcp.vertex.agent",
+ version: "1.0.0",
+ },
+ spans: [
+ {
+ traceId: Buffer.from(traceId, "hex"),
+ spanId: Buffer.from("1234567890abcde6", "hex"),
+ name: "adk-reasoning-only",
+ kind: 1,
+ startTimeUnixNano: {
+ low: 1000000,
+ high: 406528574,
+ unsigned: true,
+ },
+ endTimeUnixNano: {
+ low: 2000000,
+ high: 406528574,
+ unsigned: true,
+ },
+ attributes: [
+ {
+ key: "gen_ai.usage.reasoning.output_tokens",
+ value: { intValue: { low: 25, high: 0, unsigned: false } },
+ },
+ ],
+ status: {},
+ },
+ ],
+ },
+ ],
+ };
+
+ const events = await convertOtelSpanToIngestionEvent(
+ reasoningOnlySpan,
+ new Set(),
+ );
+ const observationEvent = events.find(
+ (e) => e.type === "generation-create" || e.type === "span-create",
+ );
+
+ expect(observationEvent).toBeDefined();
+ const usageDetails = observationEvent?.body.usageDetails as Record<
+ string,
+ number
+ >;
+ expect(usageDetails.output).toBeUndefined();
+ expect(usageDetails.output_reasoning_tokens).toBe(25);
+ expect(usageDetails["reasoning.output_tokens"]).toBeUndefined();
+ });
});
describe("Google ADK blob-derived cache usage", () => {