LLM Spend TripwireChecklistCodeHosted waitlist

src/prices.js

Commit 01f96e7. All files · Download the tarball.

// List prices in USD per million tokens, copied from https://platform.claude.com/docs/en/about-claude/pricing
// on 2026-11-16 (standard tier). Columns: input, 5-minute cache write, 1-hour cache write, cache read, output.
// These are ESTIMATES: batch (50% off), fast mode, long-context and data-residency pricing are not applied,
// and Claude Haiku 5.5 uses its prompts-up-to-100k-tokens rate. Override with the PRICES_JSON var.
export const DEFAULT_PRICES = Object.freeze({
  "claude-fable-5-2": { input: 8, cache_write_5m: 10, cache_write_1h: 16, cache_read: 0.2, output: 40 },
  "claude-opus-5-5": { input: 4, cache_write_5m: 5, cache_write_1h: 8, cache_read: 0.2, output: 20 },
  "claude-sonnet-5-5": { input: 2, cache_write_5m: 2.5, cache_write_1h: 4, cache_read: 0.1, output: 10 },
  "claude-haiku-5-5": { input: 0.1, cache_write_5m: 0.125, cache_write_1h: 0.2, cache_read: 0.01, output: 0.5 },
  // Unknown models are priced at Claude Fable 5's rate ($10 input / $50 output per million tokens) and marked in the alert. That's higher than every current model but lower than legacy Claude Opus 4 and 4.1 ($15 / $75), so those are underestimated.
  "*": { input: 10, cache_write_5m: 12.5, cache_write_1h: 20, cache_read: 1, output: 50 },
});

const FIELDS = ["input", "cache_write_5m", "cache_write_1h", "cache_read", "output"];

// PRICES_JSON: {"model-id": {"input": 3, "output": 15, ...}} merged over the defaults. Invalid JSON is ignored.
export function priceTable(json) {
  const table = { ...DEFAULT_PRICES };
  if (!json) return table;
  let extra;
  try { extra = JSON.parse(json); } catch { return table; }
  if (!extra || typeof extra !== "object" || Array.isArray(extra)) return table;
  for (const [model, p] of Object.entries(extra)) {
    if (!p || typeof p !== "object") continue;
    const base = table[model] || table["*"];
    const row = {};
    for (const f of FIELDS) row[f] = Number.isFinite(Number(p[f])) && Number(p[f]) >= 0 ? Number(p[f]) : base[f];
    table[model] = row;
  }
  return table;
}

// Exact id, else the longest table key the model id starts with (dated snapshots), else "*".
export function priceFor(model, table) {
  if (model && Object.hasOwn(table, model)) return { price: table[model], known: true };
  let best = null;
  if (model) for (const k of Object.keys(table)) if (k !== "*" && model.startsWith(k) && (!best || k.length > best.length)) best = k;
  return best ? { price: table[best], known: true } : { price: table["*"], known: false };
}

export function estimateUsd(t, price) {
  return (
    (t.uncached_input || 0) * price.input +
    (t.cache_write_5m || 0) * price.cache_write_5m +
    (t.cache_write_1h || 0) * price.cache_write_1h +
    (t.cache_read || 0) * price.cache_read +
    (t.output || 0) * price.output
  ) / 1e6;
}