🤖 The Plumbing Behind Claude Code

Source: Hacker News · Author: Siddhant Khare · Date: April 3, 2026 · Quality: ★★★★☆ (4.5/5)

Core Insight

A deep technical analysis of Claude Code's leaked source code revealing the engineering decisions behind building an AI coding tool that runs locally, touches user files, and burns their money.

Key Takeaway: Claude Code isn't magic — it's 2,592 lines of bash security, a composable prompt cache, and a structured compaction engine with circuit breakers.

1. Prompt Cache — The Biggest Cost Lever

The system prompt is split by a boundary marker: __SYSTEM_PROMPT_DYNAMIC_BOUNDARY__

Cache Break Detection: 77% of tool-related cache breaks come from tool descriptions changing (e.g., AgentTool embeds dynamic sub-agent lists). The fix: track why the cache breaks and fix the source.

Result: 90% cost reduction on cache hits.

2. Auto-Compaction with Circuit Breakers

Auto-compaction triggers at 13,000 buffer tokens. Before the circuit breaker: 1,279 sessions had 50+ consecutive failures, one session hit 3,272 failures — ~250K wasted API calls/day globally.

The Compaction Prompt: 9-section rigid template preserving:

3. Bash Security — 5 Layers of Defense

src/tools/BashTool/bashSecurity.ts — 2,592 lines of paranoia:

Why This Matters

Understanding the plumbing behind Claude Code reveals what actually matters when building production AI tools:

Original Article · Source Code (GitHub)