Foundations · Free preview

Knowing When to Stop

Halt safely, not forever

A safe agent loop must terminate on EITHER reaching its goal OR exhausting a max-steps budget, whichever comes first — and report which one fired.

Knowing when to stop

Back in lesson 2 the loop stopped because the goal was close and every step landed exactly on it. Real agents aren't that tidy. The goal might be 1000 away while each step moves 100; the model might wander, repeat itself, or chase a target it can never reach. A loop whose only exit is "reached goal" will, on a hard task, run until it times out — and with a real model that's a metered API call billing you on every turn. An agent loop that can't stop isn't slow; it's a runaway cost.

So every real loop needs two stop conditions, not one. The first is the happy path: reaching the goal. The second is the safety net: a step budget — a hard ceiling maxSteps on how many turns the loop may take regardless of progress. The loop halts on whichever fires first, and — this is the part beginners skip — it records which one so the caller can tell success from a forced cutoff.

Walk it: goal 1000, step 100, budget 8. The agent steps to 100, 200, ... 800. After the 8th step steps equals maxSteps while position is only 800 — still short of 1000. The goal check fails, the budget check fires, reason becomes "hit step budget," and the loop breaks with the work unfinished but the process safely terminated. In production that distinction is everything: "reached goal" means ship it; "hit step budget" means escalate, retry, or ask for help.

Below, replace the steps < 20 backstop logic with real guards. Inside the loop, stop with reason = "reached goal" when position >= goal, and stop with reason = "hit step budget" when steps >= maxSteps — then report it. "Done" is exactly stopped: hit step budget (pos=800, steps=8).

Never write a loop you can't prove will end. The goal check is the plan; the step budget is the seatbelt — and reporting which fired is how anyone downstream knows whether the agent succeeded or just gave up.

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

// The environment — already written for you.
// The goal is FAR away on purpose, and our step is small, so the agent
// might run for a very long time. A safe loop must guard against that.
const goal = 1000;
const maxSteps = 8; // your budget: never take more steps than this
let position = 0;

function act() {
  position += 100; // each step moves us 100 toward the goal
}

// 🛑 The guarded loop. The "steps < 20" is only a hard backstop so the page
// never hangs while you work — YOUR job is the real stop logic. It must stop
// for ONE of two reasons:
//   • "reached goal"   — position is at or past the goal
//   • "hit step budget" — we've used up maxSteps before reaching it
// TODO: add BOTH stop conditions, then print why it stopped.
let steps = 0;
let reason = "";
while (steps < 20) {
  // TODO: if position >= goal, set reason = "reached goal" and break
  // TODO: if steps >= maxSte

🔒 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 →

14-day refund · 🔒 Stripe-secured checkout · lifetime access

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

← The Agent Marketplace