Overview
Production AI agents need a bounded loop: plan, act, observe, validate, repeat — with hard limits on cost and steps.
Components
- Planner — decides the next action from state.
- Executor — runs typed tool functions.
- Guardrails — budgets, policy and validation.
- Memory — conversation and working state.
Diagram
Goal
│
▼
[ Planner ] → [ Executor ] → [ Validator ]
│ │
│ fail → retry (capped)
▼
[ Memory ] → complete? → respond
Every step passes a validation gate before the next iteration.
Control loop
agent/run.ts
for (let step = 0; step < MAX_STEPS; step++) {
const action = await planner(state);
await guardrails.assert(action, state.usage);
const result = await executor(action);
if (!(await validator(result))) continue;
state.append(result);
if (state.isComplete()) break;
}Always cap steps and tokens. Unbounded loops are the fastest path to a surprise bill.
Deployment
Run as queue workers so long runs survive crashes; persist state to Postgres for resume.
Security
Scope tool credentials to least privilege, never let the model write raw SQL, require human approval for payments and deletes.