Claude Code AI Agent Architecture

Claude Code Architecture Deep Dive

By Anirach Mingkhwan AI Developer Tools 2026
Arthur the Labrador at a futuristic architecture console

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 ได้จริง:

Developer
พิมพ์คำสั่ง
->
Claude Code CLI
Agent loop
->
Tools
Read/Write/Bash
->
Codebase
Files & Git
Key insight: Claude Code ไม่ใช่แค่ wrapper รอบ API แต่เป็น agentic system ที่มี tool use, memory, และ execution environment ครบ

Core Architecture Layers

Claude Code แบ่งเป็น 4 layers หลัก:

1
User Interface Layer
Terminal UI, VSCode Extension, input handling
2
Agent Controller
Main loop, context management, model routing
3
Tool Execution Layer
File ops, Bash, Grep, Search, MCP servers
4
System Integration
Git, filesystem, permissions, sandboxing

1. User Interface Layer

Claude Code รองรับหลาย interfaces:

2. Agent Controller - หัวใจของระบบ

Agent Controller คือ "สมอง" ที่ควบคุมการทำงานทั้งหมด:

Agent Loop
Input Processing
User message
Context injection
Model Call
Claude API
Tool selection
Execution
Run tools
Collect results
Loop until done

แต่ละ iteration ของ loop:

  1. Receive input - user message + tool results จาก iteration ก่อน
  2. Build context - รวม system prompt, CLAUDE.md, conversation history
  3. Call Claude - ส่งไป API และรับ response + tool calls
  4. Execute tools - รัน tools ที่ Claude เลือก (Read, Write, Bash, etc.)
  5. Check completion - ถ้ายังไม่เสร็จ กลับไป step 1

Tool System - ความสามารถของ Claude Code

Claude Code มี built-in tools ที่ให้ AI โต้ตอบกับ codebase ได้:

Read อ่านไฟล์
Write สร้างไฟล์ใหม่
Edit แก้ไขไฟล์
Bash รัน commands
Glob ค้นหาไฟล์
Grep ค้นหา content
Agent Spawn sub-agents
WebSearch ค้นหาเว็บ

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 ที่ผู้ใช้เลือกได้:

ModeAuto-approveUse case
Askไม่มี - ถามทุกครั้งLearning, sensitive code
Auto-editRead/Edit อัตโนมัติDevelopment workflow
Full autoทุก toolCI/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 อัตโนมัติ:

3. Context Compaction

เมื่อ conversation ยาวเกินไป Claude Code จะ compact ข้อความเก่าเป็น summary เพื่อประหยัด tokens:

Full History
100k tokens
->
Compaction
AI summarize
->
Summary + Recent
~20k tokens

MCP: Model Context Protocol

MCP คือ protocol ที่ให้ Claude Code เชื่อมต่อกับ external tools ได้ไม่จำกัด:

MCP
Database MCP
Query PostgreSQL, MySQL โดยตรง
MCP
Browser MCP
Control Playwright browser
MCP
Docker MCP
Manage containers
MCP
Custom MCPs
Build your own 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 เพื่อทำงานขนานกันได้:

Multi-agent Execution
Main Agent
Coordinate
spawns
Sub-agent 1
Research code
Sub-agent 2
Run tests
Sub-agent 3
Check docs

Sub-agent types ที่มี:


Security Model

Claude Code ออกแบบมาให้ safe by default:

1. Sandbox Execution

Bash commands รันใน sandbox ที่:

2. Prompt Injection Defense

Claude Code ระวัง prompt injection จาก external sources:

3. Git Safety

มี safeguards สำหรับ Git operations:


Model Routing

Claude Code เลือกใช้ model ตาม task:

ModelUse caseTrade-off
OpusComplex reasoning, architectureSlow, expensive
SonnetGeneral coding, defaultBalanced
HaikuSimple tasks, sub-agentsFast, 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 ที่รองรับ:


สรุป: Lessons Learned

จากการศึกษา Claude Code architecture เราได้เรียนรู้:

Loop
Agent Loop Pattern
Think -> Act -> Observe -> Repeat
Tool
Tool Abstraction
Capability = Tool definitions
Context
Context Management
Persistent + Dynamic + Compaction
Safety
Safety by Design
Permissions + Sandbox + Confirmation
Key takeaway: การสร้าง AI Agent ที่ทำงานจริงได้ต้องคิดเรื่อง tool design, context management, และ safety ไม่ใช่แค่ prompt engineering

Part of the AI Developer Tools Series