2.3 Tool Distribution & Tool Choice
Tool Distribution คืออะไร
Tool Distribution คือกลยุทธ์ในการจัดสรรว่า Claude จะเห็น tools อะไรบ้างในแต่ละ turn — ไม่จำเป็นต้องให้ทุก tool ทุกครั้ง การให้ tools ที่เกี่ยวข้องเท่านั้นช่วยให้ Claude เลือกถูกต้องมากขึ้น ใช้ tokens น้อยลง และลดโอกาสเลือก tool ผิด
tool_choice Parameter
tool_choice ควบคุมว่า Claude จะเลือก tool อย่างไร:
# auto (default) — Claude ตัดสินเองว่าจะใช้ tool หรือตอบ text
response = client.messages.create(
tools=tools,
tool_choice={"type": "auto"},
...
)
# any — บังคับให้ Claude ต้องใช้ tool ตัวใดตัวหนึ่ง (ห้ามตอบ text)
response = client.messages.create(
tools=tools,
tool_choice={"type": "any"},
...
)
# tool — บังคับให้ Claude ใช้ tool ที่ระบุ
response = client.messages.create(
tools=tools,
tool_choice={"type": "tool", "name": "search_database"},
...
)
Strategic Tool Presentation
Dynamic Tool Loading
ให้ tools ตาม context ปัจจุบัน:
def get_tools_for_step(current_step):
if current_step == "research":
return [search_tool, read_file_tool, web_fetch_tool]
elif current_step == "code":
return [edit_tool, write_tool, bash_tool]
elif current_step == "review":
return [read_tool, grep_tool]
Tool Limiting
จำกัดจำนวน tools เพื่อลด confusion:
- ช่วง research → ให้เฉพาะ read/search tools
- ช่วง write → ให้เฉพาะ edit/write tools
- ช่วง review → ให้เฉพาะ read tools (ไม่ให้แก้)
When Claude Picks Tools
Claude เลือก tool จาก:
- Description match — description ที่ตรงกับ intent ปัจจุบัน
- Parameter fit — input ที่มีอยู่ match กับ schema ของ tool
- Context — system prompt instructions ที่บอกว่าเมื่อไหร่ควรใช้ tool ไหน
Exam Tips
tool_choice: "any"= ต้องใช้ tool (ห้ามตอบ text) — ใช้เมื่อต้องการ structured outputtool_choice: {"type": "tool", "name": "X"}= บังคับ tool เฉพาะ — ใช้ใน pipeline ที่รู้ว่า step นี้ต้องทำอะไร- การให้ tools น้อยลง = Claude เลือกถูกต้องมากขึ้น + ใช้ tokens น้อยลง
- ข้อสอบอาจถามว่า tool_choice แบบไหนเหมาะกับ scenario ไหน