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.

Also available as a written course with quizzes
Book cover of Build Your Own Coding Agent by J. Owen: a stick figure carries a small box labeled while True away from a huge crate labeled AI coding agent, enterprise edition.

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})
  1. while Truethe loop
  2. self.brain.thinkthe brain: one stateless API call
  3. self.conversationthe memory: a plain Python list, for now
  4. if not thought.tool_callsthe only way out
  5. self._execute_toolthe tools: your code runs them, not the model
From Chapter 5’s nanocode.py, with the thinking display and comments removed. See the full file

Can 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
Trimmed log of a real run of the book’s agent, set up with three tools and a memory note to run pytest -q before and after a fix.
watch the loop work through it 1:12

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 free
The finished game, built by the agent you wrote.

The 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.FakeBrain runs 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

  1. 1The Zero Magic ManifestoRead free
  2. 2The Raw RequestRead free
  3. 3The Infinite LoopRead free
  4. 4The Universal Adapter

Part II: The Hands

  1. 5The Tool Protocol
  2. 6The Scratchpad (Memory)
  3. 7The Safety Harness (Plan Mode)
  4. 8The Context Pipeline (Map & Search)
  5. 9The Reality Check (Running Code)

Part III: The Frontier

  1. 10Going Dark (Local Models)
  2. 11The Extension (Web Search)
  3. 12The Capstone (Building a Game)Read free

Appendix

  1. AStreaming Responses
Build Your Own Coding Agent, The Course: cover, by J. Owen

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
Get the written course

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

One assistant response requests read_file with ID r1 and write_file with ID w2. The write raises an exception after the read succeeds. Which history can safely support the next inference?

About J. Owen

Over a decade building developer tools, CLI utilities, and backend platforms. This project brings that systems-engineering approach to AI agents: read the code, understand the behavior, and know where to put the breakpoint.

“If you can’t debug it with print(), it doesn’t belong in production.”

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.

Build Your Own Coding Agent book cover, by J. Owen

Get the book

The first chapter is free. So is the last. Read 4 chapters free

Leanpub

PDF, EPUB and web reader, with free updates and a 60‑day refund.

Buy on Leanpub

Amazon

Kindle, paperback and hardcover, from your local Amazon store. Kindle gets free updates.

Buy on Amazon

Want practice and quizzes too? Add the written course

Also in Japanese, Spanish and Simplified Chinese.