LLM Spend TripwireChecklistCodeHosted waitlist

src/index.js

Commit 01f96e7. All files · Download the tarball.

// LLM Spend Tripwire: a self-hosted Cloudflare Worker that watches your Anthropic organization's usage
// and posts to your Slack or Discord webhook. It sends data only to api.anthropic.com (to read usage)
// and to your webhook URL. MIT licensed. Swift Labs. Not affiliated with Anthropic.
import { parseConfig } from "./config.js";
import { priceTable } from "./prices.js";
import { fetchUsage, fetchActiveKeys } from "./anthropic.js";
import { step, errorAlert } from "./detect.js";
import { formatMessage, sendWebhook, webhookKind } from "./notify.js";
import { loadState, saveState } from "./state.js";
import { replaySample } from "./sample.js";

const json = (body, status = 200) => new Response(JSON.stringify(body, null, 2), { status, headers: { "content-type": "application/json", "cache-control": "no-store" } });

export async function runCheck(env, { now = Date.now(), dryRun = false, fetchImpl = fetch } = {}) {
  const cfg = parseConfig(env);
  if (!env.ANTHROPIC_ADMIN_KEY) return { ok: false, error: "ANTHROPIC_ADMIN_KEY secret is not set" };
  if (!webhookKind(env.ALERT_WEBHOOK_URL)) return { ok: false, error: "ALERT_WEBHOOK_URL secret is missing or is not a Slack/Discord incoming webhook URL" };
  if (!env.DB) return { ok: false, error: "D1 binding DB is missing" };

  const nowMin = Math.floor(now / 60000);
  const api = { fetchImpl, key: env.ANTHROPIC_ADMIN_KEY, version: cfg.anthropicVersion };
  const prev = await loadState(env.DB);
  const initialized = !!(prev && prev.initialized);
  const input = { prices: priceTable(cfg.pricesJson) };

  try {
    if (!initialized) {
      // First run: seed the baseline from hourly buckets, then read minute buckets from there on.
      input.backfillEnd = Math.floor((nowMin - cfg.settleMinutes) / 60) * 60;
      input.backfillStart = input.backfillEnd - Math.ceil(cfg.baselineMinutes / 60) * 60;
      input.backfillRows = await fetchUsage({ ...api, startMin: input.backfillStart, endMin: input.backfillEnd, width: "1h" });
      input.recentStart = Math.min(input.backfillEnd, nowMin - cfg.lookbackMinutes);
    } else {
      input.recentStart = Math.max(nowMin - 1440, Math.min(prev.storedThrough + 1, nowMin - cfg.lookbackMinutes));
    }
    input.recentRows = await fetchUsage({ ...api, startMin: input.recentStart, endMin: nowMin + 1, width: "1m" });
    if (cfg.watchKeyList) input.keyList = await fetchActiveKeys(api);
  } catch (err) {
    console.error("tripwire: usage read failed:", err && err.message);
    const { state, alert } = errorAlert(prev, err, nowMin);
    if (dryRun) return { ok: false, error: String(err && err.message), dryRun: true };
    if (alert) {
      try { await sendWebhook(fetchImpl, env.ALERT_WEBHOOK_URL, formatMessage([alert], null, 1)); } catch (e) { console.error("tripwire: webhook failed:", e.message); return { ok: false, error: String(err.message) }; }
      await saveState(env.DB, state, now);
    }
    return { ok: false, error: String(err && err.message) };
  }

  const { state, alerts } = step(prev, input, cfg, nowMin);
  const names = new Map((input.keyList || []).map((k) => [k.id, k]));
  const message = alerts.length ? formatMessage(alerts, names, cfg.maxLinesPerMessage) : null;
  const summary = { ok: true, alerts: alerts.length, rows: input.recentRows.length, firstRun: !initialized };
  if (dryRun) return { ...summary, dryRun: true, message, alertsDetail: alerts };

  if (message) {
    try { await sendWebhook(fetchImpl, env.ALERT_WEBHOOK_URL, message); } catch (e) {
      // Don't save: the next run re-detects the same alerts and retries delivery.
      console.error("tripwire: webhook failed:", e.message);
      return { ok: false, error: `webhook: ${e.message}` };
    }
  }
  await saveState(env.DB, state, now);
  return summary;
}

// Constant-time comparison of SHA-256 digests, so the secret's length and content don't leak through timing.
async function secretMatches(given, expected) {
  if (!given || !expected) return false;
  const enc = new TextEncoder();
  const [a, b] = await Promise.all([crypto.subtle.digest("SHA-256", enc.encode(given)), crypto.subtle.digest("SHA-256", enc.encode(expected))]);
  const x = new Uint8Array(a), y = new Uint8Array(b);
  let diff = 0;
  for (let i = 0; i < x.length; i++) diff |= x[i] ^ y[i];
  return diff === 0;
}

export async function handleFetch(request, env, { fetchImpl = fetch, now = Date.now() } = {}) {
  const url = new URL(request.url);
  if (url.pathname === "/" && (request.method === "GET" || request.method === "HEAD")) {
    return new Response("LLM Spend Tripwire is running.\n", { headers: { "content-type": "text/plain", "x-robots-tag": "noindex" } });
  }
  if (url.pathname !== "/test-alert" && url.pathname !== "/dry-run") return new Response("Not found\n", { status: 404 });
  if (!env.TEST_SECRET) return new Response("Not found\n", { status: 404 });
  if (request.method !== "POST") return new Response("Use POST\n", { status: 405, headers: { allow: "POST" } });
  const auth = request.headers.get("authorization") || "";
  const given = auth.startsWith("Bearer ") ? auth.slice(7) : "";
  if (!(await secretMatches(given, env.TEST_SECRET))) return json({ ok: false, error: "unauthorized" }, 401);

  if (url.pathname === "/test-alert") {
    if (!webhookKind(env.ALERT_WEBHOOK_URL)) return json({ ok: false, error: "ALERT_WEBHOOK_URL is missing or not a Slack/Discord incoming webhook URL" }, 400);
    try {
      const kind = await sendWebhook(fetchImpl, env.ALERT_WEBHOOK_URL, formatMessage([{ type: "test" }], null, 1));
      return json({ ok: true, sent_to: kind });
    } catch (e) { return json({ ok: false, error: e.message }, 502); }
  }
  if (url.searchParams.get("sample") === "1") {
    // Replay the bundled sample (no Admin API call). Posts to ALERT_WEBHOOK_URL only with &post=1.
    const post = url.searchParams.get("post") === "1";
    if (post && !webhookKind(env.ALERT_WEBHOOK_URL)) return json({ ok: false, error: "ALERT_WEBHOOK_URL is missing or not a Slack/Discord incoming webhook URL" }, 400);
    const vars = Object.fromEntries(Object.entries(env).filter(([k, v]) => typeof v === "string" && !["ANTHROPIC_ADMIN_KEY", "ALERT_WEBHOOK_URL", "TEST_SECRET"].includes(k)));
    try {
      const r = await replaySample({ webhookUrl: post ? env.ALERT_WEBHOOK_URL : undefined, post, fetchImpl, vars });
      return json({ ok: r.runs.check.ok, sample: true, posted: post, payloads: r.payloads });
    } catch (e) { return json({ ok: false, error: e.message }, 502); }
  }
  const result = await runCheck(env, { now, dryRun: true, fetchImpl });
  return json(result, result.ok ? 200 : 502);
}

export default {
  async fetch(request, env) { return handleFetch(request, env); },
  async scheduled(event, env, ctx) {
    const r = await runCheck(env, { now: event.scheduledTime || Date.now() });
    if (!r.ok) console.error("tripwire:", r.error);
  },
};