← all articles

Paying for web search inside agent loops

Two meters running at once

When a model answers from its own training, you pay for tokens. When it searches the web first, you usually pay for two things: the search itself, billed per call or per grounded request, and the tokens that come from putting the search results into the model’s context. Put that inside an agent loop that searches several times per task, and both meters run faster than people expect.

Search and grounding tools have become standard on most major model APIs. Some providers have a built-in web search tool the model can call during a response. Some sell grounding, where a request is enriched with search results before the model answers. And plenty of teams wire in a third-party search API as a custom tool. Each of these is billed differently, but nearly all of them add a per-use fee on top of token costs.

This piece is about how that fee stacks up and how to cap it. I won’t quote specific prices, because they vary by provider and change often enough that any figure here would be stale soon. Check the pricing page for whatever tool you’re using. What stays stable is the structure, and the structure is what decides whether search is a small line item or the largest part of your bill.

The short version: an agent that searches freely can spend more on search and search-result tokens than on its own reasoning, and the fixes are mostly about limiting how often it searches and how much of each result it reads.

How search gets billed

There are three common billing shapes, and it’s worth knowing which one you’re on.

Per search call is the most common for built-in tools. Each time the model invokes the search tool, you’re charged a fee, often quoted per thousand searches. A single response in which the model searches three times costs three search fees.

Per grounded request is another shape. The request is charged a fee if grounding was used, regardless of how many underlying queries the provider ran. That can be more predictable, and it can also be more expensive for simple requests that didn’t need much searching.

Third-party search APIs have their own pricing, usually per query, sometimes with a monthly plan and an allowance. When you call them as a custom tool, you pay the search vendor directly and the model provider for tokens.

On top of any of these, you pay tokens for the results. Search results come back as text, with titles, snippets, sometimes full page content, and URLs. That text is inserted into the model’s context as input. Depending on the tool, a single search can add a few hundred tokens or several thousand.

Some providers also charge for fetching a page’s full content separately from searching. If your agent searches, then opens three pages to read them, that may be one search fee plus three fetch fees plus the tokens for all of it.

Read the pricing page for the exact rules. Look specifically for what counts as one billable search, and whether results tokens are billed at the normal input rate.

Why a loop multiplies it

A single search is cheap. The trouble starts when searching happens inside a loop.

An agent works in steps. At each step, the model looks at the task and everything that’s happened so far, decides what to do, does it, and adds the result to its context. If one of its tools is search, it might search at step one, read the results, decide it needs more, search again at step two, open a page at step three, and so on.

Each search adds a fee. That part is linear: ten searches cost ten fees.

The token side is worse, because each search result stays in the context for every step after it. The results from step one are resent as input at step two, step three and every step until the agent finishes. If each search adds a few thousand tokens and the agent runs ten steps, the early results get billed many times over.

So total input tokens grow roughly with the square of the number of steps when each step adds material, which is the same dynamic that makes long chat sessions expensive. Search makes the material added at each step larger.

Put the two together and an agent that searches at every step can end up with a search bill and a results-token bill that both dwarf the cost of the model’s own reasoning. That’s the pattern to watch for.

A worked example with letters

Here’s the arithmetic without made-up prices, so you can plug in your own.

Say an agent runs N steps. At each step it makes one search, which costs a fee F. Each search adds R tokens of results to the context. The task itself, plus system prompt and instructions, is B tokens. The model’s own output per step is O tokens.

Search fees come to N times F. That’s the easy part.

Input tokens at step k are roughly B plus k times (R plus O), because every earlier result and every earlier output is still in the context. Summed over all steps, input is about N times B plus (R plus O) times N times (N plus 1) over 2.

Output tokens are N times O.

Multiply input by the input price and output by the output price, add the search fees, and you have the cost of one task.

Now look at what happens when you change things. Double N, and search fees double while the results-token term roughly quadruples. Halve R, by trimming each result to what matters, and that quadratic term halves. Search once at the start instead of at every step, and R appears once instead of N times.

That last change is often the biggest lever. An agent that gathers information once and then reasons over it costs far less than one that searches, reasons a little, searches again, and repeats.

Sub-agents multiply it again

Many agent designs now split work across several agents: an orchestrator that plans, and sub-agents that each handle part of the task. That structure can be good for quality. It’s also a multiplier on search costs that’s easy to miss.

If an orchestrator hands five sub-agents a research question each, and every sub-agent has search enabled with its own budget, the task’s total searches are five times whatever one sub-agent does. Each sub-agent also builds its own context from its own results, so the quadratic token growth happens in five places at once.

Sub-agents often overlap, too. Two of them researching related questions may run nearly identical searches and read the same pages. Without a shared cache, you pay for both.

Then the results flow back up. Each sub-agent returns a summary to the orchestrator, which adds those summaries to its own context. If the summaries are long, or if sub-agents return raw results instead of summaries, the orchestrator’s context balloons.

A few controls help. Give the whole task one search budget and divide it among sub-agents, rather than giving each its own full budget. Share a search cache across sub-agents within a task. Ask sub-agents to return short, structured findings rather than everything they read. And consider whether each sub-agent really needs search, or whether the orchestrator can run searches once and hand the relevant results down.

Before choosing a multi-agent design for a search-heavy task, estimate the cost with the arithmetic above, multiplied by the number of sub-agents.

Where the search tokens actually come from

It’s worth knowing what’s in a search result, because that’s where you can cut.

A typical result set includes several hits, each with a title, a URL, a snippet of text, and sometimes metadata like a date or source name. Some tools return only snippets. Others return large extracts of each page. Some let you choose.

If the agent then fetches a page to read it fully, that page’s content goes into the context too. Web pages are long and full of material the agent doesn’t need: navigation, cookie notices, related links, comments. Unless the fetch tool strips that away, you pay for all of it.

Citations can add tokens as well. Some tools return structured citation data alongside the text, which is useful for attribution and adds to the input on subsequent steps.

Then there’s the model’s own reaction to results. A model that has just read a long set of search results often writes a longer response, summarising or quoting what it found. That’s output, which is usually priced higher than input.

The practical upshot is that search cost depends on how much text each search injects and how long that text stays around, as well as on the number of searches. Controlling both is where the savings are.

Capping the number of searches

The most direct control is limiting how many times the agent can search.

Some built-in search tools accept a parameter for the maximum number of uses per request. Anthropic’s web search tool, for example, has a max uses setting. Other providers have similar controls or let you disable search per request. Where that’s available, set it. A low cap for simple questions and a higher one for research tasks is better than leaving it unlimited.

If you’re running your own loop, enforce a budget in code. Count searches per task and stop offering the search tool once the budget is spent. The model will have to answer with what it has, which for most tasks is fine.

You can also shape behaviour through the prompt. Tell the model how many searches it has and to plan them. Tell it to search once with a well-formed query rather than several times with vague ones. Tell it to stop searching once it has enough to answer. These instructions don’t guarantee anything, but they shift the average.

Another option is to separate the searching from the reasoning. A first step decides what queries are needed, runs them all at once, and gathers the results. A second step, possibly with a different model, reasons over the gathered material without the ability to search again. That caps searches by design and keeps results from accumulating across many steps.

Trimming what each search brings back

The second control is reducing how much text each search injects.

If the tool lets you choose between snippets and full content, prefer snippets for most tasks. Snippets are usually enough to decide which pages matter. Fetch full content only for the few pages that actually need reading.

If you control the tool, as with a third-party search API wrapped in your own function, you can post-process results before the model sees them. Keep the title, URL and the most relevant part of the snippet. Drop duplicate results, ads and results from sources you don’t trust. Limit the number of results per query.

For page fetches, extract the main content and strip boilerplate before returning it. Truncate to a sensible length. If the page is very long, return the section most relevant to the query rather than the whole thing.

You can also summarise before inserting. A small, cheap model can condense a long page into a short summary, which the main model then reads. That adds a call, but on long pages it can save far more tokens than it costs, especially when the summary stays in context for many subsequent steps.

And consider dropping old results from context. Once the agent has used a search result to answer part of the task, it may not need the full text anymore. Replacing it with a short note of what was found keeps the context smaller for the remaining steps.

Caching searches you’ve already run

Agents often repeat themselves, and so do users.

Within a single task, an agent may search for nearly the same thing twice, especially if its context has grown long and it has lost track of what it already found. Across tasks, different users often ask about the same topics, so the same queries recur.

A simple cache in front of your search tool catches both. Key it on the normalised query, lowercased and trimmed, and return stored results if the same query ran recently. The right freshness window depends on the content. For news or prices, a few minutes. For documentation or reference material, much longer.

This only works for search tools you call yourself. Built-in provider search tools usually run on the provider’s side, so you can’t insert a cache in the middle. That’s one reason some teams prefer wrapping a third-party search API: it gives them control over caching, trimming and budgets that built-in tools don’t expose.

Prompt caching on the model side helps too, in a different way. If the early part of the agent’s context stays the same across steps, including the system prompt and early search results, providers that support prompt caching may bill those repeated tokens at a discount. Structuring the context so stable material sits at the front, and new results are appended at the end, gives the cache the best chance to hit.

Neither kind of caching fixes an agent that searches too much. Both reduce the cost of the searching it does.

Retries and rate limits on the search side

Search services have their own rate limits and their own failures, and handling them carelessly adds cost.

A built-in search tool may fail or return nothing when the provider’s search backend is busy. A third-party search API may return rate-limit errors if your agents send many queries at once. Page fetches fail often, since target sites time out, block automated requests, or return errors.

The naive response is to retry. That can be right for a transient error, and it can double or triple your search fees if the agent retries every failure several times. Worse, a model that gets an empty or failed search result may simply search again with a slightly different query, which counts as a new billable search.

Decide explicitly how failures should be handled. Retry a small number of times with backoff for errors that look transient. Don’t retry fetches for pages that returned a block or a not-found error. Have the tool’s response tell the model the search failed and how many searches remain, so it can adjust its plan instead of hammering the tool.

Check whether failed searches are billed. Some services don’t charge for errors; others charge per request regardless. That affects how aggressive your retry policy can be.

If many agents run concurrently, put a rate limiter in front of your search tool. It’s better to queue searches briefly than to hit the vendor’s limit, trigger a wave of errors, and pay for the retries.

Choosing between built-in search and your own tool

Built-in search tools and self-wired search APIs each have trade-offs, and cost is only part of it.

Built-in tools are convenient. You enable them with a flag, and the provider handles querying, result formatting and often citation tracking. The model may be tuned to use them well. For a quick integration, that’s hard to beat.

What you give up is control. You usually can’t cache, can’t post-process results before the model sees them, can’t filter sources beyond what the tool’s parameters allow, and can’t swap the search backend. Pricing is whatever the provider sets. Visibility into what was searched and returned varies.

Your own tool, wrapping a third-party search API, gives you all of that control. You can cache and trim results, filter sources, set budgets and log exactly what happened. You can choose a search vendor on price or quality, and switch later. You can combine web search with your own internal search behind the same tool.

The cost is building and maintaining it: the wrapper, result parsing, page fetching and cleanup, and error handling for the search vendor’s API. And you’ll need to handle citations yourself if you want them.

For a prototype, the built-in tool is usually the right start. Once search is a meaningful part of your bill, or you need control over what the model reads, your own tool starts paying for itself.

When the agent doesn’t need to search at all

The cheapest search is the one that doesn’t happen, and a lot of agent searches are unnecessary.

Models often search for things they already know well: definitions of common terms, general explanations of stable topics, widely known facts. If the task doesn’t depend on recent information, the search adds cost and latency without improving the answer. It can even make answers worse, if the model defers to a low-quality search result over its own better knowledge.

Other searches duplicate information you already have. If your application has its own documentation, product data or knowledge base, the agent should look there first. Internal retrieval is usually cheaper and more reliable than web search for anything about your own domain.

Some searches are driven by the prompt. If the system prompt says “always verify facts by searching,” the model will search for everything, including things that need no verification. A more targeted instruction, such as searching only for information that may have changed recently or that you can’t answer confidently, cuts that.

A useful exercise is to log a week of agent searches and read them. For each one, ask whether it was needed. You’ll usually find patterns: repeated queries for the same background facts, searches for things in your own docs, searches triggered by overly cautious instructions. Each pattern suggests a fix.

Setting a per-task budget

Beyond caps on individual tools, it’s worth putting a budget on each task as a whole.

A per-task budget counts everything: search fees, input tokens, output tokens. When the task’s running total crosses the limit, the agent is told to finish with what it has, or it’s stopped and the task is flagged. This protects you from the rare task that spirals, such as an agent stuck in a loop of search, read, search again, never satisfied.

Implementing it requires tracking usage per step. Most APIs return token counts in each response, and some report tool uses. Add them up as the task runs. For search fees, count calls and multiply by the rate.

Set the budget based on what a normal task costs. Run a sample of typical tasks, look at the distribution of cost per task, and set the limit well above the typical case but far below a disaster. Tasks that hit the limit are worth reviewing: they’re either genuinely hard, which may justify a higher limit for that task type, or they’re failures of the agent’s strategy.

At the account level, set spending limits and alerts with your model provider and your search vendor. A per-task budget stops one runaway task. An account limit stops a bug that creates thousands of runaway tasks.

Knowing which feature is spending it

Once search is a noticeable part of your bill, you’ll want to know where it’s going.

The invoice from a model provider or a search vendor usually shows totals. It doesn’t tell you which feature, which customer or which type of task drove them. For that, you need your own records.

Tag every search with the context it ran in: the task ID, the feature, the user or customer, and the step number. Log the query, the result size in tokens, whether the result was used, and the fee. If you’re using a built-in tool, most APIs report tool usage in their responses, so you can capture it there.

With that in place, a few questions become easy to answer. Which features search the most? Which customers? Which task types have the highest search cost per task? Are there queries that recur constantly and would benefit from caching? Are searches from one feature mostly unused?

This also helps with pricing decisions. If you sell a product with an agent inside, search-heavy customers cost you more than light ones. Knowing the spread lets you set plan limits, or price the heavy use, from data rather than guesswork.

It doesn’t need a sophisticated system. A row per search in a table, and a weekly query that groups by feature and customer, will tell you most of what you need.

Passing the cost on to your own customers

If your product includes an agent that searches on a user’s behalf, search fees become part of your unit economics, and they deserve a place in how you price.

A flat subscription with unlimited agent use means your margin depends on how much each user searches. Light users are profitable; heavy users may not be. That’s the same bet consumer chat plans make, and it works only if you understand the distribution and cap the extremes.

Usage limits are the common answer. A plan might include a certain number of agent tasks or research requests per month, with higher tiers for more. Some products count searches directly; others count tasks and rely on per-task budgets to keep each one bounded. Tasks are easier for users to understand. Searches track your costs more closely.

Free tiers need particular care. An agent with web search, available free, is an attractive thing to abuse, whether for scraping, for bulk research, or just for heavy personal use. Low per-user limits, account verification and rate limiting keep a free tier from becoming your largest cost.

Whatever you choose, make the per-task budget the foundation. It turns an open-ended cost into a bounded one, which is the only way to price anything sensibly.

And revisit it. Search pricing changes, your agent’s behaviour changes as you tune it, and users find new ways to use the feature.

Latency is part of the bill

Search costs time as well as money, and in an agent the two compound.

Each search is a network round trip to the search service, plus time for the model to read and process the results. Each page fetch is another round trip, often slower, since it depends on the target site. In a loop with several searches and fetches per task, those delays stack up.

That matters for user-facing agents. A user waiting for an answer notices the difference between a few seconds and a minute. It also matters for background agents, because longer tasks hold resources longer and, in some billing arrangements, cost more in compute or orchestration.

The same controls that cut cost tend to cut latency. Fewer searches mean fewer round trips. Running several searches in parallel, rather than one after another, reduces wall-clock time even if the count stays the same. Snippets instead of full pages mean less to fetch and less to read. Caching means some searches return instantly.

There’s one trade-off to watch. Separating a search phase from a reasoning phase caps cost well, but it can mean the agent gathers information it doesn’t need, because it has to guess upfront what will be relevant. For some tasks that’s fine. For open-ended research, a limited amount of iterative searching may produce better answers at a reasonable cost.

What searching tells you about answer quality

Cost aside, it’s worth thinking about whether the searches are making answers better.

Search helps most when the answer depends on recent information, on niche facts the model might not know, or on specific sources the user needs cited. In those cases, grounding is the point, and paying for it is worthwhile.

Search helps less, or hurts, when results are low quality. Web search returns whatever ranks, including outdated pages, content farms and pages that contradict each other. A model that searches and then trusts the first result may give a worse answer than it would have from its own knowledge. Filtering sources, where the tool allows, and asking the model to compare a few independent pages before trusting one, both help.

Citations make answers checkable but don’t guarantee correctness. A cited answer can still misrepresent the source. If your use case depends on accuracy, spot-check citations against the pages they point to.

The combination of cost and quality suggests a policy. Search when the task needs current or specific information. Don’t search for general knowledge the model handles well. Limit how many searches a task can make and how much each one brings back. And review what the agent searched for regularly, because that log tells you about both your bill and your answer quality.

What to do this week

If you have an agent with search enabled, here’s a sequence that gets the costs under control.

Find out exactly how your search is billed: per call, per grounded request, or through a third-party API. Note whether page fetches are separate and whether results tokens are billed at the normal input rate.

Log a week of searches per task, with the query, the result size and whether the search was needed. Calculate average searches per task and the share of input tokens that come from search results.

Set a cap on searches per task, using the tool’s max-uses parameter where it exists, or a counter in your own loop.

Trim results: snippets by default, full pages only when needed, boilerplate stripped, old results dropped or summarised.

Add a per-task budget covering search fees and tokens, and account-level spending limits with every vendor involved.

Then compare the next week’s costs and answer quality with the first. The searches you cut should be the ones that weren’t helping.

We track model APIs and the search and grounding tools attached to them, including how each one bills per call and per token, over at AI Tool Gazette.

for builders
Running agents or scrapers at scale?

AI pipelines that crawl, research, or automate the web hit rate limits and geo-blocks fast. Singapore Mobile Proxy runs real 4G/5G mobile IPs that carriers still trust.

see plans →
read on
More from the Gazette

Tool reviews, model and pricing news, and build guides for people shipping real things with AI.

browse all articles →