← Back to Blog
function callingllm tool useai agent toolstool calling apillm function calling

Function Calling & Tool Use with LLMs (2026): A Practical Guide

How LLM function calling / tool use works — define tools, handle the model's tool calls, return results, and build agents. With a copy-paste example and best practices.

2026-07-15

Function Calling & Tool Use with LLMs (2026): A Practical Guide

Function calling is a control protocol: the model proposes a structured tool request, your application validates and executes it, then the model receives the result. Production reliability comes from schemas, authorization, idempotency, timeouts, and audit logs—not from trusting generated arguments.

A practical evaluation method

  • Validate every argument against a strict schema and business rules.
  • Separate read and write permissions and require confirmation for material actions.
  • Log tool name, arguments, result, latency, retries, and final user outcome.

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

  1. You send the user message + a list of tool definitions (name, description, JSON-schema parameters).
  2. Model replies either with an answer, or with a tool call (which tool + arguments).
  3. You execute the tool and send the result back.
  4. 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.

Related Posts