LLM Spend TripwireChecklistCodeHosted waitlist

src/notify.js

Commit 01f96e7. All files · Download the tarball.

// Alert delivery. Only Slack and Discord incoming-webhook hosts are accepted, so usage data can't be sent anywhere else.
const SLACK_HOSTS = new Set(["hooks.slack.com"]);
const DISCORD_HOSTS = new Set(["discord.com", "discordapp.com", "ptb.discord.com", "canary.discord.com"]);

export function webhookKind(raw) {
  let u;
  try { u = new URL(String(raw || "")); } catch { return null; }
  if (u.protocol !== "https:" || u.username || u.password) return null;
  if (SLACK_HOSTS.has(u.hostname) && u.pathname.startsWith("/services/")) return "slack";
  if (DISCORD_HOSTS.has(u.hostname) && u.pathname.startsWith("/api/webhooks/")) return "discord";
  return null;
}

const nf = new Intl.NumberFormat("en-US");
const n = (x) => nf.format(Math.round(x));
const usd = (x) => (x < 0.01 ? "<$0.01" : `$${x.toFixed(2)}`);
const utc = (minute) => new Date(minute * 60000).toISOString().slice(0, 16).replace("T", " ") + " UTC";

// Slack treats &, < and > as control characters; Discord mentions are disabled in the payload instead.
const slackEscape = (s) => String(s).replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");

function keyLabel(id, names) {
  if (id === "none") return "usage with no API key (Console Workbench or Anthropic-managed sessions)";
  const k = names && names.get(id);
  return k && k.name ? `key "${k.name}"${k.hint ? ` (${k.hint})` : ""} [${id}]` : `key ${id}`;
}

export function alertLine(a, names) {
  switch (a.type) {
    case "spike": {
      const ratio = a.ratio ? `${a.ratio >= 100 ? n(a.ratio) : a.ratio.toFixed(1)}x its average of ${n(a.baselineAvg)}/min over the last ${Math.round(a.baselineMinutes / 60)}h` : `no usage in the baseline window`;
      return `Spike: ${keyLabel(a.key, names)} used ${n(a.tokens)} tokens in the minute starting ${utc(a.minute)} (${ratio}). Estimated cost of that minute: about ${usd(a.usd)}${a.unknownPrice ? " (model not in the price table, priced at the fallback rate)" : ""}.`;
    }
    case "new_key_created":
      return `New API key: ${keyLabel(a.key, new Map([[a.key, { name: a.name, hint: a.hint }]]))} is active${a.created_at ? `, created ${a.created_at}` : ""}${a.created_by ? ` by ${a.created_by}` : ""}.`;
    case "new_key_usage":
      return `New key in use: ${keyLabel(a.key, names)} (not seen before) used ${n(a.windowTokens)} tokens recently, about ${usd(a.windowUsd)}, on ${a.models.join(", ")}.`;
    case "new_model":
      return `New model: ${a.model} was used (not seen before) by ${a.keys.map((k) => keyLabel(k, names)).join("; ")}.`;
    case "off_hours":
      return `Off-hours use: ${keyLabel(a.key, names)} used ${n(a.tokens)} tokens outside your working hours, starting ${a.local.weekday} ${a.local.hhmm} local time (${utc(a.minute)}). Estimated about ${usd(a.usd)}.`;
    case "started":
      return `LLM Spend Tripwire is now watching your Anthropic organization. It saw ${a.keys} API key(s) and ${a.models} model(s) in the last ${Math.round(a.baselineMinutes / 60)}h; those won't trigger "new key" or "new model" alerts.`;
    case "error":
      return `Tripwire could not read your usage: ${a.message}. If this is HTTP 401 or 403, check that ANTHROPIC_ADMIN_KEY is a valid Admin API key for a Claude Console organization; other errors are usually temporary. (This message repeats at most every 6 hours.)`;
    case "test":
      return "Test alert from LLM Spend Tripwire. If you can see this, alerts will reach this channel.";
    default:
      return `Alert: ${a.type}`;
  }
}

export const FOOTER = "Costs are estimates (tokens x list price), not your bill. Anthropic's cost data is daily; usage data usually lands within about 5 minutes. Check the Claude Console before acting.";

export function formatMessage(alerts, names, maxLines = 10) {
  const shown = alerts.slice(0, maxLines).map((a) => `- ${alertLine(a, names)}`);
  const more = alerts.length - shown.length;
  const head = alerts.length === 1 && ["started", "test", "error"].includes(alerts[0].type) ? "LLM Spend Tripwire" : `LLM Spend Tripwire: ${alerts.length} alert${alerts.length === 1 ? "" : "s"} for your Anthropic organization`;
  return [head, ...shown, ...(more > 0 ? [`- ...and ${more} more.`] : []), "", FOOTER].join("\n");
}

export function payloadFor(kind, text) {
  if (kind === "discord") {
    const content = text.length > 1990 ? text.slice(0, 1985) + "\n..." : text;
    return { content, allowed_mentions: { parse: [] } };
  }
  const t = slackEscape(text);
  return { text: t.length > 3900 ? t.slice(0, 3895) + "\n..." : t };
}

export async function sendWebhook(fetchImpl, url, text) {
  const kind = webhookKind(url);
  if (!kind) throw new Error("ALERT_WEBHOOK_URL must be a Slack (https://hooks.slack.com/services/...) or Discord (https://discord.com/api/webhooks/...) incoming webhook URL");
  const res = await fetchImpl(url, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(payloadFor(kind, text)), redirect: "manual" });
  if (!res.ok) throw new Error(`webhook returned HTTP ${res.status}`);
  return kind;
}