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

fix(langsmith): Record reasoning tokens as completion_reasoning_tokens metric
58 changes: 58 additions & 0 deletions js/src/instrumentation/providers/langsmith-instrumentation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ describe("registerLangSmithInstrumentation", () => {
cache_read: 1,
cache_creation: 2,
},
output_token_details: {
reasoning: 7,
},
},
},
},
Expand Down Expand Up @@ -169,6 +172,7 @@ describe("registerLangSmithInstrumentation", () => {
tokens: 5,
prompt_cached_tokens: 1,
prompt_cache_creation_tokens: 2,
completion_reasoning_tokens: 7,
time_to_first_token: 0.25,
},
span_attributes: { name: "answer", type: "llm" },
Expand All @@ -178,6 +182,60 @@ describe("registerLangSmithInstrumentation", () => {
expect(child.metadata).not.toHaveProperty("runtime");
});

it("records completion_reasoning_tokens only for numeric output_token_details.reasoning", async () => {
const cases = [
{
id: "88888888-8888-4888-8888-888888888801",
usage: { output_tokens: 4, output_token_details: { reasoning: 0 } },
expected: 0,
},
{
id: "88888888-8888-4888-8888-888888888802",
usage: { output_tokens: 4 },
expected: undefined,
},
{
id: "88888888-8888-4888-8888-888888888803",
usage: { output_tokens: 4, output_token_details: { reasoning: "7" } },
expected: undefined,
},
{
id: "88888888-8888-4888-8888-888888888804",
usage: {
output_tokens: 4,
input_token_details: { reasoning: 9 },
output_token_details: { audio: 3 },
},
expected: undefined,
},
];

for (const { id, usage } of cases) {
await langSmithChannels.createRun.invoke(
async () => undefined,
undefined,
[
{
id,
trace_id: id,
name: "reasoning",
run_type: "llm",
outputs: { usage_metadata: usage },
end_time: new Date().toISOString(),
},
],
{},
);
}

const spans = (await backgroundLogger.drain()) as any[];
for (const { id, expected } of cases) {
const span = spans.find((candidate) => candidate.span_id === id);
expect(span?.metrics?.completion_tokens).toBe(4);
expect(span?.metrics?.completion_reasoning_tokens).toBe(expected);
}
});

it("deduplicates mixed lifecycle sources and tolerates update-only runs", async () => {
const id = "33333333-3333-4333-8333-333333333333";
const update = {
Expand Down
7 changes: 7 additions & 0 deletions js/src/instrumentation/providers/langsmith-instrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,13 @@ function extractMetrics(
]);
}

assignFirstMetric(
metrics,
"completion_reasoning_tokens",
ownValue(usage, "output_token_details"),
["reasoning"],
);

if (
metrics.tokens === undefined &&
(metrics.prompt_tokens !== undefined ||
Expand Down