Skip to content

Commit 87523ba

Browse files
myk1ytZoo (VP)
andauthored
fix(api): adopt cumulative message_delta output tokens in Anthropic/MiniMax cost (Zoo-Code-Org#2242)
* fix(api): adopt cumulative message_delta output tokens in Anthropic/MiniMax cost * test(api): cover zero-token message_delta fallback in cost adoption --------- Co-authored-by: Zoo (VP) <[email protected]>
1 parent dcc6bcf commit 87523ba

4 files changed

Lines changed: 102 additions & 0 deletions

File tree

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
// npx vitest run src/api/providers/__tests__/anthropic.spec.ts
22

3+
import { type ApiStreamUsageChunk } from "../../../api/transform/stream"
34
import { AnthropicHandler } from "../anthropic"
45
import { MAX_TIMEOUT_MS } from "../utils/abort-signal"
56
import { ApiHandlerOptions } from "../../../shared/api"
7+
import { calculateApiCostAnthropic } from "../../../shared/cost"
68
import { asyncStreamFrom, collectStream } from "../../../test-utils/stream"
79
import { makeCreateMessageMetadata } from "../../../test-utils/api"
810
import type { ApiStreamChunk } from "../../../api/transform/stream"
@@ -190,6 +192,55 @@ describe("AnthropicHandler", () => {
190192
expect(mockCreate).toHaveBeenCalled()
191193
})
192194

195+
it("should adopt the cumulative message_delta output tokens for the final totalCost", async () => {
196+
mockCreate.mockImplementationOnce(async () =>
197+
asyncStreamFrom([
198+
{
199+
type: "message_start",
200+
message: {
201+
usage: {
202+
input_tokens: 100,
203+
output_tokens: 1,
204+
},
205+
},
206+
},
207+
{
208+
type: "message_delta",
209+
delta: { type: "stop_reason", stop_reason: "end_turn" },
210+
usage: { output_tokens: 500 },
211+
},
212+
{
213+
// A delta without a usable count (e.g. 0) must keep the
214+
// previously adopted cumulative value.
215+
type: "message_delta",
216+
delta: { type: "stop_reason", stop_reason: "end_turn" },
217+
usage: { output_tokens: 0 },
218+
},
219+
{
220+
type: "message_stop",
221+
},
222+
]),
223+
)
224+
225+
const stream = handler.createMessage(systemPrompt, [
226+
{
227+
role: "user",
228+
content: [{ type: "text" as const, text: "Hello" }],
229+
},
230+
])
231+
232+
const chunks = await collectStream(stream)
233+
234+
const usageChunks = chunks.filter(
235+
(chunk): chunk is ApiStreamUsageChunk => chunk.type === "usage" && chunk.totalCost !== undefined,
236+
)
237+
expect(usageChunks).toHaveLength(1)
238+
239+
const { info } = handler.getModel()
240+
expect(usageChunks[0]?.totalCost).toBe(calculateApiCostAnthropic(info, 100, 500).totalCost)
241+
expect(usageChunks[0]?.totalCost).not.toBe(calculateApiCostAnthropic(info, 100, 1).totalCost)
242+
})
243+
193244
it("should include 1M context beta header for Claude Sonnet 4.6 when enabled", async () => {
194245
const sonnet46Handler = new AnthropicHandler({
195246
apiKey: "test-api-key",

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ import { Anthropic } from "@anthropic-ai/sdk"
1212

1313
import { type MinimaxModelId, minimaxDefaultModelId, minimaxModels } from "@roo-code/types"
1414

15+
import { type ApiStreamUsageChunk } from "../../../api/transform/stream"
1516
import { MiniMaxHandler } from "../minimax"
17+
import { calculateApiCostAnthropic } from "../../../shared/cost"
1618
import { MAX_TIMEOUT_MS } from "../utils/abort-signal"
1719
import { asyncStreamFrom, collectStream } from "../../../test-utils/stream"
1820
import { makeCreateMessageMetadata } from "../../../test-utils/api"
@@ -373,6 +375,48 @@ describe("MiniMaxHandler", () => {
373375
expect(firstChunk.value).toEqual({ type: "usage", inputTokens: 10, outputTokens: 20 })
374376
})
375377

378+
it("createMessage should adopt cumulative message_delta output tokens for the final totalCost", async () => {
379+
mockCreate.mockResolvedValueOnce(
380+
asyncStreamFrom([
381+
{
382+
type: "message_start",
383+
message: {
384+
usage: {
385+
input_tokens: 10,
386+
output_tokens: 1,
387+
},
388+
},
389+
},
390+
{
391+
type: "message_delta",
392+
delta: { type: "stop_reason", stop_reason: "end_turn" },
393+
usage: { output_tokens: 20 },
394+
},
395+
{
396+
// A delta without a usable count (e.g. 0) must keep the
397+
// previously adopted cumulative value.
398+
type: "message_delta",
399+
delta: { type: "stop_reason", stop_reason: "end_turn" },
400+
usage: { output_tokens: 0 },
401+
},
402+
{
403+
type: "message_stop",
404+
},
405+
]),
406+
)
407+
408+
const chunks = await collectStream(handler.createMessage("system prompt", []))
409+
410+
const usageChunks = chunks.filter(
411+
(chunk): chunk is ApiStreamUsageChunk => chunk.type === "usage" && chunk.totalCost !== undefined,
412+
)
413+
expect(usageChunks).toHaveLength(1)
414+
415+
const { info } = handler.getModel()
416+
expect(usageChunks[0]?.totalCost).toBe(calculateApiCostAnthropic(info, 10, 20).totalCost)
417+
expect(usageChunks[0]?.totalCost).not.toBe(calculateApiCostAnthropic(info, 10, 1).totalCost)
418+
})
419+
376420
it("createMessage should pass correct parameters to MiniMax client", async () => {
377421
const modelId: MinimaxModelId = "MiniMax-M2"
378422
const modelInfo = minimaxModels[modelId]

‎src/api/providers/anthropic.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,10 @@ export class AnthropicHandler extends BaseProvider implements SingleCompletionHa
298298
// whole message (the `message_start` snapshot typically has none).
299299
const deltaThinkingTokens = chunk.usage.output_tokens_details?.thinking_tokens
300300

301+
// The API reports the cumulative output token count, so adopt
302+
// it rather than adding it to the message_start seed value.
303+
outputTokens = chunk.usage.output_tokens || outputTokens
304+
301305
yield {
302306
type: "usage",
303307
inputTokens: 0,

‎src/api/providers/minimax.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,9 @@ export class MiniMaxHandler extends BaseProvider implements SingleCompletionHand
167167
}
168168
case "message_delta":
169169
// Tells us stop_reason, stop_sequence, and output tokens
170+
// The API reports the cumulative output token count, so adopt
171+
// it rather than adding it to the message_start seed value.
172+
outputTokens = chunk.usage.output_tokens || outputTokens
170173
yield {
171174
type: "usage",
172175
inputTokens: 0,

0 commit comments

Comments
 (0)