Agentic 工作流

Agent loop engineering: designing self-correcting loops that don't spiral out of control

An agent retried 2,400 times overnight, consuming half the month's token budget. Nobody noticed because it appeared to be working. Writing a self-correcting agent is straightforward. The difficulty lies in knowing when to stop. The plan-act-observe-reflect cycle requires four controls: maximum step counts, stop-loss conditions, budget caps, and escalation exits. These prevent endless loops and uncontrolled costs.

By

Tenten AI 研究團隊

應用 AI

Published

February 13, 2026

Read time

6 分鐘

Agent 設計agent loopagentic 工作流harness 工程LLM 應用AI 上線實作

Last week we debugged a reconciliation agent for a logistics company that ran continuously through the night. Its job was to cross-check carrier invoices from three different systems. When it encountered a missing field, it would retry. This seemed reasonable until the agent encountered a malformed null value from an upstream API. It interpreted this as incomplete data and retried the same operation 2,400 times, consuming nearly half that month's token budget. No one noticed during this time because from beginning to end, the agent appeared to be functioning normally.

Most discussions of agent loop design overlook a fundamental question: when should the agent stop? Developers typically focus on making agents more capable and reflective, but fewer ask where the boundaries should be.

Breaking down the loop: plan-act-observe-reflect

A self-correcting agent operates as a structured cycle rather than a single prompt. Production systems execute these four stages repeatedly:

Plan: Based on the current goal and known state, decide what to do next. Each plan output should specify a single, executable, verifiable action, not ten steps at once. Large steps make it harder for the Observe phase to measure progress.

Act: Call a tool, query a database, write a file. Wrap this step in a clear interface. Tool inputs and outputs must have defined schemas, and errors must return structured data instead of natural language.

Observe: Feed the action results back. Many teams skip this step, placing raw responses directly into context, which wastes tokens and obscures important information. Instead, summarize and normalize the results first. Include only the information the agent needs to determine its next action.

Reflect: Compare the step's results against the goal. Ask whether progress occurred, whether the same action is repeating, and whether continuing is worthwhile. You do not need to call an expensive model every cycle. Use rule-based filters first, and invoke the model only when the filters detect unusual patterns.

Building the loop is simple. The challenge is that these four steps alone will continue indefinitely without external limits.

Four gates to keep the loop in check

The control layer determines whether an agent can run in production. It consists of four essential categories:

Control MechanismPreventsProduction Reference Setting
Max stepsInfinite loops, token burnSet 8-25 steps per task complexity, hard stop
Stop-loss conditionSpinning in place, wasted retriesStop after N consecutive steps with no progress
Budget capCost spiralSet token/cost ceiling per task
Escalation exitDead weight, nobody noticesHand back to human when ceiling hit, with full trace

Max steps is the simplest and most effective control. Every loop must have a hard limit. When the limit is reached, the loop stops regardless of whether the agent judges itself to be nearly complete. The reconciliation agent described earlier would have stopped after hitting this limit instead of running 2,400 times.

Stop-loss conditions go further than step counts. They detect whether progress is happening, not just whether steps are occurring. Common signals include consecutive Observe results that are nearly identical, or the agent calling the same tool with the same or similar parameters. When detected, the loop should stop. Detecting progress does not require complex logic. Compare the state before and after each step by hashing, or calculate how often the tool call sequence repeats. This approach works in most cases.

Budget caps provide financial limits. A loop might show normal step counts and no signs of repetition, yet still incur large costs if each step performs expensive operations like database retrieval. Maintain a separate counter for tokens or cost, independent of the step counter. When the counter reaches the ceiling, stop the loop.

Escalation exits are frequently overlooked. The first three gates address how to stop. This gate addresses what happens after stopping. A production agent should not simply report task failure. Instead, it should collect the complete sequence of plan, act, and observe steps and return them to a human operator. An engineer reviewing this trace can quickly identify where the loop stopped making progress. Without this gate, the other controls prevent financial loss but create scenarios where a stalled loop runs silently and unnoticed.

Reflection tradeoffs: common mistakes

Reflect is the step most prone to overuse. Early attempts at reflection had the agent critique itself thoroughly in each cycle. This led to excessive revisions where the agent questioned correct decisions, complicating straightforward tasks. The solution was to use a filtering approach. Apply rule-based checks by default. Reserve the expensive model-based reflection for cases where the filters detect repetition or errors. Reflection must reach a conclusion. Additional reflection cycles beyond this point do not improve outcomes.

Reflection outputs must also modify the next Plan step, or the reflection produces no effect. Genuine self-correction requires that insights from reflection change what the agent plans next. This could mean selecting a different tool, adding a preliminary check, or eliminating a path. Connect this feedback correctly, and the agent achieves real self-correction. Without this connection, the reflection step runs without impact.

Agent loop maturity does not depend on how long the agent can run autonomously. It depends on clear stop conditions and detailed logging of each step. When deploying agentic workflows to production, this is the critical challenge. Many loops function well in demonstrations, but production systems require that loops operate within cost limits and hand control back to human operators when they encounter unexpected conditions. This combination of financial accountability and human oversight is what enables production traffic.

One stuck workflow
is enough to begin

Tell us what the team does today, where it breaks down, and what a better working day should look like.