When you give an AI agent a tool, the quickest way to write it is to return whatever the underlying function gives you. A package lookup returns the whole metadata record. An API wrapper returns the whole JSON response. The model reads all of it, and you pay for every token, usually more than once.
A few words first
A token is a chunk of text, roughly three quarters of an English word, and APIs bill per million of them. Input is everything you send to the model, and it includes tool results. An agent is a loop where the model calls tools and reads their results until the task is done. A tool result is the text your code returns from a tool. It is added to the conversation, and because the API has no memory, it is sent again with every later step. Package metadata is the record Python keeps about each installed package: name, version, dependencies, license, classifiers, and the full README, stored as the "long description".
The experiment
The tool is package_info(name). A coding agent typically calls it to answer one question: which version of this package is installed, and what does it depend on? Five fields answer that: name, version, one-line summary, required Python version and the dependency list.
I wrote the tool two ways and ran both on every package installed on one of my Linux servers. That's 74 packages after skipping 168 type-stub packages, which are near-identical boilerplate. I counted tokens with tiktoken, OpenAI's tokenizer library, using the o200k_base encoding.
| What the tool returns | Tokens, all 74 packages | Change |
|---|---|---|
| Every field, pretty-printed JSON | 152,031 | baseline |
| Every field, compact JSON | 145,124 | −4.5% |
| Every field except the README | 31,996 | −79% |
| The five fields the agent needs | 8,803 | −94% |
Two things stand out. First, formatting is not where the weight is. Squeezing the whitespace out of the JSON saved 4.5%, because a run of spaces is usually a single token. Second, one field is most of the bill: the README alone is 72% of all tokens.
Per package, the median full result was about 1,110 tokens and the median trimmed one was 62, so the full version is 13.7 times bigger in the middle case. The spread is wide. For one package in ten it's under 3.9x, and for one in ten it's over 83x. requests went from 1,371 tokens to 124, numpy from 1,720 to 38. The worst case was the cloudflare package: 17,136 tokens, because its README contains a full usage guide, against 53 for the five fields. Even the nine packages with no README at all were 4.4 times heavier than needed, thanks to classifiers, project URLs and license fields.
What it costs
An agent rarely reads a tool result once. Say it looks up four packages early in a task, and each result stays in the history for 12 later steps. That's 13 reads per result, an assumption, but a modest one for a coding agent. With median-sized packages the task carries 57,746 input tokens of metadata with the full tool and 3,250 with the trimmed one.
These are list prices from the catalog snapshot of the LLM gateway I work on. On gpt-5.6, input is $2 per million tokens: $0.115 per task against $0.0065. At 100 tasks a day that's $346 a month against $19.50. On claude-opus-5.5 ($4 per million) it's $693 against $39. On claude-haiku-5.5 ($0.10 per million) it's $17.32 against $0.97, which few people would notice. A single cloudflare lookup read once on gpt-5.6 costs $0.034, versus about a hundredth of a cent for the fields that answer the question.
There is a reliability side too. The context window, the most text a model can take in one request, is finite. A few 17,000-token results fill it faster than anything the agent actually writes, and the version number it needed sits somewhere among them.
The fix: pick the fields inside the tool
Decide what question the tool answers and return only what answers it:
import importlib.metadata as md, json FIELDS = {"name": "Name", "version": "Version", "summary": "Summary", "requires_python": "Requires-Python"} def package_info(name): meta = md.metadata(name) out = {key: meta.get(field) for key, field in FIELDS.items()} out["requires_dist"] = meta.get_all("Requires-Dist") or [] return json.dumps(out, separators=(",", ":")) print(package_info("requests")) If the agent sometimes needs the README, give it a separate package_readme(name) tool with a length limit, so the expensive text is read only when asked for. The same applies to any wrapper around an API: an issue tracker, a payments API, a database row. Pick the fields and drop the empty ones before the model sees them.
What this doesn't solve
You have to guess the fields. If the agent needs the license and you dropped it, it will either answer wrong or make another call, and an extra agent step can cost more than the tokens you saved on a small result. Watch your logs for that before trimming further.
It doesn't stop the re-sending. History still grows with every step. Trimming or summarizing old tool results is a separate fix, and often the bigger one.
I didn't measure answer quality, only size. The numbers come from one server's packages, which lean towards system tooling, and from one OpenAI tokenizer. Anthropic and Google cut text differently. The four-lookups, thirteen-reads task is an assumption, and prompt caching, which bills repeated history at a discount on some APIs, would shrink the dollar figures. It wouldn't shrink the share of the context window that metadata takes up.
