This is the companion code repository for Build Your Own Coding Agent — a book that teaches you how to build an autonomous AI coding agent from scratch using pure Python.
No LangChain. No vector databases. No "orchestration frameworks." Just requests, subprocess, and code you can debug with print().
Nanocode is a terminal-based AI coding agent that can:
- Read, write, and edit files in your codebase
- Execute shell commands and iterate on errors
- Search code across your project
- Remember context across sessions
- Search the web for current information
- Run on Claude, DeepSeek, or local models via Ollama
By the end of the book, you'll have a ~700-line Python script that rivals commercial coding assistants — and you'll understand every line because you wrote it.
Each chXX/ folder contains a complete, runnable snapshot of the code at the end of that chapter:
| Chapter | What You Build |
|---|---|
ch01/ |
Event loop — the skeleton |
ch02/ |
API test — first contact with Claude |
ch03/ |
Stateful chatbot — memory within a session |
ch04/ |
Multi-provider — Claude + DeepSeek |
ch05/ |
Tools — read and write files |
ch06/ |
Persistent memory — cross-session context |
ch07/ |
Plan mode — safety harness |
ch08/ |
Codebase context — list and search files |
ch09/ |
Feedback loop — run commands, fix errors |
ch10/ |
Local models — Ollama integration |
ch11/ |
Web search — DuckDuckGo tool |
ch12/ |
Capstone — build a Snake game with AI |
appendix/ |
Streaming — real-time output with extended thinking |
# Clone the repo
git clone https://github.com/owenthereal/build-your-own-coding-agent.git
cd build-your-own-coding-agent
# Create virtual environment
python3 -m venv venv
source venv/bin/activate # Mac/Linux
# venv\Scripts\activate # Windows
# Install dependencies
pip install -r requirements.txt
# Set up API key
cp .env.example .env
# Edit .env and add your ANTHROPIC_API_KEY# Chapter 1: Event loop (no AI yet)
python ch01/nanocode.py
# Chapter 3+: Needs API key in .env
python ch03/nanocode.py
# Chapter 10+: Can use local models
NANOCODE_BRAIN=ollama python ch10/nanocode.py
# Run tests (no API key needed)
cd ch09
pytest- Python 3.10+
- An Anthropic API key (get one at console.anthropic.com)
- Optional: DeepSeek API key, Ollama for local models
The book's reference implementation is Python, but the concepts — the loop, the tools, the brain abstraction — are language-agnostic. Community ports live in the contrib/ directory:
| Language | Directory | Contributor |
|---|---|---|
| Your language here | — | See CONTRIBUTING.md |
This code accompanies the book Build Your Own Coding Agent, available on:
The book follows a "Zero Magic" philosophy — every abstraction is explained with physical metaphors, and every line of code serves a purpose you'll understand.
MIT License. See LICENSE for details.