Claude Code from Source — Book Cover

Claude Code from Source

Architecture, Patterns & Internals of Anthropic's AI Coding Agent. 18 chapters across 7 parts. ~400 pages.

Reverse-engineered from source maps. Every chapter has transferable patterns you can steal for your own systems.

The 10 Patterns That Make It Work

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