Skip to content

Claude Code reverse-engineering wiki

This wiki documents the extracted @anthropic-ai/claude-code runtime with source-anchored implementation notes. The primary readable artifact is:

claude-code-pkg/src/entrypoints/cli.js

The wiki is organized around source-anchored internals questions: how the Bun standalone runtime starts, routes command-line modes, shapes model context, exposes tools, persists sessions, supports remote control, runs agents, and exposes operational/debug/native surfaces.

Because cli.js is bundled/minified, symbol names are unstable. Source anchors are intended for searching the analyzed bundle, not as public API names. Implementation pages use Semantic alias as the first source-anchor table column, followed by the concrete Source, approximate location, and exact string/symbol for this build.

Semantic alias and minified anchor mapping

This home page is a navigation index, not a direct cli.js implementation analysis. Concrete topic pages map stable semantic aliases to version-specific minified anchors.

Semantic aliasMinified anchorScope
Wiki homeN/A — navigation pageOrients readers to the canonical sections and reading paths.
Section indexesN/A — see linked section README pagesCurated reader routes through source-anchored implementation pages.
Topic implementation pagesSee page-level source anchor tablesBundle-specific anchors live in focused implementation documents.

Internals wiki map

flowchart TD
Home[Wiki home] --> Start[00 Start here]
Home --> Runtime[01 Runtime lifecycle]
Home --> Loop[02 Context and model loop]
Home --> Tools[03 Tools, integrations, and security]
Home --> Sessions[04 Sessions, persistence, and remote]
Home --> Hosted[05 Hosted agent ops]
Home --> Agents[06 Agents and automation]
Start --> Runtime
Runtime --> Loop
Loop --> Tools
Runtime --> Sessions
Sessions --> Hosted
Loop --> Agents
click Home "./" "Open wiki home"
click Start "./00-start-here/" "Open Start here"
click Runtime "./01-runtime-lifecycle/" "Open Runtime lifecycle"
click Loop "./02-context-model-loop/" "Open Context and model loop"
click Tools "./03-tools-integrations-security/" "Open Tools, integrations, and security"
click Sessions "./04-sessions-persistence-remote/" "Open Sessions, persistence, and remote"
click Hosted "./05-hosted-agent-ops/" "Open Hosted agent ops"
click Agents "./06-agents-automation/" "Open Agents and automation"

Canonical sections

SectionPurpose
Start hereMinimal orientation: what the bundle is, how to read anchors, and the first path through the runtime.
Runtime lifecyclePackage/Bun bootstrap, CLI flags, root command routing, headless/interactive modes, and subcommands.
Context and model loopPrompt/context sources, memory/settings, model/provider/auth selection, and headless stream-JSON turns.
Tools, integrations, and securityBuilt-in tools, permissions, MCP, plugins, hooks, settings policy, IDE/Chrome/file integrations.
Sessions, persistence, and remoteLocal JSONL transcripts, resume/continue/fork/rewind, remote sessions, teleport, and Remote Control.
Hosted agent opsDebug logs, telemetry/traffic policy, doctor/update, hosted review signals, and native image/audio modules.
Agents and automationCustom/background agents, task/subagent tools, lifecycle hooks, slash commands, and auto-mode.
GoalRead this path
Get oriented quicklyStart hereGlossary and aliasesMain feature mapSystem architectureCLI main paths
Answer protocol and boundary questionsSystem architectureRuntime communication protocolsRemote control and teleport
Understand one complete local runCLI main pathsPrompt, context, and memoryPrompt assembly scenariosPrompt template catalog
Understand context budgets and model billingContext, memory, compaction, checkpoints, and rewindModel selection, calls, usage, quota, and billingHeadless streaming and resilience
Understand scriptable/headless modeCommands and flagsHeadless streaming and resilienceContext and model loop architecture
Use canonical reference tablesCommand-line referenceTool inventory and schemasHooks and events referenceSettings schema referenceEnvironment variables reference
Understand durable sessions and remote controlSessions, persistence, and remoteSession resume and transcriptsRemote control and teleportSession API, events, and storage
Review trust boundariesTools, integrations, and securityTool runtime, events, and integration flowsBuilt-in tools and permissionsSandbox and isolationMCP, plugins, and hooks
Understand voice/audio dictationHosted agent opsMedia native modulesAudio capture and voice mode
Study agents and automationAgents and automationAgents, tasks, and subagentsAgent runtime, scheduling, and completionSlash commands and automation

Cross-cutting implementation matrix

ConcernPrimary internals sectionSupporting sections
Runtime mode selectionRuntime lifecycleCommand-line reference, Start here, Sessions, persistence, and remote
Communication protocolsRuntime communication protocolsMCP, plugins, and hooks, Remote control and teleport, Agents and automation
Context engineeringContext and model loopContext, memory, compaction, checkpoints, and rewind, Agents and automation, Tools, integrations, and security
Model usage, quota, and billingModel selection, calls, usage, quota, and billingModels, providers, and auth, Headless streaming and resilience
Tool executionTool inventory and schemasBuilt-in tools and permissions, Tool runtime, events, and integration flows, Agents and automation
Session/event lifecycleSession API, events, and storageData models and frame schemas, Hooks and events reference, Hosted agent ops
Remote/hosted operationSessions, persistence, and remoteHosted agent ops, Agents and automation
Permissions and policyTools, integrations, and securityContext and model loop, Hosted agent ops
Settings, hooks, and environment referencesSettings schema referenceHooks and events reference, Environment variables reference, Settings, policy, and integrations
Sandbox and isolationSandbox and isolationBuilt-in tools and permissions, Settings, policy, and integrations
Voice/audio dictationAudio capture and voice modeMedia native modules, Prompt, context, and memory
Subagents and automationAgent runtime, scheduling, and completionAgents and automation, Tools, integrations, and security, Sessions, persistence, and remote

Mechanism-level deep dives

MechanismStart here
Semantic aliases, minified anchors, and terminologyGlossary and aliases
System module boundaries and data/control flowSystem architecture
Runtime protocol families across modules, agents, bridges, and remote serversRuntime communication protocols
Command-line flags, subcommands, and aliasesCommand-line reference
Per-module architecture deep divesRuntime lifecycle architecture, Context and model loop architecture, Tool runtime and security architecture, Session and remote-control architecture, Operations and native-support architecture, Agent and automation architecture
Headless stream/control event loopHeadless streaming and resilience
Prompt/template extraction catalogPrompt template catalog
Major prompt assembly scenariosPrompt assembly scenarios
Context compaction, memory selection, checkpoints, and rewindContext, memory, compaction, checkpoints, and rewind
Dynamic model selection, provider calls, usage, quota, and billingModel selection, calls, usage, quota, and billing
Tool runtime, events, shell execution, SDK/LSP/Web, context exclusion, settings, and persistenceTool runtime, events, and integration flows
Tool names, schema owners, and tool-family referenceTool inventory and schemas
Tool authorization and execution boundaryBuilt-in tools and permissions
Hook names, lifecycle events, and stream/control framesHooks and events reference
Known settings roots, keys, and policy groupsSettings schema reference
Command sandbox design and platform isolationSandbox and isolation
MCP runtime connection and elicitationMCP, plugins, and hooks
Resume/continue/fork state restorationSession resume and transcripts
Session API, event, storage, and remote-control referenceSession API, events, and storage
Session data models, transcript records, and frame schemasData models and frame schemas
Diagnostics and debug logsDiagnostics and debug logs
Telemetry and tracingTelemetry and tracing
Feature gatesFeature gates reference
Updater and doctorUpdater and doctor
Environment variable referenceEnvironment variables reference
Task/subagent runtime behaviorAgents, tasks, and subagents
Agent task scheduling, completion, and cronAgent runtime, scheduling, and completion
Voice capture, transcription stream, and prompt injectionAudio capture and voice mode

Source policy

  • Treat claude-code-pkg/src/entrypoints/cli.js as the primary readable source artifact.
  • Use approximate line numbers plus byte offsets because the file is bundled and many logical modules sit on very long lines.
  • Preserve exact strings, flags, environment variables, and symbols in source-anchor tables.
  • Do not treat .jsc bytecode dumps as recoverable JavaScript source.
  • Keep broad synthesis pages, implementation pages, and reference pages distinct; promote confirmed findings into the focused section page that owns the behavior.

Important caveat

These pages document a bundled/minified production artifact, not clean source. Semantic names in prose and diagrams are explanatory aliases. Generated/minified symbols are retained only when useful as search anchors for the analyzed bundle.

Created and maintained by Yingting Huang.