Leanpub Header

Skip to main content

Build Your Own Coding Agent (The Course)

The Zero-Magic Guide to AI Agents in Pure Python

The instructor has published 100% of this course.Last updated on 2026-11-05

Skip the black-box frameworks. Build a working AI coding agent from scratch in pure Python. Add tools and memory, use cloud or local models, and test it with pytest. All in one file.

Minimum price

$129

$179

You pay

Author earns

$

Also available for 1 course credit with a Learner Membership

PDF
EPUB
WEB
About

About

About the Course

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 course is a rebellion against "magic." It teaches you to build a working AI coding-agent foundation 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, writes, and surgically edits files in your codebase
  • Executes shell commands and self-corrects from errors
  • Maps and searches code with pure Python
  • Remembers context across sessions via a persistent scratchpad
  • Separates planning from execution with plan and act modes
  • Searches the web for current information


Nanocode is a teaching implementation. Act mode runs tools without per-operation approval or a filesystem/shell sandbox. 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, Edit, Run, Search)
  3. The Memory - a self-modifying Markdown file injected as the system prompt
  4. The Loop - a while True that ties it all together

Along the way, you will test everything. You will build a FakeBrain test double that lets you run full pytest suites instantly - verifying complex agent logic without burning API credits. You will swap between cloud and local models with a single command, running the brain on your own laptop for free via Ollama.

In the last lesson, you will use your agent to build a complete Snake game in Pygame without writing a single line of code yourself. You speak the plan; the agent writes, runs, debugs, and fixes the code until the game is playable.

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

Instructor

About the Instructor

J. Owen

J. Owen has spent over a decade building developer tools, CLI utilities, and backend platforms used by millions of engineers daily.

Material

Course Material

  • Preface

  • Who This Course Is For

  • What You Will Build

  • Testing Approach

  • Code Examples

  • Working Through the Course

  • Conventions Used in This Course

  • Part I: The Brain

  • Chapter 1: The Zero Magic Manifesto

  • What is an Agent, Really?

  • Practice: What is an Agent, Really?

  • What We Are Building

  • Project Setup

  • 1. Initialize the Project

  • 2. Create a Virtual Environment

  • 3. Install Dependencies

  • 4. Secure Your Keys

  • The AgentStop Exception

  • The Agent Class

  • Defining Success with Tests

  • The Main Loop

  • Run It

  • Wrapping Up

  • Chapter 1 quiz

    3 attempts allowed

  • Chapter 2: The Raw Request

  • Get an API Key

  • The Vault (.env)

  • The Anatomy of a Request

  • The Headers

  • The Payload

  • The Code

  • Practice: The Code

  • Run It

  • Troubleshooting

  • Cleaning Up

  • Wrapping Up

  • Chapter 2 quiz

    3 attempts allowed

  • Chapter 3: The Infinite Loop

  • The Illusion of Memory

  • The Testing Problem

  • Practice: The Testing Problem

  • Response Types

  • The FakeBrain Pattern

  • Defining Success

  • The Claude Class

  • Practice: The Claude Class

  • The Agent Class (Updated)

  • Practice: The Agent Class (Updated)

  • The Main Loop (Updated)

  • Verify the Tests Pass

  • Test the Memory

  • The Context Window Problem

  • Practice: The Context Window Problem

  • Wrapping Up

  • Chapter 3 quiz

    3 attempts allowed

  • Chapter 4: The Universal Adapter

  • The Adapter Pattern

  • HTTP Resilience

  • Practice: HTTP Resilience

  • The Brain Interface

  • The FakeBrain (Updated)

  • The Claude Brain (Refactored)

  • The DeepSeek Brain

  • The BRAINS Registry

  • The Agent Class (Updated)

  • Tests for Multi-Brain Support

  • The Main Loop (Updated)

  • Setting Up DeepSeek

  • Try It

  • “We Just Moved Code Around”

  • Wrapping Up

  • Chapter 4 quiz

    3 attempts allowed

  • Part II: The Hands

  • Chapter 5: The Tool Protocol

  • How Tools Actually Work

  • Practice: How Tools Actually Work

  • Defining the Tool Interface

  • The ReadFile Tool

  • The WriteFile Tool

  • Tool Helpers

  • Updating the Thought Class

  • Updating the Claude Class

  • Practice: Updating the Claude Class

  • The Agent Class with Tools

  • Practice: The Agent Class with Tools

  • The Main Loop

  • Test It

  • Wrapping Up

  • Chapter 5 quiz

    3 attempts allowed

  • Chapter 6: The Scratchpad (Memory)

  • The “Zero Magic” Memory

  • The Memory Class

  • The ToolContext Class

  • The SaveMemory Tool

  • Practice: The SaveMemory Tool

  • Updating the Claude Class

  • Crafting the System Prompt

  • Practice: Crafting the System Prompt

  • Updating the Agent Class

  • The Main Loop (Updated)

  • Testing Persistence

  • Practice: Testing Persistence

  • Wrapping Up

  • Chapter 6 quiz

    3 attempts allowed

  • Chapter 7: The Safety Harness (Plan Mode)

  • The Concept

  • Practice: The Concept

  • Tests First

  • Practice: Tests First

  • The WritePlan Tool

  • One List, Two Views

  • Telling the Brain What Mode It’s In

  • The Agent Class (Updated)

  • The Main Loop (Updated)

  • Testing the Harness

  • The Psychology of the “Plan”

  • Practice: The Psychology of the “Plan”

  • Wrapping Up

  • Chapter 7 quiz

    3 attempts allowed

  • Chapter 8: The Context Pipeline (Map & Search)

  • The ListFiles Tool

  • The SearchCodebase Tool

  • Update the Tools List

  • The “Zoom In” Test

  • Wait, is this RAG?

  • Practice: Wait, is this RAG?

  • Wrapping Up

  • Chapter 8 quiz

    3 attempts allowed

  • Chapter 9: The Reality Check (Running Code)

  • The Feedback Loop

  • Practice: The Feedback Loop

  • Tests First

  • Practice: Tests First

  • The RunCommand Tool

  • The Interactive Trap

  • Practice: The Interactive Trap

  • The Self-Healing Demo

  • The TDD Workflow

  • The Surgical Edit

  • The Closed Loop

  • Hardening the Loop

  • Context Compaction

  • Practice: Context Compaction

  • Security Considerations

  • Practice: Security Considerations

  • Wrapping Up

  • Chapter 9 quiz

    3 attempts allowed

  • Part III: The Frontier

  • Chapter 10: Going Dark (Local Models)

  • The Trade-off

  • Practice: The Trade-off

  • Installing Ollama

  • The Ollama Brain Class

  • Practice: The Ollama Brain Class

  • Running with Ollama

  • The “Infinite Loop” Experiment

  • The Practical Differences

  • Practice: The Practical Differences

  • The Hybrid Workflow

  • Practice: The Hybrid Workflow

  • Model Selection

  • Troubleshooting Ollama

  • Wrapping Up

  • Chapter 10 quiz

    3 attempts allowed

  • Chapter 11: The Extension (Web Search)

  • Step 1: The Meta-Prompt

  • Step 2: The Surgery

  • Step 3: The Reference Implementation

  • Step 4: The Tests

  • Self-Modification

  • Wrapping Up

  • Chapter 11 quiz

    3 attempts allowed

  • Chapter 12: The Capstone (Building a Game)

  • Step 1: Preparation

  • Step 2: The Architect (Plan Mode)

  • Step 3: The Builder (Act Mode)

  • Step 4: The Reality Check

  • Step 5: The Pivot (Feature Creep)

  • Check the Result

  • What Goes Wrong

  • Practice: What Goes Wrong

  • Wrapping Up

  • Epilogue

  • Chapter 12 quiz

    3 attempts allowed

  • Appendix A: Streaming Responses

  • How Streaming Works

  • The Implementation

  • Practice: The Implementation

  • What Changed from Chapter 11

  • Practice: What Changed from Chapter 11

  • The Trade-Off

  • Running the Code

  • Streaming quiz

    3 attempts allowed

  • Acknowledgments

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.

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.)

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