← Jev From First Principles

Decisions Over Time

Do several questions over one state share work, and what happens to that sharing when the state changes? Token accounting under assumed costs, plus an exploratory look at drift and stale answers.

Design chapter. this chapter computes ANALYTICAL token counts under assumed costs. No model and no inference engine runs. Every number is a consequence of the assumed state length, question length, per-call overhead and batch-scaling exponent, which are swept and stated next to the results. None of it is a measurement of Prompt Cache, SGLang, or any engine.

The question

A decision request in the vendor’s model is one state, several questions, answered in one call. The shared state is the selling point: ask several questions over one state, and the work is shared. Chapter 26 recorded the graph around this. This chapter asks the arithmetic question the claim rests on: do several questions over one state share work, and does that matter?

The tempting answer — “yes, because the state is encoded once” — is exactly what must be decomposed. Most of it may be a property of caching, which any provider can use, and only the remainder is a property of decision models.

The hypothesis and prior work

Our starting hypothesis is that prefix caching delivers most of the saving for any provider, and shared-state heads add a smaller further saving at the cost of generality.

Three sources frame it. Prompt Cache uses arXiv v2, §5.2–§5.5 and Table 1; SGLang uses arXiv v2, §6.2. Their reported measurements were checked in those primary versions; Caruana (1997) is cited at summary level.

Source Mechanism Is reuse exact? What it reports Consequence for this chapter
Prompt Cache, Gim et al. (2023), §3, §5 Reusable prompt modules declared in a markup language; their attention states are precomputed and reused wherever the module appears No, approximate. Each module is encoded attending only to itself; the paper says this “does not necessarily reduce output quality” but can hurt semantically dependent modules (§3.3, §5.3), and offers “scaffolding” at extra memory cost Time-to-first-token lower by roughly 1.5-10x on GPU and 20-70x on CPU depending on where modules live (§5.2; the abstract’s “8x and 60x” and the introduction’s ranges differ). Per-token decoding is unchanged (§5.4). Memory grows linearly with cached tokens (§5.5) The saving is a cache. It reduces the cost of the state, not of the questions, and it is an approximation whose accuracy varies by task (some LongBench scores fell, e.g. Llama2 7B passage retrieval 7.50 to 4.25, Table 1)
SGLang, Zheng et al. (2023), §3, §6 RadixAttention keeps KV caches in a radix tree over token sequences, evicts the least recently used leaves, and schedules requests to share prefixes Prefix reuse is exact in principle: KV values depend only on prefix tokens (§3). The paper does not give a dedicated output-equivalence analysis Up to 6.4x throughput and 3.7x lower latency against other systems (§6.2); cache hit rates 50-99% across benchmarks, and 52-74% in a month of chat-arena traffic An engine optimisation that any provider’s calls can use, with no model change. A change early in the prompt invalidates everything after it
Caruana (1997) One network, several output heads on a shared representation n/a (a training idea) Multitask learning can improve generalisation (summary-level only) The shared-heads scheme is a modelling choice, distinct from either cache

Two details change the chapter’s emphasis. First, Prompt Cache’s reuse is approximate and SGLang’s is prefix-exact, so “caching” is not one thing: the two have different failure modes when the state changes, which the section on time below uses. Second, both papers measure time to first token or throughput on real engines, and neither measures decision accuracy of the sort this book cares about. The accounting here counts tokens, which is an assumption-laden proxy for those quantities.

The accounting

benchmarks/shared_state/accounting.py counts tokens under four schemes. The assumptions: state length L, question length q, per-call overhead, and (for shared heads) a head_cost.

from benchmarks.shared_state.accounting import account  # type: ignore[attr-defined]

L, Q, K, HEAD = 1000, 64, 4, 8
# 1. Token counts for k questions over one state (ANALYTICAL).
print("1. tokens processed for k questions over one state (ANALYTICAL)")
print(f"   assumptions: state L={L}, question q={Q}, k={K}, head_cost={HEAD}")
for r in account(K, L, Q, HEAD, rate=1.0, overhead=0.0):
    print(f"   {r.scheme:<14} {r.tokens:>6} tokens ({r.ratio_vs_independent:.2f}x)")
# 2. The saving reproduced by prefix caching, without any shared-state model.
print("2. what prefix caching already buys (ANALYTICAL)")
a1 = account(K, L, Q, HEAD, 1.0, 0.0)
ratio = {r.scheme: r.ratio_vs_independent for r in a1}
print(f"   prefix_cached is {ratio['prefix_cached']:.2f}x; shared_heads is {ratio['shared_heads']:.2f}x")
print("   -> most of the saving is prefix caching; heads add a small increment")

The walkthrough prints:

1. tokens processed for k questions over one state (ANALYTICAL)
   assumptions: state L=1000, question q=64, k=4, head_cost=8
   independent      4256 tokens (1.00x)
   prefix_cached    1256 tokens (0.30x)
   shared_heads     1032 tokens (0.24x)
   batched          1256 tokens (0.30x)
2. what prefix caching already buys (ANALYTICAL)
   prefix_cached is 0.30x; shared_heads is 0.24x
   -> most of the saving is prefix caching; heads add a small increment

The sweep

run_ch27.py sweeps k over {1, 4, 16} and L over {1000, 8000, 32000} tokens and writes results/ch27.jsonl (mode: analytic; assumptions in every row). At k=16:

L independent prefix_cached shared_heads batched
1,000 17,024 (1.00×) 2,024 (0.12×) 1,128 (0.07×) 2,024 (0.12×)
32,000 513,024 (1.00×) 33,024 (0.06×) 32,128 (0.06×) 33,024 (0.06×)

What the results show

  1. Prefix caching captures most of the saving. At L=1,000 the cached scheme is 0.12× and the shared-heads scheme 0.07×; at L=32,000 both are ≈0.06×. The gap between them shrinks as the state grows, because the state (processed once either way) dominates. Most of the “shared state” saving is a cache any provider can use. P3, the chapter’s central claim, holds under this model.

  2. The shared-heads increment is small and assumption-bound. At L=1,000 it buys 0.05× more (0.07 vs 0.12), and only because head_cost=8 was assumed against q=64. Set head cost equal to the question length and the two schemes coincide. The shared-heads advantage is an architecture position (Caruana multitask; Chapter 13’s probes), not an inference fact. P2 holds only under its own assumption.

  3. Batching and prefix caching coincide in token count here. They differ in scheduling, not in how many tokens pass through the model; that difference is an engine detail outside this accounting.

  4. The trade is generality. Prefix caching needs no model change; shared heads require the multi-headed architecture. The vendor’s “one state, several questions” is on the cache side of the line, and on the modelling side only to the extent heads exist (P1-P4, all ANALYTICAL).

Wrong / Correct. Wrong: “The shared-state advantage proves decision models share compute.” Correct: “Under this analytic model, most of the saving is prefix caching, which any provider can use; the shared-heads increment is a small, architecture-dependent remainder.”

A design lead from outside the book: the README of the open-source project gutsy (its results not reproduced) describes state caching and runtime shortlisting of long option lists. Those are the two levers this chapter’s accounting separates, caching the shared state and reducing the options a decision reads, and it describes weaker CPU latency on long states. No universal single-pass latency claim follows from it.

Grouped bars at 1k and 32k context compare independent calls, prefix caching and shared heads. Prefix caching accounts for most of the saving.
Figure 27.1. Most of the shared-state saving is an inference-engine optimisation, not a model property. The remainder is architecture-dependent and small.

When the state changes

The title says “over time”, and the first version of this chapter did not deliver that. Preregistered prediction P4 said that reusing work across changing state returns stale answers, and that staleness depends on how much changed. The original sweep left it unmodelled and recorded “P4 not modelled”. This section adds two small models. They are EXPLORATORY: the sweep was not preregistered, the numbers are consequences of the stated assumptions, and they live in results/ch27-drift.jsonl so the original rows are untouched. Code is in benchmarks/shared_state/drift.py; run python examples/ch27-decisions-over-time/run_ch27_drift.py.

Decision flow for Chapter 27

The diagram separates two things the phrase “reuse” hides. Reusing computation (the KV cache) cannot return a wrong answer by itself, because cached values are only matched on identical tokens; what a change costs it is its saving. Reusing a result can return a wrong answer, and does whenever the key does not include the state.

Compute reuse. The state is 8,000 tokens in 16 equal blocks, k = 4 questions of 64 tokens, and each block changes independently with the stated probability per step. Assuming a warm cache at the start of each step:

per-block change prob. prefix reuse (tokens reused) tokens vs 4 independent calls module reuse (tokens reused) tokens vs 4 independent calls
0 8,000 0.01x 8,000 0.01x
0.01 7,353 0.03x 7,920 0.01x
0.05 5,319 0.09x 7,600 0.02x
0.20 1,944 0.20x 6,400 0.06x

The prefix scheme degrades faster because one changed block discards every block after it: at a 5% per-block change rate, about a third of the state has to be recomputed even though 95% of blocks are unchanged. Module reuse keeps the unchanged blocks, but Prompt Cache’s version of that is approximate, so the lower token count is bought with a quality risk this arithmetic cannot price. The 0.01x at no change reflects the warm-cache assumption: only the four questions are processed.

Result reuse. A cache of decision results, over 2,000 steps with three seeds and an assumed 50% chance that the answer flips whenever the state changes:

per-block change prob. key = question only: hit rate key = question only: stale answers key = state + question: hit rate key = state + question: stale answers
0 0.999 0.000 0.999 0.000
0.01 0.999 0.521 0.847 0.000
0.05 0.999 0.501 0.435 0.000
0.20 0.999 0.500 0.027 0.000

(Stale-answer ranges across the three seeds were 0.496-0.557 at 0.01, 0.475-0.525 at 0.05 and 0.491-0.508 at 0.20.)

The finding is uncomfortable for P4 as written. Staleness does not grow smoothly with “how much changed”. A question-only key never refreshes, so over a long stream the state has almost surely changed from the one the answer was computed for, and the stale share settles at the assumed answer-flip rate (0.5) as soon as there is any change at all. The cache hit rate stays near 1, so nothing in the cache’s own statistics reveals the problem. Keying on the state hash removes staleness entirely and costs exactly the hit rate: at a 5% change rate only 43.5% of requests hit. That trade is what Chapter 26’s state hashes are for. P4 holds in this sense: staleness exists and depends on the key; its dependence on the amount of change is weak once change is nonzero.

What the result means

Modelling idea vs inference-engine idea. Shared heads are a modelling arrangement (one representation, several heads — Caruana); prefix caching is an engine optimisation (Prompt Cache, SGLang). The vendor’s claim straddles the line; this chapter’s accounting places most of the saving on the engine side and says the modelling remainder is exactly the part that must be measured, not assumed.

What to carry forward

Under this chapter’s accounting, processing the shared state once is cache (engine); deciding several questions from one representation is model (heads). Chapter 28 builds the benchmark that would measure which holds for real providers — and its cells for this saving are NOT_OBSERVED, because no engine ran.

Limitations

  • This is ANALYTICAL arithmetic. results/ch27.jsonl is mode: analytic; the latency proxy includes a batch-scaling exponent as an assumption and says nothing about a real engine.
  • No real provider, cache engine, or GPU ran; the Chapter 13 probe-heads shared-state hypothesis is deferred (GPU).
  • Prompt Cache’s latency ranges and Table 1 accuracy example, and SGLang’s §6.2 latency, throughput and cache-hit figures, were checked in the primary versions linked above. Caruana is cited at summary level. Neither engine ran here.
  • The drift section is EXPLORATORY (not preregistered as a sweep) and constructed: the 50% answer-flip rate, the independent per-block change process and the warm-cache assumption are assumptions, and the stale-answer figures are consequences of them, not properties of any provider. Three seeds for the answer cache; the compute-reuse table is closed-form (tests compare it with a Monte Carlo draw).
  • The shared-heads increment depends on the assumed head_cost; swept values are L=1,000 / 8,000 / 32,000 and k=1 / 4 / 16, stated in the rows.

What this chapter establishes

What part of the Jev interface is a model idea and what is an inference-engine idea? Processing the shared state once is an engine idea; deciding several questions from one representation would be a model idea. The split is analytical and awaits measurement.