Claude Code

Description

Claude Code is Anthropic's agentic CLI coding tool -- a terminal-based AI assistant that can read, write, and execute code autonomously. Built as a 512,000+ line TypeScript application spanning 4,600+ files, it runs on the Bun runtime and renders its interface through a React+Ink terminal UI. The leaked codebase (via an exposed source map) revealed an architecture far more complex than the ~279 files in the official open-source repository, exposing internal subsystems for security, telemetry, payments, and multi-agent orchestration.

Architecture Overview

Claude Code is organized around the five-layer-architecture, with the queryengine-ts monolith (~46K lines) serving as the gravitational center. The core agent loop is orchestrated by the tt-function (3,167 lines, 486 cyclomatic complexity), which drives streaming API communication, tool execution, and session management.

Core Engine

Component Description
queryengine-ts 46K-line monolith handling LLM streaming, tool dispatch, transcript recording, and session state
tt-function 3,167-line "god function" orchestrating the agent run loop with 486 cyclomatic complexity
streaming-tool-executor Executes tool calls as streaming tokens arrive, enabling parallel tool execution
message-normalization 11 transformations (smooshing, Capybara surgery, etc.) applied before every API call
system-prompt-assembly Dynamic construction of the system prompt from modular components
system-reminders Mid-conversation context injections that keep the model aligned with current state

Security and Permissions

Component Description
permission-pipeline Multi-stage approval system for tool invocations
yolo-classifier 52K-line auto-mode safety classifier with fail-open design
bash-security Bash command sandboxing and dangerous pattern detection
three-layer-verification Layered verification of tool safety across multiple checkpoints
native-attestation Hardware-backed runtime environment validation
anti-distillation-defenses Fake tools, signed CoT, Zig DRM, protocol isolation
yukonsilver-vm Apple VM isolation for Claude Desktop on macOS

Context and Memory

Component Description
memory-hierarchy Multi-level memory (session, project, user, enterprise) with LLM-based retrieval
compaction-pipeline Context window management when approaching token limits
cache-economics Prompt cache optimization for cost reduction
session-persistence Checkpoint-based session save/resume across CLI restarts
auto-memory Automatic extraction and storage of project context
auto-dream Background context synthesis during idle periods

Agent and Multi-Agent Patterns

Component Description
forked-agent-pattern Parallel subagent spawning with 92% prompt cache reuse
teammate-tool Multi-agent collaboration with team-lead coordination
ultraplan Complex task decomposition and planning system
speculation-engine Predictive file prefetching to reduce latency
kairos Session-level timing and scheduling intelligence

Platform and Extensibility

Component Description
terminal-renderer Custom React reconciler with Yoga WASM flexbox and 16ms frame budget
bridge-system IDE integration layer for VS Code and JetBrains
hooks-system 21 lifecycle events for user-defined feedback loops
plugin-system Marketplace runtime with dependency resolution and cycle detection
skill-system Reusable markdown-defined workflows invoked via slash commands
toolsearch-system Lazy tool loading (77K to 8.7K tokens) with BM25 matching
vim-mode Full Vim state machine for terminal input editing
cc-protocol Deep link remote session control from phone/browser

Telemetry and Feature Management

Component Description
growthbook Remote feature flags (79+ tengu_ flags) with hourly polling and kill switches
frustration-telemetry Regex-based user frustration detection as model regression indicator
system-prompt-fingerprinting Fingerprinting for protocol isolation and anti-distillation
undercover-mode Stealth mode hiding AI identity in public repo contributions
buddy-sprite Tamagotchi Easter egg companion pet

Search and Retrieval

Component Description
grep-over-rag Agentic grep chosen over RAG after testing both approaches

Design Principles

Eight architectural design principles govern Claude Code's development:

  1. Agentic grep over RAG -- Use the model's reasoning to search codebases rather than vector databases
  2. Fail-open safety -- When safety systems error, fall back to human prompts rather than blocking
  3. Cache-first economics -- Optimize aggressively for prompt cache hits to reduce API costs
  4. Streaming-native -- Process tokens as they arrive rather than waiting for complete responses
  5. Permission at the boundary -- Enforce safety at tool execution boundaries, not in the model
  6. Memory without vectors -- Use LLM reasoning over filenames for memory retrieval
  7. Fork for parallelism -- Spawn subagents for concurrent work rather than sequential processing
  8. Extensibility via markdown -- Define skills, hooks, and configuration in human-readable formats

Ecosystem

The Claude Code ecosystem extends beyond Anthropic's official product:

Key claims

Relations

Sources

src-20260409-6913a0b93c8b