Skip to main content
Claude Code is a terminal-native AI coding assistant built as a single-binary CLI. It follows a pipeline model from raw user input to rendered terminal output.
User Input → CLI Parser → Query Engine → LLM API → Tool Execution Loop → Terminal UI
The entire UI layer is built with React + Ink (React for the terminal), making Claude Code a fully reactive CLI application with components, hooks, and state management — rendered to the terminal instead of the browser.

Core pipeline

1

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)
2

Initialization — src/entrypoints/

Startup performs parallel initialization — MDM policy reads, Keychain prefetch, feature flag checks — then core init.
FileRole
cli.tsxCLI session orchestration — the main path from launch to REPL
init.tsConfig, telemetry, OAuth, MDM policy initialization
mcp.tsMCP server mode entrypoint (Claude Code as an MCP server)
sdk/Agent SDK — programmatic API for embedding Claude Code
3

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.
4

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.
5

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:
ComponentLocationPurpose
AppStatesrc/state/AppStateStore.tsGlobal mutable state object
Context Providerssrc/context/React context for notifications, stats, FPS
Selectorssrc/state/selectors.tsDerived state functions
Change Observerssrc/state/onChangeAppState.tsSide-effects triggered on state changes
The 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:bundle feature flags for dead-code elimination at build time
  • ES modules with .js extensions (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.
import { feature } from 'bun:bundle'

// Code inside an inactive flag is stripped from the binary at build time
const voiceCommand = feature('VOICE_MODE')
  ? require('./commands/voice/index.js').default
  : null
FlagFeature
PROACTIVEProactive agent mode (autonomous actions)
KAIROSKairos subsystem
BRIDGE_MODEIDE bridge integration
DAEMONBackground daemon mode
VOICE_MODEVoice input/output
AGENT_TRIGGERSTriggered agent actions
MONITOR_TOOLMonitoring tool
COORDINATOR_MODEMulti-agent coordinator
WORKFLOW_SCRIPTSWorkflow automation scripts

Lazy loading

Heavy modules are deferred via dynamic import() until first use to keep startup time fast:
  • OpenTelemetry (~400 KB)
  • gRPC (~700 KB)
  • Other optional dependencies

Error handling and telemetry

SystemLocationPurpose
Feature flags / A/B testingsrc/services/analytics/GrowthBook integration
Distributed tracingsrc/services/analytics/OpenTelemetry traces and metrics
Cost trackingsrc/cost-tracker.tsToken usage and estimated cost per conversation turn
Diagnosticssrc/screens/Doctor.tsxAPI 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
Tools default to isConcurrencySafe: false. Only tools that explicitly declare concurrency safety run in parallel during multi-tool LLM responses.

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.

Build docs developers (and LLMs) love