2.1 Tool Interface Design
Tool Interface Design คืออะไร
Tool Interface Design คือการออกแบบ “ประตู” ที่ Claude ใช้เชื่อมต่อกับโลกภายนอก — ทุกครั้งที่ Claude อยากทำอะไรที่ไม่ใช่แค่คิดและตอบ (เช่น อ่านไฟล์, query database, เรียก API) มันต้องใช้ tool ที่เราออกแบบให้ การออกแบบ tool ที่ดีคือการทำให้ Claude เข้าใจว่า tool ทำอะไร รับ input อะไร และ return อะไร — อย่างชัดเจนที่สุด
Tool definition ประกอบด้วย 3 ส่วนหลัก: name, description, และ input_schema (JSON Schema) ทุกส่วนมีผลต่อว่า Claude จะเลือกใช้ tool นี้เมื่อไหร่และส่ง parameters ถูกต้องแค่ไหน
Tool Schema Anatomy
tools = [{
"name": "search_database",
"description": "Search the product database by name, category, or price range. Returns matching products with id, name, price, and stock status. Use this when the user asks about product availability or pricing.",
"input_schema": {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "Search term to match against product name or description"
},
"category": {
"type": "string",
"enum": ["electronics", "clothing", "food", "books"],
"description": "Filter by product category"
},
"min_price": {
"type": "number",
"description": "Minimum price in THB"
},
"max_price": {
"type": "number",
"description": "Maximum price in THB"
}
},
"required": ["query"]
}
}]
Naming Conventions
ดี
search_products— verb + noun, ชัดเจนget_user_profile— action + resourcecreate_invoice— CRUD-stylecalculate_shipping_cost— descriptive
ไม่ดี
do_stuff— ไม่ descriptivehelper— ไม่บอกว่าทำอะไรprocessData— camelCase (ใช้ snake_case)get— ไม่มี resource name
Description Writing for LLMs
Description เป็นส่วนที่สำคัญที่สุด — Claude อ่าน description เพื่อตัดสินว่าจะใช้ tool นี้เมื่อไหร่:
- บอกว่า tool ทำอะไร — “Search the product database…”
- บอกว่า returns อะไร — “Returns matching products with id, name, price…”
- บอกว่าใช้เมื่อไหร่ — “Use this when the user asks about…”
- บอก edge cases — “Returns empty array if no matches found”
Tool Granularity
| Fine-grained (แยกย่อย) | Coarse-grained (รวมกัน) |
|---|---|
| Claude เลือกได้ precise | Tool list สั้น กิน tokens น้อย |
| Permission control ง่าย | ยืดหยุ่นกว่า |
| Tool list ยาว tokens เยอะ | Claude ต้องเข้าใจ action param |
แนะนำ 5-20 tools สำหรับ agent ทั่วไป ถ้ามากกว่า 20 ให้ group หรือ dynamically load ตาม context
Parameter Design Tips
- ใช้
enumเมื่อมี fixed choices — ช่วยให้ Claude ไม่ต้องเดา - ใช้
requiredชัดเจน — required = ต้องมี, optional = Claude ตัดสินเอง - ตั้ง
descriptionทุก parameter — ไม่ใช่แค่ name - Avoid deeply nested objects — flatten เมื่อทำได้
Exam Tips
- Description ต้องบอก “when to use” — Claude ใช้ตัดสินว่าจะเรียก tool เมื่อไหร่
requiredarray กำหนดว่า Claude ต้องส่ง params ไหน — ถ้า required แต่ไม่ส่ง = API error- Tool กิน tokens ใน system prompt — ยิ่ง tools เยอะ ยิ่ง expensive
enumช่วย constrain output — ลดโอกาส hallucinate parameter values- ข้อสอบมักถามว่า tool definition ไหนดีกว่า — ดูที่ description completeness และ schema clarity