As a CLI tool invoked frequently, Claude Code aggressively optimizes startup time through parallel prefetch, lazy loading, and compile-time dead code elimination.
Before heavy module evaluation begins, main.tsx fires multiple prefetch operations as side effects:
main.tsx loads
│
├──► startMdmRawRead() # MDM/policy settings (async)
├──► startKeychainPrefetch() # macOS keychain read (async)
├──► API preconnect # TCP connection warmup
│
v
Heavy imports begin (React, Ink, tools, commands...)
│
v
Prefetch results available (no extra wait)
These operations overlap with module evaluation time, effectively making them "free" in terms of perceived latency.
Bun's bun:bundle feature flags enable compile-time dead code elimination:
// At build time, feature('VOICE_MODE') resolves to true or false
// The bundler completely strips the dead branch
const voiceCommand = feature('VOICE_MODE')
? require('./commands/voice/index.js').default
: null| Flag | Controls |
|---|---|
| PROACTIVE | Proactive/initiative-taking mode |
| KAIROS | Advanced autonomous features |
| KAIROS_BRIEF | Brief tool subsystem |
| BRIDGE_MODE | IDE bridge system |
| DAEMON | Background daemon mode |
| VOICE_MODE | Voice input processing |
| AGENT_TRIGGERS | Cron/scheduled triggers |
| AGENT_TRIGGERS_REMOTE | Remote trigger execution |
| MONITOR_TOOL | System monitoring tool |
| COORDINATOR_MODE | Multi-agent coordinator |
| TRANSCRIPT_CLASSIFIER | Auto-mode permission classifier |
| CACHED_MICROCOMPACT | Cached micro-compaction |
| EXPERIMENTAL_SKILL_SEARCH | Skill discovery tool |
This means the npm-distributed binary only includes code for enabled features, significantly reducing bundle size and parse time.
Heavy modules are deferred via dynamic import() until actually needed:
- OpenTelemetry + gRPC: Only loaded when telemetry is enabled
- Analytics (GrowthBook): Initialized async, not blocking startup
- OAuth flow: Only loaded when authentication is needed
- Compact/summarization: Loaded on first compaction trigger
- MCP SDK: Loaded when first MCP server connection requested
- Voice processing: Loaded on voice command activation
- LSP: Loaded when language server features requested
const getTeamCreateTool = () =>
require('./tools/TeamCreateTool/TeamCreateTool.js').TeamCreateTool
Functions wrapped in closures break circular dependencies AND defer loading until first call.
Performance isn't just startup -- it's also about managing LLM costs:
- Token estimation tracks prompt size before API calls
- Warning thresholds trigger proactive compaction
- Auto-compact summarizes old messages to reclaim context space
- Deferred tool loading keeps initial prompt small
- Per-model token costs tracked (input, output, cache reads/writes)
- cost-tracker.ts maintains running totals
- /cost command exposes current session costs
- Organization policy limits enforced
process.env.USER_TYPE === 'ant'
? require('./internal-tool.js')
: null
Internal-only tools completely excluded from public builds.
Biome's organize-imports rule disabled for files with ANT-ONLY markers, preserving intentional import ordering that enables dead code elimination.
- Overlap, don't sequence: Prefetch during module evaluation
- Pay for what you use: Feature flags eliminate unused code paths
- Defer the heavy stuff: Dynamic imports for rarely-used modules
- Budget-aware: Token tracking prevents context window blowups
- Build-time, not runtime: Decisions made at compile time when possible