In Spot Misuse in a Week of Traffic, a script got past course 3’s quota, which counts tokens per X-Hiker-Id per day. The script sent a fresh ID with every question, and its long pastes were charged only after their model calls.
Here, you’ll refuse those requests before the call, keep a record the sweep can’t delete, and measure the script’s cost. The first check, a question’s length, is also the cheapest.
Refuse Costly Requests Before the Model Call
charge() records that a paste used 2,400 tokens only after the call, so the new limits run before admit(). Save guards/limits.js:
guards/limits.js
export const MAX_QUESTION = 2_000; // UTF-16 code units
export const PER_ID_PER_MINUTE = 6;
export const NEW_IDS_PER_MINUTE = 5;
const perId = new Map(); // Hiker ID -> requests this minute
let minute = null;
let newIds = 0; // New IDs this minute: a count, no IDs
const peaks = new Map(); // Day -> highest newIds that day
export function limitReason(hiker, question, now, isNew) {
if (process.env.LIMITS === "off") return null;
if (now !== minute) {
minute = now;
perId.clear();
newIds = 0;
}
const n = perId.get(hiker) ?? 0;
perId.set(hiker, n + 1);
if (isNew && n === 0) {
newIds += 1;
const day = now.slice(0, 10);
peaks.set(day, Math.max(peaks.get(day) ?? 0, newIds));
}
if (question.length > MAX_QUESTION) return "too-long";
if (isNew && newIds > NEW_IDS_PER_MINUTE) return "new-ids";
if (n >= PER_ID_PER_MINUTE) return "per-id";
return null;
}
export const peakDays = () => [...peaks.keys()];
export function takePeak(day) {
const peak = peaks.get(day) ?? 0;
peaks.delete(day);
return peak;
}
Every paste passed the length check from Place Guardrails Around the Stream and the Cache, so MAX_QUESTION tightens it. .length counts UTF-16 code units, not tokens. The 2,000-unit limit comes to about 500 tokens of English and more in other languages.
newIds, the new-IDs-per-minute counter, counts each ID once per minute if the quota hasn’t admitted it today. peaks keeps the highest count for each day. The limit is shared, so during a burst it refuses honest newcomers too. Set the limit above your highest normal count: 2 in the summaries below, so use 5. With several servers, keep these counters in a shared store.
Save try-limits.js, which tries each limit:
try-limits.js
import { limitReason } from "./guards/limits.js";
const q = "Is the Valbrune ford passable today?";
const plan = "Our route, hut 2 to hut 6: ".padEnd(6_000, ".");
const at = (m) => `2026-07-16T08:0${m}`;
console.log(limitReason("h-ana", q, at(0), true));
console.log(limitReason("h-ana", plan, at(0), false));
const paste = [1, 2].map(() => limitReason("h-bo", plan, at(1), true));
const burst = Array.from({ length: 7 }, (_, i) =>
limitReason(`h-script-${i}`, q, at(1), true));
console.log([...paste, ...burst].map(String).join(" "));
const group = Array.from({ length: 7 }, () =>
limitReason("h-ana", q, at(2), false));
console.log(group.map(String).join(" "));
Run it: