Loop Engineering · Lesson 1 of the course
🌐 हिंग्लिश version →

The Map of Loop Engineering

You already have a loop. This course is about engineering it. First, the territory.

You’ve written the thing already: a while loop that calls a model, runs the tool calls it asks for, feeds the results back, and goes again. Thorsten Ball’s famous one-liner is that an agent is just “an LLM, a loop, and enough tokens”1 — and he’s right that the skeleton is ~300 lines of boilerplate.

Loop engineering is everything that happens after that skeleton works. The gap between a toy loop and an agent you’d put in front of a customer is not a flash of genius — it’s a handful of levers, each tuned deliberately. This lesson is the map of those levers. Every later lesson zooms into one.

Your mission Build better, more reliable agents. So we won’t admire the loop — we’ll find the knobs on it that move reliability, and learn to turn each one on purpose.

One turn, anatomised

Before the map, fix the unit. Loop engineering operates on the turn — one pass through the loop. Strip a turn to its bones and it’s the same six moves every time:

  1. Assemble context — build the exact token payload the model will see this turn (instructions + state + history + tool defs).
  2. Call the model — one inference. It returns either an answer or one-or-more tool calls.
  3. Parse the output — tool calls are “just structured outputs,” routed by code you control.2
  4. Execute tools — run them against the real world; capture results and errors.
  5. Append the observation — fold those results back into state. This is the ground truth that lets the agent steer.3
  6. Check the stop condition — done? out of budget? hand to a human? Else: back to step 1.

HumanLayer compresses the whole apparatus to “prompt + switch statement + context builder + loop.”2 Memorise that phrase — every lever below is a way of engineering one of those four words.

Here are those six moves as the code you’ve already written, stripped to its spine. Notice there’s no magic — the “agent” is this loop:

The six moves, as ~12 lines you already recognise
# Factor 8 — own your control flow: this loop IS the agent
state = init(task)
for turn in range(MAX_TURNS):                  # move 6 · stop: budget guard
    ctx     = build_context(state)             # move 1 · assemble context
    reply   = model(ctx)                       # move 2 · call the model
    if reply.is_final:                         # move 6 · stop: goal reached
        return reply.answer
    calls   = parse_tool_calls(reply)          # move 3 · parse output
    results = [run(c) for c in calls]          # move 4 · execute tools (+ errors)
    state   = append(state, results)           # move 5 · append observation
raise BudgetExceeded(state)                    # move 6 · stop: guard tripped

Every lever in this course is a way of engineering one of those lines: build_context is Lever 02, run and its tools are Levers 03/04, MAX_TURNS is Lever 05, the handling of errors inside run is Lever 06, and state is Lever 07. The shape of the loop itself is Lever 01.

1 Context assemble 2 Model infer 3 Parse tool calls 4 Tools + errors 5 Append observe 6 Stop? goal / budget ↺ not done → next turn done ✓
One turn = six moves. Loop engineering is tuning each box — and the arrow at move 6 that decides loop-again vs. stop.

The seven levers

This is the map. Each card is a dimension you engineer independently. You won’t master them today — you’ll learn to see them, so when an agent misbehaves you know which knob to reach for.

LEVER 01

Control flow

Who decides the next step — your code (workflow) or the model (agent)? The master lever; the rest hang off it.

LEVER 02

Context

What enters the window each turn. Anthropic calls curating it the single biggest factor in agent performance.4

LEVER 03

Tools / ACI

The action space — what the model can do, and how clearly its tools are named, typed, and documented.3

LEVER 04

Feedback

The ground truth the loop reads back each turn to judge progress. No feedback → the loop is guessing.3

LEVER 05

Stop & budget

When the loop ends. Caps on turns/tokens — roughly 3–20 steps before handoff is a common rule.2

LEVER 06

Error recovery

How failures fold back in — compacted, not dumped — so the loop self-corrects instead of drowning.2

LEVER 07

State & ownership

Who holds the loop’s state and control — you, ideally: pause/resume, human-in-the-loop, stateless reducers.2

The one distinction that organises the map

If you remember one thing, remember Lever 01. Anthropic draws the line cleanly:

Workflows are systems where LLMs and tools are orchestrated through predefined code paths.
Agents are systems where LLMs dynamically direct their own processes and tool usage, maintaining control over how they accomplish tasks. — Anthropic, Building Effective Agents

This isn’t a binary you pick once — it’s a dial per decision. Most production “agents” sit far toward the workflow end: mostly ordinary, deterministic software with the model placed at a few high-leverage decision points.2 Anthropic’s blunt advice: “add complexity only when it demonstrably improves outcomes.”3 The five named workflow patterns — chaining, routing, parallelization, orchestrator–workers, evaluator–optimizer — are all points on the workflow side of this dial. We’ll dedicate a lesson to them.

Your win today

You can now do two things you couldn’t name an hour ago: (1) decompose any agent into the six-move turn and the seven levers, and (2) classify any design point as workflow (your code decides) or agent (the model decides). That vocabulary is the diagnostic kit for everything that follows. Lock it in below.

Recall check

Don’t re-read — retrieve. Effortful recall is what turns this map into memory you’ll still have next week. Answer from your head; feedback is instant.

Primary source — read this next

Anthropic — Building Effective Agents. The single highest-trust map of this territory: the workflow/agent split, the five patterns, and the “start simple” discipline. ~20 minutes. It is the backbone of this whole course.

I’m your teacher — use me. Anything fuzzy on the map? Want to test it against an agent you’ve actually built (your TradingAgents loop, say)? Ask me to pick which of the seven levers it’s weakest on. Just type your question in the chat.
📖 Glossary Next → Lesson 2: The Dial & the five patterns

Sources

  1. Thorsten Ball — How to Build an Agent (ampcode.com). “An LLM, a loop, and enough tokens.”
  2. HumanLayer — 12-Factor Agents. Factors 8 (own control flow), 9 (compact errors), 10 (small agents, 3–20 steps).
  3. Anthropic — Effective Context Engineering for AI Agents.