Open source: context compression for coding agents, 63% less tool output ⭐ optscale-kompressor on GitHub →
AI Gateway

A Long Build Shouldn't Cost You Your Cache: Keep-Warm and Adaptive Cache Lifetime in OptScale AI Gateway

December 3, 2026

~ 9 min

By Nick Smirnoff
On this page
    Agents losing their cache mid-build?
    Book a Demo

    Coding agents spend a lot of their time waiting: on builds, test suites, deployments, background jobs. Every wait longer than five minutes quietly throws away the provider's prompt cache, and the agent's next request pays to write the whole conversation again. OptScale AI Gateway can now keep that cache alive through the wait, and shows you whether doing so paid off.

    ‍

    The five-minute problem

    Prompt caching is what makes long agent conversations affordable. Each request re-sends the full conversation, but the provider has already processed most of it, so that part is billed as a cache read at a fraction of the normal input price.

    The catch is the cache's lifetime, or TTL. Anthropic keeps a cache entry for five minutes from its last read. A chat with a person rarely notices. An agent does: it hands a ten-minute build to its shell and sends nothing until the build finishes. Five minutes in, the entry lapses. When the tool result finally goes back to the model, nothing is cached, and the entire conversation is written to the cache from scratch.

    The price difference is large. Relative to a model's base input price:

    What the request does Price multiplier
    Reads the prompt from cache 0.1× on most models; 0.05× on Opus 5.5 and Sonnet 5.5
    Writes it to the 5-minute cache 1.25×
    Writes it to the 1-hour cache 2×

    These are Anthropic's published multipliers. At the standard rate, a rewrite costs 12.5 times what a read would have, for exactly the same tokens. On Opus 5.5 and Sonnet 5.5, it costs 25 times. On Claude Sonnet 4.6, priced at $3 per million input tokens, a 100,000-token conversation costs about $0.03 to read and about $0.375 to rewrite. An agent that pauses dozens of times a day does this over and over, and the longer the conversation, the more each pause costs.

    ‍

    Keep-warm: a one-token ping that resets the clock

    With keep-warm switched on, the gateway watches for waits and bridges them:

    1.        A response ends in a tool call. The gateway knows the client is now busy running something, and starts a timer for that conversation.

    2.        Every 4.5 minutes, it sends a refresh. The refresh is the exact prompt that was just sent, to the same endpoint with the same credential, with only two changes: max_tokens is set to 1 and streaming is turned off. The provider reads the prompt from cache, which resets the five-minute clock.

    3.        The agent returns. Its next request reads the conversation from cache, as if the pause never happened. The gateway cancels the schedule at once.

    Why 1 rather than 0? Anthropic now also accepts max_tokens: 0 for pre-warming, but rejects it on requests with extended thinking, structured outputs or a forced tool choice, which are common in agent traffic. A ping changes nothing else in the request, so the gateway uses 1, which every pinged request accepts.

    In the example above, a ten-minute build needs two pings at roughly $0.03 each. The return then reads the cache instead of rewriting it: about $0.06 spent to avoid about $0.345 of extra cost.

    We first built this mechanism into the open-source OptScale Kompressor and measured it on Claude Code with Opus 5.5. There, keep-warm alone took about 11% off the Anthropic API bill after paying for its own pings. The gateway now does the same for every client that reaches Anthropic through it, with nothing to install on the client side.

    Keep-warm also covers waits that do not end in a tool call. If a turn finishes while a background command, a monitor task or a scheduled wake-up is still outstanding, the gateway keeps the cache warm until the client reports the task done, stopped or canceled, or until the ping cap is reached. For a scheduled wake-up, it sends only as many pings as the reported delay needs.

    Bounded by design

    A refresh is a real, billed request, so the feature is built to stop early rather than run on:

    •           A hard cap. At most 11 pings per wait by default. They span about 50 minutes, and the last one keeps the cache warm for about 54. At Anthropic's standard price ratios, one avoided rewrite pays for roughly eleven pings of the same prompt, which is about where the default cap sits. On Opus 5.5 and Sonnet 5.5, where reads cost half as much, it pays for about twenty-four. For shell commands the cap is tighter still: it covers the command's timeout and no more, so a two-minute command gets a single ping.

    •           It stops when there is nothing left to save. A schedule ends on the next real request, on a failed refresh, or the moment a ping reads nothing from the cache.

    •           It only starts when there is something to keep. Prompts below 1,024 tokens, responses that neither read nor wrote the cache, requests with a fixed thinking budget, and conversations already on one-hour entries are never pinged.

    •           No side effects. A refresh does not execute gateway tools, rerun compression, retry or fall back to another provider. Requests that use remote MCP servers or provider-defined tools are excluded altogether.

    •           Same governance as everything else. Each refresh revalidates the provider key and passes the gateway's governance checks before sending.

    Per-thread schedules. A main agent, its subagents and small side calls share a conversation but use different system prompts. Each gets its own schedule, so a long subagent run does not let the main prompt expire.

    Keep agent context cached through every wait

    Turn on keep-warm for your Anthropic provider and track net savings in one tab. Free for up to 10 seats

    Adaptive cache lifetime: pay for an hour only where it helps

    Anthropic also offers a one-hour cache. It survives almost any tool wait, but writing to it costs 2× the input price instead of 1.25×. Putting every conversation on it means paying that premium for the many conversations that never pause.

    The gateway's Cache lifetime setting has three options:

    •           Default (5 minutes) leaves the client's cache markers untouched.

    •           1 hour upgrades the cache markers a client already sends to the one-hour lifetime.

    •           Adaptive starts every conversation on the five-minute cache and moves it to the one-hour cache after it has been idle for more than five minutes. Conversations that never pause stay on the cheaper writes; the ones that do are promoted.

    In the same Kompressor study, a replay of earlier Anthropic runs showed the difference: putting every conversation on one-hour entries cost 2.3% more overall, while switching adaptively cost 7.7% less.

    The gateway only changes the lifetime of markers the client already set. It never adds cache markers and never touches tool inputs.

    Keep-warm and cache lifetime work together or separately. Keep-warm suits waits of a few minutes to under an hour on the five-minute cache. Adaptive lifetime suits conversations with repeated or longer pauses.

    ‍

    Turning it on

    Both settings live on the provider. Open your Anthropic provider, find Prompt caching, choose a Cache lifetime and switch on Keep cache warm. Everything is off until you opt in.

    The ping interval and caps use the gateway's defaults: a refresh every 270 seconds, up to 11 per wait. If you run the gateway component with its own configuration file, the same options and the advanced limits are available in the provider's network configuration:

    ‍

    {
      "network_config": {
    "prompt_cache": {
      "ttl": "adaptive",
      "keep_warm": true,
      "interval_seconds": 270,
      "max_pings": 11,
      "background_pings": 11
    }
      }
    }

    Two requirements: conversation tracking must be configured, and requests need an authenticated virtual key or user identity. The gateway keeps separate cache state per caller, conversation, thread, model and credential, and will not guess when identity is missing.

    ‍

    Did it pay off? The Keep-warm tab

    Pings cost money, and they do not always land. An agent may never come back, or may come back with a changed tool list that invalidates the cache anyway. The gateway therefore reports the result instead of assuming it.

    Every refresh is logged as its own billed request, marked as keep-warm, and its tokens are included in normal usage, pricing and budget totals. On top of that, Optimizations → Keep-warm answers the questions that matter:

    •           Net savings and return on ping spend: rewrites avoided minus what the pings cost. Above 1×, keep-warm is paying for itself.

    •           Wasted ping spend: pings that found the cache cold, pings before a changed prompt, pings before a return that came too late, and pings after a conversation ended.

    •           Cost by cache option: the same traffic priced three ways (keep-warm as observed, no keep-warm, and a one-hour cache) with a one-line verdict on which is cheapest.

    •           Net savings by ping limit: the period's ping chains replayed under a lower limit, so you can see whether a cap of, say, 4 pings would have netted more than 11.

    •           Returns by idle gap and a per-conversation timeline of requests and pings, for when you want to see exactly what happened.

    The analysis is deliberately conservative. A return is credited to keep-warm only when it came back after the cache would otherwise have expired, missing price data is shown as unavailable instead of zero, and the tab uses token counts and logged costs only, never request content.

    ‍

    For OpenAI models: stable cache routing

    OpenAI caches prompts automatically, but on models before GPT-5.6, a cache hit depends on how the request is routed. With openai_config.auto_prompt_cache_key enabled in the gateway's configuration for the standard OpenAI provider, the gateway adds a stable prompt_cache_key per caller and conversation for supported models before GPT-5.6, when the client did not send one. A key the client sets always wins, and the option makes no extra provider calls. As OpenAI's prompt caching guide notes, a key steers routing but does not guarantee a hit. GPT-5.6 and later are left out on purpose: OpenAI routes them for caching on its own, and there a key only separates cache accounting between users or customers.

    ‍

    Good to know

    •           Savings depend on your traffic. Keep-warm pays when agents pause for more than five minutes and then come back with the same prompt. Start with one provider, then compare total spend, including pings, in the Keep-warm tab.

    •           Pings and one-hour writes are billable. That is the trade, and it is why the feature is opt-in and capped.

    •           Scheduling is per gateway process. If you self-host with several replicas, run one owning replica or have your load balancer route each conversation to the same replica, so two replicas do not refresh the same conversation.

    •           Claude Code friendly. On scheduled-wakeup turns Claude Code adds a per-turn workload field to its billing header block. That block sits before every cache breakpoint, so the change would rewrite the whole prompt. When a prompt-cache policy is active, the gateway removes the field before the request goes upstream.

    •           Anthropic API-key providers only. Refreshes use the provider's configured API key. Requests with forwarded OAuth credentials are not pinged, and Azure, Vertex and Bedrock are not covered.

    ‍

    Summary

    Agents wait, caches expire, and the bill for rewriting context shows up as ordinary input spend that nobody questions. Keep-warm bridges those waits with cheap, capped, fully accounted one-token refreshes. Adaptive cache lifetime reserves the one-hour cache for conversations that need it. And the Keep-warm tab tells you, per model, per key and per conversation, whether the trade was worth it. Both add to the gateway's other cost levers, including cache alignment and token compression.

    Find out what cache expiry costs your team

    Route one Anthropic provider through OptScale AI and compare spend with keep-warm on and off

    Frequently asked questions

    How long does Anthropic's prompt cache last?

    Five minutes by default. Each request that reads the entry refreshes it at no extra charge, and the clock runs from the start of that request, not the end of the response. You can opt into a one-hour lifetime per cache marker by setting ttl to 1h, which raises the write price from 1.25× to 2× the base input price. Reads cost the same either way.

    When is the 1-hour prompt cache worth it?

    When a conversation goes quiet for more than five minutes and then comes back, often enough to offset the higher write price. For traffic that never pauses, the five-minute cache is cheaper, because every request refreshes it for free. That is why Adaptive mode promotes a conversation only after its first long idle gap. In our replay of earlier Anthropic runs, one-hour entries everywhere cost 2.3% more, while switching adaptively cost 7.7% less.

    How much does a keep-warm ping cost?

    A ping re-sends the prompt with max_tokens set to 1, so you pay a cache read for the cached prompt, normal input for anything after the last cache marker, and one output token. Reads cost 0.1× the base input price on most Claude models and 0.05× on Opus 5.5 and Sonnet 5.5: about $0.03 per ping for a 100,000-token conversation on Claude Sonnet 4.6. If the cache has already expired, the ping pays a full write and keep-warm stops. Every ping is logged as its own billed request.

    Is keep-warm the same as response caching in an AI gateway?

    No. Response caching, which many gateways offer, stores a model's answer and returns it when an identical request arrives again. Agent requests are almost never identical, so it rarely fires for them. Keep-warm works on the provider's prompt cache instead: the model still answers every real request, but the long conversation prefix stays billed at the cache-read rate rather than being written again.

    Does keep-warm work with Claude subscriptions, Amazon Bedrock, Vertex AI or Azure?

    It covers Anthropic providers configured with an API key only, because each refresh is sent with that key. Requests that arrive with forwarded OAuth credentials, such as subscription sign-ins, are not pinged, and Bedrock, Vertex AI and Azure providers are not covered. For OpenAI models before GPT-5.6, the gateway can add a stable prompt_cache_key instead.

    Can a keep-warm ping trigger tools or change the conversation?

    No. A refresh re-sends the exact prompt the client already sent, with only max_tokens and streaming changed. It does not execute gateway tools, rerun compression, retry or fall back to another provider, and requests that use remote MCP servers or provider-defined tools are never pinged. Each refresh also passes the same key validation and governance checks as any other request.