Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | 31x 527x 31x 52x 52x 52x 25x 25x 9x 15x 15x 15x 13x 2x 2x 16x 16x 10x 10x 16x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 14x 14x 15x | import type { UsageStats } from '../types/index.js';
import rawPricing from './model-pricing.json' with { type: 'json' };
/**
* Per-token pricing for Anthropic API models (USD per token).
* The direct API doesn't return total_cost_usd like the Claude Code SDK,
* so we calculate it from token counts.
*/
interface ModelPricing {
inputPerToken: number;
outputPerToken: number;
cacheReadPerToken: number;
cacheCreationPerToken: number;
cacheCreation1hPerToken: number;
webSearchPerRequest: number;
}
export interface UsageCostBreakdown {
freshInputUSD: number;
outputUSD: number;
cacheReadUSD: number;
cacheCreationUSD: number;
cacheCreation5mUSD: number;
cacheCreation1hUSD: number;
webSearchUSD: number;
totalUSD: number;
}
/**
* Pricing built from model-pricing.json (generated by scripts/update-pricing.ts).
* JSON values are USD per million tokens; we convert to per-token here.
*/
const MODEL_PRICING: Record<string, ModelPricing> = Object.fromEntries(
Object.entries(rawPricing).map(([id, p]) => [
id,
{
inputPerToken: p.inputPerMTok / 1_000_000,
outputPerToken: p.outputPerMTok / 1_000_000,
cacheReadPerToken: p.cacheReadPerMTok / 1_000_000,
cacheCreationPerToken: p.cacheWritePerMTok / 1_000_000,
cacheCreation1hPerToken: p.cacheWrite1hPerMTok / 1_000_000,
webSearchPerRequest: p.webSearchPer1K / 1_000,
},
]),
);
const MODEL_ALIASES: Record<string, string> = {
'claude-sonnet-4': 'claude-sonnet-4-0',
'claude-4-sonnet': 'claude-sonnet-4-0',
'claude-sonnet-4-6': 'claude-sonnet-4-5',
'claude-sonnet-4-7': 'claude-sonnet-4-6',
'claude-opus-4': 'claude-opus-4-0',
'claude-4-opus': 'claude-opus-4-0',
'claude-opus-4-6': 'claude-opus-4-5',
};
function hasModelPricing(pricing: ModelPricing | undefined): pricing is ModelPricing {
return pricing !== undefined && (
pricing.inputPerToken > 0 ||
pricing.outputPerToken > 0 ||
pricing.cacheReadPerToken > 0 ||
pricing.cacheCreationPerToken > 0 ||
pricing.cacheCreation1hPerToken > 0
);
}
function lookupModelPricing(model: string): ModelPricing | undefined {
const pricing = MODEL_PRICING[model];
return hasModelPricing(pricing) ? pricing : undefined;
}
function normalizeModelId(model: string): string {
const undated = model.replace(/-\d{8}$/, '');
if (lookupModelPricing(undated)) return undated;
return MODEL_ALIASES[undated] ?? undated;
}
function applyLongContextPricing(model: string, pricing: ModelPricing, inputTokens: number): ModelPricing {
const canonical = normalizeModelId(model);
const sonnetLongContext =
inputTokens > 200_000 &&
(canonical === 'claude-sonnet-4-0' || canonical === 'claude-sonnet-4-5');
if (!sonnetLongContext) {
return pricing;
}
const inputPerToken = pricing.inputPerToken * 2;
return {
...pricing,
inputPerToken,
outputPerToken: pricing.outputPerToken * 1.5,
cacheReadPerToken: inputPerToken * 0.1,
cacheCreationPerToken: inputPerToken * 1.25,
cacheCreation1hPerToken: inputPerToken * 2,
};
}
function getModelPricing(model: string, inputTokens = 0): ModelPricing | undefined {
const exact = lookupModelPricing(model);
if (exact) return applyLongContextPricing(model, exact, inputTokens);
const canonical = normalizeModelId(model);
const pricing = lookupModelPricing(canonical) ?? lookupModelPricing(`${canonical}-latest`);
return pricing ? applyLongContextPricing(model, pricing, inputTokens) : undefined;
}
export function estimateUsageCostBreakdown(
model: string | undefined,
usage: UsageStats,
): UsageCostBreakdown | undefined {
Iif (!model) return undefined;
const cacheReadInputTokens = usage.cacheReadInputTokens ?? 0;
const cacheCreation5mInputTokens = usage.cacheCreation5mInputTokens ?? 0;
const cacheCreation1hInputTokens = usage.cacheCreation1hInputTokens ?? 0;
const cacheCreationInputTokens = Math.max(
usage.cacheCreationInputTokens ?? 0,
cacheCreation5mInputTokens + cacheCreation1hInputTokens,
);
const cacheCreationInputTokensByTier = cacheCreation5mInputTokens + cacheCreation1hInputTokens;
const uncategorizedCacheCreationInputTokens = Math.max(
0,
cacheCreationInputTokens - cacheCreationInputTokensByTier,
);
const freshInputTokens = Math.max(
0,
usage.inputTokens - cacheReadInputTokens - cacheCreationInputTokens,
);
const webSearchRequests = usage.webSearchRequests ?? 0;
const pricing = getModelPricing(model, usage.inputTokens);
Iif (!pricing) return undefined;
const freshInputUSD = freshInputTokens * pricing.inputPerToken;
const outputUSD = usage.outputTokens * pricing.outputPerToken;
const cacheReadUSD = cacheReadInputTokens * pricing.cacheReadPerToken;
const cacheCreationUSD = uncategorizedCacheCreationInputTokens * pricing.cacheCreationPerToken;
const cacheCreation5mUSD = cacheCreation5mInputTokens * pricing.cacheCreationPerToken;
const cacheCreation1hUSD = cacheCreation1hInputTokens * pricing.cacheCreation1hPerToken;
const webSearchUSD = webSearchRequests * pricing.webSearchPerRequest;
return {
freshInputUSD,
outputUSD,
cacheReadUSD,
cacheCreationUSD,
cacheCreation5mUSD,
cacheCreation1hUSD,
webSearchUSD,
totalUSD:
freshInputUSD +
outputUSD +
cacheReadUSD +
cacheCreationUSD +
cacheCreation5mUSD +
cacheCreation1hUSD +
webSearchUSD,
};
}
/**
* Usage shape returned by the Anthropic Messages API.
*/
interface ApiUsage {
input_tokens: number;
output_tokens: number;
cache_read_input_tokens?: number | null;
cache_creation_input_tokens?: number | null;
cache_creation?: {
ephemeral_1h_input_tokens?: number | null;
ephemeral_5m_input_tokens?: number | null;
} | null;
server_tool_use?: {
web_search_requests?: number | null;
} | null;
}
/**
* Convert Anthropic API usage to our UsageStats format.
* Calculates cost from token counts using model pricing.
*
* The Anthropic API reports `input_tokens` as only the non-cached portion.
* We normalize so that `inputTokens` is the *total* input tokens
* (non-cached + cache_read + cache_creation), with the cache fields
* being subsets of that total.
*/
export function apiUsageToStats(model: string, usage: ApiUsage): UsageStats {
const outputTokens = usage.output_tokens;
const cacheReadInputTokens = usage.cache_read_input_tokens ?? 0;
const rawCacheCreationInputTokens = usage.cache_creation_input_tokens ?? 0;
const tieredCacheCreation5mInputTokens = usage.cache_creation?.ephemeral_5m_input_tokens ?? 0;
const cacheCreation1hInputTokens = usage.cache_creation?.ephemeral_1h_input_tokens ?? 0;
const hasTieredCacheCreation = usage.cache_creation !== undefined && usage.cache_creation !== null;
const tieredCacheCreationInputTokens = tieredCacheCreation5mInputTokens + cacheCreation1hInputTokens;
const cacheCreationInputTokens = Math.max(rawCacheCreationInputTokens, tieredCacheCreationInputTokens);
const cacheCreation5mInputTokens = hasTieredCacheCreation
? tieredCacheCreation5mInputTokens
: rawCacheCreationInputTokens;
const uncategorizedCacheCreationInputTokens = Math.max(
0,
cacheCreationInputTokens - cacheCreation5mInputTokens - cacheCreation1hInputTokens,
);
const webSearchRequests = usage.server_tool_use?.web_search_requests ?? 0;
// inputTokens is the total: raw API input_tokens + cache subsets.
const inputTokens = usage.input_tokens + cacheReadInputTokens + cacheCreationInputTokens;
const pricing = getModelPricing(model, inputTokens);
// Cost: deduct cache subsets from total to get the non-cached portion,
// then charge each category at its respective rate.
let costUSD = 0;
if (pricing) {
const freshInputTokens = inputTokens - cacheReadInputTokens - cacheCreationInputTokens;
costUSD =
freshInputTokens * pricing.inputPerToken +
outputTokens * pricing.outputPerToken +
cacheReadInputTokens * pricing.cacheReadPerToken +
(cacheCreation5mInputTokens + uncategorizedCacheCreationInputTokens) * pricing.cacheCreationPerToken +
cacheCreation1hInputTokens * pricing.cacheCreation1hPerToken +
webSearchRequests * pricing.webSearchPerRequest;
}
return {
inputTokens,
outputTokens,
cacheReadInputTokens,
cacheCreationInputTokens,
cacheCreation5mInputTokens: cacheCreation5mInputTokens + uncategorizedCacheCreationInputTokens,
cacheCreation1hInputTokens,
webSearchRequests,
costUSD,
};
}
|