mediumAI Engineering

What is an AI agent and how does the agent loop work?

545 views
01

Understand the problem

Model + tools + loop: the minimal architecture behind every agent framework.

agentsagent-looptool-usearchitecture
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Stuck? AI Nudge Available

Get a conceptual hint to guide your logic without spoiling the final implementation.

03

Study the solution

The solution is waiting

Give it an honest attempt first — then compare your thinking with the full walkthrough.

04

Read the code

Loop guards around the tool loop
const budget = { steps: 25, usd: 2.0, deadline: Date.now() + 120_000 };
const seen = new Set<string>();

while (true) {
  assertBudget(budget);                          // steps, cost, wall clock
  const action = await decide(messages);
  if (action.type === "final") return action.text;

  const sig = action.name + ":" + hash(action.input);
  if (seen.has(sig)) {
    messages.push(hint("You already tried this exact call. Change approach."));
    continue;                                    // loop detection
  }
  seen.add(sig);
  messages.push(await execute(action));
}
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.