AI Agent Harness Engineering
LLM là CPU, Harness là OS — engineering layer biến "text predictor" thành "autonomous agent".
01. Bản Chất Vấn Đề
LLM (Large Language Model) có khả năng reasoning ấn tượng, nhưng một mình nó không làm được gì cả:
| Giới hạn LLM | Hệ quả |
|---|---|
| Chỉ nhận text, trả text | Không đọc file, chạy code, gọi API |
| Quên hết sau mỗi session | Không học từ kinh nghiệm |
| Không verify được output | Tự tin khi sai (hallucination) |
| Fixed computation per token | Không "nghĩ sâu hơn" cho bài khó |
Harness là tất cả engineering bên ngoài LLM — biến API call thành autonomous agent. Nếu LLM là CPU, thì harness là Operating System: quản lý memory, I/O, scheduling, permissions, và process lifecycle.
---
config:
theme: neutral
look: classic
---
flowchart TB
subgraph HARNESS["⚙ HARNESS (Operating System)"]
direction TB
LOOP["🔄 Agentic Loop"]:::purple
CTX["📋 Context Manager"]:::blue
TOOL["🛠 Tool Executor"]:::green
MEM["💾 Memory System"]:::orange
PERM["🔒 Permission Layer"]:::red
AGENT["🤖 Multi-Agent"]:::cyan
end
LLM["🧠 LLM (CPU)"]:::purple2
LOOP <--> LLM
CTX --> LOOP
TOOL --> LOOP
MEM --> CTX
PERM --> TOOL
AGENT --> LOOP
classDef purple fill:#e8daef,stroke:#8e44ad,color:#1a1a1a,stroke-width:2px
classDef purple2 fill:#e8daef,stroke:#8e44ad,color:#1a1a1a
classDef blue fill:#cce5ff,stroke:#004085,color:#1a1a1a
classDef green fill:#d4edda,stroke:#28a745,color:#1a1a1a
classDef orange fill:#ffeeba,stroke:#856404,color:#1a1a1a
classDef red fill:#f8d7da,stroke:#721c24,color:#1a1a1a
classDef cyan fill:#d1ecf1,stroke:#0c5460,color:#1a1a1aThesis: Chất lượng agent phụ thuộc vào harness nhiều hơn model. Một model yếu hơn với harness tốt có thể tiệm cận hoặc vượt model mạnh hơn với harness tệ. Engineering layer quyết định kết quả cuối cùng.
02. Agentic Loop — Trái Tim Của Agent
ReAct: Reason + Act
Paper: "ReAct: Synergizing Reasoning and Acting in Language Models" (Yao et al., 2023)1
Agentic loop là pattern cốt lõi: LLM xen kẽ suy nghĩ và hành động, dùng kết quả hành động để inform suy nghĩ tiếp theo.
---
config:
theme: neutral
look: classic
---
flowchart LR
A["📥 Assemble
Context"]:::blue --> B["🧠 LLM
Inference"]:::purple --> C{"Tool call
trong output?"}:::orange
C -->|"Có"| D["🛠 Execute
Tool"]:::green --> E["📋 Inject
Result"]:::blue --> A
C -->|"Không"| F["📤 Final
Response"]:::green
classDef blue fill:#cce5ff,stroke:#004085,color:#1a1a1a
classDef purple fill:#e8daef,stroke:#8e44ad,color:#1a1a1a
classDef orange fill:#ffeeba,stroke:#856404,color:#1a1a1a
classDef green fill:#d4edda,stroke:#28a745,color:#1a1a1aAnatomy — 1 Iteration
Mỗi vòng lặp gồm 6 bước:
| Bước | Hành động | Chi phí context |
|---|---|---|
| 1. Assemble context | System prompt + history + tool results → 1 prompt | Tích lũy |
| 2. LLM inference | Model generate text + tool calls | Output tokens |
| 3. Parse tool calls | Extract structured JSON từ output | 0 |
| 4. Permission check | Allow / Deny / Ask user | 0 |
| 5. Execute tools | Chạy code, đọc file, gọi API | 0 (ngoài context) |
| 6. Inject results | Tool output → thêm vào history | Tích lũy |
Stop conditions:
- Output không chứa tool call → agent "quyết định" dừng
- Max iterations reached → safety limit
- User interrupt → manual stop
Tại Sao Loop Quan Trọng Hơn Model
---
config:
theme: neutral
look: classic
---
flowchart LR
subgraph BAD["❌ Strong model, weak harness"]
direction TB
B1["GPT-4"]:::purple
B2["No loop, 1-shot"]:::red
B3["No tools"]:::red
end
subgraph GOOD["✅ Decent model, strong harness"]
direction TB
G1["GPT-3.5"]:::blue
G2["ReAct loop"]:::green
G3["Tools + Memory"]:::green
end
classDef purple fill:#e8daef,stroke:#8e44ad,color:#1a1a1a
classDef blue fill:#cce5ff,stroke:#004085,color:#1a1a1a
classDef red fill:#f8d7da,stroke:#721c24,color:#1a1a1a
classDef green fill:#d4edda,stroke:#28a745,color:#1a1a1aPaper gốc ReAct1 chứng minh: model + tool access + reasoning loop vượt trội model alone trên HotpotQA và FEVER benchmarks. Quan trọng hơn, loop giảm hallucination vì model có thể verify suy luận bằng tool.
03. Context Window — Tài Nguyên Quý Nhất
Bản Chất
LLM chỉ "thấy" những gì trong context window. Context = RAM của agent: giới hạn cứng, dùng hết thì phải quên.
Mọi thông tin — system prompt, conversation history, tool results, memory — đều cạnh tranh cùng 1 context window.
Nghịch Lý Context
| Nhiều context | Ít context |
|---|---|
| Nhiều information | Ít information |
| Nhưng loãng attention | Nhưng tập trung hơn |
Paper: "Lost in the Middle: How Language Models Use Long Contexts" (Liu et al., 2023)2
Nghiên cứu chứng minh LLM có xu hướng bỏ qua thông tin ở giữa context window — nhớ tốt đầu và cuối, quên giữa. Đây là lý do tại sao context management không chỉ là "nhét càng nhiều càng tốt".
3 Chiến Lược Quản Lý Context
---
config:
theme: neutral
look: classic
---
flowchart TB
CW["📋 Context Window
(giới hạn)"]:::blue
CW --> COMP["🗜 Compression
Auto-compact
old messages"]:::orange
CW --> DEFER["⏳ Deferral
ToolSearch, lazy-load
skills on-demand"]:::green
CW --> ISO["🔀 Isolation
Sub-agents có
context riêng"]:::purple
classDef blue fill:#cce5ff,stroke:#004085,color:#1a1a1a,stroke-width:2px
classDef orange fill:#ffeeba,stroke:#856404,color:#1a1a1a
classDef green fill:#d4edda,stroke:#28a745,color:#1a1a1a
classDef purple fill:#e8daef,stroke:#8e44ad,color:#1a1a1a1. Compression — Khi context đạt ~95% capacity, harness tự động summarize messages cũ, giữ lại messages gần đây. Trade-off: mất chi tiết cũ để nhường chỗ cho thông tin mới.
2. Deferral — Không load tất cả vào context cùng lúc. Tool schemas chỉ load tên (defer full schema qua ToolSearch). Skills chỉ load mô tả ngắn (≤250 chars mỗi skill). Full content load on-demand khi cần.
3. Isolation — Sub-agents có context window riêng. 20K tokens work → 500 token summary trả về parent = 40x context savings.
Nguyên tắc thiết kế: Mọi cơ chế mở rộng của agent harness đều xoay quanh 1 mục tiêu: tối đa hóa chất lượng reasoning trong giới hạn context window.
04. Tool System — Tay Chân Của Agent
Từ Function Calling Đến MCP
Function calling là cơ chế cốt lõi: LLM output structured JSON mô tả tool call → harness parse và execute.
---
config:
theme: neutral
look: classic
---
flowchart LR
A["🧠 LLM output:
tool_call(Read,
path='src/app.js')"]:::purple --> B["⚙ Harness
parse JSON"]:::blue --> C["🛠 Execute
fs.readFile(...)"]:::green --> D["📋 Result
→ context"]:::orange
classDef purple fill:#e8daef,stroke:#8e44ad,color:#1a1a1a
classDef blue fill:#cce5ff,stroke:#004085,color:#1a1a1a
classDef green fill:#d4edda,stroke:#28a745,color:#1a1a1a
classDef orange fill:#ffeeba,stroke:#856404,color:#1a1a1aLLM không chạy code. Nó chỉ sinh text mô tả ý định. Harness chịu trách nhiệm parse, validate, và execute — đây là lý do tại sao tool system là deterministic code, không phải AI.
Tiến Hóa
---
config:
theme: neutral
look: classic
---
flowchart LR
A["Hardcoded
functions"]:::dim --> B["Function
Calling
(2023)"]:::blue --> C["Built-in
Tools"]:::green --> D["🌐 MCP
(2024)"]:::purple
classDef dim fill:#f0f0f0,stroke:#999,color:#666
classDef blue fill:#cce5ff,stroke:#004085,color:#1a1a1a
classDef green fill:#d4edda,stroke:#28a745,color:#1a1a1a
classDef purple fill:#e8daef,stroke:#8e44ad,color:#1a1a1a,stroke-width:2pxMCP (Model Context Protocol)3 giải quyết N×M problem: N models × M tools = N×M integrations. MCP chuẩn hóa giao thức (JSON-RPC 2.0) để bất kỳ model nào cũng dùng được bất kỳ tool nào qua 1 protocol duy nhất.
Tool Design Principles
| Nguyên tắc | Lý do |
|---|---|
| Clear schema | LLM dùng đúng tool khi hiểu rõ tool làm gì |
| Atomic operations | Nhỏ, composable — Read + Edit tốt hơn ReadAndEdit |
| Deterministic | Cùng input = cùng output. AI đã stochastic, tools không nên |
| Safe by default | Permission layer giữa LLM decision và actual execution |
05. Memory & Persistence — Vượt Qua Session
Vấn Đề
LLM weights cố định sau training. Mọi "learning" xảy ra trong context window — và biến mất khi session kết thúc.
3 Layers Of Memory
---
config:
theme: neutral
look: classic
---
flowchart TB
subgraph L1["Layer 1: Working Memory"]
direction LR
WM["📋 Context Window
Conversation history
Tool results"]:::blue
end
subgraph L2["Layer 2: Instructions"]
direction LR
INS["📝 CLAUDE.md
Persistent rules
Project context"]:::orange
end
subgraph L3["Layer 3: Learned Memory"]
direction LR
MEM["💾 MEMORY.md + files
Facts learned across sessions
User preferences"]:::green
end
L3 -->|"load on-demand"| L1
L2 -->|"always loaded"| L1
classDef blue fill:#cce5ff,stroke:#004085,color:#1a1a1a
classDef orange fill:#ffeeba,stroke:#856404,color:#1a1a1a
classDef green fill:#d4edda,stroke:#28a745,color:#1a1a1a| Layer | Scope | Lifetime | Mechanism |
|---|---|---|---|
| Working memory | 1 conversation | Session | Context window |
| Instructions | 1 project | Permanent | CLAUDE.md files (git-tracked) |
| Learned memory | Cross-session | Permanent | Memory files (write on learn) |
Design Insight
Memory là file system, không phải neural:
- Deterministic — nội dung chính xác, không approximate
- Editable — user và agent đều có thể sửa
- Transparent — đọc được bằng mắt thường
Trade-off: mỗi byte memory load vào context = tốn tokens. Memory system phải quyết định load gì, bỏ gì.
06. Permission & Safety — Kiểm Soát Agent
Bản Chất Vấn Đề
Agent có quyền chạy code, sửa file, gọi API, push code. Một agent không kiểm soát = security nightmare.
Fundamental tension: autonomy vs control. Quá nhiều autonomy = nguy hiểm. Quá ít = vô dụng (phải hỏi user mọi thứ).
Layered Permission Model
---
config:
theme: neutral
look: classic
---
flowchart TB
LLM["🧠 LLM
wants to run: rm -rf /"]:::purple
LLM --> RULES["📋 Permission Rules
Allow / Deny / Ask"]:::blue
RULES -->|"Deny"| BLOCK["🚫 Blocked"]:::red
RULES -->|"Allow"| EXEC["✅ Execute"]:::green
RULES -->|"Ask"| USER["👤 User confirms?"]:::orange
USER -->|"Yes"| EXEC
USER -->|"No"| BLOCK
EXEC --> SANDBOX["🔒 Sandbox
OS-level isolation"]:::cyan
classDef purple fill:#e8daef,stroke:#8e44ad,color:#1a1a1a
classDef blue fill:#cce5ff,stroke:#004085,color:#1a1a1a
classDef red fill:#f8d7da,stroke:#721c24,color:#1a1a1a
classDef green fill:#d4edda,stroke:#28a745,color:#1a1a1a
classDef orange fill:#ffeeba,stroke:#856404,color:#1a1a1a
classDef cyan fill:#d1ecf1,stroke:#0c5460,color:#1a1a1a4 layers chồng nhau:
| Layer | Mechanism | Ví dụ |
|---|---|---|
| Permission rules | Pattern matching (allow/deny/ask) | Allow: Bash(git:*), Deny: Bash(rm -rf:*) |
| User confirmation | Interactive prompt | "Edit this file? [Y/n]" |
| Sandbox | OS-level isolation (seatbelt/seccomp) | Filesystem + network restrictions |
| Hooks | Deterministic automation at event boundaries | Pre/PostToolUse, Stop |
Hooks — Deterministic Automation
Hooks là code chạy tại event boundaries — không phải AI, mà là deterministic shell commands:
PreToolUse → Run BEFORE tool execution (can block)
PostToolUse → Run AFTER tool execution (can validate)
Stop → Run when agent finishesTại sao hooks quan trọng: AI là stochastic (non-deterministic). Hooks bổ sung layer deterministic — chạy 100% khi event fire, không phụ thuộc vào "mood" của LLM.
07. Multi-Agent — Scaling Beyond 1 Context
Vấn Đề
1 context window không đủ cho complex tasks. Grep 40 files = 20,000 tokens vào context → loãng reasoning cho mọi thứ khác.
Sub-Agent Architecture
---
config:
theme: neutral
look: classic
---
flowchart TB
P["🖥 Parent Agent
context: task + summaries"]:::purple
P -->|"spawn"| E["🔍 Explore
haiku, read-only"]:::blue
P -->|"spawn"| G["⚡ General
full tools"]:::green
P -->|"spawn"| C["🎯 Custom
specialized"]:::orange
E -->|"summary"| R["📋 ~500 tokens"]:::dim
G -->|"summary"| R
C -->|"summary"| R
R -->|"inject"| P
classDef purple fill:#e8daef,stroke:#8e44ad,color:#1a1a1a,stroke-width:2px
classDef blue fill:#cce5ff,stroke:#004085,color:#1a1a1a
classDef green fill:#d4edda,stroke:#28a745,color:#1a1a1a
classDef orange fill:#ffeeba,stroke:#856404,color:#1a1a1a
classDef dim fill:#f0f0f0,stroke:#999,color:#666Context savings: Sub-agent làm 20K tokens work → trả 500 token summary → parent chỉ tăng 500 tokens thay vì 20K. 40x reduction.
Communication Patterns
| Pattern | Cơ chế | Use case |
|---|---|---|
| Parent-Child | Spawn → result summary | Delegated research, isolated execution |
| Agent Teams | Shared task list, DM nhau | Complex cross-domain work |
| Worktree | Git isolation | Parallel code changes, no file conflicts |
Trade-offs
| Benefit | Cost |
|---|---|
| Context isolation | 1.2-25x API cost |
| Parallel execution | Coordination overhead |
| Specialized agents | Latency (cold start) |
Khi nào dùng sub-agent vs trực tiếp? Rule of thumb: nếu task sẽ dump >5K tokens kết quả vào context (test output, grep results, log analysis), delegate. Nếu task cần <1K tokens, làm trực tiếp.
08. Sự Tiến Hóa — Từ Prompt Chain Đến Agent OS
---
config:
theme: neutral
look: classic
---
flowchart TB
subgraph GEN1["2022: Prompt Chaining"]
direction LR
G1A["LangChain"]:::dim
G1B["AutoGPT"]:::dim
end
subgraph GEN2["2023: Structured Tool Use"]
direction LR
G2A["Function Calling"]:::blue
G2B["Single-turn agents"]:::blue
end
subgraph GEN3["2023-2024: Production Agents"]
direction LR
G3A["Claude Code"]:::green
G3B["Cursor"]:::green
G3C["Devin"]:::green
end
subgraph GEN4["2024-2025: Agent Ecosystem"]
direction LR
G4A["MCP"]:::purple
G4B["Skills"]:::purple
G4C["Plugins"]:::purple
end
GEN1 -->|"fragile,
no recovery"| GEN2
GEN2 -->|"no memory,
single-turn"| GEN3
GEN3 -->|"extensible,
composable"| GEN4
classDef dim fill:#f0f0f0,stroke:#999,color:#666
classDef blue fill:#cce5ff,stroke:#004085,color:#1a1a1a
classDef green fill:#d4edda,stroke:#28a745,color:#1a1a1a
classDef purple fill:#e8daef,stroke:#8e44ad,color:#1a1a1a,stroke-width:2px| Thế hệ | Đặc điểm | Vấn đề |
|---|---|---|
| Gen 1: Prompt chains (2022) | Chuỗi prompts cố định, AutoGPT | Fragile, infinite loops, no error recovery |
| Gen 2: Function calling (2023) | Structured tool calls, single-turn | No memory, no multi-step reasoning |
| Gen 3: Production agents (2023-24) | ReAct loop, real agentic behavior | Closed ecosystems, hard to extend |
| Gen 4: Agent ecosystem (2024-25) | MCP, Skills, Plugins, Hooks | Composable, extensible, standardized |
09. Design Patterns & Anti-Patterns
Patterns Hiệu Quả
| Pattern | Mô tả |
|---|---|
| Gather → Act → Verify | Hiểu trước, làm sau, kiểm tra cuối4 |
| Read before Write | Đọc file trước khi sửa — tránh overwrite |
| Fail fast, recover gracefully | Detect errors sớm, retry có strategy |
| Context budget awareness | Biết mình đang dùng bao nhiêu context |
Anti-Patterns Cần Tránh
| Anti-Pattern | Tại sao fail |
|---|---|
| Infinite tool loops | Không có stop condition → chạy mãi |
| Context stuffing | Dump mọi thứ vào prompt → loãng attention2 |
| God agent | 1 agent làm tất cả → context overflow |
| Hallucination propagation | Kết quả sai → reasoning sai → action sai → cascade |
| No verification | Tin LLM output mà không check → silent errors |
10. Bản Chất Sâu — Tại Sao Harness Engineering Khó?
LLM Là Stochastic
Cùng input, LLM có thể cho output khác nhau mỗi lần. Harness phải handle non-determinism — verification loops, retry logic, fallback strategies.
Error Handling Cho "Sai Logic"
Software truyền thống: lỗi = crash, exception, timeout → dễ detect. AI agent: lỗi = wrong reasoning → output "đúng format nhưng sai logic" → khó detect hơn nhiều.
---
config:
theme: neutral
look: classic
---
flowchart LR
subgraph TRAD["Traditional Software"]
T1["Error = crash"]:::red --> T2["Easy to detect"]:::green
end
subgraph AI["AI Agent"]
A1["Error = wrong reasoning"]:::orange --> A2["Looks correct, is wrong"]:::red
end
classDef red fill:#f8d7da,stroke:#721c24,color:#1a1a1a
classDef green fill:#d4edda,stroke:#28a745,color:#1a1a1a
classDef orange fill:#ffeeba,stroke:#856404,color:#1a1a1aThe Alignment Tax
Mỗi safety layer = thêm latency + cost + complexity:
| Layer | Latency | Purpose |
|---|---|---|
| Permission check | ~0ms | Prevent dangerous actions |
| User confirmation | Seconds → minutes | Human oversight |
| Sandbox | ~50ms | OS-level isolation |
| Hook execution | ~100ms | Deterministic validation |
| Sub-agent verification | Seconds | Cross-check reasoning |
Fundamental tension: Agent cần tự chủ để hữu ích, nhưng cần kiểm soát để an toàn. Không có giải pháp hoàn hảo — chỉ có trade-offs phù hợp với từng use case.
11. Kết Luận
3 Pillars Của Agent Harness
---
config:
theme: neutral
look: classic
---
flowchart LR
A["🔄 Loop
(reasoning)"]:::purple
B["🛠 Tools
(action)"]:::green
C["💾 Memory
(learning)"]:::orange
A --- AGENT(["🤖 Agent"]):::blue
B --- AGENT
C --- AGENT
classDef purple fill:#e8daef,stroke:#8e44ad,color:#1a1a1a
classDef green fill:#d4edda,stroke:#28a745,color:#1a1a1a
classDef orange fill:#ffeeba,stroke:#856404,color:#1a1a1a
classDef blue fill:#cce5ff,stroke:#004085,color:#1a1a1a,stroke-width:2px5 takeaways:
- Harness > Model — engineering layer quyết định agent quality, không phải model size
- Context window là constraint trung tâm — mọi thiết kế đều xoay quanh nó2
- Loop là cốt lõi — không có ReAct loop, LLM chỉ là chatbot1
- Safety là engineering problem — permissions, sandbox, hooks tạo layered defense
- Tương lai: Harness → Agent OS — standardized (MCP3), pluggable (Skills), composable (Plugins)
AI agent không phải là LLM. AI agent là LLM + Harness. Và phần harness — phần engineering, deterministic, boring — mới là thứ quyết định agent có thực sự hữu ích hay không.
References
-
Yao, S. et al. (2023). ReAct: Synergizing Reasoning and Acting in Language Models. ICLR 2023. arXiv:2210.03629 ↩ ↩2 ↩3
-
Liu, N. F. et al. (2023). Lost in the Middle: How Language Models Use Long Contexts. TACL 2023. arXiv:2307.03172 ↩ ↩2 ↩3
-
Anthropic (2024). Model Context Protocol — Architecture. modelcontextprotocol.io ↩ ↩2
-
Anthropic (2025). How Claude Code Works — Agent loop: gather context, take action, verify results. code.claude.com ↩
AI Blog — Cập nhật 04/2026