Skip to content

Context window economics — when memory beats reloading docs

An illustrative model for comparing the token cost of reloading context with the cost of retrieving a smaller set of documents.

On this page

The hidden bill on every CLI agent session

Open Claude Code in a medium repo. Ask a question. Watch it Read the same five files it read yesterday. Ask a follow-up. Watch it Grep the same constants. The model is smart, the tool calls are cheap individually, and the per-turn waste hides inside a green checkmark — so nobody adds it up. This post models the trade-off using illustrative numbers.

One possible approach is to index content and retrieve relevant chunks instead of reloading every file on every turn. The following model illustrates the trade-off; it is not a benchmark or a claim that retrieval always saves money. The worked assumptions produce a breakeven below one turn.

The naive baseline: re-read everything

Call this the “stateless agent” baseline. Per turn the agent reloads:

  • CLAUDE.md and any @import-chained memory files — call it M tokens
  • a handful of source files for context — call it S tokens
  • recent diffs, git log, command output — D tokens

Per turn, input tokens: C = M + S + D. Across N turns: N · C. Cost: N · C · p_in, where p_in is the input-token price.

For illustration, assume an input price of $15 / 1M tokens and a cached-input price of $1.50 / 1M. These are hypothetical rates for the arithmetic below, not a provider quote. Actual rates, cache rules, workload and token counts vary.

A worked number for a 50k-line monorepo, ten-turn session, ~30k input tokens per turn (10k from rules + 15k from re-read files + 5k from logs):

text
input tokens   = 10 turns × 30_000 = 300_000
cost (cold)    = 300_000 / 1_000_000 × $15  = $4.50
cost (warm)    = 300_000 / 1_000_000 × $1.50 = $0.45

Forty-five cents per warm session sounds fine. Multiply by 12 engineers × 8 sessions/day × 21 working days and you’re at $907/month, and that’s the cached number. Actual spend depends on cache hits, request size and frequency; measure your own workload.

The memory-backed alternative

Swap the per-turn re-read for a per-turn semantic query.

  1. Index once. A background indexer chunks every file in the repo (200–400 tokens per chunk), embeds it at an assumed $0.06 / 1M tokens (amortised across sessions), writes the rows into the same memory table.
  2. Query per turn. At the point where your application builds context, embed the query and retrieve the top-k chunks. k = 6 and chunk size 300 tokens means ~1.8k tokens of injected context vs the 15k of “re-read the whole file” — an order of magnitude shave on the source-file portion of C.
  3. Re-index on change. Your application detects file changes and updates the index. Include that work and its cost in your measurements.

The per-turn input now looks like C' = M + Q + R, where Q is the embedding query (a few hundred tokens of conversation tail) and R is the retrieved chunks. In our worked example, C' ≈ 10_000 + 800 + 1_800 = 12_600, down from 30k. This assumes the retrieved chunks provide the context needed for the task.

The breakeven, plotted as algebra

Per session, the memory-backed path pays:

costmemory=NC′pin+EqueryNpembed+EindexFpembedRamort\mathrm{cost}_{\mathrm{memory}} = N C' p_{\mathrm{in}} + E_{\mathrm{query}} N p_{\mathrm{embed}} + \frac{E_{\mathrm{index}} F p_{\mathrm{embed}}}{R_{\mathrm{amort}}}

E_index is total tokens indexed, F is the change-frequency multiplier (how often files re-embed), R_amort is how many sessions the index serves before a full rebuild. Both the amortization period and embedding price are assumptions; index rebuilding and updates can materially change the result.

The naive path pays:

costnaive=NCpin\mathrm{cost}_{\mathrm{naive}} = N C p_{\mathrm{in}}

Breakeven is the turn count where the constant-cost ramp-up of the memory path is paid off by the smaller per-turn slope:

Nbreakeven=EindexFpembed/Ramort(C−C′)pin−EquerypembedN_{\mathrm{breakeven}} = \frac{E_{\mathrm{index}} F p_{\mathrm{embed}} / R_{\mathrm{amort}}}{(C-C')p_{\mathrm{in}} - E_{\mathrm{query}}p_{\mathrm{embed}}}

Plug the worked numbers (C - C' = 17_400 input tokens saved per turn, E_query = 800, p_in = $15/Mtok, p_embed = $0.06/Mtok, E_index = 5_000_000, R_amort = 200, F = 1):

text
numerator    = 5_000_000 × 0.06 / 200 / 1_000_000  ≈ $0.0015
denominator  = (17_400 × 15 - 800 × 0.06) / 1_000_000 ≈ $0.260952 per turn
N_breakeven  ≈ 0.00575 turns

Under these assumptions, the modeled per-session index cost is recovered within the first turn. This is an arithmetic result, not evidence that the system has been built or will save money in practice. If the per-turn saving is zero or negative, there is no positive breakeven in this model.

Implement and measure it

The algebra does not implement an indexer, hook, or retrieval system. The RedDB vector search walkthrough demonstrates native vector insert and retrieval against a tested release. An application still needs to chunk files, generate embeddings, keep its index current, apply access controls, and decide what context to send to the model.

Compare measured input tokens, cache hits, embedding and index maintenance costs, latency, and task quality before and after the change. A smaller prompt is not automatically a better answer; check whether retrieval misses important context or returns stale content. Include updates made outside the agent in the freshness test.

When this is the wrong call

Three cases where the naive re-read still wins:

  • Tiny repos. If the saved context is small, indexing overhead may exceed the savings. Measure against your existing cache behavior.
  • Single-shot scripts. A short session may not amortize the cost of building and maintaining an index.
  • High-churn refactors. When 40% of files change per session, reindex cost climbs and R_amort collapses. In that mode, fall back to plain Read for the touched tree and only query memory for the rest.

The result applies only to the assumptions above. Measure your own prices, cache behavior, retrieval quality and index-update cost before relying on it.

Related reading

Get the next dispatch.

New essays on multi-model storage, RAG, agents, security. No spam.

No spam. Unsubscribe anytime.