PMaker home
Think, act, observe, think again — each round writes its result into the contextThinkRead the context, decide: call a tool or answer directlyActExecute the tool call and get a resultObserveWrite the result back into the context — don't skip thisThink againRe-decide with one more round of information: continue or wrap upAll of the loop's memory lives in the context: if the window holds it, it remembers; if it gets squeezed out, it forgets

An agent isn't "doing the work by itself" — it spins inside a loop you built. All of that loop's memory lives in the context.

The Agent Loop

Think, act, observe, think again. That's how anything running dozens of rounds spins.

The whole secret behind "the agent works by itself" is one word: loop. Think, act, observe, think again — spin it a few times and it starts to look like it's completing a task. Every agent framework you've used is just a packaged version of this loop. react-paper

Symptoms you'll recognize:

  • It stops halfway and you blame "the AI isn't smart enough," when really nobody gave it a stopping rule.
  • It retries the same action on the same thing because the result never made it back into the context.
  • You discover after the fact that it ran dozens of rounds and the bill is scary.

The four steps of the loop

  • Think. The model looks at the current context and decides what to do next: call a tool, or answer directly.
  • Act. Your code executes the tool call and gets a result.
  • Observe. The result is written into the context as text — this step cannot be skipped. The model must "see" the result before it can continue.
  • Think again. The model reads the context once more, now with the latest round added, and decides whether to continue or wrap up.

That's all there is. The "persistence" and "self-correction" you observe are really just the context getting longer while the model re-decides each round with more information.

All memory lives here

The most overlooked point in a loop: the agent has no other memory. Everything it knows lives in the context.

It "remembers" what it looked up, how many times it failed, what parameters it changed — not because a brain stored it, but because each round's record piles up in the context. If the window can hold it, the agent remembers; if the middle gets squeezed out, it "forgets."

Two consequences follow directly:

  • The way you organize the context is the quality of the agent's memory. Whether to keep each tool result, whether to compact, and what to put first directly decide whether it falls apart halfway.
  • More rounds mean each round is more expensive and slower, because every round's input includes all previous rounds. An agent that runs 20 rounds thinks on a context of thousands of tokens by round 20 — and that money compounds.

How to stop

A loop needs explicit stopping rules, or it's a car without brakes. Three things are required:

  • A success condition. "Task done, give the final answer." The model must recognize completion, and you must let it stop and answer instead of always planning the next step.
  • A maximum round count. A hard cap. Over N rounds, cut it off and error or degrade. Without this, one incident can burn thousands in a loop that never converges.
  • Timeouts. A per-tool-call timeout and a whole-loop timeout. A stuck step shouldn't stall everything.

One rule that's often missed: retries need perspective. When you retry after a failure, retry with the failure reason in view, not the same request resent. If the model can see "this was tried, it failed, the reason was X," it has a chance to try something different.

Cost and round count

Every round costs money: the input (all history), the output (this round's decision), and the tool execution. Do the math: if each round averages 2,000 input tokens and 300 output tokens, 15 rounds means 30,000 input tokens and the output on top. A single loop often bills an order of magnitude more than a plain question-answer exchange.

The way to save isn't cutting rounds; it's making each round smaller. Keep only the necessary fields from tool returns, compact old records, and don't stuff irrelevant content into the context. Run the rounds you need to run, but squeeze the per-round cost.

Final point: an agent's ceiling is the combination of model, tools, and context organization. The loop just threads them together. Threaded well, ordinary tools can finish complex work; threaded badly, even a strong model starts spinning in place by round five. anthropic-agents

References

  1. ReAct: Synergizing Reasoning and Acting in Language Models
  2. Building effective agents — Anthropic