---
title: "nanopi Rebuilds a Full Coding Agent in 600 Lines of TypeScript"
subtitle: "A 600-line TypeScript walkthrough that rebuilds the pi coding agent from scratch, with a live editor and step-through trace debugger."
url: "https://alphasignal.ai/news/nanopi-rebuilds-a-full-coding-agent-in-600-lines-of-typescript"
author: "AlphaSignal Newsroom"
author_url: "https://alphasignal.ai/editorial-team"
company: "AlphaSignal"
company_url: "https://alphasignal.ai/company/alphasignal"
published: "2026-08-12T07:10:35.000Z"
updated: "2026-08-22T00:48:19.640Z"
type: "Repo"
topics:
  - "Development"
  - "Agents"
subtopics:
  - "Code Agents"
  - "Code Agents"
  - "Agent Frameworks"
  - "Tool Use"
upvotes: 12
source_url: "https://github.com/SaladDay/pi-from-scratch"
comments: 0
truncated: true
---

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

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](https://github.com/SaladDay/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](https://github.com/earendil-works/pi), an [open-source toolkit](https://pi.dev) from earendil-works. [The tutorial](https://pi-from-scratch.vercel.app) 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.ts` talks to model providers. It takes a Context (chat history, plain JSON) and emits a stream of events: `text_delta`, `tool_call`, `done`, and `error`. HTTP, SSE parsing, and stitching together fragmented tool-call arguments all live here. This maps to pi's `pi-ai` package, except nanopi only speaks the OpenAI-compatible format instead of adapting a dozen providers.

_Preview only. Full text requires an AlphaSignal Pro account._

## Sources

- [SaladDay/pi-from-scratch on GitHub](https://github.com/SaladDay/pi-from-scratch)
- [PI from Scratch tutorial site](https://pi-from-scratch.vercel.app)
- [earendil-works/pi on GitHub](https://github.com/earendil-works/pi)
