Function calling (also called tool use) is how you connect LLMs to real-world systems. Instead of generating free text, the model returns a structured JSON object naming which function to call and with what arguments. Your code runs the function and returns the result — enabling AI agents, data extraction pipelines, and dynamic applications.
1. Define your tools correctly
Every tool needs three things: a name (what the LLM calls to invoke it), a description (how the model decides when to use it), and a parameters schema (what arguments it needs). The description is the most important part — treat it like a docstring that explains when this function is appropriate. Vague descriptions lead to wrong tool selection.
- name: snake_case, descriptive (get_weather not gw)
- description: explains what the tool does AND when to use it
- parameters: JSON schema with types, required fields, and enum values where possible
- Keep parameter names self-documenting — city not c
2. Handle the tool call loop
Function calling is not a single API call — it is a loop. The model returns a tool_call object (not a final answer). You execute the function. You call the API again with the tool result. The model may call more tools or finally produce its answer. Most SDK tool runners handle this loop automatically — use them in production.
Tip
Set a maximum iteration count (e.g. 10) to prevent runaway loops. If the model hasn't produced a final answer after N steps, something is wrong.
3. Parallel tool calling
When the model determines that multiple tool calls are independent, it can invoke them all in a single response rather than sequentially. Claude Sonnet 4.6, GPT-4o, and Gemini 2.0 all support this. For example, fetching weather in three cities can be done in one round trip instead of three. Enable parallel calls and handle the array of results before the next API call.
4. Streaming tool calls
With streaming enabled, tool call arguments arrive as incremental chunks. This matters for latency — you can start parsing arguments before the model finishes generating them. Use streaming when tool execution takes time (e.g. a web search) and you want to show progress to the user. Claude and OpenAI both support streaming tool calls; handle the delta.tool_calls array in each chunk.
5. Error handling and retries
Tool calls fail in production. Database timeouts, API rate limits, network errors — the model doesn't know the difference. Return errors in a consistent format so the model can recover: `{ error: 'timeout', message: 'Database query timed out after 5s', retry: true }`. The model can then decide whether to retry, use a different approach, or tell the user.
- Return errors as tool results, not exceptions — keep the loop running
- Include enough context for the model to decide the next step
- Distinguish retryable errors from permanent failures
- Cap total tool call budget per conversation to control costs
6. Tool design best practices
Good tools are narrow, deterministic, and idempotent. Each tool should do one thing. If a tool modifies state, make sure the model understands the consequences — describe it explicitly. Prefer tools that return rich context (not just a success/fail flag). And never give the model access to tools it shouldn't be able to invoke autonomously.
Tip
For sensitive actions (deleting data, sending emails, making purchases), require explicit user confirmation before execution — don't let the model trigger them directly.
Provider format differences
OpenAI, Anthropic, and Google use different JSON formats for tool definitions and results. If you're building cross-provider, use an abstraction layer. Many frameworks (LangChain, LlamaIndex, Instructor) normalise formats. If using providers directly, write thin adapters: convert your internal tool schema to each provider's format, and normalise results back to your internal format.
