Skip to content

Commit a02b0da

Browse files
committed
fix(api): send display summarized for adaptive thinking and parse thinking_tokens
Adaptive-thinking models (the supportsReasoningBinary class) default to display omitted on the API, so thinking blocks arrived with empty text (billed but invisible in the UI). Send display summarized explicitly. Also surface usage.output_tokens_details.thinking_tokens as reasoningTokens in the Anthropic and Vertex handlers, matching every other provider.
1 parent 871bb98 commit a02b0da

6 files changed

Lines changed: 129 additions & 17 deletions

File tree

‎src/api/providers/__tests__/anthropic-vertex.spec.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,7 +1248,7 @@ describe("VertexHandler", () => {
12481248

12491249
expect(mockCreate).toHaveBeenCalledWith(
12501250
expect.objectContaining({
1251-
thinking: { type: "adaptive" },
1251+
thinking: { type: "adaptive", display: "summarized" },
12521252
}),
12531253
undefined,
12541254
)
@@ -1279,7 +1279,7 @@ describe("VertexHandler", () => {
12791279

12801280
expect(mockCreate).toHaveBeenCalledWith(
12811281
expect.objectContaining({
1282-
thinking: { type: "adaptive" },
1282+
thinking: { type: "adaptive", display: "summarized" },
12831283
}),
12841284
undefined,
12851285
)
@@ -1312,7 +1312,7 @@ describe("VertexHandler", () => {
13121312

13131313
expect(mockCreate).toHaveBeenCalledWith(
13141314
expect.objectContaining({
1315-
thinking: { type: "adaptive" },
1315+
thinking: { type: "adaptive", display: "summarized" },
13161316
}),
13171317
undefined,
13181318
)
@@ -1343,7 +1343,7 @@ describe("VertexHandler", () => {
13431343

13441344
expect(mockCreate).toHaveBeenCalledWith(
13451345
expect.objectContaining({
1346-
thinking: { type: "adaptive" },
1346+
thinking: { type: "adaptive", display: "summarized" },
13471347
}),
13481348
undefined,
13491349
)

‎src/api/providers/__tests__/anthropic.spec.ts‎

Lines changed: 86 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { AnthropicHandler } from "../anthropic"
44
import { ApiHandlerOptions } from "../../../shared/api"
55
import { asyncStreamFrom, collectStream } from "../../../test-utils/stream"
6+
import type { ApiStreamChunk } from "../../../api/transform/stream"
67
import { clearAllMocks } from "../../../test-utils/reset"
78

89
// Mock TelemetryService
@@ -247,7 +248,7 @@ describe("AnthropicHandler", () => {
247248
await collectStream(stream)
248249

249250
const requestBody = mockCreate.mock.calls[mockCreate.mock.calls.length - 1]?.[0]
250-
expect(requestBody?.thinking).toEqual({ type: "adaptive" })
251+
expect(requestBody?.thinking).toEqual({ type: "adaptive", display: "summarized" })
251252
expect(requestBody?.max_tokens).toBe(16384)
252253
})
253254

@@ -290,7 +291,7 @@ describe("AnthropicHandler", () => {
290291
await collectStream(stream)
291292

292293
const requestBody = mockCreate.mock.calls[mockCreate.mock.calls.length - 1]?.[0]
293-
expect(requestBody?.thinking).toEqual({ type: "adaptive" })
294+
expect(requestBody?.thinking).toEqual({ type: "adaptive", display: "summarized" })
294295
expect(requestBody?.max_tokens).toBe(32768)
295296
})
296297

@@ -334,7 +335,7 @@ describe("AnthropicHandler", () => {
334335
await collectStream(stream)
335336

336337
const requestBody = mockCreate.mock.calls[mockCreate.mock.calls.length - 1]?.[0]
337-
expect(requestBody?.thinking).toEqual({ type: "adaptive" })
338+
expect(requestBody?.thinking).toEqual({ type: "adaptive", display: "summarized" })
338339
expect(requestBody?.max_tokens).toBe(16384)
339340
})
340341

@@ -377,7 +378,7 @@ describe("AnthropicHandler", () => {
377378
await collectStream(stream)
378379

379380
const requestBody = mockCreate.mock.calls[mockCreate.mock.calls.length - 1]?.[0]
380-
expect(requestBody?.thinking).toEqual({ type: "adaptive" })
381+
expect(requestBody?.thinking).toEqual({ type: "adaptive", display: "summarized" })
381382
expect(requestBody?.max_tokens).toBe(32768)
382383
})
383384

@@ -400,7 +401,7 @@ describe("AnthropicHandler", () => {
400401

401402
const requestBody = mockCreate.mock.calls[mockCreate.mock.calls.length - 1]?.[0]
402403
const requestOptions = mockCreate.mock.calls[mockCreate.mock.calls.length - 1]?.[1]
403-
expect(requestBody?.thinking).toEqual({ type: "adaptive" })
404+
expect(requestBody?.thinking).toEqual({ type: "adaptive", display: "summarized" })
404405
expect(requestBody?.temperature).toBeUndefined()
405406
expect(requestBody?.max_tokens).toBe(32768)
406407
expect(requestOptions?.headers?.["anthropic-beta"]).toContain("prompt-caching-2024-07-31")
@@ -425,7 +426,7 @@ describe("AnthropicHandler", () => {
425426

426427
const requestBody = mockCreate.mock.calls[mockCreate.mock.calls.length - 1]?.[0]
427428
const requestOptions = mockCreate.mock.calls[mockCreate.mock.calls.length - 1]?.[1]
428-
expect(requestBody?.thinking).toEqual({ type: "adaptive" })
429+
expect(requestBody?.thinking).toEqual({ type: "adaptive", display: "summarized" })
429430
expect(requestBody?.temperature).toBeUndefined()
430431
expect(requestBody?.max_tokens).toBe(32768)
431432
expect(requestOptions?.headers?.["anthropic-beta"]).toContain("prompt-caching-2024-07-31")
@@ -450,7 +451,7 @@ describe("AnthropicHandler", () => {
450451

451452
const requestBody = mockCreate.mock.calls[mockCreate.mock.calls.length - 1]?.[0]
452453
const requestOptions = mockCreate.mock.calls[mockCreate.mock.calls.length - 1]?.[1]
453-
expect(requestBody?.thinking).toEqual({ type: "adaptive" })
454+
expect(requestBody?.thinking).toEqual({ type: "adaptive", display: "summarized" })
454455
expect(requestBody?.temperature).toBeUndefined()
455456
expect(requestBody?.max_tokens).toBe(32768)
456457
expect(requestOptions?.headers?.["anthropic-beta"]).toContain("prompt-caching-2024-07-31")
@@ -474,7 +475,84 @@ describe("AnthropicHandler", () => {
474475

475476
const requestBody = mockCreate.mock.calls[mockCreate.mock.calls.length - 1]?.[0]
476477
expect(requestBody?.model).toBe("claude-sonnet-5-bf")
477-
expect(requestBody?.thinking).toEqual({ type: "adaptive" })
478+
expect(requestBody?.thinking).toEqual({ type: "adaptive", display: "summarized" })
479+
})
480+
481+
it("should surface thinking_tokens from output_tokens_details in usage chunks", async () => {
482+
// Adaptive models report reasoning tokens inside
483+
// `output_tokens_details.thinking_tokens` (message_start snapshot is
484+
// typically absent; message_delta carries the final decomposition).
485+
mockCreate.mockImplementationOnce(async () =>
486+
asyncStreamFrom([
487+
{
488+
type: "message_start",
489+
message: {
490+
usage: {
491+
input_tokens: 100,
492+
output_tokens: 50,
493+
},
494+
},
495+
},
496+
{
497+
type: "content_block_start",
498+
index: 0,
499+
content_block: {
500+
type: "text",
501+
text: "Hello",
502+
},
503+
},
504+
{
505+
type: "content_block_delta",
506+
index: 0,
507+
delta: {
508+
type: "text_delta",
509+
text: " world",
510+
},
511+
},
512+
{
513+
type: "message_delta",
514+
usage: {
515+
output_tokens: 200,
516+
output_tokens_details: {
517+
thinking_tokens: 150,
518+
},
519+
},
520+
delta: {
521+
stop_reason: "end_turn",
522+
stop_sequence: null,
523+
},
524+
},
525+
]),
526+
)
527+
528+
const adaptiveHandler = new AnthropicHandler({
529+
apiKey: "test-api-key",
530+
apiModelId: "claude-opus-4-7",
531+
enableReasoningEffort: true,
532+
})
533+
534+
const stream = adaptiveHandler.createMessage("prompt", [
535+
{
536+
role: "user",
537+
content: [{ type: "text", text: "Hi" }],
538+
},
539+
])
540+
541+
const chunks: ApiStreamChunk[] = await collectStream(stream)
542+
543+
// message_start snapshot carries no thinking decomposition
544+
const usageChunks = chunks.filter(
545+
(chunk): chunk is Extract<ApiStreamChunk, { type: "usage" }> => chunk.type === "usage",
546+
)
547+
const startUsage = usageChunks.find((chunk) => chunk.inputTokens > 0)
548+
expect(startUsage).toBeDefined()
549+
expect(startUsage?.reasoningTokens).toBeUndefined()
550+
551+
// message_delta surfaces the final reasoning token count
552+
const deltaUsage = usageChunks.find((chunk) => chunk.inputTokens === 0)
553+
expect(deltaUsage).toBeDefined()
554+
expect(deltaUsage?.outputTokens).toBe(200)
555+
expect(deltaUsage?.reasoningTokens).toBe(150)
478556
})
479557
})
480558

‎src/api/providers/anthropic-vertex.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,21 +123,30 @@ export class AnthropicVertexHandler extends BaseProvider implements SingleComple
123123
case "message_start": {
124124
const usage = chunk.message!.usage
125125

126+
// `output_tokens_details.thinking_tokens` decomposes the billed
127+
// output into internal reasoning tokens; surfaced for per-turn
128+
// thinking telemetry (billing already includes it in output_tokens).
126129
yield {
127130
type: "usage",
128131
inputTokens: usage.input_tokens || 0,
129132
outputTokens: usage.output_tokens || 0,
130133
cacheWriteTokens: usage.cache_creation_input_tokens || undefined,
131134
cacheReadTokens: usage.cache_read_input_tokens || undefined,
135+
...(typeof usage.output_tokens_details?.thinking_tokens === "number"
136+
? { reasoningTokens: usage.output_tokens_details!.thinking_tokens }
137+
: {}),
132138
}
133139

134140
break
135141
}
136142
case "message_delta": {
143+
const deltaThinkingTokens = chunk.usage!.output_tokens_details?.thinking_tokens
144+
137145
yield {
138146
type: "usage",
139147
inputTokens: 0,
140148
outputTokens: chunk.usage!.output_tokens || 0,
149+
...(typeof deltaThinkingTokens === "number" ? { reasoningTokens: deltaThinkingTokens } : {}),
141150
}
142151

143152
break

‎src/api/providers/anthropic.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,12 +255,19 @@ export class AnthropicHandler extends BaseProvider implements SingleCompletionHa
255255
cache_read_input_tokens,
256256
} = chunk.message.usage
257257

258+
// `output_tokens_details.thinking_tokens` decomposes the billed
259+
// output into internal reasoning tokens (0 ⇒ the model skipped
260+
// thinking this turn). `output_tokens` already includes them for
261+
// billing; this field exists for per-turn thinking telemetry.
262+
const startThinkingTokens = chunk.message.usage.output_tokens_details?.thinking_tokens
263+
258264
yield {
259265
type: "usage",
260266
inputTokens: input_tokens,
261267
outputTokens: output_tokens,
262268
cacheWriteTokens: cache_creation_input_tokens || undefined,
263269
cacheReadTokens: cache_read_input_tokens || undefined,
270+
...(typeof startThinkingTokens === "number" ? { reasoningTokens: startThinkingTokens } : {}),
264271
}
265272

266273
inputTokens += input_tokens
@@ -270,16 +277,22 @@ export class AnthropicHandler extends BaseProvider implements SingleCompletionHa
270277

271278
break
272279
}
273-
case "message_delta":
280+
case "message_delta": {
274281
// Tells us stop_reason, stop_sequence, and output tokens
275282
// along the way and at the end of the message.
283+
// Carries the final `thinking_tokens` decomposition for the
284+
// whole message (the `message_start` snapshot typically has none).
285+
const deltaThinkingTokens = chunk.usage.output_tokens_details?.thinking_tokens
286+
276287
yield {
277288
type: "usage",
278289
inputTokens: 0,
279290
outputTokens: chunk.usage.output_tokens || 0,
291+
...(typeof deltaThinkingTokens === "number" ? { reasoningTokens: deltaThinkingTokens } : {}),
280292
}
281293

282294
break
295+
}
283296
case "message_stop":
284297
// No usage data, just an indicator that the message is done.
285298
break

‎src/api/transform/__tests__/reasoning.spec.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ describe("reasoning.ts", () => {
477477
reasoningBudget: undefined,
478478
})
479479

480-
expect(result).toEqual({ type: "adaptive" })
480+
expect(result).toEqual({ type: "adaptive", display: "summarized" })
481481
})
482482

483483
it("should omit thinking for Claude Opus 4.7 when reasoning is disabled", () => {
@@ -1241,7 +1241,7 @@ describe("reasoning.ts", () => {
12411241
reasoningBudget: undefined,
12421242
})
12431243

1244-
expect(result).toEqual({ type: "adaptive" })
1244+
expect(result).toEqual({ type: "adaptive", display: "summarized" })
12451245
})
12461246

12471247
it("should return correct types for OpenAI reasoning params", () => {

‎src/api/transform/reasoning.ts‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,19 @@ export type RooReasoningParams = {
1818
}
1919

2020
export type AnthropicReasoningParams = BetaThinkingConfigParam
21-
export type AnthropicProviderReasoningParams = AnthropicReasoningParams | { type: "adaptive" }
21+
/**
22+
* Adaptive thinking models (the `supportsReasoningBinary` class) reject
23+
* `budget_tokens` payloads and use `thinking: { type: "adaptive" }`.
24+
*
25+
* `display` is carried explicitly because the API default for this model
26+
* class is `"omitted"` — thinking blocks then arrive as empty text (still
27+
* billed, but invisible to the user). `"summarized"` returns the
28+
* server-side summarized thinking text with no extra cost (billing is on
29+
* full thinking tokens, not the summary).
30+
*/
31+
export type AnthropicProviderReasoningParams =
32+
| AnthropicReasoningParams
33+
| { type: "adaptive"; display?: "summarized" | "omitted" }
2234

2335
export type OpenAiReasoningParams = { reasoning_effort: OpenAI.Chat.ChatCompletionCreateParams["reasoning_effort"] }
2436

@@ -118,7 +130,7 @@ export const getAnthropicProviderReasoning = ({
118130
settings,
119131
}: GetModelReasoningOptions): AnthropicProviderReasoningParams | undefined => {
120132
if (model.supportsReasoningBinary && settings.enableReasoningEffort) {
121-
return { type: "adaptive" }
133+
return { type: "adaptive", display: "summarized" }
122134
}
123135

124136
return getAnthropicReasoning({ model, reasoningBudget, settings })

0 commit comments

Comments
 (0)