Skip to content

10 architectural patterns behind Claude Code

Reverse-engineered from npm source maps, these 10 design decisions explain why Claude Code is fast, safe, and recoverable under production conditions.

Source: claude-code-from-source.com

Updated May 27, 2026

The 10 patterns below distill every non-obvious design decision into a transferable form — concrete proof of a simple thesis: the workflow around a capable model decides how much of its capability ships. Each one solves a problem every production agent eventually hits: context exhaustion, token waste, unsafe concurrent tool use, or session state that drifts between restarts.

AsyncGenerator as agent loop
The outer loop yields Messages and uses a typed Terminal return, giving natural backpressure and cooperative cancellation without callbacks.
Speculative tool execution
Read-only tools start running during model streaming, before the response token stream completes — cutting perceived latency on the common read path.
Concurrent-safe batching
Tools are partitioned by safety class; reads run in parallel, writes are serialized — concurrency without race conditions.
Fork agents for cache sharing
Parallel child agents share a byte-identical prompt prefix, so the cache is hit on every child's first token — ~95% input-token savings on fan-out tasks.
4-layer context compression
Snip, microcompact, collapse, autocompact — each lighter than the next; the lightest layer that fits the context wins.
File-based memory with LLM recall
Memories live as plain files; a Sonnet side-query selects which ones are relevant at session start — semantic recall without a vector database.
Two-phase skill loading
Frontmatter is parsed at startup for routing; full skill content is loaded only on invocation — keeps startup fast regardless of skill count.
Sticky latches for cache stability
Once a beta header is sent, it is never unset mid-session — prevents prompt-cache invalidation from header churn.
Slot reservation
Output is capped at 8K tokens by default; the cap escalates to 64K on a hit — saves context in 99% of requests while allowing long outputs when needed.
Hook config snapshot
Hook configuration is frozen at startup, not re-read per tool call — prevents runtime injection attacks via config mutation.

Related