Skip to main content

Cutting Token Burn in AI Coding Agents (Claude Code & Antigravity Guide)

· 4 min read
Ilham Surya
Platform Engineer (Engineering Infra) - SRE & Agentic AI - Go, Python, TypeScript

Once you use AI coding agents every day across real repositories, token burn and slow turn latency stop being theoretical—they directly affect both cost and flow state.

Here are the highest-leverage techniques and open-source tools from my 2026 research on optimizing Claude Code, Google Antigravity, and agentic workflows.

1. Keep Prompt Caches Hot​

Prompt caching is the single biggest free win in multi-turn coding sessions. System instructions, tool definitions, and stable prefix context are cached across turns—until something invalidates the prefix.

Two habits make a huge difference:

  • Keep stable instructions at the top, dynamic inputs at the bottom. Never inject live timestamps or changing counters into system prompts.
  • Avoid mid-session churn. Switching models or toggling heavy planning modes repeatedly in the middle of a long thread often forces a full cache rebuild.
# Cache-unfriendly: changes every turn, invalidating the prefix
system_prompt = f"Current time: {datetime.now().isoformat()}\nYou are a coding assistant."

# Cache-friendly: stable prefix stays cached
system_prompt = "You are a coding assistant.\nCurrent time is passed per turn when needed."

2. Isolate Noisy Exploration in Subagents​

A classic token trap is letting your main conversation read 15 files, dump 500 lines of test output, or inspect a dozen screenshots. Every subsequent turn replays all of that noise.

Instead, offload broad research, log analysis, and bulk UI/screenshot audits to subagents:

  • Each subagent runs in its own clean context window.
  • Only the concise final summary comes back into your main thread.
  • Your main conversation stays fast, focused, and far cheaper per turn.

3. Compact Intentionally (Don’t Wait for 95%)​

Long sessions eventually accumulate stale exploration. Rather than drifting until emergency auto-compaction kicks in:

  • Run /compact when transitioning between phases (e.g., after finishing investigation and before writing code).
  • Add explicit compaction instructions in your CLAUDE.md so summaries always preserve what matters:
# Compact instructions
When compacting, focus on code diffs, failing test output, and confirmed architecture decisions.
  • Start a fresh session (/clear) whenever you switch to an unrelated task.
  • Use a SessionStart hook on compact to re-inject critical reminders (like package manager or test commands) automatically.

4. Match the Model & Mode to the Job​

You don't need a frontier reasoning model to rename variables, check git status, or summarize a short doc:

  • Fast / lightweight models (Flash, Haiku, Mini) → routine edits, targeted lookups, summaries, quick Q&A.
  • Balanced models (Sonnet, Pro medium) → day-to-day feature implementation and refactoring.
  • Heavy reasoning models (Opus, Pro high) → tricky race conditions, architecture trade-offs, and high-stakes reviews.

Similarly, use Fast Mode for straightforward edits and reserve full Planning Mode for multi-file architectural work.

5. Search Before Reading Whole Files​

Reading full 1,000-line files when you only need one function wastes thousands of tokens. Make it a default rule in your workspace instructions to:

  1. Locate files first with glob/grep (files_with_matches or symbol outlines).
  2. Read only the exact line ranges needed.

6. Open-Source Token Compression Tools Worth Watching​

For custom agent pipelines or heavy tool-calling workloads, several open-source libraries compress context programmatically:

ToolApproachReported SavingsBest Fit
Claw Compactor (open-compress/claw-compactor)14-stage deterministic pipeline (AST-aware code, JSON, diffs, logs) with zero LLM cost15–82%Mixed code/log/JSON contexts with reversible compression
Kompact (npow/kompact)Drop-in HTTP proxy with 8 transforms (schema selection, observation masking, prefix alignment)40–70% on tool schemasExisting agents where you can't change client code
LLMLingua / LLMLingua-2 (Microsoft)Small LM token scoring to drop low-information tokensUp to 20× (trade-off on quality)Long-document RAG compression

Quick Daily Checklist​

  • Keep a concise CLAUDE.md / GEMINI.md (~50–200 lines) and move specialized playbooks into on-demand SKILL.md files.
  • Delegate broad searches and screenshot reviews to subagents.
  • Compact between phases; clear between unrelated tasks.
  • Right-size models instead of running max-reasoning on every prompt.

Related showcase: Agentic Dev Environment & AI Coding Playbook