Core pipeline
Entrypoint — src/main.tsx
The CLI parser is built with Commander.js (
@commander-js/extra-typings). On startup it:- Fires parallel prefetch side-effects (MDM settings, Keychain, API preconnect) before heavy module imports
- Parses CLI arguments and flags
- Initializes the React/Ink renderer
- Hands off to the REPL launcher (
src/replLauncher.tsx)
Initialization — src/entrypoints/
Startup performs parallel initialization — MDM policy reads, Keychain prefetch, feature flag checks — then core init.
| File | Role |
|---|---|
cli.tsx | CLI session orchestration — the main path from launch to REPL |
init.ts | Config, telemetry, OAuth, MDM policy initialization |
mcp.ts | MCP server mode entrypoint (Claude Code as an MCP server) |
sdk/ | Agent SDK — programmatic API for embedding Claude Code |
Query Engine — src/QueryEngine.ts
The heart of Claude Code (~46K lines). One
QueryEngine instance owns the lifecycle of a conversation. Each call to submitMessage() starts a new turn, streaming responses and driving tool-call loops until the LLM returns a final message.See Query Engine for a full breakdown.Tool System — src/Tool.ts + src/tools/
Every capability Claude can invoke is a tool. Each tool is self-contained with an input schema, permission model, execution logic, and terminal UI components.Tools are registered in
src/tools.ts and discovered by the Query Engine during tool-call loops.See Tool System for a full breakdown.Command System — src/commands.ts + src/commands/
User-facing slash commands (
/commit, /review, /mcp, etc.) typed in the REPL. Three command types exist: PromptCommand, LocalCommand, and LocalJSXCommand.See Command System for a full breakdown.State management
Claude Code uses a React context + custom store pattern:| Component | Location | Purpose |
|---|---|---|
AppState | src/state/AppStateStore.ts | Global mutable state object |
| Context Providers | src/context/ | React context for notifications, stats, FPS |
| Selectors | src/state/selectors.ts | Derived state functions |
| Change Observers | src/state/onChangeAppState.ts | Side-effects triggered on state changes |
AppState object is passed into tool contexts, giving every tool access to conversation history, settings, and runtime state.
Build system
Bun runtime
Claude Code runs on Bun, not Node.js. Key implications:- Native JSX/TSX support without a transpilation step
bun:bundlefeature flags for dead-code elimination at build time- ES modules with
.jsextensions (Bun convention)
Feature flags and dead-code elimination
Code inside inactive feature flags is completely stripped at build time. This keeps production binaries lean by excluding unreleased or environment-specific features.Known feature flags
Known feature flags
| Flag | Feature |
|---|---|
PROACTIVE | Proactive agent mode (autonomous actions) |
KAIROS | Kairos subsystem |
BRIDGE_MODE | IDE bridge integration |
DAEMON | Background daemon mode |
VOICE_MODE | Voice input/output |
AGENT_TRIGGERS | Triggered agent actions |
MONITOR_TOOL | Monitoring tool |
COORDINATOR_MODE | Multi-agent coordinator |
WORKFLOW_SCRIPTS | Workflow automation scripts |
Lazy loading
Heavy modules are deferred via dynamicimport() until first use to keep startup time fast:
- OpenTelemetry (~400 KB)
- gRPC (~700 KB)
- Other optional dependencies
Error handling and telemetry
| System | Location | Purpose |
|---|---|---|
| Feature flags / A/B testing | src/services/analytics/ | GrowthBook integration |
| Distributed tracing | src/services/analytics/ | OpenTelemetry traces and metrics |
| Cost tracking | src/cost-tracker.ts | Token usage and estimated cost per conversation turn |
| Diagnostics | src/screens/Doctor.tsx | API connectivity, auth, tool availability, MCP status |
Token usage and estimated cost for the current session are accessible via the
/cost command.Concurrency model
Claude Code uses a single-threaded event loop (Bun/Node.js model) with:- Async/await for all I/O operations
- React’s concurrent rendering for terminal UI updates
- Web Workers / child processes for CPU-intensive tasks (gRPC, etc.)
- Per-tool concurrency declarations — each tool implements
isConcurrencySafe()to indicate whether it can run in parallel with other tools
Explore further
Query Engine
Streaming, tool-call loops, thinking mode, retry logic, and token counting.
Tool System
The
buildTool() factory, permission model, and complete tool catalog.Command System
Slash commands, command types, and how PromptCommands inject LLM context.
State & UI Layer
React + Ink terminal UI, AppState store, hooks, screens, and config schemas.