LLM Spend TripwireChecklistCodeHosted waitlist

test/world.mjs

Commit 01f96e7. All files · Download the tarball.

// A simulated organization: world(minute) returns that minute's usage results. The fake usage endpoint
// slices it into 1m or 1h buckets exactly like the documented response (empty buckets included).
import { usageResponse } from "./helpers.mjs";

export function usageFrom(world, { pageSize = Infinity } = {}) {
  return (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 buckets = [];
    for (let m = start; m + width <= end || (width === 1 && m < end); m += width) {
      const agg = new Map();
      for (let x = m; x < m + width; x++) {
        for (const r of world(x)) {
          const k = `${r.key}|${r.model || "claude-sonnet-5-5"}`;
          const a = agg.get(k) || { key: r.key, model: r.model || "claude-sonnet-5-5", in: 0, out: 0 };
          a.in += r.in || 0; a.out += r.out || 0;
          agg.set(k, a);
        }
      }
      buckets.push({ start: m, widthMin: width, results: [...agg.values()] });
    }
    const pageIdx = Number(u.searchParams.get("page") || 0);
    const slice = buckets.slice(pageIdx * pageSize, (pageIdx + 1) * pageSize);
    const has_more = (pageIdx + 1) * pageSize < buckets.length;
    return usageResponse(slice, { has_more, next_page: has_more ? String(pageIdx + 1) : null });
  };
}

// Steady use on one key: 800 input + 200 output tokens every minute on Sonnet 5.5.
export const steady = (key = "apikey_A") => () => [{ key, in: 800, out: 200 }];