Inside the Claude Code source
Short summary
The article breaks down the recently leaked source code of Anthropic's Claude Code CLI. It highlights the system's architecture, design choices, and differences from OpenAI's Codex, particularly in handling context overflow and user interactions. Key features like compaction strategies and internal versus external user instructions are explored.
- Claude Code uses a four-tiered compaction strategy (proactive token monitoring, reactive fallback, and a "snip compaction" mode for headless sessions) versus Codex's simpler diff-based approach that just minimizes data sent per turn
- The system prompt uses a boundary marker to cache roughly 3,000 tokens of static instructions across users for performance gains
- Internal users get specialized instructions specifically designed to stop the model from misrepresenting test results or giving misleading status updates
- The codebase includes feature flags and build-time checks specifically to prevent sensitive information from leaking into public builds
Full summary
Claude Code, developed by Anthropic, recently had its entire source code leaked on GitHub. The code includes around 1,900 files primarily written in TypeScript. A close examination reveals several noteworthy engineering decisions that distinguish Claude Code from OpenAI's Codex, particularly in how they handle user input and manage session context. When a user types a message, the system processes it through a series of steps, ultimately yielding a response based on a continuous event stream. This differs from Codex's approach, which relies on a Rust-based structure for handling interactions.
Claude Code employs a four-tiered compaction strategy to manage the limitations of the LLM's context window. It proactively monitors token count and summarizes older messages when nearing the limit. If this proactive measure fails, a reactive fallback compacts the messages on-the-fly. The system also features a "snip compaction" mode for headless sessions, which truncates output while preserving full history for interactive use. Claude Code's handling of context is more defensive and complex compared to Codex's simpler, diff-based method that minimizes data sent per turn.
The system prompt itself is intricately designed, combining static behavioral instructions with session-specific information. A unique boundary marker allows for efficient caching, leading to better performance by reusing around 3,000 tokens of instructions across users. Internal users receive specialized instructions that guide the model to avoid common pitfalls, such as misrepresenting test results or providing misleading information. The codebase also includes various feature flags and build-time checks to ensure that sensitive information does not leak into public builds, indicating a thorough approach to security and privacy.