This is the main entry when you type claude in your terminal.
Fast-Path Pattern
The CLI uses fast-path routing - special cases are handled before loading heavy modules:
TypeScript
// Example: --version is handled immediatelyif (args.length === 1 && args[0] === '--version') {
console.log(`${MACRO.VERSION} (Claude Code)`)
return// Exit early, no heavy imports
}
Entry Branches
Branch
Trigger
Purpose
--version
Version flag
Print version and exit
--dump-system-prompt
Debug flag
Output system prompt
--claude-in-chrome-mcp
Chrome mode
Run Chrome MCP server
--daemon-worker
Internal
Spawn daemon worker
remote-control
Bridge mode
Remote control session
daemon
Background
Run supervisor daemon
ps/logs/attach/kill
Sessions
Background session management
--print
Headless
Non-interactive output
default
Interactive
Full TUI REPL
Main Application: main.tsx
File: src/main.tsx (~800KB - the largest file)
This is where the full application initializes. Think of it as the main() function.
Startup Sequence
TypeScript
// 1. PROFILE: Mark entry pointprofileCheckpoint('main_tsx_entry')
// 2. MDM/FETCH: Start async fetches earlystartMdmRawRead() // Mobile device management settingsstartKeychainPrefetch() // macOS keychain (OAuth tokens)// 3. IMPORTS: Load heavy modulesimport { getCommands } from'./commands.js'import { getTools } from'./tools.js'// 4. INIT: Initialize subsystemsinitializeTelemetryAfterTrust()
initializeGrowthBook()
loadRemoteManagedSettings()
// 5. SETUP: CLI argument parsing
program
.option('--model <model>', 'Set the model')
.option('--verbose', 'Enable verbose mode')
// ... more options// 6. LAUNCH: Start the REPL or execute commandif (isNonInteractive) {
awaitrunNonInteractive()
} else {
awaitlaunchRepl({ ... })
}
Side-Effect Imports Pattern
Notice the pattern at the very top:
TypeScript
// These side-effects must run before all other importsimport { profileCheckpoint } from'./utils/startupProfiler.js'profileCheckpoint('main_tsx_entry')
import { startMdmRawRead } from'./utils/settings/mdm/rawRead.js'startMdmRawRead()
Why? These start async operations immediately so they run in parallel with module loading.
Bootstrap State: bootstrap/state.ts
File: src/bootstrap/state.ts
This module holds global bootstrap configuration that must be available before React/State init.
import { feature } from'bun:bundle'// Build-time dead code eliminationif (feature('BRIDGE_MODE')) {
// This code is only included if flag is enabled at build
}
feature() is a compile-time macro - not a runtime check!
2. Conditional Requires
TypeScript
// Lazy loading to break circular dependenciesconstgetTeammateUtils = () => require('./utils/teammate.js')
// Dead code elimination for ant-only featuresconst assistantModule = feature('KAIROS')
? require('./assistant/index.js')
: null
3. Profile Checkpoints
TypeScript
profileCheckpoint('some_phase')
// ... do work ...profileCheckpoint('some_phase_done')
// Later: profileReport() prints timings
Used to optimize startup performance.
4. Trust Check
TypeScript
checkHasTrustDialogAccepted()
Before full init, users must accept terms. Early exits if not trusted.
// In cli.tsx or main.tsx
program
.option('--my-flag <value>', 'Description', 'default')
// Later:const options = program.opts()
if (options.myFlag) {
setMyFlagState(options.myFlag)
}
Debugging Entrypoints
Verbose Startup
Bash
claude --verbose
Shows profile checkpoints and init flow.
Profile Report
Bash
CLAUDE_CODE_PROFILE=1 claude
Prints detailed timing breakdown.
Debug Mode
Bash
claude --debug
Enables debug logging throughout.
Summary
Entry Point
File
When Used
CLI
cli.tsx
Primary entry
Main
main.tsx
Full initialization
Bridge
bridge/bridgeMain.ts
Remote control
Daemon
daemon/main.ts
Background supervisor
SDK
entrypoints/sdk/
Programmatic API
The key insight: cli.tsx is the router, main.tsx is the application, and specialized entrypoints handle specific modes (bridge, daemon, headless).