Agents · 5 Interactives

A Brain in a Loop, With Hands

A chatbot answers once and stops. An agent runs a loop: think → call a tool → read the result → think again — until the job is done. That loop is where the magic lives, and where everything goes wrong. Run it yourself below, then break it on purpose.

The loop You are the router Retry & reroute The infinite retry The error cascade
EP 01

Think, act, look — one lap around the agent loop

Here is a simulated agent solving one question that needs three different tools: a file reader, a calculator, and web search. Step through it. Watch two things: the loop phase (top) and the scratchpad — the notes the agent accumulates, which are the only reason step 7 knows what step 2 found.

Task: “What was ACME’s average daily revenue in March, in euros?”
Press Next step to run the loop.
Scratchpad — empty
step 0 / 8 · 0 tool calls

Simulated agent with a scripted trace — real agents generate each THINK with an LLM, but the loop structure (and the scratchpad trick) is exactly this.

Why it matters: the model alone can't read your files, can't do exact math, and doesn't know today's exchange rate. Each tool patches one weakness — and the loop lets it chain them. This is the ReAct pattern that powers Claude Code, ChatGPT plugins, and every "AI agent" you've heard of.
EP 02

You are the router — pick the right tool or pay for it

At every THINK step the agent must choose which tool to call next. Now it's your job. The question needs exactly two facts. Route well and you're done in 2 calls; route badly and you burn money on calls that teach you nothing. (Every call costs real tokens.)

Q: Is 1,847 × 362 larger than the population of Iceland?
fact needed: the product = ? fact needed: Iceland's population = ?
Pick a tool. A good router asks: which tool actually holds this fact?
tool calls: 0 (2 needed) · wasted: 0 · est. cost $0.0000

Cost estimated at ~900 tokens per tool round-trip at $3 / million tokens — small per call, brutal at scale.

The lesson: math lives in the calculator, fresh facts live on the web, and neither lives on your disk — file_read can only waste a step here. Routing is most of what an agent's THINK step does, and bad routing is the #1 silent cost leak in production agents.
EP 03

The tool lied — retry, reroute, recover

Tools fail constantly: timeouts, empty payloads, HTML error pages where a number should be. The loop's superpower is that the agent reads the result before using it — if it bothers to check. Run both agents against the same flaky stock-price API and compare.

Task: “What is ACME trading at right now?” — the get_price API is having a bad day.

The validation here is a real check running in this page: “does the output parse as a price?” Real agents validate with schemas, regexes, or a second LLM pass.

The pattern: validate → retry once → reroute to a different tool, never retry forever. The naive agent's sin isn't the broken API — it's pasting an error page into its answer as if it were data. Production frameworks (LangChain, Claude's tool use) surface tool errors back to the model precisely so this recovery loop can happen.
EP 04

The infinite retry — why every agent has a kill switch

Same tool call + same broken tool = same error, forever. A loop with no exit condition will happily re-call a dead database until your credit card melts. Run it with no limit and watch the meter; then set max iterations and see the guardrail fire.

idle

The tool is db_query and the database is down. It will not come back during this run — and the agent has no way to know that.

Why max-iterations exists: an LLM in a loop has no built-in boredom. Identical input → (roughly) identical output, so attempt 138 is exactly as doomed as attempt 2. Every serious framework caps iterations and budget — the graceful failure at attempt 5 is a feature, not a bug.
EP 05

One poisoned number — the error cascade

The loop's other failure mode is quieter and scarier: a tool returns plausible garbage, the agent believes it, and every later step builds on the lie. Run the healthy pipeline, then the corrupted one — and then turn on the sanity check and run it corrupted again.

① file_read: stock count
② calc: reorder = 500 − stock
③ calc: cost @ $12.50
④ place_order
Warehouse task: keep 500 widgets in stock. The corrupted read returns the company-wide total (10,000) instead of this warehouse's count (100).

The dollar figures are computed live from the pipeline's own numbers — corrupt step ① and watch the damage compound through ②③④.

The rule: in a loop, errors don't add — they multiply. One believable-but-wrong number early beats ten obvious crashes, because crashes stop the loop and lies steer it. This is why real agent stacks put schema validation and bounds checks on every tool boundary, not just try/catch.
Keep playing
Reinforcement Learning
Multi-Agent Emergence
Three dumb rules make a flock. Nobody wrote it.
Reinforcement Learning
Memory vs Reactive Agents
Without memory, every request is Groundhog Day. Teach an agent a fact and wa…
','null']; var BACKUP='ACME: $42.17 (delayed quote)'; function looksLikePrice(s){return /\$?\d+(\.\d+)?/.test(s)&&!/get_price("ACME") — attempt 1')}, function(){row('obs',esc(FLAKY[0]))}, function(){row('done','✗ "ACME is currently trading at '+esc(FLAKY[0])+' dollars." It never looked at what came back — it just used it. 1 call, 0 validation, garbage out.');stat.textContent='naive agent: 1 call, answer built on an error page';running=false} ]; seq.forEach(function(f,i){setTimeout(f,i*550)}); } function careful(){ if(running)return;running=true;clearLog();stat.textContent=''; var attempts=0; var seq=[ function(){attempts++;row('tool','get_price("ACME") — attempt 1')}, function(){row('obs',esc(FLAKY[0]))}, function(){row('err','does it parse as a price? No — that is an HTML error page. Discard, retry once.')}, function(){attempts++;row('tool','get_price("ACME") — attempt 2')}, function(){row('obs',esc(FLAKY[1]))}, function(){row('err','"null" is not a price either. Two strikes on the same tool → stop retrying, reroute.')}, function(){attempts++;row('tool','quote_search("ACME stock price") — backup route')}, function(){row('obs',esc(BACKUP))}, function(){row('ok','parses as a price ('+(looksLikePrice(BACKUP)?'✓ $42.17':'?')+') — safe to use.')}, function(){row('done','✓ "ACME is trading around $42.17 (delayed quote; the realtime API is down)."');stat.textContent='careful agent: '+attempts+' calls · 2 rejected outputs · 1 reroute · honest caveat included';running=false} ]; seq.forEach(function(f,i){setTimeout(f,i*500)}); } document.getElementById('ep3Naive').addEventListener('click',naive); document.getElementById('ep3Careful').addEventListener('click',careful); })();