跳到正文
Skillpedia
自动化48k · #5651

runaway-guard

它能做什么

这份指南可以帮助你避免使用付费 AI 服务时产生意外费用。在运行任何调用这些 API 的代码之前,它会确保你先设置好花费上限。

适用场景

  • ✓当你使用按用量收费的 AI API 时。
  • ✓当你编写或审查会频繁调用这些 API 的代码时。
  • ✓当你想避免月底收到意外账单时。

技能链接

https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/runaway-guard/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/runaway-guard/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“我怎样才能确保使用 OpenAI API 时不会花太多钱?”

每周 5 个新技能,1 分钟读完。

工作原理

它的做法是为每次调用、每天以及每次执行设置明确的花费上限。这些限制既要写在代码中,也要在 AI 服务提供商的平台上设置。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

runaway-guard — $-Cost is the Third Complexity Dimension

Every loop has time complexity and space complexity. A loop that calls a paid API has a third: dollars per execution. The model tracks the first two automatically. It does not track the third, so it ships code where a single bug — a retry without bound, a stream reconnect storm, an agent that re-queues itself, a webhook that fires the same job twice — silently spends real money.

The canonical incident: developer writes a Fal.ai image-generation loop. Loop "obviously terminates" because it iterates over a fixed list. The list comes from a callback that fires on every Inngest retry. Each retry doubles the list. By morning, the bill is $200. Tests pass. Code review passed. The bug is not in the loop body. The bug is that no one stated the wallet invariant.

runaway-guard fixes this. State the max calls. State the max dollars per run. State the max dollars per day. Set the same caps in the provider dashboard so a code bug cannot bypass them. Then write the code.

Violating the letter of these rules is violating the spirit of the skill. "I'm only testing locally" is the exact rationalization that ships the $200 bill — local code hits the same paid API as production.

When to Use This Skill

Use runaway-guard when:

  • Writing or reviewing code that calls a paid AI / inference API in a loop, queue, retry path, agent step, webhook handler, or background job.
  • Importing or wrapping any paid-inference SDK: @fal-ai/*, fal-client, @anthropic-ai/sdk, anthropic, openai, replicate, elevenlabs, together-ai, groq-sdk, cohere-ai, @mistralai/*.
  • Designing an agent loop, fan-out pipeline, retry wrapper, polling job, stream reconnect, or self-rescheduling job that may call a billed endpoint.
  • Auditing a codebase / PR for unbounded fan-out, unbounded retries, missing idempotency keys, or missing provider-side spend caps.
  • Diagnosing an unexpected bill, runaway loop incident, or surprise overage.

The Iron Law

NO CALL TO A PAID API WITHOUT A WRITTEN $-CAP AT BOTH THE CODE AND PROVIDER LEVEL

A cap only in code can be bypassed by a bug in that code. A cap only at the provider can be hit during normal usage and degrade the product. You need both. If you cannot state both in one sentence each, you have not designed the call site — you have written a wish.

Non-negotiable rules

  1. Every call site gets a one-line cost contract. Before writing any paid-API call, state in one sentence:

    • Max calls per run: the strict upper bound on invocations in a single execution of this code path.
    • Max $ per run: max_calls × unit_cost — compute it, don't estimate.
    • Max $ per day: the provider-side hard cap that backstops the code-side bound.

    Examples:

    • "Fal flux-pro at $0.05/image; max 20 images per job; max $1 per job; provider Spend Limit $50/day."
    • "Anthropic Sonnet at ~$0.015 per request (cached); max 50 requests per agent run; max $0.75 per run; Workspace Budget hard cap $30/day."

    If you cannot fill in all three numbers, you have not designed the call site.

  2. Every loop calling a paid API gets an explicit iteration bound, not just a termination argument. invariant-guard requires a termination measure. runaway-guard requires the bound to be a concrete integer in code, not just "eventually terminates":

    // ❌ Terminates in theory. Bills $200 in practice.
    while (job.status !== 'done') {
      await fal.run(...);
    }
    
    // ✅ Concrete bound — wallet invariant explicit.
    const MAX_CALLS = 20;
    for (let i = 0; i < MAX_CALLS && job.status !== 'done'; i++) {
      await fal.run(...);
    }
    if (job.status !== 'done') throw new Error('exceeded MAX_CALLS budget');
    
  3. Every retry path is bounded by attempts AND total elapsed cost, not by time alone. Exponential backoff with no attempt cap is a wallet attack on yourself.

    • Max attempts: a small integer (3–5 for transient errors, 1 for 4xx).
    • Cap counts across the whole pipeline, not just one library — Inngest retries × SDK retries × your own retry wrapper multiply.
    • 4xx errors do not retry. Period. They will not become 2xx; they will just bill again.
  4. Every fan-out path declares a concurrency limit. Parallel calls multiply cost per wall-clock second. State the limit in code, at the queue (Inngest concurrency), and at the provider where supported:

…

常见问题

为什么需要设置上限?

如果代码中有 bug,比如一个永不停止的循环,付费 AI API 的费用可能会迅速增加。设置上限可以防止一个小错误变成一笔巨额账单。

什么是“每次运行成本”?

指一次代码执行中允许的最大花费。例如,如果上限是 10 次调用,每次花费 $0.01,那么每次运行成本就是 $0.10。

为什么要同时在代码和提供商平台上设置上限?

在代码中设置是第一道防线。在提供商平台上设置则能确保即使代码出错,花费也会从外部被停止,相当于一道安全网。

详情

作者的原始描述

Cost-safety discipline for paid AI / inference APIs: treat $-cost as a third complexity dimension alongside time and space. Forces a written per-run $-cap, per-day $-cap, max-iterations bound, concurrency limit, and a matching provider-dashboard hard cap BEFORE any call site is written.

repo: sickn33/agentic-awesome-skills

热度

GitHub 星标
48k
排名
#5651
许可证
MIT
更新时间
2026/10/15

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能