Guide · Loops
Your AI agent is stuck in a loop. Use this checklist.
Loops are rarely dramatic. An agent repeats the same tool call, retries a failing step, or keeps compacting and carrying on after the work is done. Here's what to do, in order.
Published
In one public GitHub issue, a user reported a subagent that made the same grep call 364 times over about 50 minutes, with nothing built in to stop it. [1] In another, a user reported Claude Code resending the same requests for several hours while the session was unattended. [2]
Right now (the first 5 minutes)
- ☐ Stop the process. Ctrl-C, kill the container, or interrupt the session. That's the fastest way to stop the spend if you can reach the machine.
- ☐ If you can't reach it, cut off the key at the provider:
- ☐ OpenRouter: disable the key in the Key safety tab, or
PATCH /api/v1/keys/{hash}with{"disabled": true}. This is reversible. [3][4] - ☐ Anthropic: Disable the key on the Claude Console API keys page, or set
status: "inactive"through the Admin API. This is reversible. [5][6] - ☐ OpenAI: delete the project key, which is permanent, or turn on a hard spend limit for the project to stop traffic. Limit enforcement isn't instantaneous. [7][8]
- ☐ OpenRouter: disable the key in the Key safety tab, or
- ☐ Turn off auto top-up or auto-recharge while you investigate, so the spend can't refill itself.
Within the hour: check the damage
- ☐ Find the key that spent and note when the spike started.
- ☐ Look at the agent's logs for the repeated call: same tool, same input, or the same error.
- ☐ Check whether any sub-agents or background jobs are still running. Some harnesses report a stuck subagent as "running" the whole time. [1]
- ☐ If the loop was a platform bug, contact the provider's support team with the logs. Refunds aren't guaranteed.
Before you restart: add guards in the agent
- ☐ A step or iteration cap for each task or turn loop.
- ☐ Repeated-call detection: stop or warn after N identical tool calls (same tool and same input).
- ☐ A cap on retries for failing steps, with backoff.
- ☐ A maximum wall-clock time for unattended sessions.
- ☐ A way to abort subagents from the parent session.
- ☐ Your harness's own loop guard. For example, a public issue comment says OpenCode documents a
doom_looppermission that fires when the same tool call repeats three times with identical input. That docs page was not rechecked for this guide, so treat it as a reported feature.
Before you restart: add guards on the money
- ☐ One key per agent, so one loop maps to one key.
- ☐ A provider limit on each key or its scope: an OpenRouter per-key
limitwith a daily reset; an OpenAI project hard limit; an Anthropic workspace limit. [9][8][10] - ☐ A spike rule: alert when spend over the last few minutes is a large multiple of the recent baseline. Loops show up as a change in rate long before they hit a monthly limit.
- ☐ Alerts in a channel someone watches at night, such as Slack, not just an inbox.
- ☐ A kill action decided in advance: disable (reversible) or delete (permanent).
Automating the money guards with BillCurfew
BillCurfew watches per-key spend on OpenAI, Anthropic and OpenRouter through admin APIs. You get a daily dollar cap, a spike rule, and alerts by email on every plan. Slack and optional auto-kill are paid-only, and auto-kill stays off until you turn it on. A manual kill works on every plan, including Free. There's no proxy, no code change, and no prompts are stored. It polls about every five minutes, so a loop can spend for up to one poll interval before it notices. That's why the in-agent guards above still matter.
Start free with one key, no card. Free is $0 for 1 provider and 1 key, with email alerts and a manual kill. Solo is $19/mo, Team $49/mo and Agency $149/mo, each with a 14-day trial.
FAQ
How do I know my agent is looping, not just working hard?
Look for repeated identical tool calls, the same error over and over, or spend rising at a steady rate with no new output.
What's the fastest way to stop a runaway agent?
Kill the process if you can reach it. If you can't, disable the API key at the provider.
Will disabling the key break production?
Yes, for anything that uses that key, which is why one key per agent matters.
Can I undo turning off a key?
On Anthropic and OpenRouter disabling is reversible. On OpenAI, deleting a key is permanent. [6][3][7]
Do provider spend limits stop loops?
They cap the total. OpenAI and Anthropic limits are monthly, and OpenAI notes enforcement can overshoot slightly. A spike alert catches a loop sooner. [8]
Sources
- GitHub, anomalyco/opencode #45442 (user report). https://github.com/anomalyco/opencode/issues/45442 [1]
- GitHub, anthropics/claude-code #35166 (user report). https://github.com/anthropics/claude-code/issues/35166 [2]
- OpenRouter, Security settings. https://openrouter.ai/docs/guides/overview/auth/security-settings [3]
- OpenRouter, Update an API key. https://openrouter.ai/docs/api/api-reference/api-keys/update-an-api-key [4]
- Anthropic, Update API Key. https://platform.claude.com/docs/en/api/organization/api_keys/update [5]
- Anthropic, Authentication. https://platform.claude.com/docs/en/manage-claude/authentication [6]
- OpenAI, Delete project API key. https://developers.openai.com/api/reference/resources/admin/subresources/organization/subresources/projects/subresources/api_keys/methods/delete [7]
- OpenAI, Spend limits. https://developers.openai.com/api/docs/guides/spend-limits [8]
- OpenRouter, API Authentication. https://openrouter.ai/docs/api_reference/authentication [9]
- Anthropic, Workspaces. https://platform.claude.com/docs/en/manage-claude/workspaces [10]