Lab · The Agent Loop

The Loop.

Don't read about the agent loop — run it. Step a model through plan → act → observe: it thinks, calls a tool, reads the result, and loops until it can answer. Watch the context window fill each turn, and compaction fold old turns away before it overflows.

Plan → Act → Observe · context window · compaction
PLAN

The model reads its context and decides the next move — which tool to call, or that it's done.

ACT

It emits a structured tool call. The harness — not the model — executes it.

OBSERVE

The tool's result is appended to the context, and the loop runs again with more to go on.

COMPACT

When the window is about to overflow, old turns are summarized so the agent stays under budget.

agent-loop.sim — task: "population of the capital of France × 2"
tool call ▸ ◂ observation MODEL plans & decides TOOLS search · calc ready
CONTEXT WINDOW 8 / 36 tokens
system thought observation summary final
Ready

The agent gets a task and an empty scratchpad — just the system prompt in context. Press Step to run the loop one phase at a time, or Play to watch it run.

0
Step
0
Tool calls
8/36
Tokens
0
Compactions

Why the loop is the whole game

A single prompt gives you one answer. An agent gives you many — because a harness wraps the model in a loop and feeds it back its own results. Everything hard about building agents lives in that loop: deciding when to call a tool, executing it safely, and — the part everyone underestimates — keeping the growing context under control.

01

Plan

The model reads the whole context and produces a thought plus a structured tool call — or decides it has enough to answer.

02

Act

The harness executes the tool call. The model never runs code itself; it only asks, and your code decides whether and how to run it.

03

Observe

The result is appended to the message history as an observation, and the loop repeats — now with one more fact to reason over.

04

Compact

Because every turn re-sends the full history, the window fills. When it's about to overflow, older turns (especially bulky tool output) are summarized so the agent can keep going.

Watch the context bar in the simulator: the thought and observation blocks stack up every turn. Right before the window overflows, the old blocks collapse into a single amber summary — that's compaction buying the agent room to finish.

Check yourself

1 · In the agent loop, who actually executes a tool call?

The model only emits a structured request. The harness executes it and appends the observation — which is exactly why sandboxing and permissions are the harness's job, not the model's.

2 · Why does the context window fill up as the loop runs?

Each request carries the full message history, so thoughts and (often bulky) tool outputs accumulate turn by turn until something has to give.

3 · What does compaction do when the window is about to overflow?

Compaction folds bulky old turns into a short summary and keeps the recent, relevant context intact — preserving the task thread while freeing tokens.

Questions

Is this how Claude Code, Cursor, and other agents really work?

Yes — at the core. Real harnesses add streaming, parallel tool calls, permissions, subagents and retries, but the beating heart is this loop: plan, act, observe, repeat, with context management keeping it all under budget.

What makes a good stopping condition?

The loop ends when the model returns a final answer, or a guard trips: a max step count, a token/cost budget, or a stuck-detector that spots the agent repeating itself. Without them an agent can loop forever or quit too early.

Why not just use a bigger context window?

Bigger windows help, but cost and latency grow with every token re-sent each turn, and models attend less reliably to the middle of very long contexts. Compaction keeps the agent both affordable and sharp.

You just ran an agent.

Plan → act → observe, with the context under control. That's the runtime behind every AI harness.

Finished this one? 0 / 12 Labs done

Explore the topic

See this alongside everything else on the same subject — handbooks, system designs, challenges and tools, in one place.

More Labs