Cutting AI Coding Agent Costs: Caching, Routing, Context

AI coding agent costs grow faster than most teams expect — not because the models are expensive per token, but because coding agents run long contexts, make repeated tool calls, and retry failures in tight loops. Teams running Cursor, Claude Code, GitHub Copilot’s coding agent or custom agents on production codebases watch that spend grow faster than their usage. The patterns that bring it down are architectural, not account-level.

Why Coding Agents Are So Much More Expensive Than Chat

A standard chat completion sends a user message and gets a response. A coding agent does something structurally different: it plans, calls tools, reads file contents, evaluates output, retries on error, and often loops through that cycle multiple times per task. Each iteration hits the model with a context window that grows as the conversation accumulates.

The failure mode most teams don’t anticipate is token amplification through tool output. When an agent reads a 400-line file to answer a question about a 10-line function, you pay for all 400 lines — in every subsequent turn. That multiplication compounds across a multi-step task.

An agent working a single GitHub issue can make dozens of model calls, each one resending the context it has built up so far. Multiply that by every task a developer hands an agent in a day and the monthly number is not theoretical — it lands on your invoice.

The problem is not that AI coding agents are wasteful by design. Default agent framework configurations optimize for task success rate, not token efficiency. Those are not the same objective, and no provider has an incentive to reconcile them for you.

Context Bloat Is the Primary Driver of Your Spend

Context bloat has a specific mechanism. Most coding agent loops follow this pattern: receive task, generate plan, call tool, append tool output to context, generate next action, repeat. The append-and-grow pattern means your context is strictly increasing across a task. By step 10, the agent is reading its own earlier reasoning, intermediate file contents, and error messages from three retries ago — all at input token rates.

A 120,000-token context costs $0.24 in input on every call that resends it at Claude Sonnet 5.5’s $2 per million input tokens — before a single output token is counted.

There is also a subtler amplifier: re-reading unchanged context. When an agent calls the model after a tool result, it re-sends the entire conversation history. If the system prompt is 2,000 tokens and unchanged, you pay for those 2,000 tokens on every call in the loop. A 20-call task pays for that system prompt 20 times.

Research on long-context LLM inference (Kwon et al., “Efficient Memory Management for Large Language Model Serving with PagedAttention,” SOSP 2023) addresses the serving-side cost of long contexts. On the client side, the same problem manifests as billing: you cannot escape paying for tokens you send.

How Prompt Caching Cuts Costs Without Touching Quality

Prompt caching is the highest-leverage, lowest-risk intervention available. Anthropic’s prompt caching bills cache reads at about 10% of the standard input rate (cache writes cost 1.25×). OpenAI caches automatically, at 50% of the input rate for GPT-4o. Neither requires changing your agent’s task logic — only how you structure what you send.

The rule is: stable content goes first, variable content goes last. A coding agent typically has a large, stable system prompt (repo context, coding standards, tool definitions) followed by variable task content. Move all stable content to the front and hold it consistent across calls, and the provider caches and reuses it.

Here is a concrete implementation pattern using the Anthropic API with caching enabled:

import anthropic

client = anthropic.Anthropic()

# System prompt is large and stable — cache it.
# This stays identical across all turns in a task session.
SYSTEM_PROMPT = """
You are a coding assistant with access to the following tools: read_file, write_file, run_tests, search_codebase.
[... your full repo context, coding standards, tool schemas — typically 1500–3000 tokens ...]
"""

def run_agent_turn(conversation_history: list[dict], new_user_message: str) -> str:
    # Append the new user message (variable content at the end)
    conversation_history.append({
        "role": "user",
        "content": new_user_message
    })

    response = client.messages.create(
        model="claude-sonnet-5-5",
        max_tokens=4096,
        # Mark system prompt for caching — Anthropic reuses this
        # across calls within the same session TTL (5 minutes default)
        system=[
            {
                "type": "text",
                "text": SYSTEM_PROMPT,
                "cache_control": {"type": "ephemeral"}  # enables prompt caching
            }
        ],
        messages=conversation_history
    )

    assistant_message = response.content[0].text

    # Append assistant response to maintain conversation state
    conversation_history.append({
        "role": "assistant",
        "content": assistant_message
    })

    # Log cache performance — critical for verifying savings
    usage = response.usage
    print(f"Input tokens: {usage.input_tokens}, Cache read: {usage.cache_read_input_tokens}")

    return assistant_message

def run_coding_task(task: str) -> str:
    history = []
    result = run_agent_turn(history, task)

    # Tool call loop — each iteration benefits from cached system prompt
    while needs_tool_call(result):
        tool_output = execute_tool(result)
        # Tool output is variable — appended at the end, never cached
        result = run_agent_turn(history, f"Tool result: {tool_output}")

    return result

The cache_read_input_tokens field in the response tells you exactly how much was served from cache. On a 20-call task with a 2,500-token system prompt, the first call writes those tokens to the cache at 1.25× the input rate and the next 19 read them at 0.1×, so the system prompt costs about 7,900 token-equivalents instead of 50,000.

One practical note: Anthropic’s default cache TTL is five minutes, and every cache hit resets it, so a loop whose calls start less than five minutes apart keeps the cache warm however long the session runs. Only gaps matter: if a step routinely pauses longer than that, use the one-hour TTL ("ttl": "1h", written at 2× instead of 1.25×) or re-send the prefix before it expires.

Route by Task Complexity, Not by Default

Most teams default to a single model for all agent tasks because it is the path of least resistance. This is expensive. The distribution of coding tasks is not uniform — a large fraction are simple (rename a variable, write a docstring, look up a function signature) and a small fraction are genuinely hard (refactor across modules, debug a race condition).

Running a mid-tier or frontier model on simple tasks is the same category of waste as renting a dedicated server to serve static files. A routing layer that classifies task complexity before dispatching to a model cuts spend.

Tier 1 — small/fast model (GPT-4o-mini, Claude Haiku): single-file edits, docstring generation, test case generation for an existing function, lint fix application.

Tier 2 — mid-tier model (GPT-4o, Claude Sonnet): multi-file edits, bug diagnosis, code review with suggestions.

Tier 3 — frontier model (Claude Opus or your provider’s top reasoning model): architectural reasoning, cross-repo refactors, security audit.

The classifier itself should be cheap — a small model or a rule-based heuristic on task metadata (number of files referenced, presence of architectural keywords, whether a plan was generated). Do not use a frontier model to classify whether a task needs a frontier model.

How much a routing layer saves depends on your task mix, so measure it on your own traffic before projecting a number. Quality risk concentrates on boundary conditions between tiers — invest in evaluation there, not in blanket escalation.

A common failure mode is over-classification — a keyword heuristic escalating a task because its description mentions “architecture” when the actual edit is trivial. Evaluate the classifier against a labeled set of completed tasks; task scope (lines of code touched, number of files) is usually a stronger signal than keywords.

Frequently Asked Questions

How do I estimate monthly spend for a team using AI coding agents heavily?

From your own logs. Record input, cached and output tokens per task for a week, multiply by tasks per day, and price it — before and after caching and routing. No published figure transfers, because context size, model choice and retry rates differ significantly by codebase and task type.

Does prompt caching work with OpenAI models?

Yes. OpenAI introduced automatic prompt caching for GPT-4o and GPT-4o-mini. Cached tokens are billed at 50% of the standard input rate (Anthropic’s cached rate is approximately 10%). Caching is automatic — you do not need to set cache control headers as you do with Anthropic. Observe cache hits via usage.prompt_tokens_details.cached_tokens.

How do I measure whether my context is actually bloating?

Log input_tokens on every API call within a task session. Plot the token count across turns — a healthy session grows sub-linearly as tool outputs are summarized rather than appended verbatim. A session where input tokens grow linearly or faster turn-over-turn indicates raw tool output accumulation. That signals a need for context compression or summarization at tool call boundaries.

Is it worth building a custom agent framework to control costs?

Use an existing framework unless you have specific requirements it cannot meet. Cost optimization patterns — prompt caching, model routing, context compression — are framework-agnostic and implementable in LangChain or any other orchestration layer. Rewriting the framework does not reduce costs; it delays the work of instrumenting and optimizing the one you have.

What is context compression and when should I apply it?

Context compression replaces accumulated tool outputs with a shorter summary before the next model call. Apply it when a tool output exceeds a threshold (say, 1,000 tokens) and the agent’s next decision does not require the full content — only the extracted signal. A cheap model call to summarize a 500-line file diff before appending it to context saves several times its own cost in subsequent turns.

How does retry logic affect costs?

Substantially. A naive retry on tool failure re-sends the full context with the error appended, often growing the context by 200–500 tokens per retry. Three retries on a 40k-token context add about 120k input tokens before the task makes any progress. Implement exponential backoff with a retry budget per task, and truncate error messages appended to context — full stack traces are rarely useful after the first 20 lines.

Can I run a cost-efficient coding agent on self-hosted infrastructure?

For specific, well-scoped tasks — yes. Open-weight coding models running on self-hosted infrastructure handle tier-1 tasks at near-zero marginal cost. Whether the overhead of running GPU infrastructure pays off depends on your volume; at low volume, managed API pricing is usually cheaper once engineering and infrastructure time are counted.

The architectural principle underneath everything in this post is the same: every token you send is a billable event, and agents send tokens in bulk, repeatedly, in loops. The teams controlling their costs are not using fewer AI agents — they are being precise about what goes into context, when expensive models get invoked, and how tool outputs are managed across turns. Those are engineering decisions, not procurement decisions. They belong in your codebase, not on a settings page.

At EchoForgeX, we build AI-powered tools and help businesses integrate AI into their workflows. Get in touch to learn how we can help your team work smarter with AI.

Leave a Comment