---
title: "Two Ways to Cut MCP Token Costs, and When to Use Each (2026)"
description: "More MCP servers means more tokens spent before your agent does any work. Here is how Tool Search and code execution with MCP each cut that cost, with code, and how to pick between them."
date: "2026-11-29"
tags: [ai, mcp, claude, agents, tokens, comparison]
sources:
  - title: "Code execution with MCP: Building more efficient agents (Anthropic, November 4, 2025)"
    url: "https://www.anthropic.com/engineering/code-execution-with-mcp"
  - title: "Tool search tool (Claude API docs)"
    url: "https://platform.claude.com/docs/en/agents-and-tools/tool-use/tool-search-tool"
  - title: "MCP connector (Claude API docs)"
    url: "https://platform.claude.com/docs/en/agents-and-tools/mcp-connector"
  - title: "Scale to many tools with tool search (Claude Agent SDK docs)"
    url: "https://code.claude.com/docs/en/agent-sdk/tool-search"
agent_task: "Look at the MCP servers and tools my agent loads, estimate how many tokens their definitions and results cost per request, and tell me whether I should turn on Tool Search, move to code execution with MCP, or both. Show me the config change for your recommendation."
---
So now we have all these MCPs. How do we stay efficient and keep our agents lean and sharp?

There are two answers from Anthropic right now: **Tool Search** and **code execution with MCP**. They overlap, they are not rivals, and they cost very different amounts of work. Here is what each one does, with code, and how to pick.

## Table of Contents
1. [The Problem](#the-problem)
2. [Tool Search](#tool-search)
3. [Code Execution with MCP](#code-execution)
4. [Which One Do You Need?](#which-one)
5. [What Is Next](#whats-next)

---

## Your Agent Is Reading the Manual. All of Them. {#the-problem}

MCP is an open standard that connects agents to external systems. It works well, so we keep adding servers. Then we act surprised when the context window fills up.

Anthropic [names two causes](https://www.anthropic.com/engineering/code-execution-with-mcp):

- **Tool definitions.** Many clients load every one of them up front.
- **Tool results.** Each one passes through the model, which often writes the same data out again for the next call.

To put a number on the first cause: a setup with GitHub, Slack, Sentry, Grafana and Splunk [can use about 55k tokens](https://platform.claude.com/docs/en/agents-and-tools/tool-use/tool-search-tool) on definitions. That is before any work happens.

---

## Tool Search: Stop Loading What You Do Not Use {#tool-search}

Tool Search keeps definitions out of context until Claude searches for them. By default, a search returns up to five tools. In the API, you add the search tool and mark the others as deferred:

```python
import anthropic

client = anthropic.Anthropic()

response = client.messages.create(
    model="claude-opus-5-5",
    max_tokens=2048,
    messages=[{"role": "user", "content": "What is the weather in San Francisco?"}],
    tools=[
        {"type": "tool_search_tool_regex_20251119", "name": "tool_search_tool_regex"},
        {
            "name": "get_weather",
            "description": "Get the weather at a specific location",
            "input_schema": {
                "type": "object",
                "properties": {"location": {"type": "string"}},
                "required": ["location"],
            },
            "defer_loading": True,
        },
    ],
)
```

Three details to know before the API returns a 400 and teaches you:

- At least one tool must not be deferred.
- Never defer the search tool itself.
- A deferred tool cannot have `cache_control`.

For MCP servers that use the API's MCP connector, set `defer_loading` on the `mcp_toolset` entry, not on each tool. Here is that setup for a whole MCP server, with the search tool included. The URL and token are placeholders:

```python
import anthropic

client = anthropic.Anthropic()

response = client.beta.messages.create(
    model="claude-opus-5-5",
    max_tokens=1000,
    messages=[{"role": "user", "content": "Use tools from mcp-server-2 to complete this task"}],
    mcp_servers=[
        {
            "type": "url",
            "url": "https://mcp.example2.com/sse",
            "name": "mcp-server-2",
            "authorization_token": "TOKEN2",
        }
    ],
    tools=[
        {"type": "tool_search_tool_regex_20251119", "name": "tool_search_tool_regex"},
        {
            "type": "mcp_toolset",
            "mcp_server_name": "mcp-server-2",
            "default_config": {"defer_loading": True},
        },
    ],
    betas=["mcp-client-2025-11-20"],
)
```

In the [Agent SDK](https://code.claude.com/docs/en/agent-sdk/tool-search), Tool Search is on by default as of November 2026, with some exceptions. You can also set a threshold. This turns Tool Search on when deferrable definitions reach 5% of the context window:

```typescript
import { query } from "@anthropic-ai/claude-agent-sdk";

for await (const message of query({
  prompt: "Find and run the appropriate database query",
  options: {
    mcpServers: {
      "enterprise-tools": { type: "http", url: "https://tools.example.com/mcp" },
    },
    allowedTools: ["mcp__enterprise-tools__*"], // pre-approves every tool on this server
    env: { ...process.env, ENABLE_TOOL_SEARCH: "auto:5" },
  },
})) {
  if (message.type === "result" && message.subtype === "success") {
    console.log(message.result);
  }
}
```

Tool names matter here, because search matches on names and descriptions. `search_slack_messages` will be found more often than `query_slack`. Your vague tool names now have consequences.

Anthropic [says it typically cuts definition tokens by over 85%](https://platform.claude.com/docs/en/agents-and-tools/tool-use/tool-search-tool). It also helps with choice: accuracy drops past 30 to 50 loaded tools. The price is an extra search step. With fewer than 10 tools, you probably do not need it.

---

## Code Execution: Let the Code Carry the Data {#code-execution}

Here the agent writes code that calls your tools. [Anthropic's post](https://www.anthropic.com/engineering/code-execution-with-mcp) (November 4, 2025) lays out MCP servers as TypeScript files:

```text
servers/
├── google-drive/
│   ├── getDocument.ts
│   └── index.ts
└── salesforce/
    ├── updateRecord.ts
    └── index.ts
```

Each file is a thin typed wrapper around one MCP tool:

```typescript
// servers/google-drive/getDocument.ts
import { callMCPTool } from "../../../client.js";

export interface GetDocumentInput {
  documentId: string;
}

export interface GetDocumentResponse {
  content: string;
}

export function getDocument(input: GetDocumentInput): Promise<GetDocumentResponse> {
  return callMCPTool<GetDocumentResponse>("google_drive__get_document", input);
}
```

The agent reads only the files it needs. Then it moves a transcript from Drive to Salesforce like this (adapted from the post):

```typescript
import * as gdrive from "./servers/google-drive";
import * as salesforce from "./servers/salesforce";

const { content: transcript } = await gdrive.getDocument({ documentId: "abc123" });

await salesforce.updateRecord({
  objectType: "SalesMeeting",
  recordId: "00Q5f000001abcXYZ",
  data: { Notes: transcript },
});
```

The transcript goes from one system to the other without a detour through the model. Filtering works the same way: code can read a 10,000-row sheet and show the model five rows.

```typescript
// Assumes a getSheet wrapper like getDocument above
import * as gdrive from "./servers/google-drive";

const allRows = await gdrive.getSheet({ sheetId: "abc123" });
const pendingOrders = allRows.filter((row) => row["Status"] === "pending");

console.log(`Found ${pendingOrders.length} pending orders`);
console.log(pendingOrders.slice(0, 5)); // the model sees only this sample
```

In the post's example, usage fell from about 150,000 tokens to about 2,000, or 98.7%. It is one example, not a promise.

There is one more part of the post that I like. When code works, the agent can save it as a reusable function. Add a `SKILL.md` file, and [it becomes a skill](https://www.anthropic.com/engineering/code-execution-with-mcp) that the agent can find and use again. So MCP servers are the raw tools, and skills are the tested recipes that use them. I have my own set of skills built this way. More on that another time.

Now the bill: you are running code that an agent wrote. You need a sandbox, resource limits and monitoring. "It worked on my machine" is not a security model.

---

## Which One Do You Need? {#which-one}

Start with Tool Search if definitions are your problem. It is the cheaper fix. Add code execution when large results or long tool chains are eating your context.

How the two compare:

| Question | Tool Search | Code execution with MCP |
|----------|-------------|-------------------------|
| Cuts tool definition tokens? | Yes | Yes |
| Cuts tool result tokens? | No | Yes, when code filters the data |
| Setup | Low | Higher: needs a secure sandbox |
| Main cost | Extra round trip per search | Operations and security work |
| Best fit | Many tools | Large data or multi-step chains |

This is not a cage match. The two solve overlapping problems at different levels, and the ideas can work together.

---

## What Is Next {#whats-next}

I’m going to run some tests this week and I will post a follow-up. Vendor numbers are interesting. I want to see what happens outside the example.

Have you tried either one? What did you expect, and what did you get?

---

## TL;DR {#tldr}

| Problem | Fix |
|---------|-----|
| Tool definitions fill the context window | Tool Search: defer definitions, load on demand |
| Big tool results pass through the model | Code execution: filter data in code, return only what is needed |
| Not sure which | Start with Tool Search. Add code execution for large data |

- Anthropic reports over 85% fewer definition tokens with Tool Search, typically.
- Its code execution example went from about 150,000 tokens to about 2,000. One example, not a promise.
- Code execution needs a real sandbox. Budget for it.
- Working code can become a skill: save it, add a `SKILL.md`, and reuse it.

---

**More posts:**
- [Do We Really Need All These Lines? How to Tell If Your AI Agent Writes Too Much Code (2026)](/blog/ai-agent-verbose-code-shrink-test-2026)
- [Overwhelmed Yet? How to Manage AI Agents When You're the Bottleneck (2026)](/blog/agent-overload-human-bottleneck-2026)
- [How to Track Token Usage in Claude and OpenAI (Python Tutorial, 2026)](/blog/track-token-usage-claude-openai-api)

---

*// hereshecodes.com*
