|
1 | 1 | // npx vitest run src/api/providers/__tests__/anthropic.spec.ts |
2 | 2 |
|
| 3 | +import { type ApiStreamUsageChunk } from "../../../api/transform/stream" |
3 | 4 | import { AnthropicHandler } from "../anthropic" |
4 | 5 | import { MAX_TIMEOUT_MS } from "../utils/abort-signal" |
5 | 6 | import { ApiHandlerOptions } from "../../../shared/api" |
| 7 | +import { calculateApiCostAnthropic } from "../../../shared/cost" |
6 | 8 | import { asyncStreamFrom, collectStream } from "../../../test-utils/stream" |
7 | 9 | import { makeCreateMessageMetadata } from "../../../test-utils/api" |
8 | 10 | import type { ApiStreamChunk } from "../../../api/transform/stream" |
@@ -190,6 +192,55 @@ describe("AnthropicHandler", () => { |
190 | 192 | expect(mockCreate).toHaveBeenCalled() |
191 | 193 | }) |
192 | 194 |
|
| 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 | + |
193 | 244 | it("should include 1M context beta header for Claude Sonnet 4.6 when enabled", async () => { |
194 | 245 | const sonnet46Handler = new AnthropicHandler({ |
195 | 246 | apiKey: "test-api-key", |
|
0 commit comments