Skip to the solution
MediumAI Engineering13 min read

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

546 views

The problem

Question presented to candidate: "People use the word 'agent' for almost anything with an LLM in it. Tell me what you mean by an AI agent, walk me through the loop that runs one, and tell me what stops that loop from running forever or burning money."

What a strong answer should cover:

  • A minimal definition: a model that chooses its own next action from a set of tools, inside a loop the application runs, until the goal is met or a limit stops it (model + tools + loop + stop conditions)
  • The loop step by step: send context, the model returns either tool calls or a final answer, the runtime executes the tools, the results are appended as observations, repeat; the model never executes anything itself
  • The difference between a workflow (code fixes the path) and an agent (the model picks the path at runtime), and why you start with the simplest one that works
  • Termination guards: a step or turn cap, a token or cost budget, a wall-clock deadline, and detection of repeated identical calls, each ending in a clean, explained stop
  • Context growth: every turn re-sends the whole transcript, so input tokens grow with each step and total cost grows roughly with the square of the number of steps
  • The production concerns around the loop: tool permissions, error handling, checkpoints and full traces of every step

Clarifying questions expected:

  • What kind of task is it: a known sequence of steps, or open-ended work where the next step depends on what was found?
  • Which actions have side effects (writes, payments, emails), and do any need a human approval?
  • What latency and cost budget does one task have?

Code / implementation expected: Optional โ€” sketching the loop (call model, run tool calls, append results, check stop conditions) in pseudocode or with an SDK.

  • agents
  • agent-loop
  • tool-use
  • architecture

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

13 min read ยท 11 sections

Read the code

Agent loop with stop guards and token accounting
// The agent loop: model decides, runtime acts, observation goes back, guards stop it.
// The "model" is a scripted stub so the loop itself is what runs.
const TOOLS = {
  get_order: (a) => ({ id: a.id, status: "shipped", tracking: "1Z999" }),
  track_package: (a) => ({ tracking: a.tracking, eta: "2026-10-20" }),
  search_docs: (a) => ({ query: a.query, hits: 0 }),
  read_page: (a) => ({ page: a.n, text: "lorem ".repeat(60).trim() }),
};
const SYSTEM = "You are a support agent. Call tools when you need facts. Answer when done.";
const tok = (s) => Math.ceil(s.length / 4); // rough: 4 characters per token
const show = (o) => JSON.stringify(o);

// Three scripted "models": each gets the step number and the last observation.
const MODELS = {
  happy: (step, obs) => {
    if (step === 1) return { tool: "get_order", args: { id: 1042 } };
    if (step === 2) return { tool: "track_package", args: { tracking: obs.tracking } };
    return { final: "Order 1042 has shipped and should arrive on " + obs.eta + "." };
  },
  stuck: () => ({ tool: "search_docs", args: { query: "gift card refund" } }),
  wander: (step) => ({ tool: "read_page", args: { n: step } }),
};
const DEFAULTS = { maxSteps: 8, maxRepeats: 2, maxInputTokens: 1200 };

function runAgent(name, userMsg, LIMITS) {
  console.log("== " + name + ": " + userMsg + "  limits=" + show(LIMITS));
  const messages = [SYSTEM, userMsg];
  const seen = new Map();
  let obs = null, totalIn = 0, step = 0, calls = 0, stop = "";
  while (true) {
    step += 1;
    if (step > LIMITS.maxSteps) { stop = "max_steps"; break; }
    const inputTokens = messages.reduce((n, m) => n + tok(m), 0);
    if (totalIn + inputTokens > LIMITS.maxInputTokens) { stop = "token_budget"; break; }
    totalIn += inputTokens;
    calls += 1;
    const action = MODELS[name](step, obs);
    if (action.final !== undefined) {
      console.log("  step " + step + "  input=" + String(inputTokens).padStart(4) + "  -> final: " + action.final);
      stop = "done"; break;
    }
    const sig = action.tool + show(action.args);
    const count = (seen.get(sig) || 0) + 1;
    seen.set(sig, count);
    if (count > LIMITS.maxRepeats) {
      console.log("  step " + step + "  input=" + String(inputTokens).padStart(4) + "  -> " + sig + "  (repeat #" + count + ", blocked)");
      stop = "repeated_call"; break;
    }
    obs = TOOLS[action.tool](action.args);
    console.log("  step " + step + "  input=" + String(inputTokens).padStart(4) + "  -> " + sig);
    messages.push("call " + sig);
    messages.push("result " + show(obs));
  }
  console.log("  stop=" + stop + " model_calls=" + calls + " total_input_tokens=" + totalIn);
  console.log("");
}

runAgent("happy", "Where is order 1042?", DEFAULTS);
runAgent("stuck", "Refund a gift card?", DEFAULTS);
runAgent("wander", "Summarise the handbook.", DEFAULTS);
runAgent("wander", "Summarise the handbook.", { maxSteps: 4, maxRepeats: 2, maxInputTokens: 100000 });

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 41 of 83 in AI engineering

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextMedium

What is chain-of-thought prompting and when does it actually help?

Continue