Reverse Engineering Claude Code: A Technical Guide
In the rapidly evolving landscape of AI, understanding the inner workings of advanced models like Claude Code is crucial. This article provides a detailed technical guide on reverse engineering Claude Code, focusing on the methodologies and tools developed to dissect its complex 4.6MB minified JavaScript codebase. We'll explore a practical approach that bypasses traditional code restoration, instead focusing on runtime behavior and API data.
The Challenge of Minified Code
Claude Code, a powerful AI agent, presents a significant challenge due to its heavily minified JavaScript client. While initial attempts at reverse engineering focused on source maps, these methods proved insufficient for understanding the overall architecture and were eventually discouraged. This guide introduces a more effective "v2" approach, centered on "monkey patching" API request code and analyzing the resulting logs.
Methodology: Monkey Patching and Log Analysis
The core strategy involves modifying Claude Code's cli.js
file to intercept and log API requests and responses. This is achieved by:
- Locating
cli.js
: Identifying the main client script within the Claude Code installation. - Beautifying the Code: Using tools like
js-beautify
to format the minified code for easier readability. - Patching
beta.messages.create
: Injecting custom logic into the method responsible for LLM API interactions. This patch creates amessages.log
file, capturing detailed logs of each API call and response.
This process allows us to gather crucial data on how Claude Code interacts with LLM APIs across various task scenarios. By analyzing these logs, developers can infer the structure and logic of Claude Code's operations.
Accelerating Analysis with Visualization
To streamline the reverse engineering process, the v2
approach includes a log parsing tool (parser.js
) and an interactive visualization tool (visualize.html
). This tool allows users to load the generated log files, facilitating the analysis of lengthy conversations. The visualization attempts to automatically identify common prompts, aiding in the understanding of dynamic prompt injection and context management.
Key Reverse-Engineered Components
Through this meticulous analysis, several key internal processes of Claude Code have been reverse-engineered, including:
- Quota Check: Verifies API quota using Haiku 3.5.
- Topic Detection: Identifies new topics using a specific prompt with Haiku 3.5, though it operates without conversation context.
- Core Agent Workflow: Driven by a system workflow prompt and augmented with system reminder start/end prompts for dynamic information loading and short-term memory integration. This workflow primarily uses the Sonnet 4 model.
- Context Compaction: A process that compresses conversation history into a single block to conserve context space, also utilizing the Sonnet 4 model.
- IDE Integration: Incorporates open file paths from IDEs into prompts and registers IDE-specific tools for enhanced contextual awareness.
- Todo Short-Term Memory Management: Leverages the
TodoWrite
tool to manage tasks and short-term memory via JSON files, with memory retrieval integrated into the core workflow.
Learning from Prompts and Tool Design
The prompts
and tools
directories within the repository contain valuable insights into how Claude Code is designed to interact with LLMs and manage tasks. Studying these components offers a deeper understanding of prompt engineering and agent design principles.
This detailed technical exploration provides a roadmap for anyone interested in understanding the sophisticated architecture of AI agents like Claude Code. By leveraging LLMs for analysis and employing practical code modification techniques, we can unlock a deeper comprehension of these powerful technologies.