Skip to main content

Module sizes

Module sizes 

Source
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§

ToolSize
Single-tool cost report. The total is what counts against the per-request prefix; the name_tokens and schema_tokens split 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 a verbose=true opt-in harness with --profile full. The actually-paid cost on the default code path is reported by trimmed_full_profile_total_tokens.
tool_size
Lookup a single tool by name in the verbose table. O(n) but n ≤ 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_tokens after the C4 trim (optionals hidden). This is the bare tools/list payload 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-property description prose is stripped, the top-level tool description is compacted to the first sentence, the docs field 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.