Skip to solution
mediumAI Engineering

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

546 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.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

An agent is a model that pursues a goal by repeatedly deciding an action, executing it via tools, and observing the result — the agent loop: prompt with goal and tool defs, model emits a tool call, runtime executes and appends the result, repeat until the model concludes or a budget stops it. Everything else in agent f

Solution ready — 2 min read

Classified // press E to declassify

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.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 40 of 80 decoded in the AI Engineering track. One more won't hurt.

Back to track