Làm sao nó gọi được một hàm mà nó không tự chạy được?
Tool calling (gọi hàm, function calling) · Thực hành · 5 phút · agents, format · học sau Structured output (giải mã có ràng buộc)
Tool calling cho phép model nhờ code của bạn chạy một hàm. Bạn gửi tên, mô tả và JSON schema của từng tool; model trả lời bằng một tên hàm và tham số JSON, code của bạn chạy hàm đó rồi gửi kết quả lại.
1 Đoán trước
2 Xem
One tool call, four messages
1 / 4your app sends · request 1: question + tools
user: “What’s the weather in San Francisco?”
tools: [{ "name": "get_weather", "description": "Get the current weather for a given location.", "input_schema": { "type": "object", "properties": { "location": { "type": "string", "description": "City and state, e.g. San Francisco, CA" } }, "required": ["location"] } }]
Sent on every request, called or not. Claude Haiku 5.5 also adds a hidden 286-token tool prompt.
requests and replies quoted from Anthropic’s tool-use docs (claude-opus-5-5 example) · checked 2026-10-23
Your app sends the question and a menu of tools: a name, a description and a JSON schema each.
Model chỉ viết: trước tiên là một lệnh gọi để code của bạn chạy, rồi, khi kết quả được dán lại, mới đến câu trả lời.
Model vẫn chỉ viết chữ. Khi request liệt kê các tool (hàm được mô tả bằng tên, mô tả và JSON schema), nó có thể viết một lệnh gọi theo khuôn cố định thay cho câu trả lời. API trả lệnh đó về thành một khối riêng và dừng, với stop_reason "tool_use". Có chạy hay không là do code của bạn.
4 Bạn là model
Bạn là model, có một tool: get_weather. Trong năm lượt, hãy chọn viết gì: một câu trả lời, một câu hỏi lại, hay một lệnh gọi tool.
you are the model · your one tool: get_weather (location: string, required) · what do you write next?
turn 1 of 5 · the user writes
“What’s the weather in San Francisco?”
requests and replies quoted from Anthropic’s tool-use docs (claude-opus-5-5 example) · checked 2026-10-23
You are the model. Answer, ask, or write a tool call?5 Kiểm tra
Chỗ hay hỏng. Tham số sai: thiếu thành phố, sai kiểu. Kiểm tra trước khi chạy, hoặc đặt strict: true (Anthropic, OpenAI) hay chế độ validated của Gemini; gửi lỗi lại với is_error, và Claude thử lại 2–3 lần. Gọi thừa: mô tả mơ hồ, quá nhiều tool; Gemini khuyên tối đa 10–20. Nhiều lệnh gọi nối thành vòng lặp tạo ra một agent.
7 Nói lại bằng lời bạn
8 Nhiệm vụ · 5 phút
Với một API key Gemini miễn phí, làm theo tài liệu function calling của Gemini: khai báo get_weather (location: string, bắt buộc) và hỏi gemini-3.8-flash “What’s the weather in Hanoi?”. Bạn nhận về một functionCall có name và args, không phải câu trả lời. Gửi lại một functionResponse bạn tự bịa, {"temp_c": 31}, rồi đọc câu trả lời. Sau đó hỏi “What is a cold front?”: nó có gọi tool không?
Ba câu nhanh
Rút ngẫu nhiên từ bộ câu hỏi của bài; lần sau sẽ khác.
Xong chưa?
Nhận email khi có bài mới
Bài mới nằm trong bản tin hằng tuần, chỉ gửi vào tuần có bài. Bạn xác nhận qua email trước; hủy đăng ký chỉ với một lần bấm.
Đọc thêm
Nguồn
- https://platform.claude.com/docs/en/agents-and-tools/tool-use/overview (checked 2026-10-23; no update date): the get_weather round trip on claude-opus-5-5 (tool with input_schema, location required; output “Claude called get_weather with {"location": "San Francisco, CA"}”; result “15 degrees Celsius, partly cloudy”; answer “The current weather in San Francisco is 15 degrees Celsius with partly cloudy skies.”); client tools stop with stop_reason "tool_use"; with tool_choice auto Claude “responds directly for stable knowledge”; with no location, Claude Opus “is much more likely to recognize that a parameter is missing and ask for it”, Claude Sonnet might guess {"location": "New York, NY", "unit": "fahrenheit"}; strict: true guarantees schema conformance; tool use system prompt tokens: Claude Haiku 5.5 286 (auto, none), 406 (any, tool); the tools parameter counts as input tokens
- https://platform.claude.com/docs/en/agents-and-tools/tool-use/handle-tool-calls (checked 2026-10-23): example id toolu_01A09q90qw90lq917835lq9; tool_result with tool_use_id and is_error; tool_result blocks must immediately follow the tool_use; error “ConnectionError: the weather service API is not available (HTTP 500)” and Claude’s reply “I’m sorry, I was unable to retrieve the current weather because the weather service API is not available. Please try again later.”; “Claude will retry 2–3 times with corrections before apologizing”; server tools such as web_search run on Anthropic’s infrastructure
- https://ai.google.dev/gemini-api/docs/function-calling (updated 2026-09-23, checked 2026-10-23): “The model doesn’t execute the function itself”; modes auto, any, none, validated (“Model ensures function schema adherence”); “Keep active set to 10-20 tools maximum”; examples on gemini-3.8-flash
- https://developers.openai.com/api/docs/guides/function-calling (checked 2026-10-23): five steps from a request with tools to a final response “(or more tool calls)”; “Setting strict to true will ensure function calls reliably adhere to the function schema”
- https://ai.google.dev/gemini-api/docs/pricing (updated 2026-10-22, checked 2026-10-23): Gemini 3.8 Flash free tier, input and output “Free of charge”; aifree.dev catalog google-gemini-developer-api-free-tier (verified 2026-10-12)
Đăng Oct 23, 2026 · cập nhật Oct 23, 2026