Production-grade AI Agent internals, dissected for learning.
--- This repository contains the **extracted source code** of [Claude Code](https://claude.ai/code) (Anthropic's AI programming CLI) along with **18 original deep-analysis articles** (bilingual: English & Chinese) dissecting its architecture. The source was discovered from publicly available repositories and is provided **strictly for educational and research purposes**. All intellectual property rights of the source code belong exclusively to **Anthropic, PBC**. > **Kept in sync with upstream.** This repository tracks the official Claude Code release stream and is kept current with the latest version. Each upstream release has a corresponding update applying the user-facing, tractable subset of its changelog — see [`CHANGELOG.md`](CHANGELOG.md) for the per-version record of what was applied here and what remains upstream-only. --- [Disclaimer](#disclaimer) · [Why Study This](#why-study-this-codebase) · [Architecture](#architecture-at-a-glance) · [Deep Analysis](#deep-analysis-series) · [Run from Source](#run-from-source) · [Tech Stack](#technology-stack) · [Structure](#project-structure) · [License](#license) --- ## Disclaimer > **IMPORTANT: READ BEFORE USE** **This repository is strictly for educational and research purposes only.** - **Source**: The source code was discovered from publicly available sources, including [instructkr/claw-code](https://github.com/instructkr/claw-code). The run-from-source setup is based on [JiaranI/start-claude-code](https://github.com/JiaranI/start-claude-code). It contains extracted source code of [Claude Code](https://claude.ai/code), a product by [Anthropic](https://anthropic.com). - **Ownership**: All intellectual property rights belong exclusively to **Anthropic, PBC**. This repository claims no ownership, authorship, or rights over the original code. - **Purpose**: Provided **solely** as learning material for understanding AI agent system architecture and engineering patterns — for academic study, technical research, and educational discussion. - **Prohibited uses**: - Commercial purposes of any kind - Building competing products or services - Redistribution or repackaging of the source code - Any purpose that violates Anthropic's [Terms of Service](https://www.anthropic.com/terms) or applicable laws - **Analysis articles**: The deep analysis articles (`claude-code-deep-analysis/`) are **original commentary and analysis** by the repository maintainers. Code snippets are included for commentary, criticism, and education purposes. These articles do not constitute legal advice. - **DMCA / Takedown**: If you represent Anthropic or believe this repository infringes on intellectual property rights, please contact us — we will **promptly remove** the infringing content. You may also file a DMCA takedown notice through [GitHub's DMCA process](https://docs.github.com/en/site-policy/content-removal-policies/dmca-takedown-policy). - **No warranty**: Provided "as is" without warranty of any kind. --- ## Why study this codebase? Claude Code is one of the most sophisticated production AI agent systems publicly observable. Unlike toy frameworks and demo agents, it is engineered for **real-world, hours-long coding sessions** with enterprise-grade reliability. Studying it reveals hard-won engineering decisions that no tutorial covers: | Design Decision | Insight | |:---|:---| | **Loop over Graph** | A `while(true)` loop replaces DAG-based workflow orchestration — simpler, more flexible, easier to reason about at runtime | | **Recursion over Orchestration** | Sub-agents recursively call `query()`, inheriting compression, error recovery, and streaming for free | | **Model decides, Framework executes** | The framework enforces safety constraints (concurrency, side effects, permissions); all reasoning stays in the model | | **Built for 4-hour sessions** | Four-layer context compression and three-level 413 error recovery — invisible in demos, critical in production | | **Immutability as cost optimization** | Immutable API messages maximize prompt caching hits, reducing long-session costs by ~80% | --- ## Architecture at a glance ``` CLI / VS Code / IDE Extension │ ▼ ┌───────────────────────┐ │ EntryPoint (cli.tsx) │ │ Fast-path routing │ └───────────┬───────────┘ │ ┌───────────▼───────────┐ │ QueryEngine │ │ Session management │ │ API client wrapper │ └───────────┬───────────┘ │ ┌───────────────────▼───────────────────┐ │ query() — Core Agent Loop │ │ ┌─────────────────────────────────┐ │ │ │ while (true) { │ │ │ │ messages = buildPrompt() │ │ │ │ response = stream(messages) │ │ │ │ tools = extractToolCalls() │ │ │ │ if (!tools) break │ │ │ │ results = executePar(tools) │ │ │ │ messages.push(results) │ │ │ │ maybeCompress(messages) │ │ │ │ } │ │ │ └─────────────────────────────────┘ │ └──────┬──────────┬──────────┬──────────┘ │ │ │ ┌────────▼──┐ ┌────▼────┐ ┌───▼────────┐ │ 45+ Tools │ │ Permis- │ │ Context │ │ Bash, Edit │ │ sion │ │ Compression│ │ Glob, Grep │ │ System │ │ 4 layers │ │ MCP, LSP… │ │ 5 modes │ │ + 413 heal │ └────────────┘ └─────────┘ └────────────┘ ``` ### By the numbers | Metric | Count | |:---|:---| | Core agent loop (`query.ts`) | 1,729 lines | | Main UI component (`main.tsx`) | 4,683 lines | | Tool implementations | 45+ | | Slash commands | 87+ | | React UI components | 146+ | | Utility functions | 564+ | | React hooks | 85+ | | Service modules | 38 | | Feature flags | 89 | | System prompt assembly (`prompts.ts`) | 54.3 KB | --- ## Deep Analysis Series We produced **18 original articles** with source-level deep dives into every major subsystem. Each article is available in both **English** and **Chinese**. **[Enter the analysis series (EN) →](claude-code-deep-analysis/README.en.md)** | **[进入分析系列 (中文) →](claude-code-deep-analysis/README.md)** ### Part 1: Core Agent Engine | # | Topic | English | 中文 | |:--|:------|:--------|:-----| | 00 | Core Conclusions | [EN](claude-code-deep-analysis/00-core-conclusion.en.md) | [中文](claude-code-deep-analysis/00-core-conclusion.md) | | 01 | Entry Point | [EN](claude-code-deep-analysis/01-entry-point.en.md) | [中文](claude-code-deep-analysis/01-entry-point.md) | | 02 | Main Loop | [EN](claude-code-deep-analysis/02-main-loop.en.md) | [中文](claude-code-deep-analysis/02-main-loop.md) | | 03 | Streaming | [EN](claude-code-deep-analysis/03-streaming.en.md) | [中文](claude-code-deep-analysis/03-streaming.md) | | 04 | Tool Orchestration | [EN](claude-code-deep-analysis/04-tool-orchestration.en.md) | [中文](claude-code-deep-analysis/04-tool-orchestration.md) | | 05 | Permission System | [EN](claude-code-deep-analysis/05-permission-system.en.md) | [中文](claude-code-deep-analysis/05-permission-system.md) | | 06 | Sub-Agents | [EN](claude-code-deep-analysis/06-sub-agent.en.md) | [中文](claude-code-deep-analysis/06-sub-agent.md) | | 07 | Context Window | [EN](claude-code-deep-analysis/07-context-window.en.md) | [中文](claude-code-deep-analysis/07-context-window.md) | | 08 | Message Types | [EN](claude-code-deep-analysis/08-message-types.en.md) | [中文](claude-code-deep-analysis/08-message-types.md) | | 09 | Immutable Messages | [EN](claude-code-deep-analysis/09-immutable-api-messages.en.md) | [中文](claude-code-deep-analysis/09-immutable-api-messages.md) | | 10 | Architecture Diagram | [EN](claude-code-deep-analysis/10-architecture-diagram.en.md) | [中文](claude-code-deep-analysis/10-architecture-diagram.md) | | 11 | Design Philosophy | [EN](claude-code-deep-analysis/11-design-philosophy.en.md) | [中文](claude-code-deep-analysis/11-design-philosophy.md) | ### Part 2: Peripheral Subsystems | # | Topic | English | 中文 | |:--|:------|:--------|:-----| | 12 | MCP Integration | [EN](claude-code-deep-analysis/12-mcp-integration.en.md) | [中文](claude-code-deep-analysis/12-mcp-integration.md) | | 13 | Memory System | [EN](claude-code-deep-analysis/13-memory-system.en.md) | [中文](claude-code-deep-analysis/13-memory-system.md) | | 14 | System Prompt | [EN](claude-code-deep-analysis/14-system-prompt.en.md) | [中文](claude-code-deep-analysis/14-system-prompt.md) | | 15 | Session & Bridge | [EN](claude-code-deep-analysis/15-session-resume.en.md) | [中文](claude-code-deep-analysis/15-session-resume.md) | | 16 | Tool Implementations | [EN](claude-code-deep-analysis/16-tool-implementations.en.md) | [中文](claude-code-deep-analysis/16-tool-implementations.md) | | 17 | Hook System | [EN](claude-code-deep-analysis/17-hook-system.en.md) | [中文](claude-code-deep-analysis/17-hook-system.md) | --- ## Run from Source This repository supports running Claude Code locally from source (with reduced functionality). Tracks upstream releases — currently aligned with `2.1.123` (see [`CHANGELOG.md`](CHANGELOG.md)). ### Prerequisites | Dependency | Notes | |:---|:---| | **Node.js** >= 18 | For the setup script | | **Bun** >= 1.0 | Runtime for Claude Code (auto-installed by setup) | | **Authentication** | Claude Pro/Max/Team subscription (OAuth), API key (`sk-ant-xxx`), ChatGPT subscription, or OpenAI API key | ### Quick start ```bash # 1. Install dependencies and generate shims node scripts/setup.mjs # 2. Login with your Claude subscription (opens browser) ./start.sh login # 3. Launch ./start.sh ```
Running from source with Claude Pro/Max subscription — Opus 4.6 (1M context) ✓
Running GPT via ChatGPT subscription as the agent model