Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-genkit-usage-metric-names.md
Original file line number Diff line number Diff line change
@@ -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
36 changes: 36 additions & 0 deletions js/src/instrumentation/providers/genkit-instrumentation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, any>[];
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");
});
});
4 changes: 2 additions & 2 deletions js/src/instrumentation/providers/genkit-instrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
}

Expand Down