TokenLedger
JSON API
AI FINOPS ENGINEERING PLAYBOOK

Fixing the AI Agent Token Snowball: Why a 15-Turn Agent Costs 12x More Than Expected

Mathematical breakdown of quadratic context accumulation in multi-turn tool-calling agents and how prefix caching + sliding windows cap burn.

The Quadratic Trap Inside Every ReAct / Tool-Calling Loop

In a multi-turn agent, Turn 1 sends the system prompt + user request. Turn 2 resends Turn 1 plus the tool output. By Turn 15, the model re-reads all 14 prior tool payloads—turning linear steps into quadratic O(N²) input token spend.

Why Prompt Caching Alone Is Half the Fix

Prefix caching slashes the cost of re-reading prior turns by up to 90%, yet verbose raw JSON/HTML tool outputs still degrade attention quality and inflate cache writes. Truncating raw tool payloads before appending them to message history compounds caching savings.

Sliding-Window Rolling Summaries for Long Sessions

Capping active message history to the last 4–6 raw turns plus a compact 400-token structured state summary bounds per-turn input tokens flat even on 40-turn autonomous runs.

Recommended AI Engineering & Cloud FinOps Books

Editorial note: Curated technical books for AI engineers and FinOps teams. Amazon links include affiliate tag dopabrain-20 (no extra cost to you) and help keep TokenLedger 100% free and ad-free. Interested in sponsoring TokenLedger? Open a Sponsor / Partnership thread on GitHub ↗.