Tool Calling: How LLMs Execute Actions via Structured Function Calls
This article explains how Tool Calling enables large language models to execute external functions, covering JSON Schema tool definitions, model-generated call requests with arguments, programmatic validation and execution, result feedback via call IDs, and critical error handling to distinguish failures from successful 'no data' responses.
How Tools Are Described
Tool Calling lets a model propose which tool to invoke and with what parameters; the program then executes the tool and returns the result. The article illustrates this with a weather‑query scenario: "Check Tianjin's weather tomorrow; if it rains, remind me at 8 AM to bring an umbrella."
First, the backend weather API is wrapped as a function get_weather. Its description is passed to the model using a JSON Schema (shown here in Claude’s custom‑tool format):
{
"name": "get_weather",
"description": "查询指定城市、指定日期的天气预报,不创建提醒。",
"input_schema": {
"type": "object",
"properties": {
"city": {"type": "string", "description": "城市名称"},
"date": {"type": "string", "description": "日期,YYYY-MM-DD"}
},
"required": ["city", "date"],
"additionalProperties": false
}
}nameidentifies the tool; description tells the model its purpose. The input_schema defines the parameter contract: both city and date are required strings, and no extra fields are allowed.
The author warns against vague descriptions like "handle weather." If the description only says "handle weather," the model might try to use it for air‑quality or historical‑temperature queries, causing parameter mismatches and confusing errors. Each tool must have a single, clearly defined responsibility.
What the Model Returns
Assume the application supplies the current date as 2026‑11‑14 (timezone Asia/Shanghai). The model may emit a call request like:
{
"call_id": "weather_01",
"name": "get_weather",
"arguments": {"city": "天津", "date": "2026-11-15"}
}argumentsare the parameters the model infers from the user’s request, the conversation context, and the tool schema. They are not data returned by the tool; the weather has not been queried yet. The model can misunderstand the city or miscalculate "tomorrow."
Even if the output conforms to the schema, it only guarantees format validity, not semantic correctness. The program must validate : check required fields, types, parse the date, verify it exists (e.g., 2026-02-30 is a string but not a valid date), and ensure it falls within the forecast range. If the user says "remind me before I leave" without a default time, the program should ask for clarification.
After validation, the program looks up the registered function by the tool name and calls the real API. Never execute the model‑supplied name as code directly.
How Results Are Fed Back
Suppose the API returns a light rain forecast. The program structures the result and sends it back to the model, preserving the original call_id to link the response to the request:
{
"call_id": "weather_01",
"ok": true,
"data": {"forecast": "小雨"}
}The call_id matches the request’s value; real platforms use similar correlation IDs (e.g., Claude’s id / tool_use_id). The program must include this result in the next model call — merely logging it is insufficient.
Seeing rain in the forecast, the model then requests the reminder tool with the message "出门记得带伞" and the timestamp 2026-11-15T08:00:00+08:00. The program executes it and feeds back the creation result. Only after that confirmation can the model truthfully say "reminder created."
The two tools cannot run in parallel ; the reminder depends on the weather check’s outcome, so the condition "if it rains" would be skipped if both fired simultaneously.
When a Timeout Becomes "No Rain"
During debugging, the weather service timed out, yet the final reply claimed "no rain tomorrow, no reminder needed." Logs revealed the tool caught the exception and returned an empty object; downstream code defaulted the missing precipitation field to "no rain."
The model received an incorrect conclusion. Adding a prompt like "please answer accurately" does not fix this. Tools must return an explicit failure status, e.g., ok: false with an error type such as timeout, while preserving the call_id. On seeing failure, the program must not enter the "no rain" branch nor proceed to create a reminder.
The system should perform a limited retry. If it still fails, respond: "Weather query timed out; cannot determine if an umbrella is needed; reminder not created." "Not found" and "found no rain" must be distinguished.
Tool Calling connects the model’s action requests to program execution and feeds real results back into the loop. Tools can query public data, but they cannot yet access unindexed internal documents. The model also lacks that knowledge. Tool Calling gives the Agent "hands and feet"; Retrieval‑Augmented Generation (RAG) gives it "knowledge" — the next article will cover how to inject retrieved external knowledge into the model call that generates the final answer.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
