dev.toJuly 23, 2026 AFFECTS EXAM
Herramienta

AI Agent Tool-Calling Patterns: Building Reliable Function Calling in 2026

Six patterns for reliable AI agent tool calling in 2026, with a side-by-side Anthropic vs OpenAI function-calling comparison and strict-mode rules.

> Originally published at [heycc.cn](https://heycc.cn/en/posts/ai-agent-tool-calling-patterns/). This is a mirrored copy — the canonical version is kept up to date at the source.

# AI Agent Tool-Calling Patterns: Building Reliable Function Calling in 2026

Tool calling (also called function calling) is the moment [an AI agent](https://heycc.cn/en/posts/what-are-ai-agents-2026/) stops talking and starts doing. The model reads your tool definitions, decides one is relevant, emits a structured call, your code runs it, and the result flows back into the conversation. The mechanics are simple. Making them *reliable* — so the agent picks the right tool, fills in valid arguments, recovers from failures, and doesn't get hijacked by hostile data — is where most agent projects quietly break.

An agent that nails a scripted demo and then sends a malformed argument the first time a user phrases a request sideways is the failure mode every one of these patterns exists to prevent. They are drawn from Anthropic's tool-use documentation and OpenAI's function-calling guide (both linked in Sources below). The two providers converge on the same core ideas, with small but important differences in the knobs they expose — and the comparison table in Pattern 3 is the cross-provider synthesis you can't get from either doc set alone.

The loop, precisely

Both Anthropic and OpenAI implement the same five-step cycle. When you call the API with a `tools` parameter, the platform injects a special system prompt built from your tool definitions that instructs the model on how and when to call them. Anthropic documents this constructed prompt explicitly in its "Define tools" guide.

1. **You send the request** with the user message and the `tools` array.

2. **The model emits a tool call.** With Claude, the response carries `stop_reason: "tool_use"` and contains one or more `tool_use` content blocks, each with an `id`, a `name`, and an `input` object matching the tool's schema. OpenAI returns analogous `tool_calls`.

3. **Your application runs the tool** using those inputs.

4. **You send the output back.** With Claude, you reply with a user message containing a `tool_result` block whose `tool_use_id` matches the original request `id`. With OpenAI, you send a message with `role: "tool"` and a matching `tool_call_id`.

5. **The model produces the final answer** — or requests another tool, and the loop repeats.

The matching of IDs in step 4 is not optional bookkeeping. Anthropic's "Handle tool calls" docs are strict about ordering: `tool_result` blocks must immediately follow the assistant's `tool_use` message, and they must come first in the content array, before any text. If a turn produced parallel calls, all of the corresponding results must be submitted back together before the model continues. Drop one and the conversation is malformed.

![Reliable tool-calling loop showing request, tool_use emission, execution, result return with error branch, and final response](https:/

Read full article on dev.to