Skip to Content
View all events

Cutting Token Costs of AI Coding Agents

Published by O'Reilly Media, Inc.

Optimize your use of Claude Code, Cursor, and Copilot

What you’ll learn and how you can apply it

  • Measure and attribute an AI coding agent’s token spend by task type using a local proxy and the agent’s own usage data, and track cost per completed task rather than cost per token
  • Apply context discipline to an agent workflow (scoping what enters context, keeping a stable cacheable prefix so the agent’s prompt cache hits, and compacting or resetting long sessions) and measure the savings
  • Route the right model and effort level to each coding task, escalating to a frontier model only on failure, and defend the choice against a correctness baseline
  • Cut tool and output bloat with deferred tool loading and a drop-in tool-output compression layer, verifying any vendor savings claim against the repo’s own test suite before trusting it

Course description

If you use an AI coding agent every day, the era in which someone else silently absorbed your token cost is over, and the bill is now yours. The counterweight is not using your agent less. Spending more tokens to let the agent reason hard on a genuinely tricky change is often the right call. The waste is the tokens that buy nothing.

This hands-on course with Bruce Hopkins shows you how to find and cut exactly those tokens, in the order that pays, without making your agent worse. You’ll drive your own agent (Claude Code, Cursor, or Copilot CLI) against a provided sample repository throughout, routed through a local proxy so you can see every token. You’ll measure where your agent’s spend actually goes, practice context discipline (what you feed it, and how its prompt cache hits or misses), route the right model and effort level to each task, and cut tool and output bloat with deferred tool loading and a drop-in compression layer. You’ll judge every change against the only eval that matters for coding: your test suite. You’ll leave measuring cost per completed task instead of cost per token, with a decision tree and a 30/60/90 plan you can act on right away.

This live event is for you because...

  • You’re a software developer who uses an AI coding agent (Claude Code, Cursor, Copilot, Aider, or similar) most days and feels the cost directly.
  • You lead a team that’s paying for coding agents across several developers and want those savings to scale past your own laptop.
  • You want to spend less without your agent getting dumber, and without cargo-culting tips from vendor blog posts.

Prerequisites

  • Regular use of an AI coding agent and comfort with the command line
  • The ability to read and run a project’s test suite
  • Basic familiarity with Git and how your agent is configured (its rules file, MCP servers, model picker)

Recommended preparation:

  • Have an agent installed: Claude Code, Cursor, or GitHub Copilot (in VS Code or its CLI). Any one is fine, all exercises are agent-agnostic, though Copilot's per-interaction token view is only available in the VS Code Chat Debug View.
  • Clone the course repo: github.com/bmuschko/agent-token-optimization [repo to be created], a small sample project with seeded bugs and a short feature list you'll work through with your agent.
  • Install: Git; Headroom (pip install "headroom-ai[all]", pinned to the version noted in the repo), the local measuring-and-compression proxy the exercises route through; and the two usage analyzers used in Section 2, ccusage (what you spent) and claude-usage-analyzer (why you spent it), with exact commands listed in the repo. Cursor and Copilot users substitute their own usage dashboards, also noted in the repo.
  • Credentials: Whatever your agent already uses (an Anthropic key for Claude Code, your Cursor or Copilot subscription). Budget roughly $3 of token usage for the full course. If you can't run your agent live, set REPLAY_MODE=true to work against recorded sessions.
  • Verify setup: Run headroom doctor and the repo's verify_setup script; both should print a green check.

Recommended follow-up:

Schedule

The time frames are only estimates and may vary according to how the class is progressing.

From tokenmaxxing to tokenpocalypse (17 minutes)

  • Presentation: The short version of the arc your agent bill lives in, burn as a virtue, then the meter (Copilot's June 1 credits, Cursor's repricing, the real cost of a Claude Code session), then the brakes; tokenmaxxing as a metric versus as a lever; the metric that replaces cost-per-token
  • Hands-on exercise: Run your agent through the sample repo’s first two tasks with your normal habits, routed through the proxy, and record the baseline token cost
  • Group discussion: Who got a surprise agent bill this year?
  • Q&A

Measure your agent’s token burn (18 minutes)

  • Presentation: Where a coding agent’s tokens go (rules files, loaded file context, history, tool definitions, tool outputs, model output); reading the agent’s built-in usage data; attributing spend by task type through the local proxy
  • Hands-on exercise: Route your agent through the proxy, run a mix of task types from the repo, and break the burn down by category; find the one kind of task that eats a disproportionate share
  • Group discussion: What was your most expensive task category?
  • Q&A

Context discipline and prompt caching (32 minutes)

  • Presentation: The biggest lever for agent users is what enters the context window; the cost of whole-repo loading, too many open files, a dragging session history, and a bloated rules file; how your agent’s prompt cache works and how everyday habits silently bust it; when to compact and when to start fresh
  • Hands-on exercise: Take a task your agent currently does with a bloated context, scope it down (fewer files, a fresh session, a leaner rules file), and rerun it; measure the token drop and confirm the task still passes the tests
  • Q&A
  • Break

Right model, right effort (22 minutes)

  • Presentation: Most coding tasks don’t need your most expensive model; matching model to task; dialing thinking and effort budgets down for routine steps; the escalate-on-failure pattern; switching models mid-flow in Claude Code and Cursor
  • Hands-on exercise: Run the repo’s task list on a cheaper model, escalating to a frontier model only the tasks that fail their tests; compare total cost and pass rate against running everything on the frontier model
  • Q&A

Tool and output optimization (31 minutes)

  • Presentation: The tool-definition tax; tool search and deferred loading for an ~85% cut, with accuracy rising rather than falling; efficient and programmatic tool use; memory instead of replayed history; auditing what tools return; where these techniques don’t help
  • Hands-on exercises: Measure your agent’s MCP tool-definition overhead as a share of a request, enable tool search or deferred loading, and measure again; record the delta; take a long session and apply compaction plus a memory note in the rules file so the agent stops replaying stale history; measure the burn on the next few turns
  • Demo: Wrapping the agent with Headroom in one command and replaying a task whose tools return verbose JSON and logs; showing the live token counter before and after, confirming the result is unchanged
  • Q&A

Your Instructor

Benjamin Muschko

Benjamin Muschko is a software engineer, consultant, and trainer with more than 20 years of experience in the industry. He specializes in cloud-native application development and transformation, container solutions, DevSecOps, and Continuous Integration/Continuous Delivery implementations. Ben is an author, a frequent speaker at conferences, and an avid open source advocate.

linkedinXlinksearch

Skill covered

QA / Testing