Từ "nói" sang "làm". Function calling, ReAct loop, code execution — pattern quan trọng nhất để agent tác động lên thế giới thật. Chapter 5 sách Gulli (20 trang, dày nhất Part 1).
Giới hạn của LLM thuần — knowledge cutoff, no action
Schema, tool_call, tool_result, feedback loop
Native · ReAct · Code exec · MCP · Skills
Code Python, framework compare, sandbox
5 điểm nhớ + homework
• Knowledge cutoff — không biết giá cổ phiếu hôm nay.
• Không đọc file riêng — không truy hồi DB, không xem log.
• Không tác động — không gửi email, không tạo issue, không chuyển tiền.
• Không tính toán chính xác — math phức tạp hay sai lệch.
• Không có state — không nhớ giữa turn nếu không có tool memory.
• Truy realtime — API weather, price, stock.
• Đọc/ghi file — Read, Write, Grep, filesystem.
• Gọi service — send_email, create_ticket, run_query.
• Code execution — Python sandbox tính toán chính xác.
• Kết nối agent khác — qua MCP hoặc A2A (S-10).
Tool Use là bước biến LLM từ text generator thành agent đúng nghĩa.
"name": "get_weather", "description": "Get current weather for a city. Use only when user asks about weather.", "input_schema": { "type": "object", "properties": { "city": {"type": "string", "description": "City name in English"}, "unit": {"type": "string", "enum": ["C", "F"]} }, "required": ["city"] }
Description CHÍNH LÀ prompt cho router nội bộ — viết mô tả rõ ràng quan trọng hơn viết code hàm.
Model nghĩ: "cần tool nào?"
Sinh JSON call với args.
Framework thực thi hàm.
Tool_result ⇒ context.
Model dùng kết quả ⇒ trả lời hoặc tool tiếp.
ReAct = Reason + Act (Yao et al., 2022). Loop này chạy nhiều turn cho đến khi model quyết định trả lời cuối.
OpenAI tools, Claude tool_use, Gemini function_declarations. Chuẩn API provider.
Loop text-based: Thought/Action/Observation. Dùng khi model không có native tool.
Python/JS sandbox. Anthropic Code Execution, OpenAI Code Interpreter. Math & data.
Universal protocol. Bất kỳ MCP server nào cũng plug & play. Chi tiết S-10.
Claude Skill: package (prompt + tool + resource) load theo demand. Tiết kiệm context.
Xu hướng 2025: dịch từ Native tool per app ⇒ MCP server chia sẻ ⇒ Skill catalogs theo domain.
import anthropic tools = [{ "name": "get_weather", "description": "Get current weather for a city.", "input_schema": {"type": "object", "properties": {"city": {"type": "string"}}, "required": ["city"]} }] def run_tool(name, args): if name == "get_weather": return weather_api.get(args["city"]) messages = [{"role": "user", "content": "Thời tiết Hà Nội hôm nay?"}] while True: resp = client.messages.create(model="claude-sonnet-4-6", tools=tools, messages=messages) if resp.stop_reason == "end_turn": break for block in resp.content: if block.type == "tool_use": result = run_tool(block.name, block.input) messages.append({"role": "assistant", "content": resp.content}) messages.append({"role": "user", "content": [{"type": "tool_result", "tool_use_id": block.id, "content": str(result)}]})
| Provider/Framework | Cách khai báo | Parallel tools | Code exec |
|---|---|---|---|
| OpenAI | tools=[{type:function}] |
✅ multi tool_calls | Code Interpreter (Assistants) |
| Anthropic Claude | tools=[{input_schema}] |
✅ parallel tool_use | Code Execution beta |
| Google Gemini | function_declarations |
✅ automatic function calling | Code Execution native |
| LangChain | @tool decorator |
Runnable parallel | PythonREPLTool |
| Claude Code | Native Bash/Read/Edit/Grep + Skills + MCP | ✅ nhiều tool/turn | Bash sandbox |
>50 tool trong prompt ⇒ model confuse, chọn sai. Chia theo domain / dùng Skill.
"Do stuff" ⇒ model không biết khi nào gọi. Description = prompt cho router.
Retry ⇒ gọi lại ⇒ gửi email 2 lần. Thêm request_id + idempotency key.
Loop lỗi ⇒ đấm API 1000 req/s. Circuit breaker + budget cap.
Cho exec() tuỳ ý ⇒ RCE. Dùng E2B, Firecracker VM, gVisor.
Không biết agent làm gì ⇒ audit fail, debug bó tay.
Agent thời tiết gọi API theo địa điểm ⇒ format phản hồi thân thiện.
Tool: get_weather, search web.
Kiểm tồn kho, trạng thái đơn hàng, xử lý thanh toán qua API.
Tool: check_stock, get_order.
Lấy giá cổ phiếu → gọi calculator tính lãi. LLM tự yếu math.
Tool: quote, calculator.
Trợ lý cá nhân gửi email/Slack/Zalo với nội dung trích từ user.
Tool: send_email, send_slack.
Agent lập trình chạy Python sandbox → phân tích output.
Tool: python_exec, Bash.
Nhà thông minh: tắt đèn, bật điều hoà qua API smart home.
Tool: set_device_state.
Ranh giới: function calling = hàm code định trước. Tool calling rộng hơn — có thể là API, DB query, hoặc chỉ dẫn gửi tới agent chuyên biệt khác (bắc cầu sang A2A ở S-10).
Nguồn: Cẩm nang thực hành xây dựng HTTM (VN Gulli Ch.5) · local hoá ví dụ.
Read / Write / Edit — filesystem.Bash — shell sandbox.Grep / Glob — search code.WebFetch / WebSearch.TaskCreate / TaskUpdate — plan mode (S-08).Agent — spawn subagent (A2A trong nhà).Đây là tham chiếu chuẩn để hiểu tool ecosystem. Khi build agent riêng, câu hỏi luôn là: tool nào native, tool nào Skill, tool nào MCP?
Tham chiếu: Khám phá Claude Code — Không gian thiết kế Hệ thống Tác tử (VN).
# Native tool có sẵn # Read, Write, Edit, Bash # Grep, Glob, WebFetch... # Xem allowlist tool claude # /help → xem tool có sẵn # Custom tool qua Skill mkdir .claude/skills/weather # SKILL.md + script # Config permission tool # ~/.claude/settings.json "permissions": { "allow": ["Bash(npm:*)"] } # One-shot chạy tool claude "Grep TODO" \ --permission-mode acceptEdits
# Built-in tools trong Agent # Read/Edit/Run/Search cursor . # Cmd+I → Agent mode # @file @folder @code # @web @docs @git # Custom tool qua MCP # ~/.cursor/mcp.json { "mcpServers": { "weather": { "command": "node", "args": ["server.js"] } } } # Terminal tool: run cmd # Cursor tự đề xuất # npm test → approve/deny
# Built-in tools # --tools code_execution # --tools google_search gemini --tools \ code_execution \ -p "Tính 15! bằng Python" # Sandbox tool exec gemini --sandbox \ -p "Chạy script test.py" # YOLO mode (auto-approve) # CẨN THẬN production gemini --yolo -p "..." # File attachment gemini -p "Review" \ -a src/main.py
Cẩn thận: --yolo (Gemini) và --dangerously-skip-permissions (Claude) bỏ mọi confirm — chỉ dùng khi test isolated.
Windows Bash: Claude Code chạy trên Git Bash / WSL2 tốt nhất. Gemini CLI chạy PowerShell OK. Cursor có Terminal tích hợp.
Tool Use = biến LLM thành agent thực thụ. Không có tool = chỉ là text generator.
Description quan trọng hơn code. Nó là prompt cho router nội bộ trong LLM.
ReAct loop = Reason → Act → Observe → Continue cho đến khi end_turn.
Tool side-effect luôn idempotent + rate-limited + sandboxed. Đặc biệt là code exec.
Xu hướng: Native ⇒ MCP ⇒ Skills để tool tái sử dụng liên app.