Chat is one request, one response — maybe 500 tokens. An agent is a loop. Each iteration re-sends the full context. Five mechanisms compound: re-sent context, tool-call fan-out, retry loops, quadratic attention, context rot.
A single runaway loop — the model retries a failing tool 500 times, each retry re-sending context — can burn thousands of dollars in an afternoon. Pi has no defense against this.
Layer 1: LLM inference (~20% TCO) — the token bill. What most teams track.
Layer 2: Context management — storage, retrieval, re-injection. The 62% re-sent-context problem.
Layer 3: Retrieval / RAG — embeddings, vector queries, reranking.
Layer 4: Orchestration / eval / governance — the "hidden 80%" infrastructure most teams don't budget.
The trap: optimizing only Layer 1 (switching to a cheaper model) addresses 20% of cost. The majority lives in Layers 2-4, and most teams don't instrument it.
An interceptor wraps every provider call. It tracks cumulative token usage and enforces budget at three levels:
Some spending decisions need a human. Three tiers — escalate intervention at each:
Alert fatigue trap: HITL at every turn or every 10% produces rubber-stamping. Set thresholds at meaningful decision points — one or two per session.
Anthropic prompt caching gives a 90% reduction on cached tokens. Your system prompt + tool definitions are re-sent on every call — caching them turns 200,000 billable tokens (across 100 calls) into 20,000.
The cache-killer: caching is prefix-based. A timestamp, session ID, or per-request UUID in the first tokens invalidates the cache. Frameworks that inject dynamic IDs (some LangChain configs) silently destroy the 90% discount. Audit your prefix.
The fix: put static content (system prompt, tool defs) first. Move timestamps, IDs, and per-request data AFTER the stable prefix. One change, up to 90% savings, zero quality loss.
Cost-aware selection: annotate each tool with a cost estimate. The orchestrator routes to cheaper paths when multiple tools could accomplish the same goal. At throttle threshold, skip expensive tools.
Model routing is the same principle: a 27x price spread exists between GPT-4o and GPT-4o-mini. RouteLLM (UC Berkeley) cuts cost 85% at 95% quality by routing cheap queries to cheap models.
The correct framing: cost optimization must hold success rate constant (or improve it) while reducing cost.
The trap is in the throttle action. When you switch from the strong model to the cheap model at 80% budget, the cheap model may fail. Throttle must be paired with a quality check — if output confidence is low, fall back to the strong model for that turn.
Pi (DD-01): call model → tools → repeat. No budget tracking, no throttle, no breaker, no HITL. Runs until done, model stops, or budget burns.
Cost-aware: meter.check → call model → meter.record. Budget tracked per session. Throttle at 80%. Rollback at 100%. Circuit-break on storms. HITL at thresholds.
The cost-aware loop is a wrapper, not a rewrite. You add the @token_meter interceptor at the provider-call boundary. The loop structure stays the same. This is how you turn Pi's personal-scale loop into an enterprise-scale loop.
Course 4 Module E03 (Cost-Aware Agent Architectures) builds the fleet version: per-tenant budgets, chargeback, cost dashboards, the hidden-80% instrumentation.
Lab: build a TypeScript cost-aware loop wrapper with TokenMeter, throttle/rollback/circuit-break, HITL simulation, and a runaway-loop demo.