Claude Code from Source

April 3, 2026 · View on GitHub

Architecture, Patterns & Internals of Anthropic's AI Coding Agent

Claude Code from Source — Book Cover

Read online at claude-code-from-source.com


This repository is purely educational. It contains no source code from Claude Code — not a single line. 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 or redistribute proprietary software.


When Anthropic shipped Claude Code on npm, the .js.map source maps contained a sourcesContent field with the full original TypeScript. This book is the result of studying that architecture and distilling the patterns, trade-offs, and design decisions into a technical narrative that any engineer can learn from.

18 chapters across 7 parts. ~400 pages in print equivalent.

Every chapter has layered depth: a narrative flow for technical leaders, deep-dive sections for implementers, and an "Apply This" closing that extracts transferable patterns you can steal for your own systems. Diagrams use Mermaid and render natively on GitHub.


Who This Is For

  • Senior engineers building agentic systems — steal the patterns, understand the trade-offs, implement in your own stack
  • Technical leaders evaluating architectures — follow the narrative without reading every code block
  • Anyone curious about how production AI tools actually work under the hood

Table of Contents

Part I: Foundations

Before the agent can think, the process must exist.

#ChapterWhat You'll Learn
1The Architecture of an AI AgentThe 6 key abstractions, data flow, permission system, build system
2Starting Fast — The Bootstrap Pipeline5-phase init, module-level I/O parallelism, trust boundary
3State — The Two-Tier ArchitectureBootstrap singleton, AppState store, sticky latches, cost tracking
4Talking to Claude — The API LayerMulti-provider client, prompt cache, streaming, error recovery

Part II: The Core Loop

The heartbeat of the agent: stream, act, observe, repeat.

#ChapterWhat You'll Learn
5The Agent Loopquery.ts deep dive, 4-layer compression, error recovery, token budgets
6Tools — From Definition to ExecutionTool interface, 14-step pipeline, permission system
7Concurrent Tool ExecutionPartition algorithm, streaming executor, speculative execution

Part III: Multi-Agent Orchestration

One agent is powerful. Many agents working together are transformative.

#ChapterWhat You'll Learn
8Spawning Sub-AgentsAgentTool, 15-step runAgent lifecycle, built-in agent types
9Fork Agents and the Prompt CacheByte-identical prefix trick, cache sharing, cost optimization
10Tasks, Coordination, and SwarmsTask state machine, coordinator mode, swarm messaging

Part IV: Persistence and Intelligence

An agent without memory makes the same mistakes forever.

#ChapterWhat You'll Learn
11Memory — Learning Across ConversationsFile-based memory, 4-type taxonomy, LLM recall, staleness
12Extensibility — Skills and HooksTwo-phase skill loading, lifecycle hooks, snapshot security

Part V: The Interface

Everything the user sees passes through this layer.

#ChapterWhat You'll Learn
13The Terminal UICustom Ink fork, rendering pipeline, double-buffer, pools
14Input and InteractionKey parsing, keybindings, chord support, vim mode

Part VI: Connectivity

The agent reaches beyond localhost.

#ChapterWhat You'll Learn
15MCP — The Universal Tool Protocol8 transports, OAuth for MCP, tool wrapping
16Remote Control and Cloud ExecutionBridge v1/v2, CCR, upstream proxy

Part VII: Performance Engineering

Making it all fast enough that humans don't notice the machinery.

#ChapterWhat You'll Learn
17Performance — Every Millisecond and Token CountsStartup, context window, prompt cache, rendering, search
18Epilogue — What We LearnedThe 5 architectural bets, what transfers, where agents are heading

The 10 Patterns That Make It Work

If you read nothing else:

  1. AsyncGenerator as agent loop — yields Messages, typed Terminal return, natural backpressure and cancellation
  2. Speculative tool execution — start read-only tools during model streaming, before the response completes
  3. Concurrent-safe batching — partition tools by safety, run reads in parallel, serialize writes
  4. Fork agents for cache sharing — parallel children share byte-identical prompt prefixes, saving ~95% input tokens
  5. 4-layer context compression — snip, microcompact, collapse, autocompact — each lighter than the next
  6. File-based memory with LLM recall — Sonnet side-query selects relevant memories, not keyword matching
  7. Two-phase skill loading — frontmatter only at startup, full content on invocation
  8. Sticky latches for cache stability — once a beta header is sent, never unset mid-session
  9. Slot reservation — 8K default output cap, escalate to 64K on hit (saves context in 99% of requests)
  10. Hook config snapshot — freeze at startup to prevent runtime injection attacks

How This Book Was Made

The source was extracted from npm source maps. 36 AI agents analyzed nearly two thousand TypeScript files in four phases:

  1. Exploration: 6 parallel agents read every file in the source tree
  2. Analysis: 12 agents wrote 494KB of raw technical documentation
  3. Writing: 15 agents rewrote everything from scratch as narrative chapters
  4. Review & Revision: 3 editorial reviewers produced 900 lines of feedback; 3 revision agents applied all fixes

The entire process — from source extraction to final revised book — took approximately 6 hours.


Disclaimer

This repository does not contain any source code from Claude Code. All code blocks are original pseudocode using different variable names, written to illustrate architectural patterns. No proprietary prompt text, internal constants, or exact function implementations are included. This project exists purely for educational purposes — to help engineers understand the design patterns behind production AI coding agents.

The "NO'REILLY" cover is a parody/meme for illustrative purposes only. This project has no affiliation with O'Reilly Media. The crab is just a crab.

This is an independent analysis. Claude Code is a product of Anthropic. This book is not affiliated with, endorsed by, or sponsored by Anthropic.