What Function Calling Is
Function calling (a.k.a. tool use) lets an LLM decide to call your code — a weather lookup, a database query, a calculator — instead of guessing. You describe the tools; the model returns a structured request to call one; you run it and feed the result back. It's the foundation of every serious AI agent.
The Loop
- You send the user message + a list of tool definitions (name, description, JSON-schema parameters).
- Model replies either with an answer, or with a tool call (which tool + arguments).
- You execute the tool and send the result back.
- Model uses the result to produce the final answer (or calls another tool).
Example
tools = [{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get current weather for a city",
"parameters": {
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"],
},
},
}]
payload = {
"model": "claude-sonnet-4-6",
"messages": [{"role": "user", "content": "Do I need an umbrella in Osaka?"}],
"tools": tools,
}
# 1) model returns a tool_call for get_weather(city="Osaka")
# 2) you run get_weather("Osaka") -> "rain, 18C"
# 3) append the tool result to messages and call again
# 4) model answers: "Yes—bring an umbrella, it's raining in Osaka."The exact request/response shape varies slightly by model — confirm in the docs. The advantage of one key: the pattern is the same whether you run Claude, GPT-5.6 or Gemini, so you can pick the best tool-using model per task. See multimodal AI API.
Best Practices
- Write clear tool descriptions — the model chooses tools from your text; vague descriptions cause wrong calls.
- Validate arguments before executing (never trust model output as safe input).
- Keep tools small and single-purpose; compose them.
- Cap the loop (max N tool rounds) to avoid runaway agents — and watch cost, since each round is a new call. See how much it costs to run an AI agent.
- Return structured results the model can parse — pairs well with structured outputs.
Which Models
For agentic tool use, strong picks include GPT-5.6, Claude (Sonnet/Opus), and open agents like GLM-5.1 and Kimi K2.6. Compare in best coding LLM API.
Bottom Line
Define tools with clear JSON schemas, run the call → tool-call → result → answer loop, validate inputs, and cap rounds. Master this and you can build real agents — swapping in the best tool-using model on one key.
Start free with a $1 credit and wire up your first tool.
