Claude Code Source All In One
Always up-to-date open-source mirror of Claude Code with GPT-5.4 support. Run Claude Code from source with Claude or GPT models via ChatGPT subscription. 18 deep-dive architecture articles included. For educational and reference purposes only.
Ask AI about Claude Code Source All In One
Powered by Claude Β· Grounded in docs
I know everything about Claude Code Source All In One. Ask me about installation, configuration, usage, or troubleshooting.
0/500
Reviews
Documentation
Claude Code Source β Architecture Study & Learning Material
Production-grade AI Agent internals, dissected for learning.
This repository contains the extracted source code of Claude 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.mdfor the per-version record of what was applied here and what remains upstream-only.
Disclaimer Β· Why Study This Β· Architecture Β· Deep Analysis Β· Run from Source Β· Tech Stack Β· Structure Β· 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. The run-from-source setup is based on JiaranI/start-claude-code. It contains extracted source code of Claude Code, a product by Anthropic.
- 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 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.
- 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) β ο½ θΏε ₯εζη³»ε (δΈζ) β
Part 1: Core Agent Engine
| # | Topic | English | δΈζ |
|---|---|---|---|
| 00 | Core Conclusions | EN | δΈζ |
| 01 | Entry Point | EN | δΈζ |
| 02 | Main Loop | EN | δΈζ |
| 03 | Streaming | EN | δΈζ |
| 04 | Tool Orchestration | EN | δΈζ |
| 05 | Permission System | EN | δΈζ |
| 06 | Sub-Agents | EN | δΈζ |
| 07 | Context Window | EN | δΈζ |
| 08 | Message Types | EN | δΈζ |
| 09 | Immutable Messages | EN | δΈζ |
| 10 | Architecture Diagram | EN | δΈζ |
| 11 | Design Philosophy | EN | δΈζ |
Part 2: Peripheral Subsystems
| # | Topic | English | δΈζ |
|---|---|---|---|
| 12 | MCP Integration | EN | δΈζ |
| 13 | Memory System | EN | δΈζ |
| 14 | System Prompt | EN | δΈζ |
| 15 | Session & Bridge | EN | δΈζ |
| 16 | Tool Implementations | EN | δΈζ |
| 17 | Hook System | EN | δΈζ |
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).
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
# 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) β
Alternative: Use an API key instead
export ANTHROPIC_API_KEY="sk-ant-xxx"
./start.sh
Alternative: Use GPT-5.5 via ChatGPT subscription
If you have a ChatGPT Plus/Pro/Business/Enterprise subscription, you can use GPT-5.5 as the agent model β no API key needed:
# 1. Login with your ChatGPT account (opens browser)
./start.sh chatgpt-login
# 2. Launch with GPT-5.5
./start.sh --model gpt5.5
Running GPT via ChatGPT subscription as the agent model
Available GPT model aliases:
| Alias | Resolves to | Tier required |
|---|---|---|
gpt5.5 | chatgpt:gpt-5.5 | Plus / Pro / Business / Enterprise |
gpt5.5-pro | chatgpt:gpt-5.5-pro | Pro / Business / Enterprise |
gpt5.4 | chatgpt:gpt-5.4 | Plus / Pro / Business / Enterprise (legacy) |
gpt5.4-mini | chatgpt:gpt-5.4-mini | Any paid tier |
gpt5.4-nano | chatgpt:gpt-5.4-nano | Any paid tier |
You can also use the full format: --model chatgpt:gpt-5.5
Alternative: Use OpenAI API key
export OPENAI_API_KEY="sk-xxx"
./start.sh --model openai:gpt-5.5
Other OpenAI-compatible providers are also supported:
export DEEPSEEK_API_KEY="sk-xxx"
./start.sh --model deepseek:deepseek-chat
export MISTRAL_API_KEY="xxx"
./start.sh --model mistral:mistral-large-latest
Alternative: Run a local model via Ollama (offline / no API key)
Run Claude Code entirely against a local Ollama model β no API key, no network calls to Anthropic or OpenAI. The launcher boots a bundled AnthropicβOllama proxy (scripts/ollama-proxy.mjs) and points the CLI at it.
# 1. Install and start Ollama (https://ollama.com)
ollama serve
# 2. Pull a tool-capable model
ollama pull qwen2.5-coder:7b
# 3. Launch Claude Code against the local model
./start-ollama.sh qwen2.5-coder:7b
For models without native tool calling, set PROMPTED_TOOLS=1:
PROMPTED_TOOLS=1 ./start-ollama.sh phi3:mini
Windows users
rem Use API key on Windows (OAuth login requires start.sh on macOS/Linux)
set ANTHROPIC_API_KEY=sk-ant-xxx
bun src/entrypoints/cli.tsx
- ripgrep: The
tarextraction in the setup script may fail on Windows. Install ripgrep manually and ensurergis on your PATH. - Bun path: After installing Bun, restart your terminal or add
%USERPROFILE%\.bun\binto PATH. - OAuth login:
start.sh loginis macOS/Linux only. On Windows, runbun src/entrypoints/cli.tsx auth login --claudeai.
Shim files created
These files are not part of the original source β they were created to make the source runnable:
| File | Purpose |
|---|---|
package.json | 100+ dependencies reverse-engineered from import statements |
tsconfig.json | TypeScript config with baseUrl and .js β .ts/.tsx path resolution |
bunfig.toml | Bun config specifying the preload plugin |
preload.ts | Core shim: bun:bundle mock (feature() returns false), MACRO.* global injection |
scripts/setup.mjs | One-command setup: dependency install, private package stubs, missing file generation, ripgrep download |
start.sh | macOS/Linux launcher with OAuth login (./start.sh login), credential detection (Keychain/file/env), proxy auto-detection, Bun transpiler cache cleanup |
How it works
| Challenge | Solution |
|---|---|
bun:bundle compile-time API | preload.ts provides a runtime shim; feature() returns false for all flags |
| 89 feature flags | All disabled β feature-gated code paths do not execute |
MACRO.* compile-time macros | Defined as globalThis.MACRO in preload.ts |
from 'src/...' imports | Source under src/; tsconfig.json baseUrl: "." for natural resolution |
No package.json | Reverse-engineered 100+ dependencies from import statements |
@ant/* private packages | scripts/setup.mjs creates empty stubs |
Unavailable features (missing private packages)
| Feature | Missing Package | Description |
|---|---|---|
| Computer Use | @ant/computer-use-mcp | Screenshots, mouse clicks, keyboard input |
| Native input | @ant/computer-use-input | Rust/enigo native bindings |
| macOS screen/window | @ant/computer-use-swift | Swift native bindings (macOS only) |
| Chrome integration | @ant/claude-for-chrome-mcp | Chrome extension MCP server |
| Sandbox runtime | @anthropic-ai/sandbox-runtime | Command execution sandbox |
| MCP Bridge | @anthropic-ai/mcpb | MCP protocol bridge |
All 89 feature flags (all disabled)
Since feature() returns false at runtime, all feature-gated code paths are inactive:
ABLATION_BASELINE AGENT_MEMORY_SNAPSHOT AGENT_TRIGGERS BRIDGE_MODE BUDDY BUILDING_CLAUDE_APPS CCR_AUTO_CONNECT COORDINATOR_MODE DAEMON DIRECT_CONNECT DUMP_SYSTEM_PROMPT FORK_SUBAGENT HISTORY_PICKER KAIROS MCP_SKILLS MONITOR_TOOL NATIVE_CLIPBOARD_IMAGE PERFETTO_TRACING QUICK_SEARCH SSH_REMOTE STREAMLINED_OUTPUT TEAMMEM TEMPLATES TERMINAL_PANEL TORCH ULTRAPLAN ULTRATHINK VOICE_MODE WEB_BROWSER_TOOL WORKFLOW_SCRIPTS and 59 more.
Inferred build pipeline
TypeScript source
β
ββ Bun bundler (bun build)
β ββ Inject MACRO.* constants (--define)
β ββ Resolve feature() calls β set 89 feature flags true/false
β ββ Dead Code Elimination β strip disabled feature branches
β ββ Bundle into single JS file
β
ββ Optional: Bun compile β single-file executable binary
β
ββ Publish to npm (@anthropic-ai/claude-code)
Troubleshooting
| Error | Fix |
|---|---|
Cannot find module 'src/...' | Verify source is under src/ and bunfig.toml exists |
Missing 'default' export in module '*.md' | Run node scripts/setup.mjs to regenerate stubs |
Cannot find package '@ant/...' | Run node scripts/setup.mjs to recreate stubs |
bun: command not found | Install Bun: curl -fsSL https://bun.sh/install | bash |
No authentication found | Run ./start.sh login for OAuth, or export ANTHROPIC_API_KEY="sk-ant-xxx" for API key |
| Using a non-Anthropic proxy | start.sh auto-detects; manual: set DISABLE_PROMPT_CACHING=1 and DISABLE_INTERLEAVED_THINKING=1 |
| Only works from one directory / hangs after moving source | Bun caches transpiled modules by absolute path. start.sh clears the cache automatically. If launching manually: rm -rf ~/Library/Caches/bun (macOS) or rm -rf ~/.cache/bun (Linux) |
Want the official release?
npm install -g @anthropic-ai/claude-code
Technology stack
| Layer | Technologies |
|---|---|
| Runtime | Node.js >= 18, Bun >= 1.0 |
| Language | TypeScript with React JSX |
| UI | React 19, custom terminal reconciler, 146+ components |
| CLI | Commander.js 12 |
| AI/LLM | Anthropic SDK, Claude Agent SDK, OpenAI SDK, AWS Bedrock, Azure Identity |
| Protocols | Model Context Protocol (MCP, 6 transports), LSP, OAuth/XAA |
| Observability | OpenTelemetry (traces, metrics, logs) with OTLP exporters |
| Code tools | ripgrep, Sharp (image processing), Marked, Turndown, Diff |
Project structure
claude-code-source/
βββ README.md # This document (English)
βββ README.zh.md # δΈζηζζ‘£
βββ assets/ # Images
β βββ anthropic-claude-code.webp
β βββ screenshot.png
βββ package.json # Dependencies (reverse-engineered)
βββ tsconfig.json # TypeScript configuration
βββ bunfig.toml # Bun preload configuration
βββ preload.ts # Runtime shim (feature flags, macros)
βββ start.sh # Launcher script
βββ scripts/setup.mjs # One-command setup
β
βββ claude-code-deep-analysis/ # 18 original analysis articles (EN + δΈζ)
β βββ README.en.md # Series index (English)
β βββ README.md # Series index (δΈζ)
β βββ 00-core-conclusion.en.md # Each article has .en.md + .md
β βββ 00-core-conclusion.md
β βββ ...
β βββ 17-hook-system.en.md
β
βββ src/ # Claude Code source (Anthropic)
βββ query.ts # Core agent loop (1,729 lines)
βββ QueryEngine.ts # Session management (46.6 KB)
βββ Tool.ts # Tool interface & registry (29.5 KB)
βββ main.tsx # Main React component (4,683 lines)
β
βββ entrypoints/ # Entry points (CLI, MCP, SDK types)
βββ commands/ # 87+ slash commands
βββ tools/ # 45+ tool implementations
β βββ BashTool/ # Shell execution (430 KB of safety code)
β βββ AgentTool/ # Sub-agent execution
β βββ FileEditTool/ # File editing with diff matching
β βββ MCPTool/ # Model Context Protocol
β βββ ... # 30+ more tools
β
βββ services/ # 38 service modules
β βββ api/ # Anthropic API integration
β βββ mcp/ # MCP protocol (25 subdirs)
β βββ compact/ # Context compression
β βββ SessionMemory/ # Cross-session memory
β βββ ...
β
βββ components/ # 146+ React UI components
βββ hooks/ # 85+ React hooks
βββ utils/ # 564+ utility functions
βββ permissions/ # Permission system (5 modes)
βββ bridge/ # CLI β VS Code integration
βββ constants/ # System prompt assembly & config
βββ memdir/ # Cross-session memory system
βββ skills/ # Skill/plugin system
βββ voice/ # Voice input handling
βββ ...
License
- Source code: All rights belong to Anthropic, PBC. No license is granted.
- Analysis articles (
claude-code-deep-analysis/): Original commentary by the repository maintainers. Code snippets are for educational purposes only.
Contact: For any questions, please reach out via GitHub Issues.
