Guide · Intermediate · 10 min
Reduce AI agent costs: model selection, caching, context, routing, and budgets
Reduce AI agent costs by measuring successful tasks, then adjusting models, caching, context, routing, and spending limits while maintaining verified quality.

To reduce an AI agent’s cost, first measure the cost of a successfully completed task. Then eliminate unnecessary calls, reduce the context you send, and cut down on retries before switching models. Caching, routing, and spending caps support this approach, provided you maintain a verified level of quality.
Measure successful tasks, not just tokens
The price of a call accounts for only part of the cost. An agent may retrieve documents, query paid services, generate an answer, check its work, and then try again. A cheap response that requires extensive human correction can make the overall process more expensive.
Choose a business unit of work: a processed and accepted case, an approved change, or a usable data extraction. Define acceptance criteria before comparing configurations. Otherwise, an apparent saving may simply reflect a check you have removed.
The full cost includes model usage, tools, hosting, storage, observability, and human intervention. Separate variable expenses from fixed costs. Infrastructure you have already paid for is not free, but its marginal cost differs from its total cost.
For each run, record:
- The task identifier and whether the result was accepted or rejected.
- The model and settings used.
- Input, cached input, and output volumes.
- Tool calls, retries, and errors.
- Elapsed time and any human corrections required.
A token is a unit a model processes, rather than a universal billable task. The guide to understanding LLM API pricing explains these units in detail. Here, the goal is to connect them to the result you actually obtain.
Reconstruct the bill without double-counting
For each call, distinguish the categories that are actually billed: standard input, cache reads, any cache writes, and output. Add charges for tools and associated services. Check whether the provider’s usage counters include cached tokens within total input or report them as a separate category.
The working formula is straightforward: multiply each billable volume by its applicable rate, add those amounts together, and then add other expenses. Apply this calculation to individual calls when models or rates change during a task. Do not multiply all usage by an arbitrarily chosen average rate.
Illustrative arithmetic example, not a price quote. Suppose a task consumes a total of 100,000 standard input tokens and 10,000 output tokens. The rates below come from the site’s index and exclude caching, tools, and other charges.
| Model | Input per million tokens | Output per million tokens | Calculated example cost |
|---|---|---|---|
| Claude Haiku 5.5 | $0.10 | $0.50 | $0.015 |
| Claude Sonnet 5.5 | $2 | $10 | $0.30 |
Rates: recorded by the site in October 2026, available in the linked model profiles. These calculations assume identical usage volumes. They do not establish equivalent quality or an equal number of steps.
To build your own estimate, use the LLM price calculator, then reconcile its result with the usage you are actually billed for. The article How much does an AI agent really cost per month? extends this approach to the overall budget.
Choose a model using the tasks that matter
A model with a lower price per token is useful only if its behavior remains suitable for the job. Compare how well it follows instructions, produces the expected data, selects tools, and stops at the right point. A silent error can cost more than an explicit refusal.
Build a representative set of cases: short and long inputs, incomplete documents, unavailable tools, ambiguous requests, and situations that require a refusal. Apply the same criteria to every configuration. The guide to evaluating an AI agent explains how to organize this comparison.
Measure the total cost of all trials, including failed attempts, and divide it by the number of accepted results. Also examine the most expensive tasks. A satisfactory average can hide prolonged loops involving particular documents.
The language model profiles help you shortlist candidates based on their characteristics. They do not replace an evaluation of your own process, with its specific instructions and tools.
Keeping a more expensive model for a difficult step while simplifying the others may be reasonable. On the other hand, switching models for every operation introduces additional logic to maintain. Start with a configuration you can understand, and add complexity only when it produces measurable savings.
The article The cheapest LLM for each use case covers model selection by type of need. This guide addresses the cost of the complete run.
Reduce the number of decisions assigned to the model
Your first saving may come from eliminating a call. Conventional code can check a date, add amounts, apply a filter, or confirm that a field is present. Asking a model to perform these operations again adds both a probabilistic decision and an expense.
Map the path a task follows: intake, data retrieval, interpretation, action, and validation. Identify which steps are deterministic and which require interpretation. Reserve the model for the latter, and use explicit checks for the former.
This distinction follows Anthropic’s recommendation in Building effective agents: begin with the simplest solution and add complexity when the need justifies it. The document distinguishes predefined workflows from agents that determine their own path.
In particular, avoid making an agent rediscover a stable procedure every time it runs. Provide the prerequisites, relevant tools, and a clear definition of the expected result. Precise instructions can prevent exploratory searches without prescribing every detail of the model’s reasoning.
Subagents require the same discipline. Delegating independent tasks can reduce waiting time, but every branch consumes resources and produces a result that must be integrated. Parallel execution does not, by itself, save money.
Before adding a branch, specify what it should contribute, which data it needs, and when it should stop. If nothing uses its result, remove it.
Keep context useful and verifiable
The context window describes what a model can receive, not what is worth sending. A history full of large logs, duplicate documents, and abandoned searches can increase usage without improving the next decision.
Organize information by purpose: stable instructions, current state, useful evidence, and searchable archives. The current state should identify the objective, completed actions, constraints, and unresolved issues. Archives should remain accessible without being automatically included in every request.
For tool results, request only the fields you need. A targeted query, pagination, or an excerpt around a match can avoid retrieving an entire dataset. Keep a way to return to the source when the excerpt is insufficient.
Targeted document retrieval also carries costs for search, indexing, and sometimes ranking. Compare those costs with the context volume it avoids. A short document used occasionally does not necessarily justify additional infrastructure.
History compression also needs evaluation. A summary may omit a restriction, a reference, or an action already performed. Store structured facts that must remain exact separately: identifiers, approvals, amounts, and operation status.
Finally, do not keep summarizing an earlier summary indefinitely. When the current state becomes uncertain, reconstruct it from authoritative sources. A small context saving should not force you to repeat an entire run.
Use caching without assuming it saves money
Prompt caching allows part of the input processing to be reused. It does not guarantee that an agent can answer without generating new output. Its value depends on how often content actually repeats and on the service’s rules.
Place stable material at the beginning of the context and variable data afterward when the mechanism relies on a shared prefix. Avoid inserting a changing timestamp before otherwise identical instructions. However, do not alter necessary instructions solely to preserve a cache hit.
Anthropic’s caching documentation, accessed on October 24, 2026, describes automatic or explicit prefix caching, with conditions governing duration and billing. Measure cache creation, cache reads, and standard input separately. Requests spaced too far apart or changes to prefixes may prevent reuse.
Mechanisms vary across APIs. Google’s caching documentation, updated on October 23, 2026, describes implicit caching and notes that explicit caching is not available through every interface or for every model generation. Do not assume a configuration transfers directly between providers.
Compare total costs with and without caching on representative traffic. Include writes, storage when billed, and requests that do not reuse cached content. A cache hit rate on its own does not tell you how much money you saved.
A response cache is a separate design choice: it returns a previous result. Its key must account for access permissions, the data version, and relevant settings. An outdated response, or one intended for another user, may be unusable even if retrieving it costs very little.
Route tasks using observable rules
Routing means choosing a model or workflow based on the task. Start with rules you can verify: an accepted document type, the presence of required attachments, input length, or the outcome of a validation check. An explicit rule makes it possible to understand why a request takes an expensive path.
| Strategy | Trigger | Cost to measure | Main limitation |
|---|---|---|---|
| Single model | Every request | Execution and retries | High spending on simple tasks |
| Upfront routing | Known category or constraint | Classification and selected workflow | Incorrect initial routing |
| Escalation after validation | Rejected or incomplete result | Initial attempt, validation, and retry | Duplicate work on failures |
| Deterministic workflow | Sufficient rules | Execution and maintenance | Limited handling of ambiguity |
For escalation, define a concrete signal: a missing field, an untraceable reference, an invalid format, or a failed test. A model’s stated confidence does not replace checking its output.
The calculation must include every attempt: the initial model’s cost, validation, escalations, and coordination. Compare this total with sending the task directly through the alternative workflow to determine whether the savings are real.
Give the next model the useful evidence and the reason for escalation. Avoid presenting the previous answer as an established fact. For sensitive actions, routing must not change the required validation.
Finally, limit fallbacks. An outage should not trigger a cascade of attempts across services that are more expensive or incompatible with your data processing constraints.
Control output, reasoning, and retries
Request output suited to its recipient. An internal step may return structured fields and a status, while the final response explains the result to the user. Generating lengthy commentary at every step adds content that must be produced and may later need to be read again.
An output cap provides a safeguard, but a truncated response can become unusable. Check the stop reason and define how incomplete output should be handled. Arbitrarily shortening responses may shift spending into retries.
When a model offers a reasoning effort setting, compare the available levels on your tasks. A short visible explanation does not prove that little internal computation took place. Use the usage counters and billing information for the API involved.
Distinguish between types of errors as well. Invalid input needs correction; temporary unavailability may justify another attempt. Repeating an identical request without diagnosing the problem consumes resources without addressing its cause.
For an external action, check its status before retrying. A timeout does not necessarily mean the operation failed. An operation identifier and an idempotency mechanism, where the service supports them, help prevent a retry from being treated as a new action.
Defer work that can wait
Batch processing can suit document extraction, evaluations, and enrichment tasks that do not require an immediate response. It is harder to integrate into an interactive loop where every step depends on the previous one.
As of October 24, 2026, Google’s Batch API documentation advertises billing at 50% of the standard cost and a target turnaround of 24 hours. That target is not a universal delivery guarantee. Check supported models and interfaces before scheduling your processing.
Compare usage savings with operational requirements: queuing, tracking results, handling partial failures, and matching outputs to their original cases. A batch should allow failed items to be retried without reprocessing items already accepted.
Local execution involves a different tradeoff. Calculate hardware purchase or rental, electricity, and operating costs, then relate them to the useful work completed. An underused machine may be expensive per task; a fully loaded machine may create delays the service cannot accommodate.
In both cases, assess quality using the same criteria. Moving an expense to infrastructure or delaying processing does not, by itself, demonstrate a reduction in total cost.
Set budgets that actually stop spending
Define limits by task, user, project, and time period. Add caps on steps, tool calls, runtime, and concurrency. These limits address different causes of overspending: growing request volume, loops that make no progress, or excessive delegation.
The check must live in the code that authorizes calls. An instruction asking a model to respect a budget does not replace an external counter. A notification reports usage; a blocking check prevents a new expense.
For parallel requests, reserve the maximum estimated cost before dispatch, then reconcile that reservation with the observed cost. Without shared reservations, several tasks may each assume that the same remaining balance is still available.
A gateway such as LiteLLM can centralize these controls. Its budget documentation, accessed on October 24, 2026, states that the budgets described require a database and explains reservation limitations, including those affecting batches. Verify the prerequisites and failure behavior before treating the cap as effective.
Test rejected calls, concurrent requests, and an unavailable spending counter. Also plan for a clean stop: save the state, identify what remains incomplete, and avoid reporting success when a task has been interrupted.
Apply changes while preserving quality
Start with a representative measurement period, then rank the sources of spending: context, output, tools, retries, or unnecessary workflows. Change an identifiable factor at a time so you can attribute the observed effect.
Keep a baseline configuration and compare results on the same cases. Then test on a limited share of real traffic, with a way to roll back. Savings measured outside production may disappear when inputs or concurrency change.
The final check can stay brief:
- Costs include failures, retries, tools, and human corrections.
- Acceptance criteria remain identical across configurations.
- Caching and routing savings are measured against total cost.
- Budgets actually block new calls.
- Stopping preserves a usable state and identifies incomplete tasks.
Sustained savings come from a process that completes the requested work with less unnecessary processing. Revisit the measurement whenever prices, models, documents, or quality requirements change.
Related pages
Frequently asked questions
Which model should I choose for a low-cost AI agent?
Start by defining what makes a task acceptable: accuracy, correct tool use, and no unauthorized actions. Then compare several models on the same cases, including the cost of retries and rework. The price per token alone cannot tell you which option is most economical.
Does caching eliminate the cost of an agent's context?
Some context may be billed differently when reused. New data and generated output are still billed under the service's terms. Check actual usage counters instead of assuming that an available cache automatically produces savings.
Does an assistant subscription cover API calls?
Do not assume these billing arrangements are interchangeable. Check how your agent authenticates and which agreement covers its usage. A subscription, credits, and an API key may each cover different services or types of consumption.
Is a self-hosted agent cheaper?
That depends on utilization and operating requirements. Compare hardware purchase or rental, electricity, maintenance, and the capacity available during peak demand. Include the time needed to achieve comparable quality.
How can I stop an agent from exceeding its budget?
Checks must happen before each new expense, in the program or gateway that authorizes calls. Reserve a margin for requests already in progress, and share the spending counter across parallel tasks. An alert sent after usage occurs is not a spending cap.