LLM Spend TripwireChecklistCodeHosted waitlist

src/sample.js

Commit 01f96e7. All files · Download the tarball.

// Bundled sample data for replay mode (no Admin key needed). Responses are built in the shape documented at
// https://docs.anthropic.com/en/api/admin-api/usage-cost/get-messages-usage-report and
// https://docs.anthropic.com/en/api/admin-api/apikeys/list-api-keys . The organization, keys and numbers are made up.
import { runCheck } from "./index.js";
import { formatMessage, payloadFor, webhookKind } from "./notify.js";

export const SAMPLE_NOW = Date.parse("2026-11-16T15:30:00Z");
const SPIKE_FROM = Math.floor(Date.parse("2026-11-16T15:22:00Z") / 60000);

export const SAMPLE_KEYS = [
  { id: "apikey_01SampleProdBackend00001", name: "prod-backend", partial_key_hint: "sk-ant-api03-Ab1...xY9Z", created_at: "2026-09-02T14:11:05Z" },
  { id: "apikey_01SampleNightlyJobs00002", name: "nightly-jobs", partial_key_hint: "sk-ant-api03-Cd2...wX8Y", created_at: "2026-10-01T09:30:00Z" },
];
// Appears after the baseline was taken, as an attacker-created key would.
export const SAMPLE_NEW_KEY = { id: "apikey_01SampleUnknown000000003", name: "temp-test", partial_key_hint: "sk-ant-api03-Ef3...vW7X", created_at: "2026-11-16T15:18:40Z" };

// Per-minute usage for the sample org.
export function sampleUsage(minute) {
  const out = [{ key: SAMPLE_KEYS[0].id, model: "claude-sonnet-5-5", in: 9000, read: 30000, out: 2500 }];
  if (minute % 60 < 5) out.push({ key: SAMPLE_KEYS[1].id, model: "claude-haiku-5-5", in: 20000, out: 4000 });
  if (minute >= SPIKE_FROM) out.push({ key: SAMPLE_NEW_KEY.id, model: "claude-fable-5-2", in: 600000, out: 150000 });
  if (minute >= SPIKE_FROM + 2) out.push({ key: SAMPLE_KEYS[0].id, model: "claude-opus-5-5", in: 350000, out: 90000 });
  return out;
}

const iso = (m) => new Date(m * 60000).toISOString().replace(".000Z", "Z");

function result(r) {
  return {
    account_id: null, api_key_id: r.key, cache_creation: { ephemeral_1h_input_tokens: 0, ephemeral_5m_input_tokens: 0 },
    cache_read_input_tokens: r.read || 0, context_window: null, inference_geo: null, model: r.model, output_tokens: r.out || 0,
    server_tool_use: { web_search_requests: 0 }, service_account_id: null, service_tier: null, speed: null,
    uncached_input_tokens: r.in || 0, workspace_id: null,
  };
}

export function sampleUsageResponse(url) {
  const u = new URL(url);
  const start = Math.floor(Date.parse(u.searchParams.get("starting_at")) / 60000);
  const end = Math.floor(Date.parse(u.searchParams.get("ending_at")) / 60000);
  const width = u.searchParams.get("bucket_width") === "1h" ? 60 : 1;
  const data = [];
  for (let m = start; m < end; m += width) {
    const agg = new Map();
    for (let x = m; x < Math.min(m + width, end); x++) {
      for (const r of sampleUsage(x)) {
        const k = `${r.key}|${r.model}`;
        const a = agg.get(k) || { key: r.key, model: r.model, in: 0, read: 0, out: 0 };
        a.in += r.in || 0; a.read += r.read || 0; a.out += r.out || 0;
        agg.set(k, a);
      }
    }
    data.push({ starting_at: iso(m), ending_at: iso(m + width), results: [...agg.values()].map(result) });
  }
  return { data, has_more: false, next_page: null };
}

export function sampleKeysResponse(includeNew) {
  const keys = includeNew ? [...SAMPLE_KEYS, SAMPLE_NEW_KEY] : SAMPLE_KEYS;
  return {
    data: keys.map((k) => ({ type: "api_key", ...k, created_by: { id: "user_01SampleUser", type: "user" }, expires_at: null, principal: null, scope: { type: "workspace", workspace_id: "wrkspc_01Sample" }, status: "active" })),
    first_id: keys[0].id, has_more: false, last_id: keys.at(-1).id,
  };
}

function memoryDb() {
  const db = { row: null };
  db.prepare = (sql) => {
    let args = [];
    const st = {
      bind: (...a) => { args = a; return st; },
      run: async () => { if (sql.startsWith("INSERT INTO tripwire_state")) db.row = { v: args[0] }; return {}; },
      first: async () => (sql.startsWith("SELECT v FROM tripwire_state") ? db.row : null),
    };
    return st;
  };
  return db;
}

/**
 * Replay the sample through the real check: a seeding run 40 minutes before SAMPLE_NOW, then the check at SAMPLE_NOW.
 * Uses an in-memory database (never your D1) and never calls api.anthropic.com.
 * post=false: returns the webhook payloads that would be sent. post=true: also posts them to webhookUrl.
 */
export async function replaySample({ webhookUrl = "https://hooks.slack.com/services/T000/B000/sample", post = false, fetchImpl = fetch, vars = {} } = {}) {
  const kind = webhookKind(webhookUrl);
  if (!kind) throw new Error("webhook URL must be a Slack or Discord incoming webhook URL");
  const sent = [];
  let phase = "seed";
  const fake = async (url, init = {}) => {
    const s = String(url);
    const reply = (body) => new Response(JSON.stringify(body), { status: 200, headers: { "content-type": "application/json" } });
    if (s.startsWith("https://api.anthropic.com/v1/organizations/usage_report/messages")) return reply(sampleUsageResponse(s));
    if (s.startsWith("https://api.anthropic.com/v1/organizations/api_keys")) return reply(sampleKeysResponse(phase === "check"));
    if (s === webhookUrl) {
      const payload = JSON.parse(init.body);
      sent.push({ phase, payload });
      return post ? fetchImpl(url, init) : new Response("ok", { status: 200 });
    }
    throw new Error("replay blocked an unexpected request");
  };
  const env = { ...vars, ANTHROPIC_ADMIN_KEY: "sk-ant-admin01-sample-not-a-real-key", ALERT_WEBHOOK_URL: webhookUrl, DB: memoryDb() };
  const seed = await runCheck(env, { now: SAMPLE_NOW - 40 * 60000, fetchImpl: fake });
  phase = "check";
  const check = await runCheck(env, { now: SAMPLE_NOW, fetchImpl: fake });
  return { kind, posted: post, runs: { seed, check }, payloads: sent };
}

export { formatMessage, payloadFor };