Claude Haiku 5.5 Bills 5x for Prompts Over 100K Tokens. A Node Guard That Counts First
Описание и применение
Гарант на TypeScript, который подсчитывает токены перед запросом к Claude Haiku 5.5, обрезает низкоранговые фрагменты, чтобы оставаться в пределах 97 000 токенов, предотвращая неожиданные расходы.
Задача
A prompt of 101,000 tokens costs about five times as much as a 99,000‑token prompt on Claude Haiku 5.5, causing unexpected billing spikes when traffic drifts over the 100,000‑token threshold.
Результат
The guard prevents accidental billing spikes by ensuring prompts stay within the lower pricing tier, reducing potential daily costs from $1,040 to $5,300 for 100,000 requests.
Рабочий процесс
- 1. Build request parameters with buildParams(q, chunks).
- 2. Use apiCounter(client) to estimate input_tokens via client.messages.countTokens.
- 3. If token count > TIER_LINE (100,000), perform binary search to trim lowest‑ranked chunks until count ≤ BUDGET (97,000).
- 4. Log the actual billed tier and token count.
- 5. Send the trimmed request to Claude Haiku 5.5.
- 6. Run Vitest tests that fail CI if any worst‑case prompt crosses the threshold.
Доказательства
A prompt of 99,000 tokens and a prompt of 101,000 tokens look the same in your code. On Claude Haiku 5.5 the second one costs about five times as much.
The fix is a small TypeScript guard that counts tokens before every request and drops the lowest-ranked retrieved chunks until the prompt fits.
- Дата записи: 2026-10-18T09:56:04.237Z