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

Don’t Let a Free LLM Tier Vanish: A Self‑Healing Token Budget in 80 Lines

说明与用途

一段80行Python代码跟踪免费LLM的令牌使用,并在预算耗尽前自动切换到备用提供商,避免429错误。

问题

Free LLM tiers run out unexpectedly because code does not track remaining token budget, leading to 429 errors after batch jobs burn through the allowance.

结果

The 80‑line solution prevents sudden 429 errors by monitoring token usage and automatically failing over to a backup provider before the free tier is exhausted.

工作流

  • 1. Define a BudgetMonitor class that reads/writes token usage to a CSV file.
  • 2. Implement a record method to log each prompt and tokens used.
  • 3. Add an under_budget check with a safety margin (default 1000 tokens).
  • 4. Create a ProviderRouter that holds provider functions and an active flag.
  • 5. In generate(), check monitor.under_budget() before calling the free provider; if false, switch active to backup.
  • 6. After each call, record usage via monitor.record().
  • 7. Deploy the service on a free server; schedule a cron job to reset or archive usage if needed.

证据

The router keeps a list of provider functions. Each function has the same signature: (prompt, max_tokens) -> (text, tokens_used).

来源

The safety margin prevents edge‑of‑the‑cliff failures. When you have less than 1,000 tokens left, the router should stop sending new work to the free provider.

来源

  • 记录日期: 2026-09-05T19:15:10.026Z