Tool Use and Function Calling
Single-turn function calling is solved. The hard part now is memory, dynamic decisions, and long tool chains, which is exactly where BFCL V4 (Patil et al., 2025) puts its weight: 40% agentic, 30% multi-turn, 10% live, 10% non-live, 10% hallucination. Toolformer (Schick et al., 2023) is where self-supervised tool use started.
Learning Objectives
- Explain Toolformer's self-supervised training signal: keep tool annotations only when execution reduces next-token loss.
- Name BFCL V4's five evaluation categories and what each measures.
- Implement a stdlib tool registry with schema validation, argument coercion, and execution sandboxing.
- Diagnose the four 2026 open problems: long-horizon tool chaining, dynamic decision-making, memory, and hallucination detection.
The Problem
Function calling means the model emits a structured request (a tool name plus arguments), your runtime validates and executes it, and the result goes back into the conversation. Early work asked whether a model could predict a correct call. Production asks something harder: can it chain tools across 40 steps, with memory and partial observability, recover when a tool fails, and never invent a tool that doesn't exist?
Toolformer set the baseline by showing that models can learn when to call tools with self-supervision. BFCL V4 defines the 2026 target. The distance between the two is where production agents live.
The Concept
Toolformer (Schick et al., NeurIPS 2023)
Toolformer is a training method where the model annotates its own pretraining corpus with candidate API calls and keeps only the ones that help. For each candidate, you execute the call. If including the result reduces loss on the next token, the annotation stays. Then you fine-tune on the filtered corpus.
The tools covered were a calculator, a QA system, search engines, a translator, and a calendar. The signal is purely whether the tool helps predict text, with no human labels.
The scale result matters: tool use emerges at scale. Smaller models get hurt by tool annotations, larger models gain. That's why 2026 frontier models have strong tool use baked in, while most 7B models need explicit tool-use fine-tuning before they're reliable.
Berkeley Function Calling Leaderboard V4 (Patil et al., ICML 2025)
BFCL is the benchmark that became the 2026 standard for evaluating function calling. V4 is weighted like this:
- Agentic (40%): full agent trajectories covering memory, multi-turn, and dynamic decisions.
- Multi-Turn (30%): interactive conversations with tool chains.
- Live (10%): real prompts submitted by users, a harder distribution.
- Non-Live (10%): synthetic test cases.
- Hallucination (10%): detecting when no tool should be called.
V3 introduced state-based evaluation: after a tool sequence, check the API's actual state (is the file created?) instead of matching the AST of the calls. V4 added web search, memory, and format sensitivity categories.
The headline 2026 finding is that single-turn function calling is nearly solved. Failures concentrate in four places: memory (carrying context across turns), dynamic decision-making (choosing tools based on prior results), long-horizon chains (drift after 20+ steps), and hallucination detection (declining to call when no tool fits).
Tool schema
A tool schema is the description of a tool that you hand to the model: a name, a description, and a JSON Schema for the arguments. Every provider has one, and they differ in details but share the same shape:
name: string
description: string (what it does, when to use it)
input_schema: JSON Schema (properties, required, types, enums)
Anthropic uses input_schema directly. OpenAI uses function.parameters. Both accept JSON Schema. The description is load-bearing because the model reads it to pick the tool, and bad descriptions are the number one root cause of wrong-tool-picked failures.
Argument validation
Trust no tool call. Before executing, check four things:
- Type coercion. The model may return the string "5" where the schema says int. Coerce if the meaning is unambiguous, reject if not.
- Enum validation. If the schema says
status in {"open", "closed"}and the model emits"in_progress", reject with a descriptive error. - Required fields. A missing required field becomes an immediate error observation back to the model, not a crash.
- Format validation. Dates, emails, URLs: validate with concrete parsers, not regex.
Every failure should come back as a structured observation so the model can retry with the right shape.
Parallel tool calls
Modern providers let the model emit several tool calls in one assistant turn. The loop:
- The model emits 3 tool calls with distinct
tool_use_ids. - The runtime executes them, in parallel if they're independent.
- Each result goes back as a
tool_resultblock correlated bytool_use_id.
Treat correlation IDs as load-bearing. Swap them and results get routed to the wrong calls.
Sandboxing
Tool execution is the sandbox boundary (Lesson 09 covers it in detail). The short version: every tool should declare its read/write surface, network access, timeout, and memory cap. A generic run_shell(cmd) is a red flag. A specific git_status() is much safer.
Build It
code/main.py implements a production-shaped tool registry:
- A JSON Schema subset validator (stdlib only).
- Tool registration with description, input schema, timeout, and executor.
- Argument coercion and enum validation.
- Parallel tool dispatch with correlation IDs.
- Error observations as structured strings.
Run it:
python3 code/main.py
The trace shows a mini agent dispatching five calls in one turn. Three succeed (one after coercing the string "4" to an integer), and two are deliberately bad: an enum value outside the schema and a tool that doesn't exist. The registry rejects each with a descriptive error the model can act on.
Use It
Every provider (Anthropic, OpenAI, Gemini, Bedrock) has its own tool schema. If you need to support more than one, put a translation layer in front: the OpenAI Agents SDK, the Vercel AI SDK, or a LangChain tool adapter. BFCL is the reference benchmark, so if tool use is central to your product, run it against your agent before you ship.
Ship It
outputs/skill-tool-registry.md generates a tool catalog, schema, and registry for a given task domain. It includes description-quality checks: does each tool's description tell the model when to use it?
When an agent picks the wrong tool, read the tool descriptions before you read the prompt. It's the cheapest fix and the most common cause, and people skip it because it feels too small to matter. Write each description as "what this does, and when to reach for it instead of the other tools," then reread the whole catalog as if you were the model seeing it cold.
The second habit is what the validator says when it rejects a call. "Invalid input" teaches the model nothing. "status must be one of open, closed; got in_progress" lets it fix the call on the next turn. Most of what looks like a model failing at tool use turns out to be a registry giving it nothing to learn from.
Exercises
- Add a "no-op" tool that lets the model explicitly refuse to use any other tool. Measure it on a BFCL-like hallucination test.
- Implement argument coercion for int-as-string and float-as-string. Where does coercion start to hide real bugs?
- Add a per-tool timeout and a circuit breaker (refuse the tool for 60s after 3 consecutive failures). What does this change about how the model recovers?
- Read the BFCL V4 description. Pick one category (say, "multi-turn"), run 10 example prompts through your agent, and report the pass rate.
- Port the stdlib validator to Pydantic or Zod. What did Pydantic or Zod catch that the toy missed?
Key Terms
| Term | What people say | What it actually means |
|---|---|---|
| Function calling | "Tool use" | Structured-output tool invocation with a validated schema |
| Toolformer | "Self-supervised tool annotation" | Schick 2023: keep tool calls whose results reduce next-token loss |
| BFCL | "Berkeley Function Calling Leaderboard" | 2026 benchmark: 40% agentic, 30% multi-turn, 10% live, 10% non-live, 10% hallucination |
| Tool schema | "Function signature for the model" | name, description, JSON Schema of arguments |
| tool_use_id | "Correlation ID" | Ties a tool call to its result; essential for parallel dispatch |
| Hallucination detection | "Know when not to call" | V4 category: refuse to call when no tool fits |
| Argument coercion | "String-to-int repair" | Narrow fixes for predictable schema mismatches; reject if ambiguous |
| Sandboxing | "Tool execution boundary" | Per-tool read/write surface, network, timeout, memory cap |
Further Reading
- Schick et al., Toolformer (arXiv:2302.04761): self-supervised tool annotation
- Berkeley Function Calling Leaderboard (V4): the 2026 eval benchmark
- Anthropic, Tool use documentation: how tool use works and how to define tools for Claude
- OpenAI Agents SDK docs: function tool type and Guardrails