diff --git a/src/routes/messages/anthropic-types.ts b/src/routes/messages/anthropic-types.ts index 881fffcc8..100d906ee 100644 --- a/src/routes/messages/anthropic-types.ts +++ b/src/routes/messages/anthropic-types.ts @@ -101,7 +101,7 @@ export interface AnthropicResponse { | "refusal" | null stop_sequence: string | null - usage: { + usage?: { input_tokens: number output_tokens: number cache_creation_input_tokens?: number diff --git a/src/routes/messages/stream-translation.ts b/src/routes/messages/stream-translation.ts index 55094448f..5f3a6a183 100644 --- a/src/routes/messages/stream-translation.ts +++ b/src/routes/messages/stream-translation.ts @@ -41,17 +41,6 @@ export function translateChunkToAnthropicEvents( model: chunk.model, stop_reason: null, stop_sequence: null, - usage: { - input_tokens: - (chunk.usage?.prompt_tokens ?? 0) - - (chunk.usage?.prompt_tokens_details?.cached_tokens ?? 0), - output_tokens: 0, // Will be updated in message_delta when finished - ...(chunk.usage?.prompt_tokens_details?.cached_tokens - !== undefined && { - cache_read_input_tokens: - chunk.usage.prompt_tokens_details.cached_tokens, - }), - }, }, }) state.messageStartSent = true diff --git a/tests/anthropic-response.test.ts b/tests/anthropic-response.test.ts index 352f06ea7..247b554ae 100644 --- a/tests/anthropic-response.test.ts +++ b/tests/anthropic-response.test.ts @@ -100,7 +100,10 @@ describe("OpenAI to Anthropic Non-Streaming Response Translation", () => { expect(anthropicResponse.id).toBe("chatcmpl-123") expect(anthropicResponse.stop_reason).toBe("end_turn") - expect(anthropicResponse.usage.input_tokens).toBe(9) + expect(anthropicResponse.usage).toBeDefined() + if (anthropicResponse.usage) { + expect(anthropicResponse.usage.input_tokens).toBe(9) + } expect(anthropicResponse.content[0].type).toBe("text") if (anthropicResponse.content[0].type === "text") { expect(anthropicResponse.content[0].text).toBe(