Claude Code Architecture

Hệ thống AI Agent tự động hóa lập trình — vận hành trên vòng lặp Perception → Action → Observation, module hóa thành 6 layer rõ ràng.

An AI Agent system for programming automation — operating on a Perception → Action → Observation loop, modularized into 6 distinct layers.

AI Agent Agentic Loop Context-Aware Multi-Agent Permission-Gated

Input & Permission Layer

Tiếp nhận yêu cầu & kiểm soát quyền truy cập

Receives requests & controls access permissions

Input Layer

Tiếp nhận yêu cầu từ CLI, IDE (VSCode, JetBrains), và CI/CD pipeline.

Accepts requests from CLI, IDE (VSCode, JetBrains), and CI/CD pipelines.

Multi-source

Session Manager

Duy trì trạng thái phiên: Resume (tiếp tục), Fork (rẽ nhánh), Persist (lưu trữ).

Maintains session state: Resume, Fork, and Persist modes.

Stateful

Permission Gate

Chốt an ninh YAML 3 cấp: Deny → Approve → Allow. Quan sát mọi thứ trước khi vào agent loop.

3-level YAML security gate: Deny → Approve → Allow. Observes everything before entering the agent loop.

Security

Knowledge Layer

Bộ não cung cấp dữ liệu — tối ưu xử lý ngữ cảnh lớn

The brain that supplies data — optimized for large context processing

Context Compressor

Nén ngữ cảnh 3 lớp (ngưỡng 92%): Summarize → Prune → Truncate. Ghi ra .agent_memory.md.

3-layer context compression (92% threshold): Summarize → Prune → Truncate.

92% threshold

Memory Store

Lưu trữ bộ nhớ xuyên phiên: Long-term, Short-term, và Episodic memory.

Cross-session memory storage: Long-term, Short-term, and Episodic memory.

Cross-session

Skill Registry

Tiêm (inject) kỹ năng vào prompt theo ngữ cảnh — on-demand injection.

Injects skills into prompts based on context — on-demand injection.

On-demand

Task Graph

Sắp xếp ưu tiên & dependencies giữa các tác vụ — DAG-based scheduling.

Prioritizes & manages task dependencies — DAG-based scheduling.

DAG

Master Agent Loop

Trái tim hệ thống — vòng lặp Perceive → Act → Observe

The heart of the system — Perceive → Act → Observe loop

Perceive
Đọc & phân tích context
Read & analyze context
Act
Gọi tool / Spawn agent
Call tool / Spawn agent
Observe
Thu thập & đánh giá
Collect & evaluate

Self-correcting

Tự phát hiện lỗi và thử lại với cách tiếp cận khác.

Auto-detects errors and retries with a different approach.

Goal-driven

Theo dõi mục tiêu cuối cùng, không lạc hướng giữa các bước.

Tracks the final goal, stays on course between steps.

Branching Decisions

Quyết định gọi tool trực tiếp hoặc spawn subagent cho task phức tạp.

Decides to call tools directly or spawn subagents for complex tasks.

Integration & Execution Layer

Nơi các hành động thực sự diễn ra

Where actual actions take place

🔌

MCP Runtime

Model Context Protocol — auto-discover tools từ Filesystem, Git, Custom servers.

Model Context Protocol — auto-discovers tools from Filesystem, Git, Custom servers.

Auto-discover

Tool Dispatch

Bộ điều phối: bash, read, write, grep, edit — mỗi tool có handler riêng biệt.

Dispatcher: bash, read, write, grep, edit — each tool has a dedicated handler.

Modular

Streaming Runtime

Thực thi song song (parallel execution) theo thời gian thực.

Real-time parallel execution of tool calls.

Parallel

Prompt Cache

Tái sử dụng stable prefix — giảm chi phí gọi model xuống còn ~10%.

Reuses stable prefix — reduces model call cost to ~10%.

Cost: 10%

Multi-Agent Layer

Chia nhỏ tác vụ phức tạp cho các agent con

Breaks down complex tasks for child agents

Subagent Spawner

Tạo agent con với ngữ cảnh cô lập — giao việc rành mạch, không chia sẻ state.

Creates child agents with isolated context — clear delegation, no shared state.

Isolated

Teammate Mailboxes

Redis pub/sub — gửi tin nhắn tức thời giữa các agent. FSM: IDLE → REQUEST → WAIT → RESPOND.

Redis pub/sub — instant messaging between agents. FSM: IDLE → REQUEST → WAIT → RESPOND.

Redis pub/sub

Worktree Isolator

Branch riêng cho từng task — zero conflicts. Atomic lock từ Autonomous Board đảm bảo merge an toàn.

Separate branch per task — zero conflicts. Atomic lock ensures safe merges.

Zero conflicts

Observability & Output Layer

Giám sát, trả kết quả & feedback loop

Monitoring, delivering results & feedback loop

Event Bus

Bắt sự kiện vòng đời (lifecycle hooks): tool_start, tool_end, error, session_end.

Captures lifecycle events: tool_start, tool_end, error, session_end.

Lifecycle

Background Executor

Daemon threads chạy non-blocking: logging, metrics, state persistence.

Non-blocking daemon threads: logging, metrics, state persistence.

Non-blocking

Task Result

Output đã xác minh — tự động cập nhật Memory Store để hệ thống "học" từ thao tác vừa thực hiện.

Verified output — auto-updates Memory Store so the system "learns" from performed actions.

Verified

📎 Mapping: Kiến trúc ↔ Cấu trúc Project · Architecture ↔ Project Structure

Layer Files trong Scaffold · Scaffold Files Vai trò · Role
Input & Permission .claude/settings.json Permissions, hooks, model selection
Knowledge CLAUDE.md · .claude/rules/ · .claude/skills/ Context, rules, on-demand skills
Agent Loop Claude Code runtime (built-in) Core orchestration engine
Integration .mcp.json · .claude/hooks/ MCP servers, event scripts
Multi-Agent .claude/agents/*.md Subagent definitions
Observability .claude/hooks/ · Event scripts Monitoring, output, feedback