Expand description
v0.6.4-005 — Static schema-size table.
Computes the per-tool BPE token cost of every MCP tool registered by
crate::mcp::tool_definitions, using the tiktoken-rs cl100k_base
tokenizer (the same BPE Claude/GPT use for context-window accounting,
and the same one v0.6.3.1 P6/R1 already wires for budget_tokens).
The table is computed lazily on first access and cached behind a
OnceLock. The cost of the first call is one full pass over every
tool schema (~7 ms on Apple M2) followed by cache hits forever after.
§Why lazy and not literally compile-time
The “build-time” framing in the v0.6.4 issue spec referred to the
desire that operators be able to query the table without running
the full MCP register_tools() dance — the runtime cache satisfies
that constraint. A real build-time approach would need either a
proc-macro or a build.rs that re-parsed the JSON-emitting Rust
source, both of which trade simplicity for marginal warm-cache
performance that nobody is paying for here. The lazy approach also
keeps the BPE table out of cargo bench cold paths — every place
that doesn’t run doctor --tokens pays exactly nothing.
§CI gate
tool_sizes_under_ci_gate() returns the largest single tool cost.
The unit test no_tool_exceeds_1500_tokens enforces the v0.6.4-005
acceptance gate that no individual tool definition exceeds 1500
tokens. The number is high enough to permit growth on the more
schema-heavy KG/governance tools and low enough that doubling a
tool’s schema by accident lands in CI red.
Structs§
- Tool
Size - Single-tool cost report. The
totalis what counts against the per-request prefix; thename_tokensandschema_tokenssplit is useful for the doctor’s diagnostic output.
Functions§
- full_
profile_ total_ tokens - Sum of every tool’s
total_tokens(verbose schema) — the worst-case prefix cost on averbose=trueopt-in harness with--profile full. The actually-paid cost on the default code path is reported bytrimmed_full_profile_total_tokens. - tool_
size - Lookup a single tool by name in the verbose table.
O(n)butn ≤ 57(v0.7.0 L1-5 added 5 skill tools). - tool_
sizes - Runtime-computed table of every tool’s tokenized schema cost
at the verbose ceiling — every optional param, every default,
every per-property description. This is the upper bound a host
can ever pay (only reachable via
memory_capabilities { verbose=true, family=…, include_schema=true }since v0.7 C4). - tool_
sizes_ under_ ci_ gate - Highest-cost tool in the verbose table. Used by the CI gate.
- trimmed_
full_ profile_ total_ tokens - v0.7 C4 — sum of every tool’s
total_tokensafter the C4 trim (optionals hidden). This is the baretools/listpayload cost under--profile full. - trimmed_
tool_ sizes - v0.7 C4 + #859 — runtime-computed table of every tool’s tokenized
schema cost as actually shipped on
tools/list. Per-propertydescriptionprose is stripped, the top-level tooldescriptionis compacted to the first sentence, thedocsfield is dropped, but every property entry survives so MCP clients can discover the call surface (per [crate::mcp::trim_optional_params] + [crate::mcp::strip_docs_from_tools] +wire_compact_descriptions). This is what an MCP host pays per request on the default code path.