LLM Function Calling (Tool Use) Explained with Examples
How function calling works: defining tools with JSON Schema, the request-response loop, parallel calls, error handling and designing tools models use well.
Published December 1, 2026
Function calling, also called tool use, lets a model ask your application to run a function and give it the result. The model never executes anything itself; it returns a structured request ("call get_weather with city=Pune"), your code runs it, and the result goes back to the model. This is the foundation of agents, assistants that take actions, and natural-language interfaces to APIs.
The concept
You send the model a list of tools, each with a name, a description and a JSON Schema for its arguments. If the model decides a tool would help, its response contains one or more tool calls instead of (or alongside) text. Your application validates the arguments, executes the function, and sends the result back in a tool-result message. The model then continues, possibly calling more tools, until it produces a final answer.
Tool design matters as much as prompts. Models choose tools based on names and descriptions, so make them specific and unambiguous. Keep inputs minimal and typed. Return concise results containing what the model needs to continue, and return errors as readable messages so the model can recover.
The function calling loop
Tool design: bad vs better
| Bad tool design | Better |
|---|---|
| do_stuff(action, params) | Separate tools: get_order, cancel_order |
| "Gets data" | "Look up an order by ID. Returns status, items and delivery date." |
| Returns full 5 KB API payload | Returns the 6 fields the task needs |
| Throws exception on not found | Returns "No order with ID A999. Ask the user to check the ID." |
| 20 overlapping search tools | 3 clear tools with distinct purposes |
Parallel tool calls
Practical examples
Tool definition
JSON Schema arguments with descriptions.
{
"name": "get_order",
"description": "Look up a customer order by its ID (format A followed by digits). Returns status, items and expected delivery date.",
"input_schema": {
"type": "object",
"properties": { "order_id": { "type": "string", "pattern": "^A\\d+$" } },
"required": ["order_id"]
}
}Dispatching tool calls safely
Validate, execute, and return errors as content.
def handle_tool_call(call):
fn = REGISTRY.get(call.name)
if fn is None:
return f"Unknown tool {call.name}"
try:
args = fn.schema.validate(call.arguments)
return json.dumps(fn(**args))
except ValidationError as e:
return f"Invalid arguments: {e}"
except NotFound as e:
return str(e)Forcing or disabling tools
APIs let you control whether the model must call a tool.
tool_choice = "auto" # model decides
tool_choice = {"type": "tool", "name": "get_order"} # force a specific tool (syntax varies by provider)
tool_choice = "none" # text onlyCommon mistakes
- Executing tool calls without validating arguments.
- Giving models tools with destructive side effects and no confirmation step.
- Long, vague tool descriptions or many overlapping tools.
- Crashing on tool errors instead of returning a helpful message.
Key takeaways
- The model requests tool calls; your application executes them.
- Tools are defined by name, description and JSON Schema.
- Good tool design (specific, minimal, concise results) drives reliability.
- Validate inputs and handle errors as content the model can use.
FAQ
Is function calling the same as MCP?
No. Function calling is the model-API mechanism; MCP is a protocol for discovering and connecting tools to applications, which then expose them via function calling.
Can the model call tools in a loop by itself?
Only if your application keeps sending results back. The loop lives in your code.
How many tools can I provide?
APIs allow many, but accuracy drops as the list grows. Load only tools relevant to the current task.