Skip to main content
← Guides

Cost control for agents

Cost control

Where the money goes and how to curb it: token accounting, prefix caching, context bloat, and step caps — mechanisms only, no prices.

What this guide covers

An agent's cost is tokens times steps, times the context each step carries. Three factors, three levers: cut useless tokens, shorten the context, cap the steps. This guide covers only mechanisms and practices, and cites no price numbers — for actual prices go to each vendor's official pricing page.

First, account for it

If you don't know where the money goes, saving is out of the question. OpenAI's caching docs model the minimal accounting unit: each response's usage reports four token classes separately — input, output, cache write, cache read. Build the ledger on these four first, then talk optimization — otherwise you can't even tell "did the cache hit at all".

A typical cost structure:

  1. Input tokens: longer history costs more; in multi-turn conversations this is often the bulk.
  2. Output tokens: usually pricier per token than input, but the total is controllable.
  3. Steps: every agent step is a full call; loop ten times, pay ten times.
  4. Tool round-trips: every tool call's definition and result enter the context.

Lever one: prefix caching

Sending the same content to the model over and over is the biggest waste. Mainstream vendors' answer is prefix caching: cache and reuse the exactly-identical opening of requests.

OpenAI: caching applies automatically to eligible requests, no code changes. A hit requires the prefix to match exactly. OpenAI official text: "Cache hits are only possible for exact prefix matches within a prompt."

The official advice: put static content (instructions, examples) at the start of the prompt, changing content (user information) at the end. On GPT-5.6 and later models explicit cache breakpoints are also supported, and prompt_cache_key routes requests sharing a long prefix together. Caching is bucketed in the usage ledger as cached_tokens.

Anthropic: mark cache breakpoints with cache_control; cacheable parts include tool definitions, the system prompt, and conversation messages. There is a minimum-length threshold; below it caching is silently skipped, no error. Caches have a lifetime — short by default, renewed on hit.

The cache is organized in the hierarchy tools → system → messages; changing a layer invalidates that layer and everything after it. The cache_creation_input_tokens and cache_read_input_tokens in responses are the ledger of caching's effect.

Two universal disciplines: static first, change last; confirm the hit before celebrating — lots of creation and little reading means the prefix never stayed stable.

Lever two: context hygiene

Context bloat is the invisible driver of agent cost. Several documented measures:

Summary continuation: compress a long conversation into a summary and continue, rather than continuing in full. That is compression, not memory — see the boundary discussion in the memory systems guide.

Recall on demand: do not stuff whole knowledge-base articles into the prompt; retrieve only when needed — the RAG-server approach, see the context7 dossier.

Wrap up promptly: stop when the task is done; don't let the agent idle through "one more check". Step caps must be hard-set at the harness layer; do not count on the model's self-discipline.

Tier the models: use fast small models for simple steps (classification, extraction), big models only for hard ones. OpenAI official text: the guardrails docs' example uses a fast economical model to check first, "saving time and money", standing in front of the expensive model.

Lever three: block before you pay

The cheapest call is the one that never happens. The OpenAI Agents SDK's guardrails have two execution modes with completely different cost semantics.

Parallel execution (default): the guardrail and the agent run together, lowest latency. But when it trips, the agent may already have burned tokens and executed tools.

Blocking execution: the guardrail finishes first, the agent starts after. Trip and halt — no tokens spent, no tool side effects. OpenAI official text: blocking execution "is ideal for cost optimization and when you want to avoid potential side effects from tool calls". For "malicious input should never reach the expensive model" scenarios, blocking execution is the clearly cheaper choice.

Note output guardrails do not support the parallel flag — they always run after the agent completes.

Hidden costs

Anthropic official text: "Agentic systems often trade latency and cost for better task performance."

Latency is also a cost — users who cannot wait will leave. And the rework cost of compounding errors: one step wrong, every later step wrong, and the bill for ten reruns can exceed one big-model answer done directly. Do the total accounting first, then decide which segment to save.

Sources

SourceURL
OpenAI official docs · Prompt caching (verified via official-page snapshot 2026-10-30, fetched 200 via proxy; direct access still blocked by Cloudflare)https://developers.openai.com/docs/guides/prompt-caching
Anthropic official docs · Prompt caching (verified via official-page snapshot 2026-10-30, fetched 200 via proxy; direct access still blocked by regional policy)https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching
OpenAI Agents SDK official docs · Guardrails (direct fetch 200 on 2026-10-30)https://openai.github.io/openai-agents-python/guardrails/
Anthropic engineering blog · Building effective agents (official page verified via WebFetch on 2026-10-30)https://www.anthropic.com/engineering/building-effective-agents

Source snapshot: 2026-10-30; item-specific rechecks and their scope are recorded in the text or source notes. Features and terms may change; check the original sources before use.

This page appears in: Make music with AI