Claude Code CLI
This documentation was generated with Doccupine a free and open-source documentation platform that turns a folder of MDX files into a production-ready site with AI search, built-in components, and an MCP server in one command. Check out the Doccupine docs to learn more.
Claude Code is an AI-powered command-line interface for software engineering tasks. It operates as an agentic coding assistant that can read, write, and reason about code in real-time, directly from your terminal. Built on Anthropic's Claude models, it provides an interactive REPL as well as a headless SDK mode for programmatic integration. This documentation covers the internal source code architecture -- the tools, services, state management, query pipeline, and extension systems that make it all work. Whether you are contributing to the CLI, building integrations on top of it, or just curious about how an agentic coding tool is put together, you will find detailed breakdowns of every major subsystem here.
This documentation covers the internal source code architecture of Claude Code. It is intended for contributors and developers working on the CLI itself, not end-user usage guides.
High-Level Architecture
The following diagram shows how the major subsystems connect during a typical session:
CLI Entry (cli.tsx)
|
v
Initialization (init.ts)
|-- Config & Environment
|-- Shutdown Handlers
|-- Telemetry & OAuth
|
v
Main Bootstrap (main.tsx)
|-- MDM Subprocess (parallel)
|-- Keychain Prefetch (parallel)
|-- File System & Project Setup
|-- Prefetch (API, MCP, GrowthBook)
|
+---> Interactive Mode (REPL) Headless Mode (SDK / -p flag)
| | |
| v v
| UI Rendering (Ink/React) QueryEngine.submitMessage()
| | |
+-------+-------------------------------+
|
v
Query Pipeline (query.ts)
|-- Permission Check
|-- Message Normalization
|-- Context Building (System Prompt, User/System Context)
|-- API Call (Streaming)
|-- Tool Orchestration (StreamingToolExecutor)
|-- Stop Hooks
|-- Response Streaming (AsyncGenerator<StreamEvent | Message>)
|
v
Tool Execution
|-- Built-in Tools (Bash, Edit, Read, Write, Grep, Glob, ...)
|-- MCP Tools (external servers)
|-- Agent Tools (sub-agents)
|-- Plugin ToolsKey Concepts
Tools
Tools are the primary mechanism through which Claude interacts with the file system, executes commands, and performs actions. Each tool defines its own input schema, permission requirements, and execution logic. Tools range from file operations (Read, Write, Edit) to shell execution (Bash) to search (Grep, Glob) and beyond.
Services
Services provide shared infrastructure that tools and the query pipeline depend on. This includes the API client for communicating with Claude models, MCP (Model Context Protocol) server management, analytics and telemetry, compaction (context window management), and LSP integration.
Commands
Commands are slash-commands available in the interactive REPL (e.g., /help, /compact, /config). They are registered declaratively and can modify session state, trigger actions, or display information.
Queries
The query pipeline is the core agentic loop. It takes a user message, builds the full prompt context (system prompt, tools, conversation history), streams the response from the API, orchestrates any tool calls the model makes, and yields the results back as an async generator of StreamEvent and Message objects.
State
State is split into two layers:
- Bootstrap State -- mutable global state in
bootstrap/state.tsthat holds session-level values likecwd,sessionId, model overrides, telemetry counters, and feature flags. Set once during startup and mutated sparingly. - AppState -- an immutable (via
DeepImmutable) store managed throughAppStateStore.tsthat drives the UI. It holds settings, model selection, tool permissions, MCP connections, plugin state, and UI-specific flags.
Permissions
The permission system controls which tools the model can invoke and under what conditions. It supports multiple modes (default, auto, bypass) and integrates with policy limits, managed settings, and per-tool permission contexts.
Documentation Sections
Architecture
Startup lifecycle, state management, query processing pipeline, and streaming architecture.
Entry Points
CLI bootstrap, initialization phases, interactive vs headless modes, and SDK usage.
Tools
Built-in tools, tool registration, input schemas, permission handling, and MCP tool integration.
Services
API client, MCP server management, analytics, compaction, LSP, and remote managed settings.
Commands
Slash-command system, command registration, and built-in commands reference.
State & Query
AppState store, bootstrap state, the query loop, QueryEngine, and streaming event types.
UI
Ink/React rendering, REPL components, prompt input, message display, and theming.
Extensions
Plugin system, skill framework, hooks, and extending Claude Code with custom functionality.
Advanced
Permissions, bridge mode, remote sessions, vim/voice, cost tracking, and feature flags.
Claude Code uses Bun as its JavaScript runtime and bundles with bun:bundle feature flags for dead code elimination. Many internal modules are conditionally imported via feature() gates that strip entire subsystems from external builds.