18%

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 + resource
  • create_invoice — CRUD-style
  • calculate_shipping_cost — descriptive

ไม่ดี

  • do_stuff — ไม่ descriptive
  • helper — ไม่บอกว่าทำอะไร
  • processData — camelCase (ใช้ snake_case)
  • get — ไม่มี resource name

Description Writing for LLMs

Description เป็นส่วนที่สำคัญที่สุด — Claude อ่าน description เพื่อตัดสินว่าจะใช้ tool นี้เมื่อไหร่:

  1. บอกว่า tool ทำอะไร — “Search the product database…”
  2. บอกว่า returns อะไร — “Returns matching products with id, name, price…”
  3. บอกว่าใช้เมื่อไหร่ — “Use this when the user asks about…”
  4. บอก edge cases — “Returns empty array if no matches found”

Tool Granularity

Fine-grained (แยกย่อย)Coarse-grained (รวมกัน)
Claude เลือกได้ preciseTool 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 เมื่อไหร่
  • required array กำหนดว่า Claude ต้องส่ง params ไหน — ถ้า required แต่ไม่ส่ง = API error
  • Tool กิน tokens ใน system prompt — ยิ่ง tools เยอะ ยิ่ง expensive
  • enum ช่วย constrain output — ลดโอกาส hallucinate parameter values
  • ข้อสอบมักถามว่า tool definition ไหนดีกว่า — ดูที่ description completeness และ schema clarity