Teaching a model to act: a ReAct agent loop from scratch

For four weeks my model could read, remember, and retrieve — but it couldn’t do anything. This week
I gave it hands. An agent loop lets a model use tools: each turn it writes a thought and either calls a
tool or gives a final answer; my loop runs the tool, hands back the result, and asks again. Suddenly a
model that can’t multiply in its head or recall a fact it never learned can answer questions that need
both — by acting, observing, and acting again. Building the loop took an afternoon. The interesting
part was discovering, concretely, where the magic actually lives.

What I built

A ReAct runtime: a loop over two tools (calculator, lookup) that parses the model’s output,
dispatches the requested tool, feeds the observation back, and repeats until an answer. Ask it “What
is the number of legs on a spider times 12?”
— a question needing a fact and a calculation — and
with a competent policy it chains them:

Action: lookup[legs on a spider]   -> Observation: 8
Action: calculator[8 * 12]         -> Observation: 96
=> 96

Two tools, one right answer, from a model that can neither recall nor multiply on its own.

The concepts that made it work

  • An agent is a tiny interpreter. parse → dispatch → observe → loop. It’s a control loop over a
    dispatch table — much closer to a request router than to anything that feels like intelligence. The
    cleverness is entirely in the policy; the loop is plumbing.
  • Tools turn a frozen model into an open system. The model’s weights are fixed, but through tools
    it can reach a calculator, a database, the web — anything you expose. The weights supply the
    reasoning; the tools supply the facts and actions.
  • Structured output is the brittle seam. My parser has to recover a clean tool[arg] from free
    text. When the text doesn’t parse, the agent stalls. This one seam is why production systems use
    constrained decoding / function-calling — the model is forced to emit valid JSON or a valid call,
    so parsing can’t fail.

The thing that bit me

I built the loop, tested it with a scripted policy — it worked perfectly — and then plugged in my
actual model, GPT-2 small, expecting at least a wobbly attempt. It produced:

=> Thought: 12

No action. No tool call. It didn’t follow the protocol at all — it blurted a token and stopped. For a
moment I assumed my loop was broken. It wasn’t. The loop was fine; the model simply can’t do this.
Following “Thought → Action → Observation” across turns is a learned skill that a base language model
never acquired — it takes instruction tuning. That was the clarifying moment of the whole sprint: the
loop is easy, and the policy is the entire ballgame. Every impressive agent demo you’ve seen is maybe
20% this loop and 80% a model trained to drive it.

Tradeoffs & the road not taken

  • Single agent, not a crew. One model, one loop. The next step up is multi-agent — a planner that
    delegates to workers, with shared memory and budgets — which is orchestration on top of this loop.
  • A scripted policy for the tests. To prove the runtime is correct I drive it with a deterministic
    stand-in; the real model is only in the (honest) demo. You test the plumbing and the brain separately
    — the same componentised-evaluation lesson RAG taught me last week.
  • No constrained decoding. The honest fix for GPT-2’s flailing, and the obvious stretch — force
    valid tool syntax so the parser can’t be surprised.

Where this sits in the system

Weeks 1–4 built a model that reads and retrieves; this week it acts. It’s also where my toy model
finally hits its ceiling: a working runtime that only a capable policy can drive — which is the honest
argument for learning the parts I’ve been deferring (running a real instruction-tuned model locally;
eventually, fine-tuning one). Under the hood, this loop is what LangGraph, CrewAI, and MCP all wrap. Next
I go back to systems ground: distributed inference — replicas, routing, parallelism — reusing the
Raft and sharding muscle from the last series. Same spine as always: one system, a layer at a time.


Part of a series building a modern AI stack from scratch, told visually. (Last time: RAG from scratch.)