Not magic.
Just a while loop.
You’ve used coding agents. Now build one you can debug and extend: one Python file, about 800 lines, with no agent framework.
- Free ebook updates
- Full source on GitHub
- Reader reviews on Amazon
The loop is 16 lines.
This is the real agent loop from Chapter 5. An agent is four things, and all four are here.
while True:
thought = self.brain.think(self.conversation)
self.conversation.append({"role": "assistant", "content": thought.raw_content})
if thought.text:
output_parts.append(thought.text)
if not thought.tool_calls:
break
tool_results = []
for tool_call in thought.tool_calls:
result = self._execute_tool(tool_call.name, tool_call.args)
tool_results.append({
"type": "tool_result",
"tool_use_id": tool_call.id,
"content": result
})
self.conversation.append({"role": "user", "content": tool_results}) while Truethe loopself.brain.thinkthe brain: one stateless API callself.conversationthe memory: a plain Python list, for nowif not thought.tool_callsthe only way outself._execute_toolthe tools: your code runs them, not the model
nanocode.py, with the thinking display and comments removed. See the full fileCan it actually code?
Give it a failing test.
The prompt: “Fix average.py so the test passes.” Here’s what it did.
read_file average.py
read_file test_average.py
run_command pytest -q
E assert 12 == 4
1 failed in 0.02s
edit_file average.py
- return sum(values)
+ return sum(values) / len(values)
run_command pytest -q
1 passed in 0.00s pytest -q before and after a fix.Or give it a whole game.
The capstone has one rule: you can’t write a line of Python. You describe a Snake game in English, play what it builds, and tell it what’s wrong. The agent plans, writes, runs and fixes the code until the game works.
it can’t see the window, so you’re the QA team
Read the capstone chapter freeThe book is about the other 800.
The final agent is about 800 lines. Most of them exist because of problems like these.
the part the demos skip
- Testing costs money.
FakeBrainruns the whole suite with no API calls.Chapter 3 - The model starts editing while you’re still planning.Plan mode hides act-only tools such as
write_file, and refuses them at dispatch.Chapter 7 - The agent can’t find anything in your codebase.Listing and keyword search, with no vector database.Chapter 8
- A command waits for input that never comes.A timeout stops the wait and warns the model about what may still be running.Chapter 9
- The context window fills up.Compaction summarizes the session so the work can continue.Chapter 9
- The API meter makes you nervous.Run the brain on your own laptop with Ollama.Chapter 10
What’s in the book
Twelve chapters and an appendix. You start with one HTTP request and finish with an agent that can build a game. Chapters 1, 2, 3 and 12 are free, about a quarter of the book.
Part I: The Brain
Part II: The Hands
- 5The Tool Protocol
- 6The Scratchpad (Memory)
- 7The Safety Harness (Plan Mode)
- 8The Context Pipeline (Map & Search)
- 9The Reality Check (Running Code)
Part III: The Frontier
- 10Going Dark (Local Models)
- 11The Extension (Web Search)
- 12The Capstone (Building a Game)Read free
Appendix
- AStreaming Responses
Read it. Then test what stuck.
The written course builds 227 questions into the book’s lessons. They check whether you can reason through tool failures, conversation history and context limits, not just whether you read about them.
- 29 practice sets and 13 quizzes, 227 questions in all
- Practice sets show the answers as you go, and quizzes show the ones you missed
Sold separately on Leanpub. It includes every lesson in the book, so you can start with the book and add it later, or go straight to the course.
Try a real question from the course’s Chapter 5 quiz
Questions before you buy
The code is free on GitHub. Why buy the book?
The repo shows what the final code looks like. The book walks you through why each piece exists, what breaks without it, and how to test it. The code is MIT-licensed, so read it first and decide.
Why not just use Claude Code or Cursor?
Keep using them. This book is for understanding how tools like that work, so you can debug one, extend one, or build your own.
Is this a toy project?
It’s small on purpose, and it does real work: the agent reads and edits files, runs commands, searches code and the web, keeps memory across sessions, and compacts its context. It’s built to teach rather than to ship, with no filesystem or shell sandbox and no per-operation approval, so run it only where that’s acceptable.
Is it up to date?
The book is updated when models and APIs change. Leanpub and Kindle buyers get those updates free; print editions don’t. The repo’s README lists the exact model and Python versions the current edition uses.
Do I need AI or machine-learning experience?
No. You should be comfortable writing Python. AI concepts are explained from the ground up, with concrete examples and physical metaphors.
Which models does it work with?
Claude, DeepSeek, and local models through Ollama. Chapter 4’s adapter shows how to add another provider.
What do I need to get started?
Python, a few small packages, and an API key for a hosted model or Ollama for a local one. Model-provider usage is billed separately from the book or course, and the chapter tests run without live API calls. Exact versions are in the repo’s README.
What’s the refund policy?
Leanpub offers a 100% refund within 60 days of purchase. Amazon purchases follow Amazon’s policy.
Should I get the book or the course?
Start with the book if you want to read and build. Add the course when you want practice checks and quizzes to test what stuck. It includes every lesson in the book, so if you already know you want the practice, you can go straight to it.
Get the book
The first chapter is free. So is the last. Read 4 chapters free
Amazon
Kindle, paperback and hardcover, from your local Amazon store. Kindle gets free updates.
Buy on AmazonWant practice and quizzes too? Add the written course
Also in Japanese, Spanish and Simplified Chinese.