AI agent infinite loops: step budgets, loop detection and kill switches
How to stop an AI agent infinite loop from burning your LLM budget: step and token budgets, repeated-call detection, wall-clock limits and a per-run kill switch.
Durable execution lets a long-running AI agent resume after a crash, deploy or timeout instead of starting over. How checkpointing and workflow engines differ, and when you need either.
An agent that triages a support backlog, researches a lead or reconciles a month of invoices can run for many minutes or hours. Then a deploy rolls the server, a worker is evicted, or a provider times out at step 31 of 40, and the whole run starts again, paying for every model call twice. Durable execution for AI agents is the pattern that prevents that: record progress as you go, and let any worker resume from the last recorded step.
This post is about the domain, not any particular deployment of ours. Facts come from public docs, linked inline.
A request handler lives and dies with one process. Anything held in memory, such as the message list, the tool results gathered so far and the loop counter, disappears with it. That is tolerable for a two-second chat reply. It is expensive for a run that has already spent minutes and tokens.
The usual triggers are mundane:
Our post on why AI agents fail in production lists lost state among the architectural causes. This one is the fix for it.
Temporal's documentation describes it as running your code to completion across crashes and outages. When a worker dies, the service hands the work to another worker, which replays the recorded event history and resumes at the line where execution stopped, with local variables and progress intact. A workflow can keep running "whether that takes seconds or months".
The agent loop maps onto this directly: each model call or tool call is a recorded step, and a finished step is not executed again on resume.
The two common approaches solve overlapping but different problems.
Checkpointing saves the agent's state after steps. LangGraph's persistence layer saves graph state as checkpoints per thread, and lists conversation continuity, human-in-the-loop, time travel and fault tolerance as uses. Its docs warn that in-memory savers lose data on restart, so production needs a persistent one such as the Postgres saver, and that checkpoints accumulate over long conversations, raising latency and storage cost unless you prune them.
A workflow engine such as Temporal owns the orchestration: timeouts, retries with exponential backoff and heartbeats are configuration, and the engine, not your app, decides when to resume.
| In-memory loop | Checkpointer | Workflow engine | |
|---|---|---|---|
| Survives a crash | No | Yes, if the store is persistent | Yes |
| Survives a deploy | No | Yes, you resume the thread | Yes |
| Waits days for a human | No | Possible, you build the trigger | Built for it |
| Retry and timeout policy | You write it | You write it | Configuration |
| Extra infrastructure | None | A database | A service and workers |
| Fits runs that last | Seconds | Minutes to hours | Hours to months |
The last row is a rule of thumb, not a benchmark. Pick on the failure you cannot afford, not on the tool you have heard of most.
Three catches show up in practice.
Long runs hit history limits. Temporal's event history logs a warning at 10,240 events and terminates the workflow beyond 51,200, with further caps on Updates (2,000) and Signals (10,000). Its documented remedy is Continue-As-New, which closes the current execution and starts a new one with a fresh history. An agent that loops over thousands of tool calls needs that designed in from the start.
Resuming can repeat the step that was in flight. Durable execution guarantees progress is not lost, not that a side effect happened exactly once. If a worker dies after the email is sent but before the step is recorded, the step can run again. Pair this with idempotency keys as described in AI agent retries and idempotency.
State grows. Every recorded message and tool output is stored. Prune or summarise, as discussed in AI agent memory cost, and decide how long run records are retained, particularly if they hold personal data (see the DPDP Act post).
Answer four questions:
A sensible path is to start with a persistent checkpointer, add idempotency keys on every side-effecting tool, and move to a workflow engine when waits, deploy-safety or retry policy become the main source of incidents.
Take your longest-running agent flow and kill the worker midway in a staging environment. If the run restarts from zero, repeats a side effect, or silently disappears, you have found your durability gap. Fix that flow first.
Before adding more agents to a long-running flow, check whether you need them: single agent vs multi-agent AI has a checklist. If you are designing agents that must run unattended, our AI automation service covers architecture reviews and builds for exactly this class of workflow.
Drafted by CodeKrypt's AI writing pipeline, built and maintained by our engineers, under fixed rules on sources and claims. How we write
How to stop an AI agent infinite loop from burning your LLM budget: step and token budgets, repeated-call detection, wall-clock limits and a per-run kill switch.
Single agent vs multi-agent AI systems: when extra agents help, when they only add cost and coordination bugs, and a decision checklist to use before you build.
AI agent memory cost is the budget line most teams discover only after their context window balloons. What naive context injection actually costs, what tiered retrieval saves, and real 2026 pricing to plan against.