You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Deep architecture analysis of Claude Code — Anthropic's official CLI for Claude, an AI-powered interactive development assistant.
Project Overview
Claude Code is a sophisticated TypeScript/React terminal application built with a custom Ink-like React-to-terminal renderer. It provides an AI-powered interactive shell for software engineering tasks including code editing, debugging, search, and multi-agent coordination.
Language: TypeScript + React (TSX)
Runtime: Bun (primary), with bun:bundle feature flags for compile-time dead code elimination
Terminal UI: Custom Ink-like implementation using React
Primary entry point. Handles startup profiling, prefetching (MDM settings, keychain, MCP registry), model initialization, settings/migration loading, permission setup, analytics, and routing to interactive vs headless mode.
entrypoints/cli.tsx (~39KB)
Fast-path bootstrap for special CLI flags (--version, --dump-system-prompt, --daemon-worker, --claude-in-chrome-mcp, etc.) that bypass the full app loading.
entrypoints/init.ts (~13KB)
Core system initialization before main loop — trust dialog, analytics, policy limits.
Core Engine
File
Description
QueryEngine.ts (~46KB)
Core conversation engine. Orchestrates message processing, streaming API calls with retry logic, tool execution via StreamingToolExecutor, and multiple compaction strategies (auto-compact, microcompact, reactive compact, context collapse). Manages permissions, hooks, extended thinking, and budget tracking.
query.ts (~68KB)
Main query function. Builds query config from runtime gates, prepares system/user context, fetches attachment messages (memory, CLAUDE.md), calls the API with streaming, processes tool use blocks, records transcripts, and manages token/cost budgets.
Tool System
File
Description
Tool.ts (~29KB)
Defines the fundamental Tool interface — name, description, JSON Schema parameters, ToolUseContext for execution, ToolPermissionContext for permission management, progress types, and result/error handling.
tools.ts (~17KB)
Central tool registry. getAllBaseTools() is the source of truth for all tools. assembleToolPool() combines built-in + MCP tools with dedup and sort-stable ordering for prompt cache stability. Many tools are conditionally loaded via feature() or process.env checks.
tools/ (44+ subdirectories)
Each tool is a self-contained module. Core tools include: BashTool, FileReadTool, FileEditTool, FileWriteTool, GlobTool, GrepTool, WebFetchTool, WebSearchTool, AgentTool, SkillTool, NotebookEditTool, and many more.
Tool Categories
Core: Bash, File Read/Edit/Write, Glob, Grep, Web Fetch/Search
services/teamMemorySync/: Team memory synchronization
UI Layer
File/Directory
Description
ink/
Custom terminal UI framework (React-to-terminal renderer). Core rendering engine ink.tsx (~251KB), Ansi.tsx (~33KB) for ANSI code generation, plus DOM abstraction, focus management, frame rendering, and color utilities.
screens/REPL.tsx (~895KB)
Main interactive shell screen — the primary user-facing UI.
screens/ResumeConversation.tsx (~59KB)
Session recovery UI.
screens/Doctor.tsx (~73KB)
Diagnostics and health check.
components/ (33 subdirectories)
React/Ink components: design-system (themed Box/Text), messages, permissions, tasks, mcp, diff visualization, agents, Spinner, PromptInput, Settings, and more.
Multi-Agent Coordination
File
Description
coordinator/coordinatorMode.ts (~19KB)
Multi-agent orchestration system. A coordinator spawns worker agents via AgentTool, communicates via SendMessage, and manages a durable cross-worker scratchpad. Feature-gated via COORDINATOR_MODE.
Remote Execution (CCR)
File
Description
remote/RemoteSessionManager.ts (~9KB)
Manages remote CCR (Cloud Code Runtime) sessions.
remote/SessionsWebSocket.ts (~12KB)
WebSocket client for session streaming with reconnection logic.
remote/sdkMessageAdapter.ts (~9KB)
Maps SDK messages to/from remote format.
utils/teleport/
Clone remote repository locally, validate git state, process messages for resume.
main.tsx (startup & prefetching)
→ entrypoints/init.ts (system initialization)
→ screens/REPL.tsx (interactive shell)
→ User input via PromptInput component
→ query() function (query.ts)
→ QueryEngine.ts (conversation orchestration)
→ queryModelWithStreaming (Claude API call)
→ runTools (execute tool calls)
→ normalizeMessages / compact (context management)
→ Response rendered back to UI
Multi-Agent Coordination
User message → Coordinator (query.ts)
→ AgentTool spawns worker agents
→ Workers run in separate contexts with limited tools
→ Worker results returned as task-notification messages
→ Coordinator synthesizes results for user
Remote Execution (CCR)
Local CLI (CLAUDE_CODE_REMOTE=true)
→ RemoteSessionManager
→ SessionsWebSocket (WebSocket streaming)
→ Remote CCR server → Remote QueryEngine → Remote tools
→ Results streamed back via WebSocket
Architecture Patterns
Feature Gating (Three Levels)
Compile-time: feature() from bun:bundle for dead code elimination