From 1c6aaf4a8eb918ad946a1b46d3e549ff7e521b87 Mon Sep 17 00:00:00 2001 From: Alex Castillo Date: Thu, 19 Nov 2026 10:11:03 -0400 Subject: [PATCH] fix(genkit): record cached and reasoning tokens under canonical metric names Co-Authored-By: Claude Sonnet 5.5 --- .changeset/fix-genkit-usage-metric-names.md | 5 +++ .../providers/genkit-instrumentation.test.ts | 36 +++++++++++++++++++ .../providers/genkit-instrumentation.ts | 4 +-- 3 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 .changeset/fix-genkit-usage-metric-names.md diff --git a/.changeset/fix-genkit-usage-metric-names.md b/.changeset/fix-genkit-usage-metric-names.md new file mode 100644 index 000000000..c2dca120f --- /dev/null +++ b/.changeset/fix-genkit-usage-metric-names.md @@ -0,0 +1,5 @@ +--- +"braintrust": patch +--- + +fix(genkit): Record cached and reasoning tokens under the canonical prompt_cached_tokens and completion_reasoning_tokens metric names diff --git a/js/src/instrumentation/providers/genkit-instrumentation.test.ts b/js/src/instrumentation/providers/genkit-instrumentation.test.ts index 066c3a3a4..5de72c7ca 100644 --- a/js/src/instrumentation/providers/genkit-instrumentation.test.ts +++ b/js/src/instrumentation/providers/genkit-instrumentation.test.ts @@ -116,4 +116,40 @@ describe("registerGenkitInstrumentation stream patching", () => { "second", ]); }); + + it("records cached and reasoning tokens under the canonical metric names", async () => { + const logger = _exportsForTestingOnly.useTestBackgroundLogger(); + registerGenkitInstrumentation(); + + await genkitChannels.generate.invoke( + () => + Promise.resolve({ + text: "ok", + usage: { + inputTokens: 10, + outputTokens: 5, + totalTokens: 20, + cachedContentTokens: 4, + thoughtsTokens: 5, + }, + }), + undefined, + [{ prompt: "hi" }], + {}, + ); + + const spans = (await logger.drain()) as Record[]; + const llm = spans.find( + (s) => s.span_attributes?.name === "genkit.generate", + ); + expect(llm?.metrics).toMatchObject({ + prompt_tokens: 10, + completion_tokens: 5, + tokens: 20, + prompt_cached_tokens: 4, + completion_reasoning_tokens: 5, + }); + expect(llm?.metrics).not.toHaveProperty("cached_tokens"); + expect(llm?.metrics).not.toHaveProperty("reasoning_tokens"); + }); }); diff --git a/js/src/instrumentation/providers/genkit-instrumentation.ts b/js/src/instrumentation/providers/genkit-instrumentation.ts index 902abcebe..9845c3a5b 100644 --- a/js/src/instrumentation/providers/genkit-instrumentation.ts +++ b/js/src/instrumentation/providers/genkit-instrumentation.ts @@ -605,8 +605,8 @@ function parseGenkitUsageMetrics( tokens: usage.totalTokens, prompt_tokens: usage.inputTokens, completion_tokens: usage.outputTokens, - cached_tokens: usage.cachedContentTokens, - reasoning_tokens: usage.thoughtsTokens, + prompt_cached_tokens: usage.cachedContentTokens, + completion_reasoning_tokens: usage.thoughtsTokens, }); }