Skip to content
← Notebook

Automation & Agents

Code execution with MCP: how to lower an AI agent's cost per query

If your AI agent loads all its tools on every query, you're paying for text it doesn't use. What code execution with MCP is, what savings Anthropic reports, what the catches are and what to ask your provider.

Date
Nov 10, 2026
Reading time
6 min
Result
No measurement of our own yet • Anthropic: 150,000 → 2,000 tokens (−98.7%) in its example • Tool Search: −85%

Translated from the Spanish original. The code, data and READMEs on GitHub are in Spanish.

An AI agent that loads all its tools and all its intermediate data on every query pays for text it doesn’t need. The code execution technique with MCP avoids that: the agent writes a small program, the program works with the data outside the model and only the result reaches the model. Anthropic reports savings of between 37% and 98.7% in its examples. We haven’t measured it on a small-business case yet: here we explain what it is, where those figures come from and when it’s worth asking your provider for it.

An earlier version of this page said “90% token savings” and “100% accurate”. There was no code or measurement of ours to back it up, so we’ve removed it.

Definitions in one sentence

  • AI agent: a program that does tasks for you following instructions, for example reading an email, looking up the customer in the accounting software and preparing a reply.
  • Token: the chunk of text AI providers charge for. More text sent to the model means more money and more waiting.
  • MCP (Model Context Protocol): an open standard for connecting AI applications to external systems (files, databases, tools). Its official documentation compares it to a USB-C port for AI.
  • Code execution: instead of the model calling each tool and reading each full response, the model writes code that runs in an isolated environment (sandbox) and returns only what’s needed.

The problem, with a small-business example

Imagine an accounting firm with an agent connected to email, the accounting software and the shared client folder. The question comes in: “which invoices does Construcciones López have outstanding this quarter?”.

With the usual setup, two things happen that you pay for:

  1. Tool definitions. Before it starts, the agent receives the description of every tool it might use: sending emails, searching attachments, creating journal entries, listing folders… Even if this query only needs one.
  2. Intermediate results. If the agent asks for the list of invoices, the whole list passes through the model, even if only five lines matter. If it then has to copy that data somewhere else, it passes through again.

This example is illustrative: we haven’t measured how many tokens a real accounting firm uses. What is published is the order of magnitude. In its November 2025 article on advanced tool use, Anthropic describes a setup with five MCP servers (GitHub, Slack, Sentry, Grafana and Splunk) whose 58 tools take up about 55,000 tokens before the conversation even starts.

What changes with code execution

The agent no longer receives every tool and every piece of data:

  1. It looks for the tool it needs, and only that one is loaded.
  2. It writes a short program that requests the data, filters what matters and does the calculations.
  3. The program runs in an isolated environment; the intermediate data stays there.
  4. The model receives the final answer, something like “three outstanding invoices, the oldest from August”, instead of the full list.

There’s a privacy benefit that matters in Spain because of the GDPR: intermediate data doesn’t have to pass through the model. According to Anthropic, the environment can even replace personal data with placeholders before it reaches the model. That doesn’t remove your data protection obligations, but it reduces what leaves your system. If this concerns you, see also our experiment on anonymising personal data before sending it to ChatGPT.

Where the figures come from

They’re all Anthropic’s, from its own examples, and the main article doesn’t detail how they were measured. Use them to get an idea, not as a promise for your case.

Source (Anthropic) Technique Before After Savings
Code execution with MCP (4/11/2025) The agent writes code; moving a Google Drive transcript to Salesforce 150,000 tokens 2,000 tokens 98.7%
Advanced tool use (24/11/2025) Tool Search: load only the tools being searched for ~77,000 tokens ~8,700 tokens 85%
Advanced tool use (24/11/2025) Programmatic tool calling on complex research tasks 43,588 tokens on average 27,297 tokens 37%

The range is wide because the savings depend on how much useless text the agent was moving before. An agent with two tools and short responses will save little. One connected to many systems that moves long documents can save a lot.

The catch: it isn’t free to set up

Anthropic says so in the same article: running code written by the agent requires a secure, well-isolated execution environment, and that adds infrastructure work and security risks that have to be weighed against the savings. In practice, for a small business that means:

  • Someone has to maintain the isolated environment. If the provider hasn’t already solved it, it’s a new cost.
  • More pieces that can fail. The code the agent writes may have bugs; you need to log what it runs.
  • It doesn’t pay off for small agents. If your agent only checks a calendar, the savings won’t cover the setup.

When to ask your provider for it

It’s worth asking if two or more of these conditions apply:

  • The agent is connected to many tools (email, ERP, CRM, folders, calendar).
  • It works with long documents or listings: transcripts, bank statements, reports, catalogues.
  • The API bill is growing faster than the number of queries.
  • Responses take longer than is reasonable.

If you want to understand the underlying problem, in context engineering: the two kinds of context bloat we explain why a full context not only costs more but makes the agent make more mistakes. And if your agent works in Spanish, bear in mind that Spanish uses between 12.5% and 33.8% more tokens than English: every word you save is worth a bit more.

What you can do on Monday

  1. Ask your provider for the consumption per query: how many tokens go in and out on a typical query, and what share is tool definitions.
  2. List the tools connected to your agent and mark the ones actually used. Disconnecting the unused ones is the cheapest saving.
  3. Ask whether intermediate data passes through the model or is filtered beforehand. If it passes through, ask whether they can apply code execution or on-demand tool loading (Tool Search).
  4. Ask for a test with your real queries before and after, with the same model, and compare tokens, time and accuracy.

If you’re choosing an agent, AI agents for business: use cases and AI agent harnesses: OpenClaw, Hermes Agent and Claude Code compared cover the rest of the decision.

Sources

Checked on 10 November 2026.

Want to know how much your agent would save before paying for the change? We measure it with your real queries: commission the test.

Discuss the experiment on X →

#MCP (Model Context Protocol)#Python#Claude Code