Skip to content
Apixo
Blog
news· 4 min read· via Towards AI

Optimizing AI Agent Costs: Managing Token Leaks and Subagent Effort

Subagents in modern AI coding tools can unknowingly drain token budgets. Here is how effort levels, background updates, and routing strategies impact developer costs.

Optimizing AI Agent Costs: Managing Token Leaks and Subagent Effort

When software development teams enforce spending caps on AI usage, token consumption suddenly comes under detailed scrutiny. In modern multi-agent coding workflows, high usage is rarely driven by primary prompt turns alone. Instead, subagents, context cache re-reads, and framework overhead often consume the majority of a team's token allocation without explicit notice.

Understanding how agent execution environments allocate resources—and where configuration settings override intended behavior—is essential for keeping automated software engineering cost-effective.

Understanding the Effort Leak and Precedence Rules

A primary driver of unexpected API expenditure stems from subagent configuration defaults. As highlighted in a summary of a post by Boris Cherny, subagents in Claude Code inherit the main session's effort level by default. If a developer runs a primary session at maximum reasoning effort, every background task spawned by that session will execute at maximum effort as well, even for simple file searches.

Benchmark data published by Wmedia’s Claude Code Hub illustrates the cost disparity of this behavior. In a single-task test explaining a file in five lines, a subagent consumed 586 output tokens at low effort, 701 tokens at high effort, 656 tokens at extra-high effort, and 10,921 tokens at maximum effort. For minor lookup operations, running at maximum effort can increase token output by an order of magnitude.

Managing subagent resource consumption requires navigating specific configuration hierarchies. In Claude Code, effort levels follow a set resolution order:

  1. CLAUDE_CODE_EFFORT_LEVEL: A global environment variable that overrides every session and subagent setting, including individual agent definition files.
  2. Per-call effort parameters: Explicitly passed during an agent invocation (available in version 2.1.292 and later).
  3. Frontmatter configuration: The effort: property set in .claude/agents/<name>.md files.
  4. Fallback settings: Introduced in version 2.1.316, CLAUDE_CODE_SUBAGENT_EFFORT serves as a fallback for subagents before defaulting to the primary session's effort level.

Setting CLAUDE_CODE_EFFORT_LEVEL globally can accidentally strip out subagent-specific lower effort configurations. Model selection follows a different precedence hierarchy, where the CLAUDE_CODE_SUBAGENT_MODEL environment variable sits below frontmatter configuration, allowing individual agents to define their own execution models.

Beyond Reasoning: Context Reruns and Harness Overhead

Effort levels are only one component of API consumption. Several operational patterns contribute significantly to overall token costs:

  • Background Status Updates: Analysis published by Flamerix AI noted that the agent_summary feature—which generates short label updates every 30 seconds—re-sent context transcripts and accounted for roughly 15% to 20% of session tokens. A test by Level Up Coding on version 2.1.312 measured this at 14.6% across three background agents. A Claude Code team member on Hacker News clarified that while specific background-heavy setups see higher numbers, the user average is closer to 2% (or 6% in terminal views).
  • Agent Teams: Anthropic documentation notes that multi-agent teams running in plan mode consume approximately 7x more tokens than standard sessions because each agent maintains a separate context window.
  • Cache Reads: Analyses by Max Woolf, exe.dev, and Martin Alderson highlight that context cache re-reads dominate final bills. In an exe.dev simulation, cache reads represented 87% of a $12.93 total cost. Because every turn re-reads the cumulative context, cost scales quadratically with turn count.
  • Harness Overhead: Research by Cekikj on Microsoft's agent framework demonstrated that harness abstraction increased execution from 6 calls and 26,000 input tokens in a plain setup to 15–31 calls and 169,000–344,000 input tokens.

What it means for developers

To control spending, engineering teams must evaluate workflows based on cost per accepted change rather than cost per individual API call. Routing tasks between specialized model tiers is an effective way to balance performance and expenditure.

A proven pattern involves deploying high-capability models for planning and verification, while assigning mechanical edits to cheaper models without execution permissions like Bash. Restricting shell tools prevents low-tier models from entering prolonged, costly retry loops when fixing test failures.

Experiments conducted by Naresh Jain and reported by Wasowski illustrate the financial trade-offs of model routing. Running a cheap model without a testing harness resulted in a 30% pass rate at $0.086 per run, yielding an effective cost of ~$0.287 per accepted change. Adding a harness brought the pass rate to 100% at $0.273 per accepted change. By contrast, executing the task entirely on a strong model with a harness cost $2.679 per accepted change.

When building multi-tiered agent systems, developers can try top AI models cheaply through one API at https://apixoai.online to evaluate different model routing strategies without managing multiple vendor subscriptions.

To optimize subagent budgets, developers should implement a few actionable steps:

  1. Avoid setting global effort environment variables like CLAUDE_CODE_EFFORT_LEVEL, which flatten per-agent optimizations.
  2. Define CLAUDE_CODE_SUBAGENT_EFFORT=low (on supported versions) to ensure unconfigured background agents default to minimal reasoning costs.
  3. Explicitly set tool permissions in agent definition files, restricting read-only lookup agents from modifying code.
  4. Terminate and restart long-running subagents periodically to prevent context buildup from inflating cache re-read costs.

Source: The Effort Leak: Why Your Subagents Cost as Much as Your Main Session — Towards AI. Written by the Apixo team from that report.

#ai-news#ai#claude-code#llm-costs#developer-tools#software-engineering
Try it with your own tools

One key for Claude, GPT, GLM, DeepSeek and more. Pay per token with crypto.

Get your API key

Keep reading