Skip to content
Merged
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/anthropic-thinking-tokens-metric.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"braintrust": patch
---

fix(anthropic): Record thinking tokens as metric

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,7 @@ span_tree:
│ }
│ }
│ metrics: {
│ "completion_reasoning_tokens": 42,
│ "completion_tokens": 48,
│ "prompt_cache_creation_1h_tokens": 0,
│ "prompt_cache_creation_5m_tokens": 0,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,7 @@ span_tree:
│ }
│ }
│ metrics: {
│ "completion_reasoning_tokens": 42,
│ "completion_tokens": 48,
│ "prompt_cache_creation_1h_tokens": 0,
│ "prompt_cache_creation_5m_tokens": 0,
Expand Down
5 changes: 5 additions & 0 deletions e2e/scenarios/anthropic-instrumentation/assertions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,12 @@ export function defineAnthropicInstrumentationAssertions(options: {
time_to_first_token: expect.any(Number),
prompt_tokens: expect.any(Number),
completion_tokens: expect.any(Number),
completion_reasoning_tokens: expect.any(Number),
});
const metrics = span?.metrics as Record<string, number>;
expect(metrics.completion_reasoning_tokens).toBeLessThanOrEqual(
metrics.completion_tokens,
);
expect(
output?.content?.some((block) => block.type === "thinking"),
).toBe(true);
Expand Down
70 changes: 70 additions & 0 deletions js/src/instrumentation/plugins/anthropic-plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,42 @@ describe("parseMetricsFromUsage", () => {
});
});

it("should map output_tokens_details.thinking_tokens to completion_reasoning_tokens", () => {
const usage = {
input_tokens: 100,
output_tokens: 80,
output_tokens_details: {
thinking_tokens: 20,
},
};

const result = parseMetricsFromUsageForTest(usage);

expect(result).toEqual({
prompt_tokens: 100,
completion_tokens: 80,
completion_reasoning_tokens: 20,
});
});

it("should ignore a null or non-number output token breakdown", () => {
expect(
parseMetricsFromUsageForTest({
input_tokens: 100,
output_tokens: 80,
output_tokens_details: null,
}),
).toEqual({ prompt_tokens: 100, completion_tokens: 80 });

expect(
parseMetricsFromUsageForTest({
input_tokens: 100,
output_tokens: 80,
output_tokens_details: { thinking_tokens: "not a number" },
}),
).toEqual({ prompt_tokens: 100, completion_tokens: 80 });
});

it("should ignore non-number token values", () => {
const usage = {
input_tokens: "not a number",
Expand Down Expand Up @@ -283,6 +319,40 @@ describe("aggregateAnthropicStreamChunks", () => {
});
});

it("should carry thinking tokens through from message_delta without double counting", () => {
const chunks = [
{
type: "message_start",
message: {
usage: {
input_tokens: 10,
output_tokens: 0,
},
},
},
{
type: "content_block_delta",
delta: { type: "thinking_delta", thinking: "hmm" },
},
{
type: "message_delta",
usage: {
output_tokens: 80,
output_tokens_details: { thinking_tokens: 20 },
},
},
];

const result = aggregateAnthropicStreamChunksForTest(chunks);

expect(result.metrics).toMatchObject({
prompt_tokens: 10,
completion_tokens: 80,
completion_reasoning_tokens: 20,
tokens: 90,
});
});

it("should concatenate multiple text deltas", () => {
const chunks = [
{
Expand Down
8 changes: 8 additions & 0 deletions js/src/instrumentation/plugins/anthropic-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,14 @@ export function parseMetricsFromUsage(
}
}

// Thinking tokens are already included in output_tokens.
if (isObject(usage.output_tokens_details)) {
const thinkingTokens = usage.output_tokens_details.thinking_tokens;
if (typeof thinkingTokens === "number") {
metrics.completion_reasoning_tokens = thinkingTokens;
}
}

if (isObject(usage.server_tool_use)) {
for (const [name, value] of Object.entries(usage.server_tool_use)) {
if (typeof value === "number") {
Expand Down
4 changes: 4 additions & 0 deletions js/src/vendor-sdk-types/anthropic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ export interface AnthropicUsage {
cache_read_input_tokens?: number;
cache_creation_input_tokens?: number;
cache_creation?: AnthropicCacheCreationUsage | null;
output_tokens_details?: {
thinking_tokens?: number;
[key: string]: unknown;
} | null;
server_tool_use?: AnthropicServerToolUseUsage;
[key: string]: unknown;
}
Expand Down
Loading