How to navigate and study the Claude Code source code.
This is a read-only reference codebase — there’s no build system or test suite. The goal is to understand how a production AI coding assistant is built.
| What | Where |
|---|---|
| CLI entrypoint | src/main.tsx |
| Core LLM engine | src/QueryEngine.ts (~46K lines) |
| Tool definitions | src/Tool.ts (~29K lines) |
| Command registry | src/commands.ts (~25K lines) |
| Tool registry | src/tools.ts |
| Context collection | src/context.ts |
| All tool implementations | src/tools/ (40 subdirectories) |
| All command implementations | src/commands/ (~85 subdirectories + 15 files) |
src/tools/{ToolName}/{ToolName}.ts or .tsxUI.tsxprompt.tsExample — understanding BashTool:
src/tools/BashTool/
├── BashTool.ts ← Core execution logic
├── UI.tsx ← How bash output renders in terminal
├── prompt.ts ← What the system prompt says about bash
└── ...
src/commands/{command-name}/ (directory) or src/commands/{command-name}.ts (file)getPromptForCommand() function (PromptCommands) or direct implementation (LocalCommands)| Feature | Start Here |
|---|---|
| Permissions | src/hooks/toolPermission/ |
| IDE bridge | src/bridge/bridgeMain.ts |
| MCP client | src/services/mcp/ |
| Plugin system | src/plugins/ + src/services/plugins/ |
| Skills | src/skills/ |
| Voice input | src/voice/ + src/services/voice.ts |
| Multi-agent | src/coordinator/ |
| Memory | src/memdir/ |
| Authentication | src/services/oauth/ |
| Config schemas | src/schemas/ |
| State management | src/state/ |
Trace from user input to API response:
src/main.tsx ← CLI parsing
→ src/replLauncher.tsx ← REPL session start
→ src/QueryEngine.ts ← Core engine
→ src/services/api/ ← Anthropic SDK client
→ (Anthropic API) ← HTTP/streaming
← Tool use response
→ src/tools/{ToolName}/ ← Tool execution
← Tool result
→ (feed back to API) ← Continue the loop
buildTool() — Tool FactoryEvery tool uses this pattern:
export const MyTool = buildTool({
name: 'MyTool',
inputSchema: z.object({ ... }),
async call(args, context) { ... },
async checkPermissions(input, context) { ... },
})
import { feature } from 'bun:bundle'
if (feature('VOICE_MODE')) {
// This code is stripped at build time if VOICE_MODE is off
}
if (process.env.USER_TYPE === 'ant') {
// Anthropic employee-only features
}
Most directories have an index.ts that re-exports the public API:
// src/tools/BashTool/index.ts
export { BashTool } from './BashTool.js'
Heavy modules are loaded only when needed:
const { OpenTelemetry } = await import('./heavy-module.js')
.js ExtensionsBun convention — all imports use .js extensions even for .ts files:
import { something } from './utils.js' // Actually imports utils.ts
The largest files contain the most logic and are worth studying:
| File | Lines | What’s Inside |
|---|---|---|
QueryEngine.ts |
~46K | Streaming, tool loops, retries, token counting |
Tool.ts |
~29K | Tool types, buildTool, permission models |
commands.ts |
~25K | Command registry, conditional loading |
main.tsx |
— | CLI parser, startup optimization |
context.ts |
— | OS, shell, git, user context assembly |
src/Tool.ts — understand the buildTool interfaceFileReadTool in src/tools/FileReadTool/QueryEngine.ts calls tools during the tool loopsrc/hooks/toolPermission/src/screens/REPL.tsx — the main screensrc/components/ — pick a few componentssrc/hooks/useTextInput.ts — how user input is capturedsrc/ink/ — the Ink renderer wrappersrc/bridge/bridgeMain.tsbridgeMessaging.ts for the message protocolbridgePermissionCallbacks.ts for how permissions route to the IDEreplBridge.ts for REPL session bridgingsrc/types/plugin.ts — the plugin API surfacesrc/services/plugins/ — how plugins are loadedsrc/plugins/builtinPlugins.ts — built-in examplessrc/plugins/bundled/ — bundled plugin codesrc/services/mcp/ — the MCP clientsrc/tools/MCPTool/ — how MCP tools are invokedsrc/entrypoints/mcp.ts — Claude Code as an MCP serversrc/skills/mcpSkillBuilders.ts — skills from MCPThis repo includes a standalone MCP server (mcp-server/) that lets any MCP-compatible client explore the source code. See the MCP Server README for setup.
Once connected, you can ask an AI assistant to explore the source:
Useful grep/ripgrep patterns for finding things:
# Find all tool definitions
rg "buildTool\(" src/tools/
# Find all command definitions
rg "satisfies Command" src/commands/
# Find feature flag usage
rg "feature\(" src/
# Find Anthropic-internal gates
rg "USER_TYPE.*ant" src/
# Find all React hooks
rg "^export function use" src/hooks/
# Find all Zod schemas
rg "z\.object\(" src/schemas/
# Find all system prompt contributions
rg "prompt\(" src/tools/*/prompt.ts
# Find permission rule patterns
rg "checkPermissions" src/tools/