diff --git a/.changeset/langsmith-reasoning-tokens.md b/.changeset/langsmith-reasoning-tokens.md new file mode 100644 index 000000000..9f46d8eec --- /dev/null +++ b/.changeset/langsmith-reasoning-tokens.md @@ -0,0 +1,5 @@ +--- +"braintrust": patch +--- + +fix(langsmith): Record reasoning tokens as completion_reasoning_tokens metric diff --git a/js/src/instrumentation/providers/langsmith-instrumentation.test.ts b/js/src/instrumentation/providers/langsmith-instrumentation.test.ts index 9cb7b0cd2..562d2cf59 100644 --- a/js/src/instrumentation/providers/langsmith-instrumentation.test.ts +++ b/js/src/instrumentation/providers/langsmith-instrumentation.test.ts @@ -117,6 +117,9 @@ describe("registerLangSmithInstrumentation", () => { cache_read: 1, cache_creation: 2, }, + output_token_details: { + reasoning: 7, + }, }, }, }, @@ -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" }, @@ -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 = { diff --git a/js/src/instrumentation/providers/langsmith-instrumentation.ts b/js/src/instrumentation/providers/langsmith-instrumentation.ts index b8968f17b..32122482b 100644 --- a/js/src/instrumentation/providers/langsmith-instrumentation.ts +++ b/js/src/instrumentation/providers/langsmith-instrumentation.ts @@ -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 ||