Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions apps/gateway/src/chat/tools/extract-token-usage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,50 @@ describe("extractTokenUsage", () => {
});
});

describe("vertex-anthropic", () => {
// Vertex Anthropic streams the same Anthropic Messages wire format, so it
// must extract usage identically to the direct anthropic provider rather
// than falling through to the OpenAI-shaped default branch.
it("extracts cache and reasoning tokens from a streaming message_start", () => {
const data = {
type: "message_start",
message: {
usage: {
input_tokens: 100,
cache_creation_input_tokens: 50,
cache_read_input_tokens: 800,
output_tokens: 2928,
output_tokens_details: { thinking_tokens: 1502 },
},
},
};

const result = extractTokenUsage(data, "vertex-anthropic");

expect(result.promptTokens).toBe(950); // 100 + 50 + 800
expect(result.cachedTokens).toBe(800);
expect(result.cacheCreationTokens).toBe(50);
expect(result.completionTokens).toBe(2928);
expect(result.reasoningTokens).toBe(1502);
expect(result.totalTokens).toBe(3878); // 950 + 2928, reasoning not double-counted
});

it("matches the direct anthropic provider for the same chunk", () => {
const data = {
usage: {
input_tokens: 51,
cache_read_input_tokens: 20,
output_tokens: 136,
reasoning_output_tokens: 31,
},
};

expect(extractTokenUsage(data, "vertex-anthropic")).toEqual(
extractTokenUsage(data, "anthropic"),
);
});
});

describe("alibaba", () => {
it("extracts prompt_tokens_details.cache_creation_input_tokens into 5m cache write fields", () => {
const data = {
Expand Down
1 change: 1 addition & 0 deletions apps/gateway/src/chat/tools/extract-token-usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ export function extractTokenUsage(
}
break;
case "anthropic":
case "vertex-anthropic":
{
const usage = data.message?.usage ?? data.usage;
if (!usage) {
Expand Down
Loading