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