# How to Stop an AI Agent Stuck in an Infinite Loop

DevFeed: [How to Stop an AI Agent Stuck in an Infinite Loop](<https://devfeed.tech/articles/how-to-stop-an-ai-agent-stuck-in-an-infinite-loop-129837.md>)

Original publisher: [Read original article](<https://openrouter.ai/blog/tutorials/ai-agent-infinite-loop/>)

Author: OpenRouter

Published: 2026-11-12T00:00:00Z

Content type: tutorial

Language: en

Sources: [OpenRouter](<https://devfeed.tech/sources/openrouter.md>)

Topics: [AI Agent](<https://devfeed.tech/topics/ai-agent.md>), [Agent Harness](<https://devfeed.tech/topics/agent-harness.md>), [function calling](<https://devfeed.tech/topics/function-calling.md>), [Multi-step AI processes](<https://devfeed.tech/topics/multi-step-ai-processes.md>), [AI Context Management](<https://devfeed.tech/topics/ai-context-management.md>)

Tags: [agent](<https://devfeed.tech/tags/agent.md>), [agent-sdk](<https://devfeed.tech/tags/agent-sdk.md>), [agents](<https://devfeed.tech/tags/agents.md>), [ai](<https://devfeed.tech/tags/ai.md>), [ai-agent](<https://devfeed.tech/tags/ai-agent.md>), [ai-agents](<https://devfeed.tech/tags/ai-agents.md>), [api](<https://devfeed.tech/tags/api.md>), [api-key](<https://devfeed.tech/tags/api-key.md>), [arguments](<https://devfeed.tech/tags/arguments.md>), [billing](<https://devfeed.tech/tags/billing.md>), [budget](<https://devfeed.tech/tags/budget.md>), [bug](<https://devfeed.tech/tags/bug.md>), [cost](<https://devfeed.tech/tags/cost.md>), [costs](<https://devfeed.tech/tags/costs.md>), [how-to](<https://devfeed.tech/tags/how-to.md>), [infinite](<https://devfeed.tech/tags/infinite.md>), [model](<https://devfeed.tech/tags/model.md>), [retry](<https://devfeed.tech/tags/retry.md>), [stop](<https://devfeed.tech/tags/stop.md>), [tool](<https://devfeed.tech/tags/tool.md>)

## AI overview

The guide recommends four layers to stop runaway agent retries: enable doom-loop detection, normalize equivalent tool inputs with a loopKey, cap each run by steps and cost, and set an API-key credit limit. In tests across six models, detection reduced calls from 314 to 64 and spend from $0.29 to $0.05 across 18 runs, but exact-argument matching could be evaded by small input changes. Per-run caps cover novel retries; the key limit is enforced before provider routing and protects against bugs or many concurrent sessions. With BYOK, include_byok_in_limit must be enabled. A request whose worst-case input plus max_tokens exceeds the remaining limit fails without charge; in-flight requests can leave spend slightly above the cap. The guide also notes that step caps may allow pending tool calls and a final turn to run, so actual calls can exceed the configured step count.

## Source excerpt

An AI agent gets stuck in an infinite loop when a model keeps calling a tool that fails, usually with the same arguments, because nothing in the agent loop tells it to stop. Each retry resends the whole conversation, so every turn costs more than the last, and a loop left running overnight can cost thousands of dollars.