Repository navigation
feat(router): opt-in one-hour prompt cache per agent config - #2536
Merged
Merged
Conversation
Agents with a long shared prefix (tools and instructions) whose sessions
or turns arrive minutes apart rewrite that prefix every time the default
five-minute cache expires. An agent config can now ask for an hour.
- Agent config gains prompt_cache_ttl (5m | 1h, the PromptCacheTtl enum)
on create, update, patch and sync (agent.yaml prompt_cache_ttl: 1h).
Any other value is a 400. Unset sends requests exactly as before.
- Anthropic: with 1h, the tools and system breakpoints carry
cache_control {type: ephemeral, ttl: 1h}; the conversation's rolling
breakpoints stay 5m, since a longer TTL must come before a shorter one.
- Usage: message_start's cache_creation.ephemeral_1h_input_tokens is
reported as InputTokensDetails.CacheWrite1hTokens (a part of
CacheWriteTokens) and metered as cache_write_1h_input_tokens, apart
from cache_write_input_tokens. Request.Quantities and Stat.Use count it
as prompt, so fresh input, spend stats and cost sources stay right.
- Pricing: the new meter is a context-tier input meter; vendor.json
carries 2x input for every Anthropic model (Haiku 5.5's long-context
tier included). An entry without the rate, such as one already stored
in a database, prices it at twice its input rate and price.
- Migration 20270301008300 adds agent_configs.prompt_cache_ttl.
- Regenerated openapi.yaml, the Go, Python and JS clients; Go SDK reads
the agent.yaml key. Note left for the other SDKs.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015WLTK4LXi6LyXHjpnjuD4C
Contributor
Author
|
/jeffery review |
|
Thanks for asking Jeffery for review. I'll stay on this PR until I can approve it. How it works
Commands (each as a whole comment)
|
…pt_cache_ttl The CI generator checks of those four SDKs failed on the new PromptCacheTtl schema. The data-move reference test lists agent_configs.prompt_cache_ttl among the columns that hold no row's id. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015WLTK4LXi6LyXHjpnjuD4C
There was a problem hiding this comment.
Round 1 on c40696168237 · 🆕 1 new
Still open: 1 should-fix. Full list on the checks tab.
A client written before the field, such as an older dashboard, saves an agent config with a PUT that does not send it. That reset a stored 1h to the provider's default. updateAgentConfig now keeps the stored value when the body leaves it out, as it does for routing_policy, identity and the other fields older clients do not send. Left out on a create, it is still the provider's default. Clients regenerated for the field's new doc. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015WLTK4LXi6LyXHjpnjuD4C
Contributor
Author
|
/jeffery review |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
An agent with a long shared prefix (many tools, long instructions) whose sessions or turns arrive minutes apart pays a full cache write every time Anthropic's default five-minute cache expires. A one-hour TTL keeps that prefix warm across the gaps. Its writes bill at 2x input rather than 1.25x, so it is opt-in per agent, and the router has to meter those writes apart to bill them right.
What
Setting.
prompt_cache_ttlon an agent config, thePromptCacheTtlenum (5m,1h). It is accepted onPOST,PUTandPATCH /v1/agents/configsand on a sync, soagent.yamltakes a top-levelprompt_cache_ttl: 1h. Any other value is a 400. Left out on a patch or sync, the stored value stays. Unset means requests go out byte-for-byte as before; a test compares the raw bodies.Request.
store.AgentConfig.PromptCacheTTLgoes tosession.Spec, then toLLMOverwrites(),options.LLM.PromptCacheTTL,ResponseParams.OverwriteandPromptCacheOptions.TTL. That is the path the existing per-request options already take, for both text and voice sessions. With1h,breakpoints()setsttl: "1h"on the tools and system breakpoints only. The conversation's rolling breakpoints stay 5m, because Anthropic takes a longer-lived breakpoint only before a shorter one. OpenAI drops a TTL it doesn't offer, as it did before.Usage.
message_start'scache_creation.ephemeral_1h_input_tokensbecomesInputTokensDetails.CacheWrite1hTokens, a part ofCacheWriteTokens, which keeps meaning all writes, so logs and telemetry are unchanged.Response.Used()splits the writes intocache_write_input_tokens(5m) and the newcache_write_1h_input_tokens.Billing readers kept consistent. I swept every reader of the cache-write meter:
store.Request.Quantitiessubtracts both write meters from fresh input.routing.Stat.Useadds both to the prompt'sinput_tokens, so spend stats andcost_sourcesstill see the whole prompt.pricing.InputMeters(context-tier thresholds) and in the meter whitelist that price-list validation uses.Pricing.
vendor.jsoncarries 2x input for every Anthropic model, Haiku 5.5's long-context tier included. Rates were read from the published pricing page.hourWriteFactor). This covers an entry a database already stores, because a release only adds price-list items a database doesn't have yet. BYOK twins and price-change factors go through the sameRateOf.Schema and clients.
20270301008300_agent_prompt_cache_ttl.sql, plusschema/global.sqlregenerated withcmd/schema.openapi.yaml, the Go, Python and JS clients regenerated.agent.yamlkey..claude/skills/sdk/notes/2026-11-26-prompt-cache-ttl.mdlists the other SDKs to update.Tests
5mproduce identical request bodies with nottl. The 1h write count is read from usage.Used()splits the two write meters.Request.QuantitiesandStat.Useagree when the row is stored and priced again.24h. Integration tests check create, patch and GET round-trip through the update column list, refuse an invalid value with a 400, and show nothing when unset. The spec carries the value onto every request. The Go SDK sends theagent.yamlkey.go testandgo vetpassed on the touched packages, plus-tags integration ./internal/api -testify.m PromptCacheTTLagainst a local Postgres.Not done
agent.yamlreaders: see the note above.🤖 Generated with Claude Code
https://claude.ai/code/session_015WLTK4LXi6LyXHjpnjuD4C