Implementation-ready reverse engineering specifications for Anthropic's Claude Code CLI tool. Extracted via source map analysis of the npm package (March 2026).
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Claude Code CLI β
β β
β ~1,900 files Β· 512,000+ LOC Β· TypeScript Strict Β· Bun β
β β
β ββββββββββββ ββββββββββββ ββββββββββββ ββββββββββββ β
β β React β β Ink β β Yoga β βCommander β β
β β 19 β β (custom β β (WASM β β .js β β
β β β β fork) β β layout) β β β β
β ββββββ¬ββββββ ββββββ¬ββββββ ββββββ¬ββββββ ββββββ¬ββββββ β
β ββββββββββββββββ΄βββββββββββββββ΄βββββββββββββββ β
β Terminal UI β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β Core Engine β β
β β βββββββββββ ββββββββββββ βββββββββββββ ββββββββββββββββ β β
β β β 45+ β β Query β β Streaming β β Cost β β β
β β β Tools β β Engine β β Executor β β Tracker β β β
β β βββββββββββ ββββββββββββ βββββββββββββ ββββββββββββββββ β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β Extension Layer β β
β β βββββββ ββββββββββ ββββββββββ ββββββββ βββββββ ββββββββ β β
β β β MCP β βPlugins β β Skills β βHooks β β LSP β βBridgeβ β β
β β βββββββ ββββββββββ ββββββββββ ββββββββ βββββββ ββββββββ β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β Services β β
β β ββββββββββ ββββββββ βββββββββββββ ββββββββββ βββββββββββ β β
β β β API β β Auth β β Analytics β βSettingsβ βMigrationβ β β
β β β(4 back)β βOAuth β β DD+OTel β β5-layer β β chain β β β
β β ββββββββββ ββββββββ βββββββββββββ ββββββββββ βββββββββββ β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β Security & Permissions β β
β β ββββββββββββ βββββββββββββ ββββββββββββ ββββββββββββββββ β β
β β β 7 Perm β β 23 Bash β β Path β β Auto-Mode β β β
β β β Modes β β Checks β βValidator β β Classifier β β β
β β ββββββββββββ βββββββββββββ ββββββββββββ ββββββββββββββββ β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
docs/01-architecture-overview.mdΒ· 1,101 lines
Startup Sequence (5 phases)
βββββββββββββββββββββββββββββββββββββββββββ
Phase 0 ββΊ Module-level side effects (MDM, Keychain prefetch)
Phase 1 ββΊ main() entry, basic initialization
Phase 2 ββΊ Commander preAction β init() β migrations
Phase 3 ββΊ action() β setup() β auth/permissions
Phase 4 ββΊ REPL render + deferred prefetches
| Topic | Key Details |
|---|---|
| Module System | ESM-only, .js extensions required, lazy require() for cycles |
| Build | Bun bundler, feature() flags for dead code elimination |
| Config | 7 layers: Global β Project β Settings(5 sources) β CLI β MDM |
| Feature Flags | 89 flags via GrowthBook, bun:bundle compile-time DCE |
| State | 110 global fields in bootstrap/state.ts, DO NOT ADD MORE policy |
docs/02-core-engine.mdΒ· 1,352 lines
Tool Execution Pipeline
βββββββββββββββββββββββββββββββββββββββββββ
User Input βββΊ QueryEngine βββΊ LLM API (streaming)
β
tool_use block
β
βββββββββββββββββΌββββββββββββββββ
βΌ βΌ βΌ
ββββββββββββ ββββββββββββ ββββββββββββ
β Bash β β FileEdit β β Agent β
β Tool β β Tool β β Tool β
ββββββ¬ββββββ ββββββ¬ββββββ ββββββ¬ββββββ
β β β
βΌ βΌ βΌ
ToolResult ToolResult ToolResult
{ data, newMessages?, contextModifier?, mcpMeta? }
β
βββββββ
Next LLM turn with results
| Topic | Key Details |
|---|---|
| Tool Interface | 60+ fields, buildTool() factory, 7 defaultable keys |
| Concurrency | isConcurrencySafe flag β safe tools run parallel, unsafe exclusive |
| Recovery | 8-stage chain: streaming fallback β model fallback β collapse drain β reactive compact |
| Cost Tracking | Per-session USD accumulator, multi-provider normalization |
docs/03-permission-security.mdΒ· 1,178 lines
Permission Decision Pipeline (4 steps + auto-mode)
βββββββββββββββββββββββββββββββββββββββββββββββββββ
Step 0 Config deny rules βββΊ deny (immediate)
Step 1 Tool.checkPermissions() βββΊ allow/deny/ask/passthrough
Step 2 Permanent allow rules βββΊ allow (skip prompt)
Step 3 passthrough β ask conversion
Step 4 Post-processing:
βββ dontAsk mode βββΊ auto deny
βββ auto mode βββΊ classifier pipeline:
β βββ acceptEdits fast-path
β βββ safe-tool allowlist
β βββ YOLO classifier (2-stage)
β βββ allowed βββΊ allow
β βββ blocked βββΊ deny + denial tracking
β βββ unavailable βββΊ iron_gate fail-closed
βββ headless βββΊ hook or deny
| Topic | Key Details |
|---|---|
| 7 Modes | default, plan, acceptEdits, bypassPermissions, dontAsk, auto, bubble |
| Bash Security | 23 check IDs, 19 cross-platform + 7 Unix + 11 ANT-only patterns |
| Path Validation | 6-step: normalize β symlink resolve β traversal β Windows patterns β scope |
| TOCTOU Warning | Path validation and file access are not atomic β race condition window |
docs/04-multi-agent-memory.mdΒ· 1,416 lines
Agent Hierarchy
βββββββββββββββββββββββββββββββββββββββββββ
βββββββββββββββββ
β Coordinator β (orchestrator β no direct tool use)
βββββββββ¬ββββββββ
ββββββββββββββΌβββββββββββββ
βΌ βΌ βΌ
ββββββββββββ ββββββββββββ ββββββββββββ
β Worker β β Worker β β Worker β
β (fork) β β (spawn) β β(in-proc) β
ββββββββββββ ββββββββββββ ββββββββββββ
Memory Hierarchy (5 layers)
βββββββββββββββββββββββββββββββββββββββββββ
[1] Session Memory ββ in-context summaries
[2] Project Memory ββ .claude/memory/ files
[3] User Memory ββ ~/.claude/memory/
[4] Team Memory ββ server-synced shared memory
[5] External ββ MCP servers, LSP, git
| Topic | Key Details |
|---|---|
| 3 Swarm Backends | fork (shared context), spawn (independent), in-process teammate |
| Task Types | 7 types with ID prefixes: bash, workflow, agent, teammate, remote, monitor, dream |
| Mailbox | File-based: .claude/teams/{team}/inboxes/{agent}.json |
| Team Sync | ETag-based conflict detection (412), no local file locking |
docs/05-extension-systems.mdΒ· 1,709 lines
6 Extension Points
βββββββββββββββββββββββββββββββββββββββββββ
βββββββ ββββββββββ ββββββββββ ββββββββ βββββββ ββββββββ
β MCP β βPlugins β β Skills β βHooks β β LSP β βBridgeβ
β β β β β β β β β β β β
β8 cfgβ βmarket- β β17 bun- β β27 β βcode β βIDE β
βtypesβ βplace + β βdled + β βeventsβ βintelβ βcomms β
β β βbuiltin β βcustom β β β β β β β
βββββββ ββββββββββ ββββββββββ ββββββββ βββββββ ββββββββ
| Topic | Key Details |
|---|---|
| MCP Transports | TransportSchema: 6 types; McpServerConfig: 8 (+ ws-ide, claudeai-proxy) |
| Hook Priority | deny > ask > allow β any hook's deny overrides all allows |
| Plugin Loading | Parallel (marketplace + session) β builtins β merge β dependency verify |
| Reconnection | MAX_ERRORS_BEFORE_RECONNECT=3, SSE maxRetries=2, 15min poll timeout |
docs/06-ui-layer.mdΒ· 1,784 lines
Render Pipeline
βββββββββββββββββββββββββββββββββββββββββββ
React Component Tree
β
βΌ
Custom Reconciler (React 19 HostConfig)
β
βΌ
Yoga Layout Engine (WASM)
β ββββββββββββββββββββββββ
ββββ Box (flexbox node) β
ββββ Text (measure func) β
ββββ Button (interactive) β
ββββββββββββββββββββββββ
β
βΌ
Output Renderer (ANSI diff)
β
βΌ
Terminal stdout (throttled at FRAME_INTERVAL_MS)
| Topic | Key Details |
|---|---|
| Ink Fork | 96 files in src/ink/, custom reconciler, Yoga WASM integration |
| Components | 346 .tsx files in src/components/ |
| Hooks | 85+ custom hooks (useTextInput, useTerminalSize, useStdin, etc.) |
| Concurrency | maySuspendCommit() always returns false β Suspense disabled |
docs/07-services-infrastructure.mdΒ· 1,845 lines
API Client Factory (4 backends)
βββββββββββββββββββββββββββββββββββββββββββ
ββββββββββββββββββββ
β buildFetch() β
β + withRetry() β
ββββββββββ¬ββββββββββ
ββββββββββββββββββΌββββββββββββββββββββββββββββββββ
βΌ βΌ βΌ βΌ
ββββββββββββ ββββββββββββ ββββββββββββ ββββββββββββ
β Anthropicβ β Bedrock β β Foundry β β Vertex β
β (Direct) β β (AWS) β β (Azure) β β (GCP) β
ββββββββββββ ββββββββββββ ββββββββββββ ββββββββββββ
Retry Strategy: 429 β Retry-After (β€20s: sleep, >20s: 30min cooldown)
529 β MAX_529_RETRIES=3 (non-subscriber only)
401 β token refresh β retry (no max refresh limit!)
| Topic | Key Details |
|---|---|
| Analytics | Datadog (44 allowed events, 16 tag fields) + OpenTelemetry + 1P logger |
| OAuth | PKCE flow, macOS Keychain with plaintext fallback (30s TTL cache) |
| Settings Merge | 5 sources: user β project β local β flag β policy (arrays concat, not override!) |
| Migrations | 11 sync + 1 async, includes migrateLegacyOpusToCurrent (ant-only DCE) |
docs/08-types-schemas-api.mdΒ· 2,194 lines
Type System Layers
βββββββββββββββββββββββββββββββββββββββββββ
βββββββββββββββββββββββββββββββββββββββββββ
β Branded IDs β
β SessionId, AgentId, TaskId, ToolUseId β
βββββββββββββββββββββββββββββββββββββββββββ€
β Core Types β
β Tool<I,O,P>, ToolResult<T>, β
β ValidationResult, ToolUseContext (30+) β
βββββββββββββββββββββββββββββββββββββββββββ€
β Zod Schemas β
β HookCommand (discriminated union), β
β SettingsSchema (~100 fields), β
β TransportSchema (6 literals) β
βββββββββββββββββββββββββββββββββββββββββββ€
β AppState β
β DeepImmutable<> wrapper, β
β ~85 fields + Store<T> pattern β
βββββββββββββββββββββββββββββββββββββββββββ€
β Environment Variables (60+) β
β Feature Flags (89) β
β Analytics Events (44 Datadog) β
βββββββββββββββββββββββββββββββββββββββββββ
| Topic | Key Details |
|---|---|
| SettingsJson | ~100 fields across 15 categories (auth, permissions, MCP, hooks, model, UI...) |
| Settings Merge | policySettings internal priority: remote > HKLM/plist > file > HKCU |
| Env Vars | 60+ variables across 7 groups (API, model, auth, debug, feature, proxy, internal) |
| Feature Flags | 89 bun:bundle feature() calls, compile-time dead code elimination |
infrastructure/β Complete deployment blueprints with Terraform IaC
Deployment Options
βββββββββββββββββββββββββββββββββββββββββββ
ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ
β Local Dev β β AWS β β Azure β β Supabase β
β β β β β β β β
β Ollama/vLLM β β ECS Fargate β β Container β β Edge Funcs β
β Docker Stack β β Bedrock β β Apps β β Realtime β
β RTX 4090 β β Lambda β β Azure OpenAI β β PostgreSQL β
β β β Cognito β β AD B2C β β Auth β
β $0-50/mo β β $45-180/mo β β $50-190/mo β β $0-25/mo β
ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ
| Document | Description |
|---|---|
01-local-development.md |
Ollama, vLLM, LM Studio, Docker Compose full stack |
02-aws-architecture.md |
VPC, ECS, Bedrock, Lambda sandbox, CloudWatch |
03-azure-architecture.md |
Container Apps, Azure OpenAI, Functions, Monitor |
04-supabase-backend.md |
10-table schema, RLS policies, Edge Functions |
05-cost-analysis.md |
Real pricing: $9/mo solo β $25/dev enterprise |
06-architecture-diagram.md |
7 ASCII diagrams: system, data flow, auth, agents |
terraform/ |
10 .tf files β production-ready AWS IaC |
| Scale | Supabase + API | AWS Full | Hybrid (Local GPU) |
|---|---|---|---|
| Solo (1 dev) | $9β103/mo | $240β280/mo | $50/mo |
| Team (10 devs) | $46β116/dev | $65β110/dev | $35β60/dev |
| Enterprise (100 devs) | N/A | $45β111/dev | $25/dev |
LLM token costs represent 60β95% of total spend at every scale.
These documents went through 8 rigorous verification passes against the actual source code:
Pass 1 ββββββββββββββββ 16 corrections (type/value accuracy)
Pass 2 ββββ 4 fixes (blocking implementation gaps)
Pass 3 βββββββ 7 corrections (line-by-line precision)
Pass 4 βββ 3 fixes (missing schemas/flows)
Pass 5 βββββββ 7 additions (Bridge, MDM, Keychain, Plugin)
Pass 6 ββββββββββββββ 42 caveats (structural problem warnings)
Pass 7 ββ 2 micro-fixes (convergence check)
Pass 8 βββββββββββββββ CONVERGED (24/24 spot-checks verified)
βββββββββββββββ
Total: 39 corrections + 42 implementation caveats
# Clone the repo
git clone https://github.com/jung-wan-kim/claude-code-reverse-engineering.git
cd claude-code-reverse-engineering
# Read the design docs (start here)
open docs/01-architecture-overview.md
# Deploy infrastructure (AWS)
cd infrastructure/terraform
cp terraform.tfvars.example terraform.tfvars
# Edit terraform.tfvars with your settings
terraform init && terraform planclaude-code-reverse-engineering/
βββ README.md β You are here
βββ docs/
β βββ 01-architecture-overview.md (1,101 lines)
β βββ 02-core-engine.md (1,352 lines)
β βββ 03-permission-security.md (1,178 lines)
β βββ 04-multi-agent-memory.md (1,416 lines)
β βββ 05-extension-systems.md (1,709 lines)
β βββ 06-ui-layer.md (1,784 lines)
β βββ 07-services-infrastructure.md (1,845 lines)
β βββ 08-types-schemas-api.md (2,194 lines)
βββ infrastructure/
βββ 01-local-development.md
βββ 02-aws-architecture.md
βββ 03-azure-architecture.md
βββ 04-supabase-backend.md
βββ 05-cost-analysis.md
βββ 06-architecture-diagram.md
βββ terraform/
βββ main.tf
βββ variables.tf
βββ vpc.tf
βββ compute.tf
βββ database.tf
βββ storage.tf
βββ auth.tf
βββ monitoring.tf
βββ outputs.tf
βββ terraform.tfvars.example
This is a reverse-engineering effort for educational and security research purposes only. All rights to the original Claude Code software belong to Anthropic.
MIT (documentation only β does not cover the original Claude Code software)