Foundations · Free preview

The Agent Loop

Drive a tiny agent to its goal

An agent is a loop: perceive → decide → act → observe, repeated until the goal is reached. The model proposes the action; your runtime disposes.

An agent is a loop

In the last lesson the model was a pure function — one call, text in, text out. That's enough to answer a question but not to get something done. Say the goal is "move from position 0 to position 5." A single function call can't do that: it returns one suggestion, then forgets everything. To make progress you have to call it again, tell it where you ended up, and let it suggest the next move — again and again until you arrive. That repetition is the whole difference between a model and an agent.

The shape of the repetition is the agent loop: perceive the world, decide an action, act on it, observe the result, repeat. Crucially the split is the model proposes, your runtime disposes. The brain (decide) only ever returns a word like "step" or "stop" — it never touches the world directly. Your code (act) is what actually changes position. That separation is what keeps an agent governable: the model can suggest anything, but only your runtime can execute it.

Trace one pass. perceive reports {position: 0, goal: 5}; decide sees 0 < 5 and returns "step"; act bumps position to 1; the loop comes around and perceives 1. Five "step" decisions later position is 5, decide returns "stop", and the loop breaks. No single call did the work — the loop did.

Below, the environment and the perceive / act steps are written for you; the brain is missing. Implement decide(obs) so it returns "step" while position is below goal and "stop" once it arrives. "Done" is the agent walking 0→5 and halting exactly on arrival.

The loop is the whole idea. Everything else in this academy — tools, memory, planning, stop conditions — is something you add inside this loop.

In the full academy, you write and run this — live, graded:

// The agent's environment — already written for you.
const goal = 5;
let position = 0;

function perceive() {
  return { position, goal };
}

// 🧠 The agent's brain. Decide what to do given the observation.
// Return "step" to move toward the goal, or "stop" when you've arrived.
function decide(obs) {
  // TODO: replace this with your decision logic.
  return "stop";
}

function act(action) {
  if (action === "step") position += 1;
}

// The loop — perceive → decide → act → observe, until done.
let steps = 0;
while (steps < 20) {
  const obs = perceive();
  const action = decide(obs);
  console.log(`step ${steps}: pos=${obs.position} -> ${action}`);
  if (action === "stop") break;
  act(action);
  steps += 1;
}
console.log(`done at pos=${position}`);

🔒 Live code execution, real agent runs, mastery tracking and verifiable credentials unlock with the full academy.

This is 1 of 50 lessons.

The full academy: write real code, watch real agents run, and earn verifiable credentials — across 8 tracks, in a 3D campus.

Unlock the full academy — $100 →

More free lessons: An LLM Is a Function  ·  Define a Tool  ·  Give an Agent a Tool  ·  Durable State

← The Agent Marketplace