fix: update remaining providers to use new calculateApiCost signature

- Fixed anthropic.ts to destructure totalCost correctly
- Fixed deepinfra.ts to destructure totalCost correctly
- Updated cost test file for both Anthropic and OpenAI functions
This commit is contained in:
Hannes Rudolph 2025-11-03 22:48:21 -07:00
parent dd6d37ebd1
commit b69e2b524e
3 changed files with 11 additions and 27 deletions

View file

@ -230,7 +230,7 @@ export class AnthropicHandler extends BaseProvider implements SingleCompletionHa
}
if (inputTokens > 0 || outputTokens > 0 || cacheWriteTokens > 0 || cacheReadTokens > 0) {
const { totalCost } = calculateApiCostAnthropic(
const totalCost = calculateApiCostAnthropic(
this.getModel().info,
inputTokens,
outputTokens,

View file

@ -131,9 +131,9 @@ export class DeepInfraHandler extends RouterProvider implements SingleCompletion
const cacheWriteTokens = usage?.prompt_tokens_details?.cache_write_tokens || 0
const cacheReadTokens = usage?.prompt_tokens_details?.cached_tokens || 0
const { totalCost } = modelInfo
const totalCost = modelInfo
? calculateApiCostOpenAI(modelInfo, inputTokens, outputTokens, cacheWriteTokens, cacheReadTokens)
: { totalCost: 0 }
: 0
return {
type: "usage",

View file

@ -22,9 +22,7 @@ describe("Cost Utility", () => {
// Input cost: (3.0 / 1_000_000) * 1000 = 0.003
// Output cost: (15.0 / 1_000_000) * 500 = 0.0075
// Total: 0.003 + 0.0075 = 0.0105
expect(result.totalCost).toBe(0.0105)
expect(result.totalInputTokens).toBe(1000)
expect(result.totalOutputTokens).toBe(500)
expect(result).toBe(0.0105)
})
it("should handle cache writes cost", () => {
@ -34,9 +32,7 @@ describe("Cost Utility", () => {
// Output cost: (15.0 / 1_000_000) * 500 = 0.0075
// Cache writes: (3.75 / 1_000_000) * 2000 = 0.0075
// Total: 0.003 + 0.0075 + 0.0075 = 0.018
expect(result.totalCost).toBeCloseTo(0.018, 6)
expect(result.totalInputTokens).toBe(3000) // 1000 + 2000
expect(result.totalOutputTokens).toBe(500)
expect(result).toBeCloseTo(0.018, 6)
})
it("should handle cache reads cost", () => {
@ -46,9 +42,7 @@ describe("Cost Utility", () => {
// Output cost: (15.0 / 1_000_000) * 500 = 0.0075
// Cache reads: (0.3 / 1_000_000) * 3000 = 0.0009
// Total: 0.003 + 0.0075 + 0.0009 = 0.0114
expect(result.totalCost).toBe(0.0114)
expect(result.totalInputTokens).toBe(4000) // 1000 + 3000
expect(result.totalOutputTokens).toBe(500)
expect(result).toBe(0.0114)
})
it("should handle all cost components together", () => {
@ -59,9 +53,7 @@ describe("Cost Utility", () => {
// Cache writes: (3.75 / 1_000_000) * 2000 = 0.0075
// Cache reads: (0.3 / 1_000_000) * 3000 = 0.0009
// Total: 0.003 + 0.0075 + 0.0075 + 0.0009 = 0.0189
expect(result.totalCost).toBe(0.0189)
expect(result.totalInputTokens).toBe(6000) // 1000 + 2000 + 3000
expect(result.totalOutputTokens).toBe(500)
expect(result).toBe(0.0189)
})
it("should handle missing prices gracefully", () => {
@ -72,16 +64,12 @@ describe("Cost Utility", () => {
}
const result = calculateApiCostAnthropic(modelWithoutPrices, 1000, 500, 2000, 3000)
expect(result.totalCost).toBe(0)
expect(result.totalInputTokens).toBe(6000) // 1000 + 2000 + 3000
expect(result.totalOutputTokens).toBe(500)
expect(result).toBe(0)
})
it("should handle zero tokens", () => {
const result = calculateApiCostAnthropic(mockModelInfo, 0, 0, 0, 0)
expect(result.totalCost).toBe(0)
expect(result.totalInputTokens).toBe(0)
expect(result.totalOutputTokens).toBe(0)
expect(result).toBe(0)
})
it("should handle undefined cache values", () => {
@ -90,9 +78,7 @@ describe("Cost Utility", () => {
// Input cost: (3.0 / 1_000_000) * 1000 = 0.003
// Output cost: (15.0 / 1_000_000) * 500 = 0.0075
// Total: 0.003 + 0.0075 = 0.0105
expect(result.totalCost).toBe(0.0105)
expect(result.totalInputTokens).toBe(1000)
expect(result.totalOutputTokens).toBe(500)
expect(result).toBe(0.0105)
})
it("should handle missing cache prices", () => {
@ -108,9 +94,7 @@ describe("Cost Utility", () => {
// Input cost: (3.0 / 1_000_000) * 1000 = 0.003
// Output cost: (15.0 / 1_000_000) * 500 = 0.0075
// Total: 0.003 + 0.0075 = 0.0105
expect(result.totalCost).toBe(0.0105)
expect(result.totalInputTokens).toBe(6000) // 1000 + 2000 + 3000
expect(result.totalOutputTokens).toBe(500)
expect(result).toBe(0.0105)
})
})