Claude Code คือ AI Coding Assistant แบบ CLI จาก Anthropic ที่ไม่ใช่แค่ chatbot แต่เป็น autonomous agent ที่อ่าน/เขียนไฟล์, รัน commands, และทำงานจริงใน codebase ของคุณได้
วันนี้มาเจาะลึก สถาปัตยกรรมเบื้องหลัง ว่า Claude Code ออกแบบระบบยังไงให้ AI ทำงานได้อย่างปลอดภัยและมีประสิทธิภาพ
Overview: Claude Code คืออะไร?
Claude Code ต่างจาก ChatGPT หรือ web-based AI ตรงที่มันรันใน terminal ของคุณ และมีสิทธิ์เข้าถึง filesystem, Git, และ shell commands ได้จริง:
Key insight: Claude Code ไม่ใช่แค่ wrapper รอบ API แต่เป็น agentic system ที่มี tool use, memory, และ execution environment ครบ
Core Architecture Layers
Claude Code แบ่งเป็น 4 layers หลัก:
1. User Interface Layer
Claude Code รองรับหลาย interfaces:
- CLI Mode - รันใน terminal ด้วย
claudecommand - VSCode Extension - Embedded ใน editor พร้อม context awareness
- API/SDK Mode - สำหรับ automation และ CI/CD
2. Agent Controller - หัวใจของระบบ
Agent Controller คือ "สมอง" ที่ควบคุมการทำงานทั้งหมด:
แต่ละ iteration ของ loop:
- Receive input - user message + tool results จาก iteration ก่อน
- Build context - รวม system prompt, CLAUDE.md, conversation history
- Call Claude - ส่งไป API และรับ response + tool calls
- Execute tools - รัน tools ที่ Claude เลือก (Read, Write, Bash, etc.)
- Check completion - ถ้ายังไม่เสร็จ กลับไป step 1
Tool System - ความสามารถของ Claude Code
Claude Code มี built-in tools ที่ให้ AI โต้ตอบกับ codebase ได้:
Tool Execution Flow
User: "Fix the bug in auth.ts"
Claude thinks: I need to read the file first
-> Tool: Read(file_path="src/auth.ts")
<- Returns: file content
Claude thinks: I see the issue, let me fix it
-> Tool: Edit(file_path="src/auth.ts", old_string="...", new_string="...")
<- Returns: success
Claude thinks: Let me verify by running tests
-> Tool: Bash(command="npm test")
<- Returns: All tests passed
Claude: "Fixed the null check bug in auth.ts. Tests passing."
Permission Model
Claude Code มีระบบ permission modes ที่ผู้ใช้เลือกได้:
| Mode | Auto-approve | Use case |
|---|---|---|
| Ask | ไม่มี - ถามทุกครั้ง | Learning, sensitive code |
| Auto-edit | Read/Edit อัตโนมัติ | Development workflow |
| Full auto | ทุก tool | CI/CD, automation |
Context Management
หนึ่งในความท้าทายของ AI agent คือ context window มีจำกัด Claude Code จัดการด้วยหลายวิธี:
1. CLAUDE.md - Project Memory
ไฟล์ CLAUDE.md ในโปรเจคเป็น persistent context ที่ถูกโหลดทุกครั้ง:
# CLAUDE.md
## Project Overview
E-commerce API built with Node.js + PostgreSQL
## Development
- Run tests: npm test
- Start dev: npm run dev
- DB migration: npm run migrate
## Conventions
- Use camelCase for variables
- All API routes in /src/routes/
- Error handling via middleware
2. Automatic Context Injection
Claude Code ฉีด context อัตโนมัติ:
- Git status - current branch, uncommitted changes
- IDE selection - highlighted code ใน VSCode
- Recent conversation - compressed summary ถ้ายาวเกิน
- Tool results - output จาก tools ก่อนหน้า
3. Context Compaction
เมื่อ conversation ยาวเกินไป Claude Code จะ compact ข้อความเก่าเป็น summary เพื่อประหยัด tokens:
MCP: Model Context Protocol
MCP คือ protocol ที่ให้ Claude Code เชื่อมต่อกับ external tools ได้ไม่จำกัด:
MCP Architecture
Claude Code
|
+-- Built-in Tools (Read, Write, Bash, etc.)
|
+-- MCP Client
|
+-- stdio:// -> Local MCP Server
+-- sse:// -> Remote MCP Server
+-- docker:// -> Containerized MCP
MCP servers ทำให้ Claude Code ขยายความสามารถได้ไม่จำกัด โดยไม่ต้องแก้ core code
Sub-agents: Parallel Execution
Claude Code สามารถ spawn sub-agents เพื่อทำงานขนานกันได้:
Sub-agent types ที่มี:
- Explore - ค้นหาและเข้าใจ codebase
- Plan - วางแผน implementation
- General-purpose - ทำงานทั่วไป
- Background - รันแบบ async ไม่ block main
Security Model
Claude Code ออกแบบมาให้ safe by default:
1. Sandbox Execution
Bash commands รันใน sandbox ที่:
- จำกัด network access
- ป้องกันการเข้าถึงไฟล์นอก working directory
- Block dangerous commands (rm -rf /, etc.)
2. Prompt Injection Defense
Claude Code ระวัง prompt injection จาก external sources:
- Tool outputs ถูก sanitize
- File contents ถูก mark เป็น untrusted
- User ได้รับ warning ถ้าพบ suspicious content
3. Git Safety
มี safeguards สำหรับ Git operations:
- ไม่ force push ไป main/master โดยไม่ถาม
- ไม่ reset --hard โดยไม่ warning
- Commit message ต้องมี Co-Authored-By
Model Routing
Claude Code เลือกใช้ model ตาม task:
| Model | Use case | Trade-off |
|---|---|---|
| Opus | Complex reasoning, architecture | Slow, expensive |
| Sonnet | General coding, default | Balanced |
| Haiku | Simple tasks, sub-agents | Fast, cheap |
ผู้ใช้สามารถ override ด้วย /model command หรือตั้งค่าใน settings
Hooks System
Claude Code มี hooks ที่ให้ inject custom logic ได้:
# .claude/settings.json
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"command": "echo 'Running: $TOOL_INPUT' >> ~/.claude/bash.log"
}
],
"PostToolUse": [
{
"matcher": "Edit",
"command": "prettier --write $FILE_PATH"
}
]
}
}
Hook types ที่รองรับ:
- PreToolUse - ก่อนรัน tool
- PostToolUse - หลังรัน tool
- Notification - แจ้งเตือน events
สรุป: Lessons Learned
จากการศึกษา Claude Code architecture เราได้เรียนรู้:
Key takeaway: การสร้าง AI Agent ที่ทำงานจริงได้ต้องคิดเรื่อง tool design, context management, และ safety ไม่ใช่แค่ prompt engineering