Agentic Control Loops: Building Autonomous Systems That Stop

7 min readYaseen Khatib · AI Systems Architect

A chatbot answers. An agent acts. The line between them isn't a bigger model or a cleverer prompt — it's a control loop. Observe the state, decide the next action, act through a tool, observe the result, repeat. The intelligence is in the loop, not in any single inference.

Agents are loops over tools, not bigger prompts

The instinct when an LLM can't do something is to add more to the prompt. The agentic instinct is to give it a tool and a loop. Each iteration the model sees the current state, picks one action from a defined toolset, and the runtime executes it and feeds the result back. A single inference can't book a flight; a loop that can call search, select, and pay across several turns can.

observedecideactsearch()db.query()sendEmail()done? / budget?else stop
An agent is a loop over tools: observe → decide → act → observe. The step budget and termination gate are what separate an autonomous system from a runaway one.

Every loop needs a way to stop

The defining risk of autonomy is the runaway loop: an agent that never decides it's done, burning tokens and calling tools in circles. So termination is not an afterthought — it's a first-class part of the design. Every loop needs an explicit success condition and a hard step budget, so that "it didn't finish" degrades to a clean stop instead of an unbounded spend.

agent.ts
// the loop is the architecture — note the two ways out
let state = init(task);
for (let step = 0; step < MAX_STEPS; step++) {   // hard budget
  const action = await model.decide(state, TOOLS);
  if (action.type === "finish") return action.result; // success exit
  const result = await TOOLS[action.name](action.args);
  state = reduce(state, result);                // carry memory forward
}
return escalate(state); // budget exhausted → hand off, don't loop

State between iterations is the hard part

The reasoning step is the part everyone demos; the state management is the part that decides whether it works. What does the agent carry from one iteration to the next, and how does it fit in the token budget? Naively appending every tool result blows the context window in a handful of steps. Real agents reduce state — summarising, pruning, and keeping only what the next decision needs. The loop is easy; the memory is the engineering.

Autonomy isn't a smarter answer. It's a disciplined loop — with a budget, a stop condition, and a memory you actively manage.

Keep each specialist in the loop a grounded RAG agent, route between them with the router pattern, and run the whole thing at the edge. Continue on the roadmap.

Looking to architect a similar system?

Let's ship it at AI-speed.

Start a conversation →