Tool use and function calling
Tool schemas, the round trip, constrained decoding, and retries.
From parsing free text
Chain of thought gave the model room to compute, but no new facts and no actuators. Early LM agents wrote actions as free text (search[Dunmore]) and a regex parsed them. The format is only a convention: wrong casing, quotes, or a missing tool name each cost a step. If one action in 50 is malformed (), twenty actions fail at least once with probability .
Key idea. Declare every tool as a typed function — name, model-facing description, JSON Schema for arguments. The model emits structured calls; the runtime validates, executes, and returns results (including errors) as messages. Where validity must be guaranteed, constrain decoding.
A function definition
A tool definition has three parts: name, description (the only docs the model sees), and parameters as JSON Schema (type, required, enum, pattern, additionalProperties: false, …). Providers wrap the same content differently (parameters / input_schema / inputSchema). Definitions sit in context on every step — tokens paid every turn.
The round trip of a call
The model proposes calls; the program runs them.
- Call. One or more calls with identifiers in one turn.
- Validate. Schema check; invalid arguments become error results.
- Execute. Independent valid calls may run in parallel — latency is the slowest, not the sum.
- Return. One tool message per id; order need not match.
- Continue. The model reads results (and errors) and may call again or answer.
Identifiers pair results with calls. Errors are observations. The model never executes: permissions, confirmations, and sandboxes live in the runtime. Tool results are data — text that looks like an instruction is still data (Agent security).
Constrained decoding
Validation catches bad calls after generation. Constrained decoding prevents them. At prefix , mask tokens that are not live (no completion in ):
By induction every prefix stays live and decoding stops only in . Checks run over tokens, not characters. Per-step masking is not conditioning on : it can overweight unlikely valid strings. Valid shape ≠ right tool or argument. Often let the model reason freely and constrain only the call block.
Toolformer and code as action
Toolformer (Schick et al., 2023) built tool-call training data without human labels: sample candidate calls in plain text, execute, keep a call if its result lowers next-token loss by at least versus no call / call without result. Fine-tune on the filtered text. GPT-J 6.7B with tools beat much larger models on several math-word sets.
Code as action (CodeAct): the model writes a short Python program; the interpreter’s stdout/stderr is the observation. More composition in one step — and a stricter sandbox need. Coding agents push this to repositories.
Errors, retries and next
Four failure modes: malformed output (decoder/runtime), invalid arguments (schema → model), transient execution errors (runtime retries with backoff on idempotent calls), valid-but-wrong (only later checks). Three independent attempts at failure rate 0.3 succeed with probability . Model retries help only when the context changes — hence informative error messages.
MCP lets separate servers advertise the same three fields; Workflows and protocols covers it.
Next: combine reasoning and tools over many steps — ReAct and the agent loop.
Check your understanding
The same questions as in Pulse. Answers are shared between modes.
Answers are saved on the server and tied to this browser until you sign in.