Skip to content

Agent anatomy: loop, planner, tools and memory

The minimal system behind an AI agent: a goal, a reasoning loop, tool access, state and a stop condition.

7 MIN READUPDATED 2026-11-18Agents & agentic AI
ORIGINAL DIAGRAM · AGENT LOOPPLANnext stepTOOL CALLtyped inputOBSERVEresultVERIFYcheckGOALinstruction

Original agent diagram — not a copied image

§ 00Notes & rules
Always set stop conditions
An agent without iteration, time and spend budgets can loop indefinitely. Treat budgets and escalation paths as part of the core design, not an afterthought.
State is explicit
Reliable agents pass structured state between steps (goal, plan, observations, tool results). Avoid relying on the model to remember everything from free-form text alone.
§ 01Key concepts
AgentA system that uses a model to choose actions, call tools and iterate toward a goal.
PlannerThe component that decomposes a goal into steps and decides the next action.
ToolA typed function the agent can call, with defined inputs, outputs and permissions.
Stop conditionThe rule that ends the loop: success, budget exhausted, or human hand-off.
§ 02When to use
  • The task needs several dependent steps and tool calls.
  • The path to the answer is not known in advance.
  • Work must continue across turns or sessions.
§ 03How it works
  1. 01Receive a goal and constraints (budget, allowed tools, success criteria).
  2. 02Plan the next action using the model and current state.
  3. 03Execute one tool call and observe the result.
  4. 04Update state and memory, then re-plan or stop.
§ 04Use cases & scenarios

Failed-order triage

Context
An operations team investigates why customer orders fail to fulfil.
Problem
Engineers manually search logs and order records for each incident.
Approach
Give an agent lookup and log-reading tools, explicit state, iteration limits and a human hand-off.
Outcome
A proposed root cause with a step-by-step audit trail.

Inventory replenishment across sessions

Context
A restock agent runs nightly and continues work the next day.
Problem
Context from the previous run is lost, so decisions are repeated.
Approach
Persist structured state with goals, observations and decisions between runs.
Outcome
The agent resumes where it stopped instead of restarting the analysis.
§ 05Examples

Minimal agent loop (conceptual)

ts
type State = { goal: string; steps: string[]; observations: string[] };

async function runAgent(goal: string, tools: ToolRegistry) {
  const state: State = { goal, steps: [], observations: [] };
  for (let i = 0; i < MAX_ITERATIONS; i++) {
    const action = await planNextAction(state, tools.describe());
    if (action.type === "final") return action.answer;

    const result = await tools.execute(action.tool, action.input);
    state.steps.push(action.tool);
    state.observations.push(JSON.stringify(result));
  }
  return escalateToHuman(state); // budget exhausted
}

Keep planning, tool execution and stop/escalation logic separate so each can be tested and observed.

Incident triage agent with budgets

json
{
  "goal": "Find the cause of a failed order and propose a fix",
  "allowed_tools": ["lookup_order", "read_logs"],
  "max_iterations": 12,
  "step_budget": 20,
  "stop": ["resolved", "needs_human"],
  "state": { "observations": [], "decisions": [] }
}

Pass state explicitly between steps and hand off to a human when the budget is exhausted — never guess a root cause.

§ 06Trade-offs

Single loop

Up: Simple to build and debug

Down: Struggles with long, branching tasks

Hierarchical planner

Up: Handles complex goals

Down: More tokens, more failure modes

Unbounded iterations

Up: Can solve harder problems

Down: Cost and loop risk without strict budgets

§ 07Implementation checklist
  • Give every tool a narrow, typed contract and explicit permission scope.
  • Cap iterations, tool calls and spend per run.
  • Log each step with input, output and decision reason.
  • Define what happens on low confidence: ask a human, not guess.
§ 08Configuration reference
KeyTypical valueNote
max_iterations10–30Hard stop for the loop.
allowed_toolsexplicit listLeast privilege by default.
step_budgetper taskPrevents runaway cost.
§ 09Troubleshooting
SymptomCauseFix
Agent loops foreverNo goal check or iteration capAdd explicit stop conditions and budgets.
Agent calls the wrong toolOverlapping tool descriptionsMake tool contracts mutually exclusive and specific.
§ 10Frequently asked questions

Is an agent just a prompt in a loop?

The loop is necessary but not sufficient. Reliable agents add typed tools, state, budgets, logging and escalation.

Do agents need long-term memory?

Only if work spans sessions. Start with in-run state and add persistent memory when a real use case demands it.

More in Agents & agentic AI