DEV Community

Zied Mnif
Zied Mnif

Posted on

The 20% of your AI agent's tool schemas that's pure cruft (and the one-liner to strip it)

Your AI agent re-sends every tool's JSON schema on every single turn. A surprising chunk of that — often ~20% — is non-semantic cruft: tokens that carry zero tool-selection signal, billed on every request.

I found this measuring the per-turn token cost of 13 real agents (full study). Here's where the waste hides, and the one-liner to remove it.

Where the cruft comes from

When you generate tool schemas from code, the converter quietly adds fields the model doesn't need:

  • Pydantic adds a "title" to every field via model_json_schema().
  • zod-to-json-schema appends "$schema" and "additionalProperties" to everything.
  • Pretty-printing alone swings a tool ~20%: the Fetch MCP tool is 236 tokens compact vs 288 pretty-printed — pure whitespace, re-sent every turn.

None of it helps the model pick the right tool. On a 20-tool agent that's easily hundreds of wasted tokens per turn, paid on every message.

The fix (Node / TypeScript)

const strip = (o) => Array.isArray(o) ? o.map(strip)
  : (o && typeof o === "object")
    ? Object.fromEntries(Object.entries(o)
        .filter(([k]) => !["$schema", "additionalProperties", "title"].includes(k))
        .map(([k, v]) => [k, strip(v)]))
    : o;

// compact, cruft-free — send this to the model
const tools_slim = JSON.stringify(strip(tools));
Enter fullscreen mode Exit fullscreen mode

The fix (Python)

import json

def strip(o):
    if isinstance(o, list):  return [strip(x) for x in o]
    if isinstance(o, dict):  return {k: strip(v) for k, v in o.items()
                                     if k not in ("$schema", "additionalProperties", "title")}
    return o

tools_slim = json.dumps(strip(tools), separators=(",", ":"))  # compact, cruft-free
Enter fullscreen mode Exit fullscreen mode

One caveat: additionalProperties:false is occasionally intentional (strict validation) — drop it from the strip list if you rely on it.

Measure before/after

Want to see exactly how much your schemas carry (and which tool is worst)? The free Agent Token Profiler flags this cruft automatically and shows the per-turn cost across models — paste your tools, no signup, no key.


From AgentLoop, a readable MIT Claude-agent starter. Production token-metering + multi-provider routing live in AgentLoop Pro.

Top comments (0)