cost-calculation-long-context.test.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { describe, expect, test } from "vitest";
  2. import { calculateRequestCost } from "@/lib/utils/cost-calculation";
  3. describe("calculateRequestCost long-context", () => {
  4. test("uses long-context output pricing when total input context exceeds threshold", () => {
  5. const cost = calculateRequestCost(
  6. {
  7. input_tokens: 250000,
  8. output_tokens: 100000,
  9. },
  10. {
  11. mode: "chat",
  12. model_family: "claude-sonnet",
  13. input_cost_per_token: 0.000003,
  14. input_cost_per_token_above_200k_tokens: 0.000006,
  15. output_cost_per_token: 0.000015,
  16. output_cost_per_token_above_200k_tokens: 0.0000225,
  17. },
  18. 1,
  19. false
  20. );
  21. expect(Number(cost.toString())).toBe(3.75);
  22. });
  23. test("does not charge 1h cache long-context price when base cache creation price is missing", () => {
  24. const cost = calculateRequestCost(
  25. {
  26. input_tokens: 250000,
  27. cache_creation_1h_input_tokens: 1000,
  28. },
  29. {
  30. mode: "chat",
  31. model_family: "gpt",
  32. input_cost_per_token: 0.0000025,
  33. output_cost_per_token: 0.000015,
  34. cache_creation_input_token_cost_above_1hr_above_272k_tokens: 0.5,
  35. },
  36. 1,
  37. false,
  38. false
  39. );
  40. expect(Number(cost.toString())).toBe(0.63);
  41. });
  42. });