KDCube
← Engineering
KDCube Engineering · Deep Dive

How the model input is cached — and kept fresh

The expensive prefix is reused; the freshest signals stay fresh; junk drops out early — four moving cache points, an always-fresh tail, marked cold turns, pruning, and compaction.

22 June 2026Engineering10 minExperienceWorkshop Manual
prompt cachingcache pointspruningcompactioncold turneditable zoneTTL

Caching only works on a prefix that is byte-for-byte identical to last time. So the whole strategy comes down to one design choice: put the stable things first, the volatile things last.

In #1 we mapped the shape of the model input: a stable per-agent front (instructions · tool catalog · skill catalog), a timeline that grows oldest → newest, and a volatile tail at the very end. That shape wasn’t an accident. It’s what makes the input cheap to reuse and safe to keep fresh at the same time. This is #2 of three: what happens across calls — how most of the input is reused instead of re-sent, how the freshest signals stay fresh, and how the agent can drop junk early without throwing away the cache. The trick isn’t pretending context never changes. It’s shaping what stays visible.

01Why caching is even possible here

Sending a long input to the model is expensive. The instruction envelope, the catalogs, and the older history don’t change between one round and the next — so re-sending them every time is pure waste. Prompt caching lets the model reuse an exact prefix it has already seen, and only pay full price for what’s new after it. The catch: change one thing near the front and everything after that point has to be re-sent. The expensive part is reused; the freshest part is never cached, so it can change every round without a thought. To make that reuse precise, the runtime drops cache points along the way.

02The tail is never cached — on purpose

THE RULE

The tail is never cached. The sources pool and the live view sit at the very end, after every cache point, and they are rebuilt every round.

This is deliberate. Fast-moving state — the current budget, a live user interrupt, a fresh projection of the workspace — has to be current on every call. If it were baked into the cached prefix, you’d face a bad choice: either churn the expensive prefix every round to update it, or let the agent read stale state. By keeping that state in an uncached tail, you sidestep both. The prefix stays calm; the tail stays fresh. (The tail’s own structure is the subject of #3.) Caching by shaping visibility, not by freezing the input.

03The four cache points

A cache point is a boundary that says “everything up to here is stable; reuse it.” There are four, and where each sits is the whole design.

FULL MODEL INPUT — ALL FOUR CACHE POINTSInstruction envelope — runtime + agent/domain instructions · tool & skill catalogsthe stable per-agent prefix from #1① envelope / prefix pointTimeline… prior turns …② previous-turn point[ current turn ]round N−4 — last block③ pre-tail point (N−4)round N−3round N−2round N−1the editable zone(hide junk here — prefix untouched)round N — last block④ tail pointTail — sources pool · live viewrecomputed every roundafter ④: NEVER cached① REUSES THE EXPENSIVE FRONT · ② REUSES ACROSS TURNS · ③+④ ARE THE MOVING FRONTIER
① reuses the expensive front; ② reuses across turns; ③ and ④ are the moving frontier.
  1. Envelope / prefix point — anchors the whole instruction envelope: the runtime and agent/domain instructions plus the tool and skill catalogs. This is the stable per-agent prefix from #1, and the boundary that lets the entire front be reused across calls.
  2. Previous-turn point — right after the last block of the prior turn. The cross-turn anchor: once a new turn begins, the whole conversation up to here can be reused for the rest of the turn.
  3. Pre-tail point — the last block of round N−4, a deliberate extra checkpoint set a few rounds back from the newest activity (it appears only once the turn has enough rounds). This is what opens the editable zone.
  4. Tail point — right after the most recent stable round, the last block in the cacheable stream.

Everything after point 4 — the sources pool and the live view — is the uncached tail; no cache point ever reaches it.

04The extra checkpoint lets the agent forget garbage early

The pre-tail point earns its keep. Without it, the agent’s only safe cache boundary would be at the very edge of activity — no room to drop anything without busting the cache. With the extra checkpoint a few rounds back, there’s a small editable zone between it and the tail. If the agent pulls a huge, useless result — a giant page it shouldn’t have fetched — it can hide that block: the bulky payload leaves the model’s eyes, a short note takes its place, and the original stays recoverable by its path. Because the hide happens below the pre-tail point, the cached prefix above it is untouched.

05Cache points move with the turn

The previous-turn point is fixed once the turn starts. The other two are not — they march forward as rounds accumulate.

CACHE POINTS MOVE WITH THE TURNEARLY IN THE TURNprevious-turn point (fixed)round 1round 2tail pointnot enough rounds yet fora pre-tail pointLATER IN THE TURNprevious-turn point (fixed)round 1 …round N−4pre-tail point ↓ moved downround N−3 · N−2 · N−1round Ntail point ↓ moved downtail: sources · live viewtail: sources · live viewTHE EDITABLE ZONE IS A WINDOW THAT TRACKS THE TURN — THE CACHED PART GROWS, THE FRONTIER STAYS NEAR THE ACTION
The editable zone is a window that tracks the turn’s structure.

Early on there may not be enough rounds to place a pre-tail point at all; later it appears, and both it and the tail point slide downward as the agent works. That’s what you want in a live loop: the cached part keeps growing while the editable frontier stays near the action.

06When the user changes the agent: cold turns, marked and governed

Everything above assumed the front of the input holds still — and between rounds it does. But since per-user agent customization, the user can change the front itself: toggle a tool group, an MCP server, a namespace, or a skill from the chat composer, or switch the model. The “same bytes across calls” is exact — per selection: the front is stable precisely as long as the selection that produced it stands.

TWO WAYS THE FRONT GOES COLDSAME MODEL, NEW CATALOG BYTESSelection toggle→  a tool group / MCP server / namespace / skill flips→  the catalogs render inside the one system block→  that block’s bytes change — system comes first→  every cache point below it is invalidated→  the history cannot survive a catalog changeONE cold turn — then warm again, same namespaceNEW MODEL, SAME BYTESModel switch→  provider caches are per model→  the newly picked model has no cache at all→  nothing to reuse — not even the envelope→  the rebuild happens in that model’s→  own cache namespacecold — then warms in the new namespacethe rebuild turn is charged at full input rates while the cache re-writes — the next turn is warm againMARKED, NOT SILENT: [CACHE] IN THE LIVE VIEW · cache_cold_turn IN ACCOUNTING · THE USER HOLDS THE APPLY POLICY
A toggle colds the prompt for one turn; a model switch starts a different cache namespace.

The cold turn is marked, not silent. Each conversation keeps the last-applied selection snapshot alongside its warmness signal (cache_last_touch_at plus the stored TTL). When a changed selection applies on a warm conversation, the runtime sets a cold-turn marker: the agent sees a one-line [CACHE] section in the live view, the decision call’s accounting metadata carries cache_cold_turn, and a log line correlates the change with the next cache attempt. Accounting already records cache_creation_tokens and cache_read_tokens on every call — the marker joins that rebuild premium to the action that caused it, one identifiable component within the turn’s spend sum.

And the user decides when to pay. The user pays for the cache, so the user holds the policy for selection changes: accept, confirm (the platform default), defer_cold, or defer_conversation — admin config sets the default and bounds the allowed set. Under confirm, the composer turns a costly change into an inline choice at the moment it matters: apply now, from the next conversation, or when the cache is cold — where applying is free. A deferred change parks as a pending delta the runtime promotes when its trigger fires.

07Keeping it small over time: pruning vs. compaction

Reuse handles cost within a warm window. Two other mechanisms handle the input getting large over a long conversation, and they’re different in kind.

TTL pruning is the freshness rule. After a cache window goes cold, older payloads are hidden behind short replacement text — the recent turns stay visible, the old bulk doesn’t. Crucially, pruning hides payloads, not availability: the hidden block is still there, still addressable by its path, still re-openable on demand. Compaction is the hard ceiling. When the visible input genuinely approaches the model’s window (today, roughly 90% of the budget), the runtime summarizes a stretch of older blocks into a single summary block and renders only from that summary onward. The summarized turns still survive behind their paths and turn indexes.

TTL pruningCompaction
Whena cache window goes coldinput nears the model’s hard limit
What it doeshides old payloads, keeps recentreplaces an old stretch with a summary
Stays wholehidden blocks, reachable by pathsummarized turns, reachable by path/index
Rolekeep the hot prompt lightthe ceiling that can’t be crossed

Neither one is amnesia. Both keep logical availability even as they shrink what’s visible.

08Render order is part of the design

All of this happens in a fixed order each render, and the order is the point.

EVERY RENDER, IN THIS ORDER — THE ORDER IS THE POINT1apply TTL pruninghide cold payloads behind short replacement text2compact if over the ceilingsummarize the oldest stretch into one summary block3apply hidden replacementsthe junk the agent chose to drop4recompute cache pointson the stream as it now stands — never on bytes about to move5append sources + live viewthe uncached tail, always lastSTABLE FIRST, VOLATILE LAST — MAXIMAL REUSE, BY CONSTRUCTION
Cache points are computed last among the stable steps, on a stream that’s already settled.

Cache points are computed last among the stable steps, on a stream that’s already settled — so they never get placed on bytes that are about to move. And the tail goes on at the very end, keeping the uncached part truly at the bottom. Stable first, volatile last: maximal reuse, by construction.

09Recovery is deliberate, not a lucky cache hit

Because pruning and compaction shrink what’s visible, the agent has a real path back to what left the prompt — and it’s a deliberate route, not a hope that something’s still cached. A visible path reads back directly. A summary points at a turn index, which lists the exact refs to re-open. Topic-only? A search returns the refs or the index to read. The summaries are the semantic map; the logical paths are the handles; the stored turn logs and artifacts are the source of exact data.

This is why the honest promise isn’t “the model never forgets.” It’s stronger and more truthful: the model can forget aggressively when it should, and still recover what matters, because the system leaves enough path-and-summary structure behind.

10The point: availability, not perfect recall in the prompt

Caching here isn’t about freezing context. It’s about keeping the expensive prefix reusable, the freshest signals fresh, and everything that scrolls out of view still reachable. The input’s shape — stable front, growing middle, volatile tail — is what makes all three possible at once.

Read more

KDCube Engineering
22.06.2026 · updated 23.07.2026