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
13 changes: 10 additions & 3 deletions src/lib/llm/__tests__/usage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@ import { estimateCostMicroCents } from '../usage';

describe('estimateCostMicroCents', () => {
it('estimates cost for Haiku model', () => {
// Haiku: $0.00025/1k input, $0.00125/1k output
// 1000 input + 1000 output = $0.00025 + $0.00125 = $0.0015 = 150 micro-cents
// Haiku 4.5: $1/MTok input, $5/MTok output → $0.001/1k in, $0.005/1k out
// 1000 input + 1000 output = $0.001 + $0.005 = $0.006 = 600 micro-cents
const cost = estimateCostMicroCents('claude-haiku-4-5-20251001', 1000, 1000);
expect(cost).toBe(150);
expect(cost).toBe(600);
});

it('estimates cost for Opus model', () => {
// Opus 4.8: $5/MTok input, $25/MTok output → $0.005/1k in, $0.025/1k out
// 1000 input + 1000 output = $0.005 + $0.025 = $0.03 = 3000 micro-cents
const cost = estimateCostMicroCents('claude-opus-4-8', 1000, 1000);
expect(cost).toBe(3000);
});

it('estimates cost for Sonnet model', () => {
Expand Down
10 changes: 7 additions & 3 deletions src/lib/llm/usage.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import type { LLMResponse } from './index';

// Cost in micro-cents per 1k tokens (1 micro-cent = $0.00001)
// Cost in micro-cents per 1k tokens (1 micro-cent = $0.00001).
// Per-1k micro-cents = (USD per 1M tokens) × 100. Rates per Anthropic pricing:
// Haiku 4.5 $1 / $5 Sonnet 4.6 $3 / $15 Opus 4.7/4.8 $5 / $25
const MODEL_COSTS: Record<string, { inputPer1k: number; outputPer1k: number }> = {
'claude-haiku-4-5-20251001': { inputPer1k: 25, outputPer1k: 125 },
'claude-haiku-4-5-20251001': { inputPer1k: 100, outputPer1k: 500 },
'claude-haiku-4-5': { inputPer1k: 100, outputPer1k: 500 },
'claude-sonnet-4-6': { inputPer1k: 300, outputPer1k: 1500 },
'claude-sonnet-4-20250514': { inputPer1k: 300, outputPer1k: 1500 },
'claude-opus-4-7': { inputPer1k: 1500, outputPer1k: 7500 },
'claude-opus-4-8': { inputPer1k: 500, outputPer1k: 2500 },
'claude-opus-4-7': { inputPer1k: 500, outputPer1k: 2500 },
};

export function estimateCostMicroCents(
Expand Down
Loading