nanopi Rebuilds a Full Coding Agent in 600 Lines of TypeScript
A 600-line TypeScript walkthrough that rebuilds the pi coding agent from scratch, with a live editor and step-through trace debugger.
ProTakeaways
- pi-from-scratch rebuilds a working coding agent in about 600 lines of TypeScript.
- The project is a teaching cut of earendil-works/pi, keeping only the core data flow.
- Five files: llm, agent, tools, tui, cli, each mapped to a real pi package.
- Ships four tools: read_file, write_file, edit, and run_bash as pure functions.
- The companion site reveals code as you read, plus a step-through trace debugger.
- MIT-licensed, needs Node.js 22 and any OpenAI-compatible API to run locally.
If you have ever poked around a production coding agent and given up somewhere between the streaming SSE parser and the tool-call reducer, there is now a friendlier way in. pi-from-scratch rebuilds a working coding agent in roughly 600 lines of TypeScript across five files, paired with an interactive website that reveals the code as you read.
The target it copies is pi, an open-source toolkit from earendil-works. The tutorial frames pi as a production-grade AI coding agent of many thousands of lines, and calls its own version nanopi, a 600-line teaching cut. The stated goal is to strip pi down to its core ideas and follow the data flow instead of the engineering scaffolding.
Five files, one agent loop
Rather than opening with an architecture diagram, the walkthrough introduces components only when the data flow demands them. Nanopi maps to exactly five TypeScript files, each corresponding to a real package in upstream pi:
llm.tstalks to model providers. It takes a Context (chat history, plain JSON) and emits a stream of events:text_delta,tool_call,done, anderror. HTTP, SSE parsing, and stitching together fragmented tool-call arguments all live here. This maps to pi'spi-aipackage, except nanopi only speaks the OpenAI-compatible format instead of adapting a dozen providers.agent.tsis the loop itself. It callsstream()fromllm.ts, and if the model asks for a tool, it runs the tool, puts the result back into Context, and asks again, continuing until the model says it is done. It emits higher-level AgentEvents (assistant_text,tool_call,tool_result,turn_end) so the UI never has to touch raw provider responses.tools.tsships four tools:read_file,write_file,edit, andrun_bash. Each is a pure function that takes arguments and returns a string result, with no knowledge of agent state or Context.tui.tshandles the terminal UI usingreadlinefor input, streaming stdout writes for output, and Ctrl+C for abort. It only understands AgentEvents, so swapping in a web frontend does not require touching agent code.cli.tsis the glue: it wires config, model, tools, and TUI together, and appends each turn's messages to~/.nanopi/session.jsonlfor session persistence.
The real lesson is the dependency graph
Events come in two layers, and that split does most of the design work. LLM-level StreamEvents stay inside agent.ts, and the outside world only sees AgentEvents. The UI layer only needs to know AgentEvent and does not care about specific model responses. The same event stream that a terminal renders can be pushed over SSE to a browser without changing the agent.
Upstream pi has extra machinery the tutorial deliberately skips, including precise token estimation and compaction strategies, an extensions and skills system, multi-provider adapters with model routing, session branching, and the pi-tui differential renderer. Cutting those keeps nanopi close to the pedagogical core: read a message, decide, call a tool, feed the result back, repeat.
How the website works
The reading experience is the other half of the pitch. As you scroll the article, the editor on the right progressively fills in code, and by the time you finish, the full nanopi source is on screen. A Trace view lets you set breakpoints and step through a pre-recorded run line by line, so you can watch the loop execute against real model output without spending any tokens.
Running it locally
The repo is MIT-licensed and needs Node.js 22 or later plus an OpenAI-compatible API. Setup is three commands:
npm install
export NANOPI_API_KEY=your-api-key
npm run dev
Optional environment variables include NANOPI_MODEL for the model name and NANOPI_BASE_URL for the endpoint, which defaults to https://api.openai.com/v1. Point it at any OpenAI-compatible gateway and you have a working file-reading, code-editing, shell-running agent on your machine.
Who this is actually for
If you have already shipped agents with LangGraph, the AI SDK, or a homegrown loop, most of nanopi will look familiar and you can skim it in an afternoon. The value sits elsewhere:
- People who have used Claude Code, Cursor, or pi and want to understand what is happening between the model and the filesystem.
- Anyone planning to read the upstream pi source but bouncing off the size. The tutorial explicitly positions itself as scaffolding: read this first, then the real codebase makes sense.
- Teams building their own domain-specific coding agents who want a clean reference for splitting LLM plumbing, agent state, tools, and UI into replaceable layers.
The main limitation is exactly what the author advertises: it is a teaching artifact, not a competitor to a real coding agent. There is no permission system, no sandboxing, no token accounting, no multi-provider routing, and only one prompt-to-tools cycle to study. For learning the shape of an agent loop, that trade works. For anything you would run against a production repo, use the real thing.