> **Try the finished agent first โ 5 minutes, $0:**
>
> ```bash
> git clone https://github.com/decodingai-magazine/building-a-coding-agent-from-scratch-course.git
> cd building-a-coding-agent-from-scratch-course
> make install
> cp .env.example .env # set LLM API key
> uv run decode
> ```
>
> Then type `/demo-` and pick a demo. See [what they do](#-see-it-work) below. [Full setup guide.](#-running-the-code)
Type /demo- and the six demos are one keystroke away.
## ๐ About This Course
In [LangChain's Terminal-Bench experiment](https://www.langchain.com/blog/the-anatomy-of-an-agent-harness), changing only the harness (with the same model) moved a coding agent from ~30th place into the top 5: the harness, not the model, is what makes a coding agent good.
### The agent is ~20 lines. The course is everything else, known as the harness.
```python
agent = Agent(
build_model(settings.llm_provider), # gemini | openrouter | modal
deps_type=AgentDeps, # cwd, event sink, permission gate
output_type=[str, DeferredToolRequests], # final answer, or tools paused for approval
)
register_tools(agent) # read, edit, bash, grep, ...
async with agent.iter(prompt, message_history=history) as run:
async for node in run: # model request โ tool calls โ repeat
stream_events(node)
```
That's the _entire_ tool-calling agent. Everything else in this repo (the tools, skills, permission layer, sandbox, steering queue, memory, compaction, session recording & replay, remote execution, subagent fan-out, and evals) **is the harness**. That's what you're here to build.
A fresh session powered by Qwen3.6-35B hosted on Modal
We spent months under the hood of Claude Code (via its leaked source), [OpenCode](https://github.com/anomalyco/opencode), [Pi](https://github.com/earendil-works/pi), and [Aider](https://github.com/aider-ai/aider), then distilled what we learned into 8 articles and 6 videos where you'll build **decode**, your own coding agent, from scratch. One headless core hooked to two modes: an interactive TUI and Modal serverless functions running N copies in parallel, fired by CLI, webhook, or cron.
> [!WARNING]
> Building a coding harness from scratch can make you dangerously good at building any other AI product, whether in finance, medicine, or e-commerce.
The harness architecture of the coding agent you will build during this course.
## ๐ฎ See It Work
The finished agent ships with demo skills under [`.decode/skills/`](.decode/skills/). Open the TUI, type `/demo-`, pick one, and watch the harness you're about to build do real work:
Implement the Skills Standard Type /demo- and the six demos are one keystroke away.
Capable of Creating Games /demo-1-terminal-arcade โ one prompt, a playable Snake game
Fetching Data & Creating Dashboards /demo-3-repo-pulse โ live GitHub API data rendered as a dashboard
Extracting Ontologies & Rendering Graphs /demo-6-article-kg โ web articles scraped into an interactive knowledge graph
And the infra that powers the agents.
Record & Replay for AI Agents Every run recorded step by step in Kitaru โ replay it on a Kitaru Worker with the model swapped, then compare the two runs
Remote Sandboxing The agent's bash runs in disposable Modal sandboxes
Powered by Open-Source Models Your own Qwen3.6-35B served on an H200 via a Modal endpoint
Adding AI Evals & Observability Every session traced in Opik
## ๐ค You'll Walk Away Knowing How To
- Design a coding agent harness from scratch ([article 1](https://www.decodingai.com/p/building-a-coding-agent-from-scratch-system-design) ยท [video 1](https://www.youtube.com/watch?v=sJpop1juVBQ))
- Implement the coding agent loop as a headless harness ([article 2](https://www.decodingai.com/p/the-coding-agent-loop) ยท [video 1](https://www.youtube.com/watch?v=sJpop1juVBQ))
- Attach the headless harness to multiple interfaces: a terminal UI, a CLI, or even a remote runtime ([article 6](https://www.decodingai.com/p/coding-agents-in-remote-headless) ยท [video 4](https://www.youtube.com/watch?v=nIhTdQH6z5g))
- Execute the agent's tools within local Docker or remote Modal sandboxes ([article 3](https://www.decodingai.com/p/run-coding-agents-safely) ยท [video 2](https://www.youtube.com/watch?v=7CHMb8jWs6A))
- Host open-source models as SGLang servers on Modal ([article 2](https://www.decodingai.com/p/the-coding-agent-loop) ยท [video 1](https://www.youtube.com/watch?v=sJpop1juVBQ))
- Implement guardrails by adding a permission layer ([article 1](https://www.decodingai.com/p/building-a-coding-agent-from-scratch-system-design) ยท [video 1](https://www.youtube.com/watch?v=sJpop1juVBQ))
- Build essential context engineering techniques: memory, compaction, skills ([article 4](https://www.decodingai.com/p/context-engineering-for-coding-agents) ยท [video 3](https://www.youtube.com/watch?v=dx77BRFZ0_M))
- Hook up an LSP server for faster feedback loops ([article 4](https://www.decodingai.com/p/context-engineering-for-coding-agents) ยท [video 3](https://www.youtube.com/watch?v=dx77BRFZ0_M))
- Implement a configurable agents catalog: build, plan, code reviewer, and exploration agents ([article 5](https://www.decodingai.com/p/subagents-are-context-engineering))
- Deploy the headless harness on Modal, triggering remote agents via the CLI, a webhook, or a cron job ([article 6](https://www.decodingai.com/p/coding-agents-in-remote-headless) ยท [video 4](https://www.youtube.com/watch?v=nIhTdQH6z5g))
- Spawn parallel subagents via fan-out strategies ([article 5](https://www.decodingai.com/p/subagents-are-context-engineering))
- Add observability ([article 2](https://www.decodingai.com/p/the-coding-agent-loop) ยท [video 1](https://www.youtube.com/watch?v=sJpop1juVBQ))
- Design an eval harness for benchmarking the agent and checking for regressions ([article 7](https://www.decodingai.com/p/evaluate-ai-agents-benchmarks-regression-tests) ยท [video 5](https://www.youtube.com/watch?v=3QQaonJyUf4))
- Organically grow your regression suite from failed agent traces ([article 8](https://www.decodingai.com/p/transform-agent-traces-into-regression-cases) ยท [video 6](https://www.youtube.com/watch?v=HuRLvkG7tVg))
- Reproduce agent failures and check for regressions when changing your prompts or models by replaying traces with Kitaru ([article 8](https://www.decodingai.com/p/transform-agent-traces-into-regression-cases) ยท [video 6](https://www.youtube.com/watch?v=HuRLvkG7tVg))
Plan mode, live: the agent breaks the Snake demo into a task list with the todo tool โ [x] done, [~] in progress.
### Tech Stack
The code is written in Python, with the following frameworks and libraries:
- **Agent Framework:** [Pydantic AI](https://ai.pydantic.dev)
- **LLM Providers:** [Modal](https://modal.com/docs/guide/endpoints?source=decodingai&campaign=harnesseng) (open weights you serve yourself via SGLang), OpenRouter (open weights as a service), or Gemini (proprietary).
- **Session Recording & Replays:** [Kitaru](https://www.zenml.io/product/kitaru?utm_source=decodingai&utm_medium=referral&utm_campaign=coding-agent-course&utm_content=brand)
- **Observability & Evals:** [Opik](https://www.comet.com/site/?utm_source=workshop&utm_medium=partner&utm_campaign=paul&utm_content=coding_agent_course)
- **Sandboxing:** local Docker & remote [Modal sandboxes](https://modal.com/docs/guide/sandboxes?source=decodingai&campaign=harnesseng)
- **Deploying:** [Modal](https://modal.com/?source=decodingai&campaign=harnesseng) as headless agents (fired by hand, by cron, or by webhook)
Otherwise, we build all the functionality from scratch to teach you the foundations that last, not frameworks that abstract away the hard parts.
## ๐ฌ Learn Harness Engineering
> Join 45k+ engineers subscribed to [the Decoding AI Magazine](https://www.decodingai.com/) to learn to build coding agents from scratch.
## ๐ก The code tells you _what_. The lessons tell you _why_.
For the full experience, go through the articles and videos that cover what the code can't. **The why behind every decision.**
- What the essential components of a coding agent are, and what is optional. ([article 1](https://www.decodingai.com/p/building-a-coding-agent-from-scratch-system-design) ยท [video 1](https://www.youtube.com/watch?v=sJpop1juVBQ))
- Why we have a headless harness and two interface modes: TUI + Remote. ([article 1](https://www.decodingai.com/p/building-a-coding-agent-from-scratch-system-design) ยท [video 1](https://www.youtube.com/watch?v=sJpop1juVBQ))
- Why we plugged in 9 tools, no more, no less. ([article 2](https://www.decodingai.com/p/the-coding-agent-loop) ยท [video 1](https://www.youtube.com/watch?v=sJpop1juVBQ))
- What guardrails are actually useful. ([article 3](https://www.decodingai.com/p/run-coding-agents-safely) ยท [video 2](https://www.youtube.com/watch?v=7CHMb8jWs6A))
- Why compaction fires at ~80% of the window instead of at the limit. ([article 4](https://www.decodingai.com/p/context-engineering-for-coding-agents) ยท [video 3](https://www.youtube.com/watch?v=dx77BRFZ0_M))
- Why subagents are context engineering: scoped windows instead of one bloated context. ([article 5](https://www.decodingai.com/p/subagents-are-context-engineering))
- Why your agents should keep working after you close your laptop lid. ([article 6](https://www.decodingai.com/p/coding-agents-in-remote-headless) ยท [video 4](https://www.youtube.com/watch?v=nIhTdQH6z5g))
- Why you need both benchmarks and regression tests. ([article 7](https://www.decodingai.com/p/evaluate-ai-agents-benchmarks-regression-tests) ยท [video 5](https://www.youtube.com/watch?v=3QQaonJyUf4))
- Why we record every run, and what a replay buys you that a re-run doesn't. ([article 8](https://www.decodingai.com/p/transform-agent-traces-into-regression-cases) ยท [video 6](https://www.youtube.com/watch?v=HuRLvkG7tVg))
## ๐ Course Outline
Lesson
Written Lesson
Video Lesson
Running the Code
1 Building a Coding Agent From Scratch
Sketch the full harness: a headless core, six modules, TUI and remote modes, and an evals layer. โฑ 17-min read
## ๐ ๏ธ Keep Building From Scratch
> Enjoyed building Claude Code from scratch? Keep going with [CodeCrafters](https://app.codecrafters.io/join?via=iusztinpaul), where you can rebuild real developer tools like Redis, Git, a Shell, or an HTTP Server through step-by-step challenges with automated tests, in your language of choice. Signing up is free, and my affiliate link gets you 40% off if you upgrade. Or start with their free [build-your-own-x](https://github.com/codecrafters-io/build-your-own-x) guides.
## ๐ฅ Who Should Join?
**Engineers who learn by building.** You finish with a working coding agent that teaches you harness engineering patterns to steal for your own agentic applications.
Best for **ML/AI engineers** who want to level up their craft and for **software engineers and data scientists** who want to transition into building agentic systems from scratch.
## ๐ Prerequisites
| Category | Requirements |
| ------------ | ------------------------------------------------------------------------------------- |
| **Skills** | - Python (Intermediate) - LLMs & agents (Beginner) |
| **Hardware** | Any modern machine will do. No GPU required, as we run all the LLMs in the cloud. |
| **Level** | Intermediate (but with a little sweat and patience, anyone can do it) |
| **Time** | ~4โ8 hours for the whole course โ 4 if you read and watch, 6โ8 if you run everything. |
## ๐ฐ Cost Structure
Running the code costs **$0** if you stick to free tiers:
| Service | Cost |
| ---------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------ |
| Gemini API (default provider โ easy setup, but limited API requests) | free tier ([Google AI Studio](https://aistudio.google.com/apikey)) |
| [Modal](https://modal.com?source=decodingai&campaign=harnesseng) (recommended provider, remote sandbox, remote agents) | $30 free credits โ enough to run the course |
| OpenRouter (alternative provider) | $0 on `:free` models (optional $10 credit raises the daily cap) |
| [Opik](https://www.comet.com/site/?utm_source=workshop&utm_medium=partner&utm_campaign=paul&utm_content=coding_agent_course) (tracing + evals) | free tier |
| [Kitaru](https://www.zenml.io/product/kitaru?utm_source=decodingai&utm_medium=referral&utm_campaign=coding-agent-course&utm_content=brand) (recording + replays) | free โ the OSS server runs on your laptop (`make kitaru-local`) |
_**Only reading? Everything's free!**_
## โ๏ธ How It Works
As an open-source course, it is entirely self-paced and based on this repository, plus the attached lessons that walk you through the code. No paywall. No platform.
Read the lessons on the [Decoding AI Magazine](https://www.decodingai.com), watch the videos on the [Decoding AI Channel](https://www.youtube.com/@itsdecodingai), run the code on your own machine, break it, fix it, and learn from the process.
## ๐๏ธ Project Structure
One Python package; each module maps to one part of the architecture:
```
.
โโโ docs/
โ โโโ adr/ # Architecture Decision Records โ the "why" of every choice
โ โโโ glossary.md # one canonical name per concept
โโโ evals/ # benchmark tasks + regression cases + the Opik harness
โโโ scripts/ # operator scripts: Kitaru bootstrap + eval table generation
โโโ tests/{unit,integration}/ # mirrors src/ 1:1; milestone capstones prove each milestone
โโโ src/decode/
โโโ cli.py # Click entrypoint โ launches the TUI
โโโ tui/ # input: prompt_toolkit ยท output: Rich
โโโ harness/ # message queue + priority gate around the loop
โโโ agent/ # the Pydantic-AI ReAct loop (LLM โ tools)
โโโ agents/ # agents catalog: Build / Plan / Code-Reviewer + Explore subagent
โโโ tools/ # file I/O, bash, web, todo, skills dispatch, LSP, ask_user
โโโ permissions/ # allow/ask/deny ยท modes ยท settings.json
โโโ sandbox/ # bash + file tools seam: none (host) / docker / modal
โโโ services/lsp/ # hand-rolled stdio LSP client (ty)
โโโ runtime/ # plain headless decode run + the Recording Seam
โโโ remote/ # decode remote deploy|run|attempts|logs โ the Modal Headless App (CLI, webhook, cron)
โโโ context/ # compaction + conversation log (JSONL)
โโโ memory/ # AGENTS.md / MEMORY.md loading + write-back
โโโ observability/ # Opik tracing
โโโ config/, entities/ # settings singleton ยท shared models
```
## ๐ Running the Code
The guides live under [`running_the_code/`](running_the_code/). Follow them in order; each ends with a link to the next:
| Guide | What's inside |
| ------------------------------------------------------------------- | ------------------------------------------------------------------ |
| [00_troubleshooting.md](running_the_code/00_troubleshooting.md) | Every known failure, and its fix |
| [01_install_and_usage.md](running_the_code/01_install_and_usage.md) | Start here: install, one key, first session |
| [02_modal_endpoints.md](running_the_code/02_modal_endpoints.md) | Serving open models on Modal |
| [03_sandboxing.md](running_the_code/03_sandboxing.md) | Docker (local) / Modal (remote) sandboxing + the sandbox git token |
| [04_deploy.md](running_the_code/04_deploy.md) | The headless harness on Modal โ CLI, webhook, cron |
| [05_evals.md](running_the_code/05_evals.md) | Benchmarks and regression cases on Opik |
| [06_evals_replays.md](running_the_code/06_evals_replays.md) | Kitaru on your laptop: record, replay & the full evals loop |
## ๐ค Sponsors
Special thanks to Modal, Opik (by Comet), and Kitaru (by ZenML) for sponsoring this open-source course and keeping it free!
## ๐ฃ๏ธ Questions and Troubleshooting
Open a [GitHub issue](https://github.com/decodingai-magazine/building-a-coding-agent-from-scratch-course/issues) for course questions, setup trouble, or concept clarifications. Known gotchas are documented in the [`running_the_code/`](running_the_code/) guides.
## โ FAQ
**Do I need a paid API key?**
No. The default Gemini provider has a free tier, OpenRouter routes across `:free` models, and [Modal](https://modal.com?source=decodingai&campaign=harnesseng) gives $30 in credits โ see [Cost Structure](#-cost-structure).
**Why Python and not TypeScript or Go?**
Accessibility: our audience knows Python. The course focuses on the design decisions, which transfer to any language.
**Why build from scratch instead of extending Pi, DeepAgents, or an existing harness?**
Because adding custom logic to an existing harness is the easy part. _Knowing what to add_ requires understanding the internals. Those fundamentals are what still make AI engineers valuable. Build a coding agent once and you're equipped to build a custom agent for any use case.
## ๐ฅ Contributing
Found a bug and know the fix? Fork, fix, run `make ci` (no API key needed), and open a pull request. Future readers will thank you ๐ค
## ๐จโ๐ซ Course Author
Paul Iusztin
Senior AI Engineer, Educator & Founder of Decoding AI. Author of the best-selling LLM Engineer's Handbook.
## ๐ฌ Learn Harness Engineering
> Join 45k+ engineers subscribed to [the Decoding AI Magazine](https://www.decodingai.com/) to learn to build coding agents from scratch.
## โญ One More Thing
If you found this course useful, consider starring the repository so others can find it too.
## License
Released under [Apache-2.0](LICENSE). Clone, fork, and build on it; keep the LICENSE and credit this repo.