Architecture Deep Dive β’ Flow Diagrams β’ How Every Feature Works Under the Hood
Ever wondered how Claude Code actually works? How it shrinks memory, executes tools, manages permissions, or streams responses? This repo breaks it all down β one diagram at a time.
Claude Code (and its open-source sibling Claw Code) is an agentic AI coding assistant that lives in your terminal. It can read files, write code, run commands, search the web, and orchestrate complex multi-step tasks β all while managing context, permissions, and tool execution autonomously.
This repository is a visual architecture guide. No code. Just diagrams, explanations, and deep dives into every subsystem that makes the magic happen.
Whether you're:
- ποΈ Building your own AI agent and want to learn from Claude Code's architecture
- π Curious about how agentic loops work under the hood
- π Studying software architecture patterns in production AI systems
- π οΈ Contributing to Claw Code / Claude Code and need to understand the internals
...this repo is for you.
The 30,000-foot view. How all the pieces fit together.
graph TB
subgraph "π₯οΈ User Interface"
CLI["CLI / REPL"]
SLASH["Slash Commands<br/>/compact /model /help"]
end
subgraph "π§ Core Runtime"
LOOP["Agentic Conversation Loop"]
PROMPT["System Prompt Builder"]
COMPACT["Memory Compaction"]
SESSION["Session Manager"]
end
subgraph "π§ Tool Ecosystem"
TOOLS["Built-in Tools<br/>bash, read, write, edit, glob, grep"]
MCP["MCP Servers<br/>stdio, SSE, WebSocket"]
HOOKS["Hook System<br/>PreToolUse / PostToolUse"]
end
subgraph "π Security Layer"
PERMS["Permission Model<br/>ReadOnly β WorkspaceWrite β DangerFull"]
SANDBOX["Sandbox Execution"]
AUTH["Auth: OAuth PKCE + API Keys"]
end
subgraph "π External"
API["Anthropic API<br/>Streaming SSE"]
CONFIG["Config Hierarchy<br/>User β Project β Local"]
GIT["Git Integration"]
end
CLI --> LOOP
SLASH --> LOOP
LOOP --> PROMPT
LOOP --> COMPACT
LOOP --> SESSION
LOOP --> TOOLS
LOOP --> MCP
TOOLS --> HOOKS
TOOLS --> PERMS
TOOLS --> SANDBOX
LOOP --> API
PROMPT --> CONFIG
PROMPT --> GIT
API --> AUTH
π Full Architecture Overview β
Each doc below zooms into one specific feature with flow diagrams, sequence diagrams, and detailed explanations.
| # | Feature | What You'll Learn | Status |
|---|---|---|---|
| 00 | Architecture Overview | High-level system design, crate structure, data flow | β |
| 01 | The Agentic Conversation Loop | How the core loop streams, calls tools, and iterates | β |
| 02 | Memory Shrinking & Context Compaction | How Claude Code fits infinite conversations into finite context | β |
| 03 | Tool System | Tool registration, execution, input schemas, built-in tools | β |
| 04 | Permission Model | Three-tier authorization, interactive prompting, escalation | β |
| 05 | MCP Integration | Model Context Protocol β extending tools via external servers | β |
| 06 | Hook System | Pre/Post tool hooks, exit codes, environment variables | β |
| 07 | Session Management | Persistence, resume, message serialization | β |
| 08 | Streaming & SSE Parsing | Server-Sent Events, incremental parsing, real-time output | β |
| 09 | Configuration System | Multi-level config, deep merge, feature extraction | β |
| 10 | Authentication | OAuth PKCE flow, API keys, token refresh | β |
| 11 | CLI & REPL | Terminal rendering, markdown output, input handling | β |
| 12 | Sandbox Execution | Linux namespace isolation, container detection | β |
| 13 | System Prompt Building | Dynamic prompt construction, CLAUDE.md discovery | β |
| 14 | Slash Commands | Command registry, parsing, execution | β |
| 15 | Error Handling & Retry | Error types, exponential backoff, retryability | β |
| 16 | Bootstrap Lifecycle | Startup phases, initialization sequence | β |
graph LR
A["π€ User Message"] --> B["π‘ Stream to API"]
B --> C{"π€ Response Type?"}
C -->|"Text"| D["π¬ Display Text"]
C -->|"Tool Use"| E["π§ Execute Tool"]
E --> F["π Check Permission"]
F --> G["β‘ Run Hook: PreToolUse"]
G --> H["π Execute"]
H --> I["β‘ Run Hook: PostToolUse"]
I --> J["π¨ Send Result to API"]
J --> C
C -->|"End Turn"| K["β
Done"]
graph TD
A["Token Count Check"] --> B{"Exceeds Budget?<br/>default: 200K tokens"}
B -->|"No"| C["Continue Normally"]
B -->|"Yes"| D["Identify Old Messages"]
D --> E["Generate Summary<br/>of Removed Turns"]
E --> F["Inject Summary as<br/>System Context"]
F --> G["Preserve Recent N<br/>Messages Intact"]
G --> H["Resume with<br/>Compacted Context"]
graph LR
RO["π’ ReadOnly<br/>read files, glob, grep"]
WW["π‘ WorkspaceWrite<br/>+ write files, edit"]
DA["π΄ DangerFullAccess<br/>+ bash, network, web"]
RO --> WW --> DA
| Component | Details |
|---|---|
| Modular Architecture | Separate modules for API, commands, runtime core, CLI, and tools |
| Built-in Tools | 18 tools β file ops, shell, search, web, orchestration |
| MCP Transports | 6 types β stdio, SSE, HTTP, WebSocket, SDK, proxy |
| Permission Tiers | 3 levels β ReadOnly, WorkspaceWrite, DangerFullAccess |
| Config Sources | 5 locations β user, project, local, CLI flags, env vars |
| Slash Commands | 15+ β /compact, /model, /permissions, /cost, /diff, and more |
| Bootstrap Phases | 12 ordered startup phases |
| Supported Models | Opus, Sonnet, Haiku model families |
inside-claude-code/
βββ README.md β You are here
βββ LICENSE
βββ assets/
β βββ banner.svg
βββ docs/
β βββ 00-architecture-overview/ β Start here
β βββ 01-conversation-loop/
β βββ 02-memory-and-context/
β βββ 03-tool-system/
β βββ 04-permission-model/
β βββ 05-mcp-integration/
β βββ 06-hook-system/
β βββ 07-session-management/
β βββ 08-streaming-and-sse/
β βββ 09-config-system/
β βββ 10-authentication/
β βββ 11-cli-and-repl/
β βββ 12-sandbox-execution/
β βββ 13-system-prompt-building/
β βββ 14-slash-commands/
β βββ 15-error-handling-and-retry/
β βββ 16-bootstrap-lifecycle/
The agentic AI coding assistant space is exploding. Claude Code, Cursor, Windsurf, Copilot β they all share similar architectural patterns. Understanding how one works gives you superpowers to:
- Build your own agentic coding tools
- Debug issues when things go wrong
- Extend and customize existing tools
- Learn production architecture patterns for AI systems
This repo distills thousands of lines of Rust into clear, visual documentation anyone can understand.
Found a mistake? Want to add a diagram? PRs welcome!
- Fork the repo
- Create a feature branch
- Add or improve docs in the
docs/folder - Use Mermaid for diagrams
- Submit a PR
MIT β Use these docs however you like.
If this helped you understand how AI coding assistants work, drop a star!
Built with π curiosity and π diagrams
By @Luxshan2000