{"version":"v1","id":"ai-agent-loop-cost-calculator","matrix":"workflows","status":"live","updated":"2026-11-18","title":{"en":"AI Agent Loop Cost Calculator","zh":"AI Agent 死循环成本计算器"},"h1":{"en":"AI Agent Cost Calculator: Runaway Loops & Budget Burn","zh":"AI Agent 成本计算器：死循环账单与预算熔断"},"description":{"en":"Sogoh prices a runaway agent loop across workers, then gives you the breaker limits that keep it inside budget.","zh":"Sogoh 把一次死循环的上限账单按并发线程放大，并给出可直接抄进代码的熔断参数。"},"answer":{"en":"Sogoh’s loop-cost model shows a runaway agent’s bill is not linear: with full-history accumulation, input tokens grow quadratically. At 2,000 input and 800 output tokens per step, 50 retries send about 3.53M input tokens — roughly 141× a 3-step run on Claude Sonnet 5.5. Cap iterations, cap tokens per run, and enforce a dollar hard stop at the API gateway.","zh":"Sogoh 的死循环成本模型说明：账单不是线性增长，全量历史累积下输入 token 按轮数平方增长。以每步 2,000 输入、800 输出计算，50 轮会发送约 353 万输入 token，约为 Claude Sonnet 5.5 正常 3 轮执行的 141 倍。必须在 Agent 定义与 API 网关同时设置轮数、token 与金额熔断。"},"keywords":{"en":["ai agent cost calculator","llm token cost calculator","ai agent infinite loop","runaway ai agent","ai agent circuit breaker","max iterations langgraph","doom loop llm"],"zh":["AI Agent 成本计算器","大模型 token 费用计算","AI Agent 死循环","Agent 烧钱","AI 熔断器","max_iterations 设置","LLM 成本估算"]},"formula":[{"title":{"en":"Measure one step, not the task","zh":"先量单步，不量整次任务"},"body":{"en":"Read input and output tokens from a single tool call in your gateway logs. 2,000 input and 800 output is a typical tool-using step; reasoning models often double the output side, and every extra tool result lands in the next input.","zh":"从网关日志里取一次工具调用的输入与输出 token。2,000 输入、800 输出是常见的工具调用步骤；推理模型的输出侧常常翻倍，而每个工具结果都会进入下一轮输入。"}},{"title":{"en":"Pick the accumulation mode honestly","zh":"如实选择上下文累积方式"},"body":{"en":"A static window means the framework truncates history. Full history means every retry re-sends earlier errors and outputs. Only the second produces quadratic growth — check whether your loop appends the previous failure to the next prompt.","zh":"静态窗口表示框架会截断历史；全量历史表示每次重试都把之前的报错与输出重新发一遍。只有后者会产生平方级增长——检查你的循环是否把上一轮失败追加进了下一次提示词。"}},{"title":{"en":"Read the ceiling, then the burn rate","zh":"先看上限，再看烧钱速度"},"body":{"en":"The headline number is the worst case across all workers: single-thread bill × concurrency. Compare it with a 3-step normal run, then check when the loop would cross your budget red line at 3.5 seconds per step.","zh":"首屏数字是全部线程的上限：单线程账单 × 并发数。先和 3 轮正常执行对比，再按每轮 3.5 秒估算它多久会撞上预算红线。"}},{"title":{"en":"Enforce the breaker in two places","zh":"熔断要写两层"},"body":{"en":"Set max_iterations in the graph and a hard dollar/token stop in the API gateway. An outer workflow restart resets in-process counters, but it cannot reset a gateway cap; alert at 50% of budget so a silent loop is visible.","zh":"在 Graph 里设 max_iterations，在 API 网关设金额与 token 硬上限。外层工作流重启会重置进程内计数器，但重置不了网关上限；同时在预算 50% 处告警，沉默的死循环才会被发现。"}}],"faq":[{"q":{"en":"Why does a looping agent’s bill grow quadratically?","zh":"为什么 Agent 死循环的账单会按平方增长？"},"a":{"en":"If each retry appends the previous error and output, the input at step i is T + (i − 1) × (T + O). Total input over N steps is N × T + (T + O) × N(N − 1) / 2: the second term is quadratic. At 2,000 input and 800 output tokens, 50 steps send 3.53M input tokens instead of 100k. This is the doom loop pattern: a failed tool call, a retry with the error appended, and no external stop.","zh":"如果每次重试都追加上一轮报错与输出，第 i 轮的输入就是 T + (i − 1) × (T + O)。N 轮总输入为 N × T + (T + O) × N(N − 1) / 2，第二项是平方级。以 2,000 输入、800 输出计算，50 轮会发送 353 万输入 token，而不是 10 万。这就是所谓的 doom loop：工具调用失败、把报错追加进上下文再重试，而外层没有任何停止条件。"}},{"q":{"en":"What max_iterations should I set?","zh":"max_iterations 应该设多少？"},"a":{"en":"Set it from your budget, not from a blog post. With a $50 cap, five workers and the defaults above, the calculator recommends about 40 iterations to stay under half the budget. Remember that a LangGraph super-step may contain several model calls, so count the calls that actually bill.","zh":"按预算反推，不要照抄博客。以 50 美元预算、5 个线程和上面的默认值为例，计算器建议约 40 轮，把账单压在一半预算以内。注意 LangGraph 的一个超级步可能包含多次模型调用，要按真正计费的调用次数计数。"}},{"q":{"en":"Does this include prompt caching or batch discounts?","zh":"计算包含缓存折扣或批处理折扣吗？"},"a":{"en":"No. It uses list prices without cache reads, batch rates or tool surcharges. Caching can cut the input price by roughly 75–90% on supported providers, but it slows the curve rather than removing the quadratic term. Enter your effective rate under Custom.","zh":"不包含。这里用的是不含缓存读取、批处理与工具附加费的列表价。支持缓存的供应商通常能把输入价格降约 75–90%，但它只是放缓曲线，不会消除平方项。按实际有效单价填进「自定义」。"}},{"q":{"en":"Why cap the context per request?","zh":"为什么要给单次请求设 token 上限？"},"a":{"en":"Every model has a context window, and most frameworks truncate or reject oversized prompts. Without a cap the math produces bills no real request could ever generate. The default 200k cap keeps the estimate actionable; lower it if your model or gateway limit is smaller.","zh":"每个模型都有上下文窗口，框架也会截断或直接拒绝超大提示词。不设上限会算出真实请求根本产生不了的数字。默认 200k 让估算保持可执行；模型或网关限制更小时，把它调低。"}},{"q":{"en":"How do I stop an infinite loop in n8n or LangGraph?","zh":"n8n 或 LangGraph 里的死循环怎么停？"},"a":{"en":"Use more than one layer: recursion_limit or max_iterations inside the graph, a per-run timeout and token cap, and a hard spend stop at the API gateway or provider. Then test the breaker by forcing a failing tool call; a limit you have never triggered is not a guarantee.","zh":"至少两层：Graph 内的 recursion_limit 或 max_iterations，单次运行的超时与 token 上限，以及 API 网关或供应商侧的金额硬停。然后强制制造一次工具报错，验证熔断真的会触发；从未触发过的限制不算保障。"}},{"q":{"en":"How current are the prices, and why do they differ from my invoice?","zh":"价格是最新的吗？为什么和我的账单不一样？"},"a":{"en":"Model prices were read from the OpenRouter models API on 18 November 2026. Provider-direct billing, cache reads, batch discounts, long-context surcharges and negotiated rates can differ. Switch to Custom and enter the rate you actually pay.","zh":"模型单价于 2026 年 11 月 18 日读取自 OpenRouter models API。供应商直连计费、缓存读取、批处理折扣、长上下文加价与协议价都可能不同。切到「自定义」，填入你实际支付的单价。"}}],"sources":[{"label":"OpenRouter — Models API (pricing fields)","url":"https://openrouter.ai/api/v1/models"},{"label":"Anthropic — API pricing","url":"https://www.anthropic.com/pricing"},{"label":"OpenAI — API pricing","url":"https://openai.com/api/pricing/"},{"label":"Google — Gemini API pricing","url":"https://ai.google.dev/gemini-api/docs/pricing"},{"label":"DeepSeek — API pricing","url":"https://api-docs.deepseek.com/quick_start/pricing"},{"label":"LangGraph — graph API and recursion limit","url":"https://github.com/langchain-ai/langgraph"}],"localized":{"en":{"url":"https://www.sogoh.com/tools/ai-agent-loop-cost-calculator/"},"zh":{"url":"https://www.sogoh.com/zh/tools/ai-agent-loop-cost-calculator/"}},"license":"Free to reuse with attribution and a link to the source page."}