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/langchain-reasoning-tokens-metric.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"braintrust": patch
---

fix(langchain): Record reasoning tokens as metric
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@
"tags": []
},
"metrics": {
"completion_reasoning_tokens": "<number>",
"completion_tokens": "<number>",
"end": 0,
"prompt_cached_tokens": "<number>",
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 @@ -113,6 +113,7 @@ span_tree:
│ "model": "gpt-4o-mini-2024-07-18"
│ }
│ metric_keys: [
│ "completion_reasoning_tokens",
│ "completion_tokens",
│ "prompt_cached_tokens",
│ "prompt_tokens",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@
"tags": []
},
"metrics": {
"completion_reasoning_tokens": "<number>",
"completion_tokens": "<number>",
"end": 0,
"prompt_cached_tokens": "<number>",
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 @@ -113,6 +113,7 @@ span_tree:
│ "model": "gpt-4o-mini-2024-07-18"
│ }
│ metric_keys: [
│ "completion_reasoning_tokens",
│ "completion_tokens",
│ "prompt_cached_tokens",
│ "prompt_tokens",
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 @@ -155,6 +155,7 @@ span_tree:
│ "tags": []
│ }
│ metrics: {
│ "completion_reasoning_tokens": 0,
│ "completion_tokens": 2,
│ "prompt_cached_tokens": 0,
│ "prompt_tokens": 12,
Expand Down Expand Up @@ -553,6 +554,7 @@ span_tree:
│ ]
│ }
│ metrics: {
│ "completion_reasoning_tokens": 0,
│ "completion_tokens": 2,
│ "prompt_cached_tokens": 0,
│ "prompt_tokens": 18,
Expand Down Expand Up @@ -716,6 +718,7 @@ span_tree:
│ "tags": []
│ }
│ metrics: {
│ "completion_reasoning_tokens": 0,
│ "completion_tokens": 6,
│ "prompt_cached_tokens": 0,
│ "prompt_tokens": 22,
Expand Down Expand Up @@ -940,6 +943,7 @@ span_tree:
│ "tags": []
│ }
│ metrics: {
│ "completion_reasoning_tokens": 0,
│ "completion_tokens": 16,
│ "prompt_cached_tokens": 0,
│ "prompt_tokens": 72,
Expand Down Expand Up @@ -1191,6 +1195,7 @@ span_tree:
│ "tags": []
│ }
│ metrics: {
│ "completion_reasoning_tokens": 0,
│ "completion_tokens": 21,
│ "prompt_cached_tokens": 0,
│ "prompt_tokens": 76,
Expand Down Expand Up @@ -1457,6 +1462,7 @@ span_tree:
"tags": []
}
metrics: {
"completion_reasoning_tokens": 0,
"completion_tokens": 11,
"prompt_cached_tokens": 0,
"prompt_tokens": 106,
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 @@ -155,6 +155,7 @@ span_tree:
│ "tags": []
│ }
│ metrics: {
│ "completion_reasoning_tokens": 0,
│ "completion_tokens": 2,
│ "prompt_cached_tokens": 0,
│ "prompt_tokens": 12,
Expand Down Expand Up @@ -553,6 +554,7 @@ span_tree:
│ ]
│ }
│ metrics: {
│ "completion_reasoning_tokens": 0,
│ "completion_tokens": 2,
│ "prompt_cached_tokens": 0,
│ "prompt_tokens": 18,
Expand Down Expand Up @@ -716,6 +718,7 @@ span_tree:
│ "tags": []
│ }
│ metrics: {
│ "completion_reasoning_tokens": 0,
│ "completion_tokens": 6,
│ "prompt_cached_tokens": 0,
│ "prompt_tokens": 22,
Expand Down Expand Up @@ -940,6 +943,7 @@ span_tree:
│ "tags": []
│ }
│ metrics: {
│ "completion_reasoning_tokens": 0,
│ "completion_tokens": 16,
│ "prompt_cached_tokens": 0,
│ "prompt_tokens": 72,
Expand Down Expand Up @@ -1191,6 +1195,7 @@ span_tree:
│ "tags": []
│ }
│ metrics: {
│ "completion_reasoning_tokens": 0,
│ "completion_tokens": 21,
│ "prompt_cached_tokens": 0,
│ "prompt_tokens": 76,
Expand Down Expand Up @@ -1457,6 +1462,7 @@ span_tree:
"tags": []
}
metrics: {
"completion_reasoning_tokens": 0,
"completion_tokens": 11,
"prompt_cached_tokens": 0,
"prompt_tokens": 106,
Expand Down
62 changes: 62 additions & 0 deletions js/src/wrappers/langchain/callback-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,66 @@ describe("BraintrustLangChainCallbackHandler metrics", () => {
tokens: 12,
});
});

it("preserves reasoning metrics from message usage metadata", async () => {
const { endLog } = await finishChatModelRun({
generations: [
[
{
message: {
usage_metadata: {
input_tokens: 10,
output_tokens: 20,
total_tokens: 30,
output_token_details: {
reasoning: 16,
},
},
},
},
],
],
});

expect(endLog.metrics).toEqual({
prompt_tokens: 10,
completion_tokens: 20,
completion_reasoning_tokens: 16,
tokens: 30,
});
});

it("reports zero reasoning tokens rather than dropping the field", async () => {
const { endLog } = await finishChatModelRun({
generations: [
[
{
message: {
usage_metadata: {
input_tokens: 12,
output_tokens: 2,
total_tokens: 14,
input_token_details: {
audio: 0,
cache_read: 0,
},
output_token_details: {
audio: 0,
reasoning: 0,
},
},
},
},
],
],
});

expect(endLog.metrics).toEqual({
prompt_tokens: 12,
completion_tokens: 2,
prompt_cached_tokens: 0,
completion_reasoning_tokens: 0,
tokens: 14,
});
});
});
4 changes: 4 additions & 0 deletions js/src/wrappers/langchain/callback-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,7 @@ function getMetricsFromResponse(
}

const inputTokenDetails = usageMetadata.input_token_details;
const outputTokenDetails = usageMetadata.output_token_details;
return normalizeTokenMetrics({
total_tokens: usageMetadata.total_tokens,
prompt_tokens: usageMetadata.input_tokens,
Expand All @@ -533,6 +534,9 @@ function getMetricsFromResponse(
prompt_cached_tokens: isRecord(inputTokenDetails)
? inputTokenDetails.cache_read
: undefined,
completion_reasoning_tokens: isRecord(outputTokenDetails)
? outputTokenDetails.reasoning
: undefined,
});
}

Expand Down