Skip to content
HN On Hacker News ↗

Claude Code from Source — Architecture, Patterns & Internals

▲ 20 points • 5 comments • by pykello • 3w ago • HN discussion ↗

Pangram verdict · v3.3

We believe that this entire text is AI.

75 %

AI likelihood · overall

AI
0% human-written 100% AI-generated
SEGMENTS · HUMAN 0 of 1
SEGMENTS · AI 1 of 1
WORD COUNT 793
PEAK AI % 75% · §1
Analyzed
Sep 18
backend: pangram/v3.3
Segments scanned
1 windows
avg 793 words each
Distribution
0 / 100%
human / AI fraction
Verdict
AI
Pangram v3.3

Article text · 793 words · 1 segments analyzed

Human AI-generated
§1 AI · 75%

How Anthropic built the most widely used AI coding agent When Claude Code shipped on npm, the source maps came with it. We read every file. This book distills the architecture, design decisions, and transferable patterns into 18 chapters you can learn from and apply to your own systems. Start reading What you'll learn The agent loop How an async generator drives the entire system — streaming model output, executing tools, recovering from errors, and compressing context across 4 layers. Tool execution at scale A 14-step pipeline from model request to tool result. Permission resolution, speculative execution, concurrent batching by safety classification. Multi-agent orchestration How sub-agents share prompt cache prefixes to cut costs by 95%. Fork agents, coordinator mode, swarm teams with mailbox messaging. Memory without a database File-based memory with an LLM-powered recall system. Four memory types, staleness warnings, and a Sonnet side-query that beats embedding search. Performance engineering Startup in 240ms via parallel I/O. Slot reservation saving context in 99% of requests. Bitmap pre-filters for fuzzy search. Every millisecond accounted for. Extensibility and security Two-phase skill loading (metadata at startup, content on demand). 27 lifecycle hooks with config snapshots frozen at startup to prevent injection. Explore the architecture Six core abstractions power Claude Code. Drag nodes to rearrange, hover for details, click to read the chapter. Who this is for Engineers building agentic systems. Every chapter ends with "Apply This" — 5 transferable patterns with concrete adaptation advice. Steal the architecture, skip the mistakes. Technical leaders evaluating architectures. Follow the narrative without reading every code block. Understand why decisions were made, not just what was built. Anyone curious about how production AI tools work. Claude Code is used by hundreds of thousands of developers. This is how it works under the hood. Table of contents Part 1Foundations Before the agent can think, the process must exist. 1 The Architecture of an AI Agent The 6 key abstractions, data flow, permission system, build system 2 Starting Fast — The Bootstrap Pipeline 5-phase init, module-level I/O parallelism, trust boundary 3 State — The Two-Tier Architecture Bootstrap singleton, AppState store, sticky latches, cost tracking 4 Talking to Claude — The API Layer Multi-provider client, prompt cache, streaming, error recovery Part 2The Core Loop The heartbeat of the agent: stream, act, observe, repeat. Part 3Multi-Agent Orchestration One agent is powerful. Many agents working together are transformative. Part 4Persistence and Intelligence An agent without memory makes the same mistakes forever. Part 5The Interface Everything the user sees passes through this layer. Part 6Connectivity The agent reaches beyond localhost. Part 7Performance Engineering Making it all fast enough that humans don't notice the machinery. How this book was made The source was extracted from npm source maps — the .js.map files that shipped with Claude Code contained a sourcesContent field with the full original TypeScript. Nearly two thousand files comprising the complete architecture. 36 AI agents analyzed and wrote the entire book in four phases: Exploration 6 parallel agents read every file in the source tree Analysis 12 agents wrote 494KB of raw technical documentation Writing 15 agents rewrote everything from scratch as narrative chapters Review & Revision 3 reviewers produced 900 lines of feedback; 3 agents applied all fixes The entire process — from source extraction to final revised book — took approximately 6 hours. A final audit pass ensured no verbatim source code remained — every code block was rewritten as pseudocode with different variable names. The 10 patterns that make it work If you read nothing else, these are the architectural bets that define Claude Code. 1AsyncGenerator as agent loop — yields Messages, typed Terminal return, natural backpressure and cancellation 2Speculative tool execution — start read-only tools during model streaming, before the response completes 3Concurrent-safe batching — partition tools by safety, run reads in parallel, serialize writes 4Fork agents for cache sharing — parallel children share byte-identical prompt prefixes, saving ~95% input tokens 54-layer context compression — snip, microcompact, collapse, autocompact — each lighter than the next 6File-based memory with LLM recall — Sonnet side-query selects relevant memories, not keyword matching 7Two-phase skill loading — frontmatter only at startup, full content on invocation 8Sticky latches for cache stability — once a beta header is sent, never unset mid-session 9Slot reservation — 8K default output cap, escalate to 64K on hit (saves context in 99% of requests) 10Hook config snapshot — freeze at startup to prevent runtime injection attacks Purely educational. This book contains no source code from Claude Code — every code block is original pseudocode written to illustrate architectural patterns. The goal is to help engineers understand how production AI agents are built, not to reproduce proprietary software. The "NO'REILLY" cover is a parody/meme for illustrative purposes only — no affiliation with O'Reilly Media.