The heart of the system. Every time the user sends a message, a query loop starts:
Plain text
while (true) {
1. Prepare messages (compact/snip/collapse if needed)
2. Call the AI model (streaming)
3. For each tool_use in response → execute the tool
4. If no more tool_use → done, return
5. If tool results → loop back to step 1
}
The loop handles recovery from errors (prompt too long, max output tokens, model fallback), context compaction, and streaming.
2. Tools
Tools are how the AI interacts with the world. Each tool has:
Core tools: Bash, FileRead, FileEdit, FileWrite, Grep, Glob, WebSearch, WebFetch, TodoWrite, AskUserQuestion, Skill, and 30+ more.
3. Commands (Slash Commands)
User-facing commands typed as /command in the REPL: /compact, /model, /help, /clear, /plan, etc. Commands can be:
builtin — shipped with the app
skills — loaded from /skills/ directories
plugin — loaded from installed plugins
MCP — loaded from MCP servers
4. State Management
Two layers:
Bootstrap State (bootstrap/state.ts): A singleton module with getter/setter functions for global session state (session ID, cost, cwd, model, telemetry). This is the "global variables" layer.
AppState (state/): A Redux-like store using useSyncExternalStore. Holds all UI state (which panels are open, MCP connections, tasks, permissions, settings). Components subscribe to slices via useAppState(selector).
5. Ink Rendering
A custom React renderer that renders to the terminal instead of the DOM. Uses:
react-reconciler for the React fiber tree
Yoga (WASM) for flexbox layout
Double-buffered frame system for efficient terminal updates
ANSI escape sequences for output
6. Bridge (Remote Control)
Allows controlling a local Claude Code session from claude.ai/code (web) or the mobile app. Two modes:
Standalone bridge: claude remote-control — runs as a server, spawns child processes
REPL bridge: /remote-control — bridges an existing session
Uses WebSocket (v1) or SSE (v2) transport with JWT authentication.
7. Multi-Agent (Coordinator Mode)
The AI can act as a coordinator that spawns worker agents (sub-processes) to work on tasks in parallel. Workers report back via <task-notification> blocks.
8. Plugins & Skills
Plugins: Versioned packages that can add tools, commands, hooks, and MCP servers
Skills: Prompt-based capabilities discovered from /skills/ directories in the project