Claude Code's 6 Implementable Agent Patterns — The Architecture Details That Change Your Design
Why This Matters Now
Previous days established that scaffolding beats model scale. Today, 13+ independent analyses of Anthropic's leaked 500K-line codebase give you the specific production patterns to implement. This isn't theory — it's what Anthropic actually ships, including retry logic, error handling, and the engineering decisions that distinguish production agents from demos.
Pattern 1: 3-Layer Hierarchical Memory
Claude Code solves context budget allocation with a tiered memory architecture that separates routing from retrieval:
- MEMORY.md index — always loaded, ~150 chars per pointer line. A routing table, not a knowledge store.
- Topic files — loaded on demand based on task context. Selective hydration.
- Full transcripts — never read directly; grep-only fallback.
Write discipline is critical: write to topic file first, then update index. Memory is treated as a hint, not truth — the agent verifies before using. This directly mirrors distributed systems caching: stale reads are the enemy, and cache-miss cost (re-deriving) is acceptable if it prevents hallucination.
Most agent memory is either 'stuff everything into context' or 'RAG with top-K.' Claude Code's 3-layer approach separates the routing decision from the retrieval operation — the model decides what domain to access before paying token cost to load it.
Pattern 2: KV-Cache Fork-Join Parallelism
Subagents inherit the parent's full context via byte-identical KV cache prefix sharing. Five parallel subagents cost barely more than one because they fork from cached state and diverge only at task-specific suffixes. This is a fork-join execution model from parallel computing applied to LLM inference. Key constraint: depends on your provider's cache TTL and hit rates. Anthropic controls both, giving them an end-to-end optimization advantage.
Pattern 3: Tool Gating — 19 of 60+
Despite having 60+ tools, only 19 are enabled by default. The most revealing category: Cognitive Control tools (EnterPlanModeTool, ExitPlanModeV2, BriefTool, TodoWriteTool). These aren't environment tools — they're metacognitive scaffolding where the agent manages its own reasoning process. The agent explicitly switches between planning and execution phases.
Pattern 4: autoDream Offline Consolidation
A sandboxed forked subagent with limited tool access runs consolidation with 8 distinct phases and 5 compaction strategies. It merges, deduplicates, prunes, and removes contradictions — structurally analogous to LSM-tree compaction in databases. Critical isolation: autoDream cannot write to main context, preventing compounding errors.
Pattern 5: Fake Tool Interception
Instead of blocking dangerous tool calls (which breaks agent loops), Claude Code redirects them through dummy endpoints returning safe responses. The agent continues reasoning uninterrupted, never knowing it was intercepted. This is fundamentally different from the "refuse and explain" pattern most frameworks use.
Pattern 6: Prompt Cache Boundary Splitting
SYSTEM_PROMPT_DYNAMIC_BOUNDARY partitions every prompt into a cached static front half and dynamic back half. Tags like DANGEROUS_uncachedSystemPromptSection explicitly mark cache-breakers. If you're not partitioning system prompts for cache reuse, you're paying full input-token costs on unchanged content every call — typically 30-60% waste.
Cross-Source Tensions
Sources disagree on the harness's model-agnosticism. Multiple analyses claim dropping DeepSeek or Gemini into the architecture yields improved coding ability. The claw-code project (75K+ GitHub stars) is testing this. But one source cautions that Anthropic's end-to-end control of cache policies gives them optimization advantages competitors can't replicate via the same architecture alone.
Security Warning
Malicious npm packages (color-diff-napi, modifiers-napi) specifically target developers trying to compile the leaked code. This is a confirmed active supply chain attack. Read the architecture analysis. Do not clone or execute anything from leaked forks.
What to do
Implement 3-layer memory hierarchy (index → topic → transcript) in your longest-running agent pipeline this sprint
Restructure agent DAGs to share a common context prefix and benchmark KV-cache fork-join on your provider's API
Audit your agent's tool inventory: if >20 tools are exposed by default, implement task-aware tool gating
Build an offline memory consolidation job (autoDream pattern) for any agent persisting across sessions