Loading page...Загружаем страницу...正在加载页面...
AtlasRepo

Claude Haiku 5.5 Bills 5x for Prompts Over 100K Tokens. A Node Guard That Counts First

说明与用途

一个 TypeScript 保护器,在向 Claude Haiku 5.5 发送请求前计数令牌,裁剪低排名的块以保持在 97,000 令牌以下,记录计费层级,并在提示超过 100,000 令牌时使 CI 失败。

问题

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