Skip to content

Commit 99442c3

Browse files
author
Zoo (VP)
committed
fix(api): adopt cumulative message_delta output tokens in Anthropic/MiniMax cost
1 parent f797477 commit 99442c3

4 files changed

Lines changed: 87 additions & 0 deletions

File tree

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
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 { ApiHandlerOptions } from "../../../shared/api"
6+
import { calculateApiCostAnthropic } from "../../../shared/cost"
57
import { asyncStreamFrom, collectStream } from "../../../test-utils/stream"
68
import { clearAllMocks } from "../../../test-utils/reset"
79

@@ -187,6 +189,48 @@ describe("AnthropicHandler", () => {
187189
expect(mockCreate).toHaveBeenCalled()
188190
})
189191

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

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

Lines changed: 37 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 { asyncStreamFrom, collectStream } from "../../../test-utils/stream"
1719
import { clearAllMocks } from "../../../test-utils/reset"
1820

@@ -281,6 +283,41 @@ describe("MiniMaxHandler", () => {
281283
expect(firstChunk.value).toEqual({ type: "usage", inputTokens: 10, outputTokens: 20 })
282284
})
283285

286+
it("createMessage should adopt cumulative message_delta output tokens for the final totalCost", async () => {
287+
mockCreate.mockResolvedValueOnce(
288+
asyncStreamFrom([
289+
{
290+
type: "message_start",
291+
message: {
292+
usage: {
293+
input_tokens: 10,
294+
output_tokens: 1,
295+
},
296+
},
297+
},
298+
{
299+
type: "message_delta",
300+
delta: { type: "stop_reason", stop_reason: "end_turn" },
301+
usage: { output_tokens: 20 },
302+
},
303+
{
304+
type: "message_stop",
305+
},
306+
]),
307+
)
308+
309+
const chunks = await collectStream(handler.createMessage("system prompt", []))
310+
311+
const usageChunks = chunks.filter(
312+
(chunk): chunk is ApiStreamUsageChunk => chunk.type === "usage" && chunk.totalCost !== undefined,
313+
)
314+
expect(usageChunks).toHaveLength(1)
315+
316+
const { info } = handler.getModel()
317+
expect(usageChunks[0]?.totalCost).toBe(calculateApiCostAnthropic(info, 10, 20).totalCost)
318+
expect(usageChunks[0]?.totalCost).not.toBe(calculateApiCostAnthropic(info, 10, 1).totalCost)
319+
})
320+
284321
it("createMessage should pass correct parameters to MiniMax client", async () => {
285322
const modelId: MinimaxModelId = "MiniMax-M2"
286323
const modelInfo = minimaxModels[modelId]

‎src/api/providers/anthropic.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,9 @@ export class AnthropicHandler extends BaseProvider implements SingleCompletionHa
290290
case "message_delta":
291291
// Tells us stop_reason, stop_sequence, and output tokens
292292
// along the way and at the end of the message.
293+
// The API reports the cumulative output token count, so adopt
294+
// it rather than adding it to the message_start seed value.
295+
outputTokens = chunk.usage.output_tokens || outputTokens
293296
yield {
294297
type: "usage",
295298
inputTokens: 0,

‎src/api/providers/minimax.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ export class MiniMaxHandler extends BaseProvider implements SingleCompletionHand
148148
}
149149
case "message_delta":
150150
// Tells us stop_reason, stop_sequence, and output tokens
151+
// The API reports the cumulative output token count, so adopt
152+
// it rather than adding it to the message_start seed value.
153+
outputTokens = chunk.usage.output_tokens || outputTokens
151154
yield {
152155
type: "usage",
153156
inputTokens: 0,

0 commit comments

Comments
 (0)