Codebase Stats
This page provides a quantitative overview of the Claude Code codebase, helping you understand its scale, structure, and where complexity concentrates.
Overall Scale
Section titled “Overall Scale”| Metric | Value |
|---|---|
| Total files | ~1,884 |
| Total lines | 512,000+ |
| Primary language | TypeScript (100% of application code) |
| Runtime | Bun |
| Build system | Bun bundler with feature flags |
Claude Code is a large-scale TypeScript application — comparable in size to mid-size open-source projects like Vite or Remix.
Module Distribution
Section titled “Module Distribution”The source code is organized into several major directories under src/:
Top-Level Files (Core Orchestration)
Section titled “Top-Level Files (Core Orchestration)”The root of src/ contains the critical orchestration files:
| File | Role | Estimated Size |
|---|---|---|
main.tsx | CLI framework, Commander.js registration | ~785KB (largest file) |
commands.ts | Command registry, 60+ command management | ~755 lines |
query.ts | Agentic loop, turn orchestration | Large |
QueryEngine.ts | Session lifecycle manager | ~1,296 lines |
Tool.ts | Tool interface definition | ~793 lines |
tools.ts | Tool pool assembly | ~390 lines |
setup.ts | Post-init environment setup | ~478 lines |
cost-tracker.ts | Usage/cost tracking | Medium |
Major Directories
Section titled “Major Directories”pie title Source Code Distribution (estimated) "tools/" : 25 "utils/" : 20 "services/" : 15 "commands/" : 15 "components/" : 8 "entrypoints/" : 3 "types/" : 3 "Other" : 11src/tools/ — Tool Implementations (~25% of codebase)
Section titled “src/tools/ — Tool Implementations (~25% of codebase)”The largest directory, containing 30+ tool implementations:
| Tool Directory | Purpose |
|---|---|
AgentTool/ | Sub-agent spawning and orchestration |
BashTool/ | Shell command execution |
FileEditTool/ | Surgical file editing with diff-based patches |
FileReadTool/ | File reading with image/PDF support |
FileWriteTool/ | File creation and overwriting |
GlobTool/ | File pattern matching (ripgrep-based) |
GrepTool/ | Content search (ripgrep-based) |
WebFetchTool/ | URL fetching and content extraction |
WebSearchTool/ | Web search integration |
NotebookEditTool/ | Jupyter notebook editing |
SkillTool/ | Skill invocation bridge |
TodoWriteTool/ | Task list management |
ToolSearchTool/ | Deferred tool discovery |
MCPTool/ | MCP tool wrapper |
TaskOutputTool/ | Background task monitoring |
TaskStopTool/ | Task termination |
EnterPlanModeTool/ | Plan mode activation |
ExitPlanModeTool/ | Plan mode exit |
AskUserQuestionTool/ | User interaction requests |
SendMessageTool/ | Inter-agent messaging |
TeamCreateTool/ | Agent swarm creation |
REPLTool/ | REPL environment (ant-only) |
PowerShellTool/ | Windows PowerShell support |
LSPTool/ | Language Server Protocol bridge |
src/utils/ — Utility Modules (~20%)
Section titled “src/utils/ — Utility Modules (~20%)”Cross-cutting utility functions and subsystems:
| Subdirectory | Purpose |
|---|---|
settings/ | Configuration loading, validation, caching |
permissions/ | Permission engine, filesystem guards |
model/ | Model selection, deprecation, provider routing |
plugins/ | Plugin loading, caching, hot-reload |
hooks/ | Tool use hooks, file change watching |
swarm/ | Agent swarm reconnection, teammate snapshots |
src/services/ — Service Layer (~15%)
Section titled “src/services/ — Service Layer (~15%)”Backend services and external integrations:
| Subdirectory | Purpose |
|---|---|
api/ | claude.ts, withRetry.ts, errors.ts, bootstrap.ts |
analytics/ | GrowthBook feature flags, telemetry |
mcp/ | MCP client, server management, config |
compact/ | Auto-compaction, reactive compaction |
tools/ | StreamingToolExecutor, toolOrchestration |
oauth/ | OAuth client and token management |
policyLimits/ | Enterprise policy enforcement |
remoteManagedSettings/ | Remote settings synchronization |
src/commands/ — Slash Commands (~15%)
Section titled “src/commands/ — Slash Commands (~15%)”60+ user-facing slash commands:
| Category | Commands |
|---|---|
| Navigation | /help, /status, /doctor, /config |
| Model | /model, /fast, /effort |
| Session | /resume, /session, /compact, /clear |
| Files | /files, /diff, /context |
| Git | /commit, /pr_comments, /review |
| System | /login, /logout, /upgrade |
| Customization | /theme, /color, /vim, /keybindings |
| Development | /mcp, /skills, /hooks, /plugin |
src/components/ — UI Components (~8%)
Section titled “src/components/ — UI Components (~8%)”React/Ink TUI components:
| Component | Purpose |
|---|---|
Spinner.js | Activity indicator with mode support |
MessageSelector.js | Message filtering and selection |
| Various UI primitives | Text rendering, progress, dialogs |
Core File Ranking (Top 20 by Importance)
Section titled “Core File Ranking (Top 20 by Importance)”These are the files you should read first to understand Claude Code’s architecture:
| Rank | File | Why It Matters |
|---|---|---|
| 1 | src/main.tsx | CLI entry, Commander.js, REPL launch — the spine |
| 2 | src/query.ts | Agentic loop — the brain |
| 3 | src/QueryEngine.ts | Session lifecycle — SDK/headless entry |
| 4 | src/Tool.ts | Tool interface — the contract every tool implements |
| 5 | src/tools.ts | Tool pool assembly — which tools are available |
| 6 | src/commands.ts | Command registry — slash command management |
| 7 | src/services/api/claude.ts | API streaming — raw communication layer |
| 8 | src/services/api/withRetry.ts | Retry logic — resilience backbone |
| 9 | src/services/api/errors.ts | Error handling — user-facing error messages |
| 10 | src/setup.ts | Environment setup — post-init configuration |
| 11 | src/entrypoints/cli.tsx | Bootstrap — fast-path routing |
| 12 | src/entrypoints/init.ts | Initialization — configs, proxy, telemetry |
| 13 | src/services/tools/StreamingToolExecutor.ts | Concurrent tool execution |
| 14 | src/utils/settings/settings.ts | Configuration loading and merging |
| 15 | src/utils/settings/constants.ts | Setting sources and precedence |
| 16 | src/utils/claudemd.ts | CLAUDE.md loading and processing |
| 17 | src/utils/permissions/permissions.ts | Permission engine core |
| 18 | src/services/compact/autoCompact.ts | Auto-compaction logic |
| 19 | src/tools/BashTool/BashTool.ts | Shell execution (most complex tool) |
| 20 | src/tools/AgentTool/AgentTool.ts | Sub-agent orchestration |
Language and Technology Breakdown
Section titled “Language and Technology Breakdown”TypeScript Patterns
Section titled “TypeScript Patterns”The codebase extensively uses:
- Zod schemas for runtime validation (tool inputs, settings)
- AsyncGenerator for streaming data flow
- Discriminated unions for message types and command types
- Branded types for IDs (
AgentId,UUID) - Conditional types for tool definitions (
BuiltTool<D>)
Build-Time Features
Section titled “Build-Time Features”Bun’s feature() function enables dead code elimination:
// Features are compile-time constantsfeature('PROACTIVE') // Proactive agent featuresfeature('KAIROS') // Assistant modefeature('BRIDGE_MODE') // Remote control bridgefeature('COORDINATOR_MODE') // Multi-agent coordinatorfeature('HISTORY_SNIP') // History snippingfeature('WORKFLOW_SCRIPTS') // Workflow automationfeature('VOICE_MODE') // Voice inputfeature('UDS_INBOX') // Unix domain socket messagingfeature('CONTEXT_COLLAPSE') // Context window managementDependency Analysis
Section titled “Dependency Analysis”Core runtime dependencies:
| Category | Packages |
|---|---|
| API | @anthropic-ai/sdk |
| CLI | @commander-js/extra-typings, chalk |
| UI | react, ink (Ink renderer) |
| Validation | zod/v4 |
| Utilities | lodash-es, strip-ansi, picomatch |
| Markdown | marked |
| MCP | @modelcontextprotocol/sdk |
| Search | ripgrep (via subprocess) |
Internal module dependencies (high fan-out files):
| File | Approximate Import Count |
|---|---|
main.tsx | 100+ imports |
query.ts | 40+ imports |
QueryEngine.ts | 30+ imports |
claude.ts | 40+ imports |
Tool.ts | 20+ imports |
Complexity Hotspots
Section titled “Complexity Hotspots”Areas with the highest density of logic:
-
src/main.tsx— The 785KB monolith handles CLI parsing, MCP config, authentication, and REPL launch. This is the primary candidate for future decomposition. -
src/services/api/errors.ts— 1,200+ lines of error classification covering every API error variant across direct, Bedrock, and Vertex providers. -
src/services/api/withRetry.ts— 823 lines handling 10+ retry scenarios including persistent retry for unattended sessions, fast mode fallback, and OAuth token refresh. -
src/utils/claudemd.ts— Complex file discovery traversing directory trees, handling@includedirectives, frontmatter parsing, and deduplication. -
src/services/tools/StreamingToolExecutor.ts— Concurrent tool execution with ordering guarantees, error propagation, and abort handling.