I love terminal agents. In the last three months, Claude Code has saved me dozens of hours of grunt work. But at the end of my first month of heavy usage, my Anthropic API bill was $184.20—and that was just for one seat. Across our small three-person engineering team, we were staring at a monthly LLM spend of nearly $500.

AI-assisted development is incredibly fast, but if left unoptimized, it makes your API billing even faster. For scale, Anthropic’s own cost guide puts the enterprise average at about $13 per developer per active day and $150 to $250 per developer per month, so our numbers were not unusual.

In April, we set out to systematically audit and optimize how we interact with our AI stack—specifically Cursor 3 and Claude Code. By modifying our prompt structures, managing our context sizes, and using Anthropic’s prompt caching mechanics, we cut our API billing by 64% in May ($66.30 for my seat), while actually getting better code outputs.

Here is the exact playbook we used to tame our token consumption.


The Economics of the Agentic Loop

To optimize your costs, you have to understand where the tokens are actually going. In a standard chat interface, you pay for your prompt and the response. In an agentic tool like Claude Code or Cursor’s Composer, you are running an agentic loop:

  1. You write a prompt: “Find the bug in our auth pipeline.”
  2. The agent reads a file. (API Call 1)
  3. The agent runs a terminal command to test. (API Call 2)
  4. The agent writes a fix to the file. (API Call 3)
  5. The agent runs the test suite again. (API Call 4)
  6. The agent reports back to you. (API Call 5)

Every single step in that loop is a separate API call. And because LLM chats are stateless, each step sends the entire conversation history, all read files, and all command outputs back to the model.

If you are working inside a 20,000-word codebase and have a conversation that stretches for ten turns, you aren’t just paying for ten prompts. You are paying for the cumulative sum of all previous context, ten times over. By turn ten, a single command execution can cost you 80,000 input tokens.

Accumulating Context in an Agentic Loop:
Turn 1: [System Prompt] + [Your Prompt]
Turn 2: [System Prompt] + [Your Prompt] + [File A Content] + [Turn 1 History]
Turn 3: [System Prompt] + [Your Prompt] + [File A Content] + [Terminal Output] + [Turn 2 History]
...
Turn 10: [Massive History & File Content Sent Again] — Exponential Cost Growth!

1. Master Anthropic’s Prompt Caching

Anthropic introduced prompt caching in 2024, and in 2026 it remains the most powerful tool for cost optimization. It allows the API to store a “prefix” of your prompt (like system instructions, project-level configurations, or long conversation histories) on Anthropic’s servers.

Subsequent API calls that share that identical prefix read it from the cache instead of processing it from scratch.

The pricing differences are dramatic:

Transaction TypeCost MultiplierCost per 1M Tokens (Sonnet 5.5)
Standard Input1.0x (Base)$2.00
Cache Write (TTL 5m / 1h)1.25x / 2.0x$2.50 / $4.00
Cache Read (Hit)0.05x (95% off)$0.10

(Source: Anthropic prompt caching docs, October 2026. Most other Claude models use a 0.10x cache-read multiplier.)

Because a cache hit costs only 5% of a standard input token on Sonnet 5.5 and Opus 5.5 (10% on most other Claude models), a single cache write pays for itself after just two turns in a chat. Current per-token prices for every major model are on our AI model pricing tracker.

How to maximize cache hits in Claude Code:

Claude Code manages prompt caching automatically, but it relies on you keeping the prefix of your conversation stable.

  • Avoid modifying your system rules mid-session: Changing your .cursorrules or project-level CLAUDE.md in the middle of a coding session invalidates the cache, forcing Claude Code to write a brand new cache at the 1.25x premium.
  • Run long, focused sessions instead of scattered queries: If you query the agent once, wait ten minutes (exceeding the cache TTL), and query it again, you will pay the cache write premium repeatedly without ever getting a cache hit. Group your work so that you are interacting with the agent continuously within the cache’s time-to-live window. The API default is five minutes; per Anthropic’s docs, Claude Code on a subscription uses a one-hour lifetime, which drops to five minutes once you are drawing on usage credits.

2. Keep Chats Scoped (New Task = New Chat)

The single biggest mistake developers make is keeping a single chat window open for hours, using it to solve five different problems.

By hour three, you are carrying around 100,000 tokens of dead context—conversations about a CSS bug you fixed two hours ago, which are now being sent to the model while you try to write a database migration.

We instituted a strict “Single-Purpose Chat” rule (Anthropic’s guide gives the same advice: run /clear when you switch to unrelated work):

  • One issue, one session: As soon as a feature is built or a bug is squashed, close the terminal session or chat panel and start a brand new one.
  • The “Summarize and Reset” technique: If you are working on a massive refactor that genuinely requires hours of iteration, do not let the chat drag on. At turn eight or nine, type:

    “Summarize our progress, the current architecture we have agreed upon, and the remaining TODOs.” Copy that summary, close the session, open a fresh one, paste the summary, and continue. This instantly trims away the conversational deadweight, saving us thousands of input tokens per turn.


3. Limit Context Scope Aggressively

Both Cursor and Claude Code make it incredibly easy to include your entire repository in the context. In Cursor, typing @codebase is a habit. In Claude Code, letting the agent search the whole project is the default.

But sending your whole repository to a model is like hiring a contractor and paying them to read your entire house’s blueprints just to change a lightbulb in the kitchen.

### Rules of Context Engagement:

1. **Avoid `@codebase` for routine edits:** Only use codebase search when you genuinely don't know where a function is defined.
2. **Use `@selection` or `@file`:** Limit the model's sight to the precise files you are editing. If you are changing a React component, there is no reason the model needs to see your database schema or Tailwind configuration.
3. **Ignore node_modules and dist:** Ensure your `.gitignore` and `.cursorignore` files are properly configured. If they are not, AI indexers will scan compiled build files and dependencies, ballooning your token count on every query.

4. Move Rules to Modular Files

In 2025, developers loved writing massive, 5,000-line global .cursorrules files containing every coding standard, UI pattern, and package configuration they could think of.

The problem? That entire 5,000-line file is injected as a system prompt on every single keystroke and query, burning through your premium fast-credit pool or costing you massive API fees.

Cursor’s answer is modular project rules in .cursor/rules/. Per Cursor’s rules docs, each rule is an .mdc file with frontmatter, and a rule can apply always, only to files matching a glob, when the agent decides it is relevant, or only when you @-mention it:

---
description: Rules for React components and styling
globs: src/components/**/*.tsx
alwaysApply: false
---
Use function components. Style with Tailwind utility classes only.

By splitting our massive global rules file into six highly targeted .mdc files, we reduced our baseline system prompt size by 82% for the vast majority of our queries. The styling rules only load when we edit CSS/TSX; the database rules only load when we edit Prisma/SQL files. Claude Code has the same lever: its docs suggest keeping CLAUDE.md under 200 lines and moving workflow-specific instructions into skills, which load only when invoked. Our AI context window optimization guide goes deeper on that pattern.


5. Implement a Hybrid “Ask → Inline → Debug” Workflow

Agentic modes (like Cursor’s Composer or Claude Code’s autonomous loops) are extremely powerful, but they are cost-heavy because they make multiple API calls to self-correct. For minor edits, using a full agent is overkill.

We trained our team on a hybrid workflow that matches the tool to the task:

  1. Use Chat for Architecture & Logic: Ask the chat agent to explain a concept or write a high-level plan. This is a single, inexpensive query-response turn.
  2. Use Inline Edit (Cmd+K) for Implementation: Once you have the plan, highlight the specific block of code you want to change, hit Cmd+K (or equivalent inline editor), and describe the change. Inline models are highly optimized, use far less context history, and apply modifications directly.
  3. Use Agent Mode ONLY for System-Wide Changes: Reserve the fully autonomous agent loops for complex refactoring that spans three or more files, or when hunting a tricky bug that requires running active test commands in the terminal.

6. Configure Local Offline Fallbacks

Not every task requires a frontier model like Claude Sonnet 5.5 or GPT-6.1 Sol. If you are writing standard boilerplate, generating basic test structures, or converting an array format, you are wasting money using a premium model.

We set up Ollama on our local developer machines, running DeepSeek-Coder-V2 (16B) and Llama-3-70B.

  • Routine Coding (Local): For day-to-day autocomplete, documentation drafting, and boilerplate, we switch our local editor models to our offline Ollama instances. They cost zero dollars, run at zero latency, and handle routine tasks beautifully.
  • Complex Reasoning (Cloud): We reserve our premium Claude API keys for the hard problems—architectural decisions, security reviews, and multi-file debugging.

The same split now works inside Claude Code without leaving Anthropic: Claude Haiku 5.5 costs $0.10 per million input tokens on prompts up to 100K tokens, and Anthropic’s docs recommend setting model: haiku on simple subagents. If you are on Cursor, note that its X Subscription pricing change now bills Claude separately at API rates, which changes the BYOK math in the FAQ above.


The Verdict: Reclaiming Your Budgets

By treating our API consumption like any other engineering bottleneck—measuring it, identifying waste, and applying technical optimizations—we transformed our AI tools from a luxury spend into a highly profitable utility.

You do not have to stop using agents to save money. You just have to stop sending them dead context, use modular rules, and let prompt caching do the heavy lifting.

Your development speed will stay exactly the same. Your finance lead, however, will be much happier.

(Updated October 16, 2026: corrected the prompt caching table, which used Claude 3.5 Sonnet prices for a model Anthropic retired on October 28, 2025; it now shows Claude Sonnet 5.5 at $2.00 base input and $0.10 cache reads, a 0.05x multiplier rather than 0.10x. Replaced the Cursor FAQ answers about unlimited Auto mode and 500 fast requests with Cursor’s current plans, which bill Claude at API list price. Fixed the Cursor rules example, which uses Markdown frontmatter in .mdc files rather than JSON, and removed the claim that Cursor introduced project rules in 2026. Added a sources list and swapped the PNG cover for an SVG.)