Awesome Reviewers

When implementing algorithmic utilities (parsers, normalizers, token/complexity estimators), specify and enforce invariants, and test the edge cases that break naive implementations.

Practical rules:

Example (estimator invariant: payload size shouldn’t move the estimate):

export const ESTIMATED_TOKENS_PER_IMAGE = 1_600;

export function estimateRequestInputTokens({ messages }: { messages: unknown[] }) {
  const base = estimateTokens(JSON.stringify({ messages } ).length);
  const imageCount = countImagesInMessages(messages); // implementation scans for {type:"image"}
  return base + imageCount * ESTIMATED_TOKENS_PER_IMAGE;
}
// Test invariant: increasing base64 length must not change the per-image delta.

If you can’t clearly state the invariant, you probably don’t have a robust algorithm yet—write one (what must stay unchanged, what must change) before coding, then lock it down with the edge-case tests.