Before you start: words you need#
| Word | Plain meaning |
|---|---|
| Tool definition | The name, description and JSON schema of a function the model may call. It is sent as prompt text on every request. |
| Tool call | The model’s request to run a tool, with arguments, written in the model’s own markup and parsed by the server (18.i). |
| Structured output | Forcing the output to follow a schema or grammar by masking tokens that would break it (18.e). |
| Envelope | The markup around a tool call (tags, function name). Different from the arguments inside it. |
| Strict mode | Constraining the arguments to the tool’s schema as well as the envelope. |
| MCP | Model Context Protocol: a standard way for agents to discover and call tools served by separate “MCP servers”. |
| Deferred loading | Keeping most tool definitions out of the prompt until the model searches for one. |
1. What is it?#
Lessons 18.e and 18.i show how vLLM turns tool definitions into prompt text, parses calls out of the output, and constrains output with a grammar. This lesson is about what changes when that runs millions of times a day inside agent loops:
- Tool definitions are prompt tokens on every turn. Fifty tools can be tens of thousands of tokens before the task even starts.
- Tool results are most of the context. In an agent session, the model writes a few hundred tokens a turn and tools append thousands (27.b).
- Rare failures become common. A 1% malformed-call rate per call is a 35% chance per 43-turn session that something breaks.
- Constraint has a CPU cost in the engine core, per constrained request, per step.
Facts checked on 13 November 2026.
2. Why does it matter?#
Tool use is where the model meets the rest of the system. When it fails, the failure is not a slightly worse answer; it is an exception in the agent loop, a retry, or a stuck task. And the costs hide in places a chat-era dashboard does not show: prompt tokens spent on unused tool schemas, engine-core CPU spent on grammars, and retries that double some sessions’ bills.
3. Simple analogy#
A help desk with a binder of forms.
- Handing every caller the entire binder (all tool definitions) takes time before they can say anything. Handing them an index and fetching the right form on request (deferred loading) is faster, and they pick the right form more often.
- A form with boxes (strict schema) is harder to fill wrongly than a blank sheet (free text).
- Checking every box as it is written (grammar masking) costs a clerk’s time per form, so a desk that processes only forms needs more clerks, not a faster printer.
4. Tiny example: compounding failures#
The per-call failure rates below are illustrative; measure your own model, parser and tools.
mode per call 10 turns 43 turns 100 turns retry tokens per 1k sessions
free text + parser 2.0% 18.3% 58.1% 86.7% 351020
envelope constrained 0.5% 4.9% 19.4% 39.4% 86432
strict arguments 0.1% 1.0% 4.2% 9.5% 17217The columns for 10, 43 and 100 turns are the probability that a session sees at least one bad call. At agent lengths, “only 2%” means most sessions hit a problem. Constraining the call cuts the rate by an order of magnitude; it does not reach zero, because a call can be well-formed and still wrong (the wrong tool, a valid but nonsensical argument).
5. Technical explanation#
The tool-definition tax#
Every definition is prompt text, rendered by the chat template (18.i), on every turn.
- Size. Anthropic’s documentation gives an example: a multi-server setup (GitHub, Slack, Sentry, Grafana, Splunk) “can consume ~55k tokens in definitions before Claude does any work”. Its tool search feature “typically reduces this by over 85 percent”. Vendor figures, but the order of magnitude is easy to check with your own tokenizer.
- Selection accuracy. The same page says the model’s ability to pick the right tool “degrades once you exceed 30–50 available tools”. Expect similar behaviour from open models; measure it.
- Caching. Definitions sit near the start of the prompt, so they are cached if, and only if, they are byte-identical every turn (27.b). Sort them, version them per session, and never inject per-request values into descriptions.
Deferred loading keeps most definitions out of the prompt. Anthropic’s tool search tool takes
the full list on every request but excludes defer_loading: true tools “from the system-prompt
prefix”; when the model searches and finds one, the definition is appended inline in the
conversation. That design keeps the cached prefix stable while the set of loaded tools grows. A
self-hosted platform can copy the pattern: a small always-loaded set plus a search tool whose
results append definitions to the history.
Tool results: the real context growth#
Tool outputs (file contents, search results, logs) dominate context growth in agent sessions. The serving cost of a 20,000-token tool result is a 20,000-token prefill on the next turn and 20,000 tokens of KV for the rest of the session. Controls belong in the tools, not the engine: paginate, truncate with a pointer to “read more”, summarise large outputs, and return structured data rather than raw dumps.
Making calls reliable#
Layer the defences from cheapest to most expensive:
| Layer | What it catches | Cost |
|---|---|---|
Envelope constraint (vLLM --tool-strict-level function, 18.i) | Malformed markup, unknown function names | Grammar work only inside the call region |
Strict arguments (strict: true per tool, or --tool-strict-level parameter) | Arguments that do not match the schema | More grammar states; schemas must be in the supported subset |
| Validation in the agent loop | Values that are valid but wrong (a path that does not exist) | Application code |
| Retry with the error message | Anything the model can fix when told | Another turn: decode tokens plus a cached prefill |
With a warm prefix cache, a retry costs roughly the decode tokens of the call (about 400 tokens in the example) plus a short prefill of the error message. Without the cache it costs a full prefill of the session, which is why cache hit rate and reliability interact.
The engine-side cost of constraint#
From 18.e, in one paragraph: vLLM compiles each schema to a grammar asynchronously, then each step fills a one-bit-per-vocabulary-token mask per constrained request on the engine-core CPU while the GPU runs the forward pass. At scale:
- Compilation is cached. vLLM v0.32.0 creates the xgrammar compiler with its cache enabled and
a size limit from
VLLM_XGRAMMAR_CACHE_MB(default 512). Repeated schemas compile once. A client that generates a slightly different schema per request (embedding a user ID in anenum, say) defeats the cache and pays compilation on every request. - Mask filling scales with constrained requests per step. A fleet where most traffic is JSON extraction can be engine-core-CPU-bound while the GPU has room. Watch engine-core CPU; scale out replicas, and prefer envelope-only constraint where full strictness is not needed.
- Structural tags only constrain the call region, so free text before and after the call costs nothing extra (18.i).
Parallel tool calls and turn count#
Many models can emit several independent calls in one turn (parallel_tool_calls, on by default in
vLLM’s chat API). Each saved turn is a saved round trip: one less prefill of the new tokens, one
less TTFT, and one less tool-wait gap for the scheduler to bridge (24.h). Agent frameworks that run
returned calls concurrently and return all results together make sessions shorter in wall time
and in turns.
Tool output is untrusted input#
Text returned by a tool (a web page, an email, an issue comment) can contain instructions aimed at the model (prompt injection). That is a security topic (23.i), but it has a serving consequence: platforms increasingly run classifiers or a second model over tool results, which adds inference calls per turn to the capacity plan.
6. Under the hood: the failure model#
// toolfail.go — small per-call failure rates become large per-session ones.
package main
import (
"fmt"
"math"
)
func main() {
// Per-call probability that a tool call cannot be used (bad markup, invalid
// arguments). ILLUSTRATIVE values: measure your own model and parser.
modes := []struct {
name string
p float64
}{
{"free text + parser", 0.02},
{"envelope constrained", 0.005},
{"strict arguments", 0.001},
}
turnsList := []int{10, 43, 100} // columns: P(session sees at least one failure)
const retryTokens = 400.0 // a retry re-decodes the call; the prefix is cached
fmt.Print("mode per call")
for _, t := range turnsList {
fmt.Printf(" %3d turns", t)
}
fmt.Println(" retry tokens per 1k sessions")
for _, m := range modes {
fmt.Printf("%-22s %7.1f%%", m.name, 100*m.p)
for _, t := range turnsList {
fmt.Printf(" %8.1f%%", 100*(1-math.Pow(1-m.p, float64(t))))
}
// expected retries per call with retry-until-success = p/(1-p)
retries := 43 * m.p / (1 - m.p) * 1000
fmt.Printf(" %28.0f\n", retries*retryTokens)
}
}Output:
mode per call 10 turns 43 turns 100 turns retry tokens per 1k sessions
free text + parser 2.0% 18.3% 58.1% 86.7% 351020
envelope constrained 0.5% 4.9% 19.4% 39.4% 86432
strict arguments 0.1% 1.0% 4.2% 9.5% 17217The formula is 1 − (1 − p)ⁿ: the probability that n independent calls all succeed, subtracted from one. Real failures are not independent (a model that misformats once often does it again in the same session), which makes retries less effective than the last column suggests.
7. Performance implications#
- Definitions are prefill, cached or not; deferred loading shrinks both the prompt and the KV per session.
- Strict mode moves cost from the GPU to the CPU. Fewer retries (GPU tokens saved) against per-step mask work (engine-core CPU spent). At high constrained concurrency, CPU wins the argument for a bigger host, not a bigger GPU.
- Unique schemas cost compilation latency on the request path; a request waiting for its grammar is skipped by the scheduler until it is ready (18.e).
- Parallel calls cut turns, and each turn avoided saves a TTFT and a tool-wait gap.
8. Production implications#
- Measure per-call failure by type: unparseable markup, unknown tool, schema-invalid arguments, semantically wrong arguments. Each has a different fix.
- Default to envelope constraint for agent traffic (
--tool-strict-level function), and turn on strict arguments for tools whose arguments are executed directly (SQL, shell, payments). - Budget tool tokens: track tokens of tool definitions and tool results per session; set limits on result size in the tool layer.
- Keep the tool list stable per session and sorted; version it so a change is a deliberate cache reset.
- Alert on engine-core CPU for replicas serving extraction-heavy tenants.
- Test parsers on every model or engine upgrade with a fixed set of recorded calls (18.i).
9. Common mistakes#
- Loading every tool from every MCP server into every request.
- Generating a fresh JSON schema per request, defeating the grammar cache.
- Treating “valid JSON” as “correct call” and skipping validation in the agent loop.
- Returning whole files or pages from tools when a summary or a page would do.
- Retrying without including the error message, so the model repeats the same mistake.
- Scaling GPUs for a JSON-heavy tenant whose bottleneck is engine-core CPU.
10. Hands-on exercise#
A. Count the tax. Take the tool definitions from three MCP servers you use. Render them with your model’s chat template and count tokens. What fraction of a typical first turn are they?
B. Failure rates. Record 1,000 tool calls from a small model on vLLM with --tool-strict-level
auto, function and parameter. Classify failures as in section 8. Compare with the table in
section 4.
C. Grammar cache. Send 200 requests with the same schema and 200 with a schema that embeds a random value. Compare p50 and p99 TTFT.
D. Parallel calls. Run an agent task with parallel_tool_calls on and off. Compare the number
of turns, total prefill tokens and wall time.
11. Interview questions#
- Why does a 1% malformed-call rate matter for agents but not for chat?
- Where does structured output cost CPU, and how would you notice it in production?
- How can tool definitions be loaded on demand without breaking prefix caching?
- What fails even with strict, schema-constrained tool calls?
- A tenant’s p99 TTFT jumped after they “added user context to the tool schema”. Why?
- How do parallel tool calls change serving cost?
12. Further reading#
Checked on 13 November 2026.
- Anthropic — Tool search tool and deferred loading
- Anthropic Engineering — Advanced tool use
- Model Context Protocol specification
- vLLM v0.32.0 — OpenAI-compatible server (tool calling options)
- vLLM structured output internals: 18.e. Tool calling and reasoning parsers: 18.i.