Leanpub Header

Skip to main content

Build Your Own Coding Agent

The Zero-Magic Guide to AI Agents in Pure Python

Skip the black-box frameworks. Build a production-grade AI coding agent from scratch in pure Python. Master the loop, own the code.

Minimum price

$19.99

$29.99

You pay

$29.99

Author earns

$23.99
$

...Or Buy With Credits!

You can get credits with a paid monthly or annual Reader Membership, or you can buy them here.
PDF
EPUB
WEB
About

About

About the Book

You are a software engineer who is skeptical of AI hype.

You have seen the demos. You have tried the frameworks. You have watched your LangChain app hallucinate its way into disaster. And you thought: "There has to be a better way."

There is.

This book is a rebellion against "magic." It teaches you to build a production-grade AI coding agent from scratch with pure Python. No LangChain. No Pydantic. No vector databases. Just code you can debug with print().

By the end, you will have built Nanocode, a terminal-based coding agent that:

  • Reads and writes files in your codebase (The Hands)
  • Executes shell commands and tests (The Feedback Loop)
  • Searches code with git grep (The Eyes)
  • Remembers context across sessions (The Memory)
  • Asks permission before dangerous operations (The Safety Harness)

You will learn that an agent is just four things:

  1. The Brain: A stateless API call (Claude, DeepSeek, or Ollama)
  2. The Tools: Python functions (Read, Write, Run)
  3. The Memory: A managed context window
  4. The Loop: A while True loop that ties it all together

Along the way, you will test everything. You will build a FakeBrain test double that allows you to run full pytest suites instantly—verifying complex agent logic without burning API credits.

The Final Exam: In the last chapter, you will use your agent to build a complete Snake game in Pygame without writing a single line of code yourself. You will speak the plan, and your agent will write, run, debug, and fix the code until the game is playable.

If you can read Python and want to understand what's actually happening inside AI agents, this book is for you.

Author

About the Author

Owen Ou

Owen Ou maintains `jq`, the JSON processor used by millions of developers daily. For more than a decade, he has built developer tools and platforms at CrowdStrike, Heroku, Salesforce, and Amazon.

He wrote this book to demystify AI agents for working engineers. If you can't debug it with `print()`, it doesn't belong in production.

Contents

Table of Contents

Preface

  1. Who This Book Is For
  2. What You Will Build
  3. Testing Approach
  4. Code Examples
  5. Conventions Used in This Book

Chapter 1: The Zero Magic Manifesto

  1. What is an Agent, Really?
  2. What We Are Building
  3. Project Setup
  4. The AgentStop Exception
  5. The Agent Class
  6. Defining Success with Tests
  7. The Main Loop
  8. Run It
  9. Wrapping Up

Chapter 2: The Raw Request

  1. Step 1: Get an API Key
  2. Step 2: The Vault (.env)
  3. Step 3: The Anatomy of a Request
  4. Step 4: The Code
  5. Step 5: Run It
  6. Troubleshooting
  7. Cleaning Up
  8. Wrapping Up

Chapter 3: The Infinite Loop

  1. The Illusion of Memory
  2. The Testing Problem
  3. Response Types
  4. The FakeBrain Pattern
  5. Defining Success
  6. The Claude Class
  7. The Agent Class (Updated)
  8. The Main Loop (Updated)
  9. Verify the Tests Pass
  10. Test the Memory
  11. The Context Window Problem
  12. Wrapping Up

Chapter 4: The Universal Adapter

  1. The Adapter Pattern
  2. HTTP Resilience
  3. The Brain Interface
  4. The FakeBrain (Updated)
  5. The Claude Brain (Refactored)
  6. The DeepSeek Brain
  7. The BRAINS Registry
  8. The Agent Class (Updated)
  9. Tests for Multi-Brain Support
  10. The Main Loop (Updated)
  11. Setting Up DeepSeek
  12. Try It
  13. Why This Matters
  14. Wrapping Up

Chapter 5: The Tool Protocol

  1. How Tools Actually Work
  2. Defining the Tool Interface
  3. The ReadFile Tool
  4. The WriteFile Tool
  5. Tool Helpers
  6. Updating the Thought Class
  7. Updating the Claude Class
  8. The Agent Class with Tools
  9. The Main Loop
  10. Test It
  11. Wrapping Up

Chapter 6: The Scratchpad (Memory)

  1. The “Zero Magic” Memory
  2. The Memory Class
  3. The ToolContext Class
  4. The SaveMemory Tool
  5. Updating the Claude Class
  6. Crafting the System Prompt
  7. Updating the Agent Class
  8. The Main Loop (Updated)
  9. Testing Persistence
  10. Why This is Powerful
  11. Wrapping Up

Chapter 7: The Safety Harness (Planner Mode)

  1. The Concept
  2. Tests First
  3. Extending ToolContext
  4. The Guarded WriteFile Tool
  5. The Agent Class (Updated)
  6. The Main Loop (Updated)
  7. Testing the Harness
  8. The Psychology of the “Plan”
  9. Wrapping Up

Chapter 8: The Context Pipeline (Map & Search)

  1. The ListFiles Tool
  2. The SearchCodebase Tool
  3. Update the Tools List
  4. The “Zoom In” Test
  5. Wait, is this RAG?
  6. The Architectural Significance
  7. Wrapping Up

Chapter 9: The Reality Check (Running Code)

  1. The Feedback Loop
  2. Tests First
  3. The RunCommand Tool
  4. The Interactive Trap
  5. The Self-Healing Demo
  6. The TDD Workflow
  7. The Surgical Edit
  8. Why This Changes Everything
  9. Security Considerations
  10. Wrapping Up

Chapter 10: Going Dark (Local Models)

  1. The Trade-off
  2. Installing Ollama
  3. The Ollama Brain Class
  4. Running with Ollama
  5. The “Infinite Loop” Experiment
  6. The Practical Differences
  7. The Hybrid Workflow
  8. Model Selection
  9. Troubleshooting Ollama
  10. Wrapping Up

Chapter 11: The Extension (Web Search)

  1. Step 1: The Meta-Prompt
  2. Step 2: The Surgery
  3. Step 3: The Reference Implementation
  4. Step 4: The Test
  5. Why This Matters
  6. Wrapping Up

Chapter 12: The Capstone (Building a Game)

  1. Step 1: Preparation
  2. Step 2: The Architect (Plan Mode)
  3. Step 3: The Builder (Act Mode)
  4. Step 4: The Reality Check
  5. Step 5: The Pivot (Feature Creep)
  6. The Result
  7. Wrapping Up
  8. Epilogue: The End of the Beginning

Acknowledgments

Get the free sample chapters

Click the buttons to get the free sample in PDF or EPUB, or read the sample online here

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.

You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!

So, there's no reason not to click the Add to Cart button, is there?

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $14 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub