Skip to content

feat(router): opt-in one-hour prompt cache per agent config - #2536

Merged
adityaalifn merged 3 commits into
mainfrom
cache-ttl-1h
Nov 26, 2026
Merged

adityaalifn merged 3 commits into
mainfrom
cache-ttl-1h

Conversation

@adityaalifn

Copy link
Copy Markdown
Contributor

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_ttl on an agent config, the PromptCacheTtl enum (5m, 1h). It is accepted on POST, PUT and PATCH /v1/agents/configs and on a sync, so agent.yaml takes a top-level prompt_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.PromptCacheTTL goes to session.Spec, then to LLMOverwrites(), options.LLM.PromptCacheTTL, ResponseParams.Overwrite and PromptCacheOptions.TTL. That is the path the existing per-request options already take, for both text and voice sessions. With 1h, breakpoints() sets ttl: "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's cache_creation.ephemeral_1h_input_tokens becomes InputTokensDetails.CacheWrite1hTokens, a part of CacheWriteTokens, which keeps meaning all writes, so logs and telemetry are unchanged. Response.Used() splits the writes into cache_write_input_tokens (5m) and the new cache_write_1h_input_tokens.

Billing readers kept consistent. I swept every reader of the cache-write meter:

  • store.Request.Quantities subtracts both write meters from fresh input.
  • routing.Stat.Use adds both to the prompt's input_tokens, so spend stats and cost_sources still see the whole prompt.
  • The new meter is in pricing.InputMeters (context-tier thresholds) and in the meter whitelist that price-list validation uses.

Pricing.

  • vendor.json carries 2x input for every Anthropic model, Haiku 5.5's long-context tier included. Rates were read from the published pricing page.
  • An entry without the rate prices the meter at twice its own input rate and price (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 same RateOf.

Schema and clients.

  • Migration 20270301008300_agent_prompt_cache_ttl.sql, plus schema/global.sql regenerated with cmd/schema.
  • openapi.yaml, the Go, Python and JS clients regenerated.
  • The Go SDK reads the agent.yaml key.
  • .claude/skills/sdk/notes/2026-11-26-prompt-cache-ttl.md lists the other SDKs to update.

Tests

  • Anthropic: the 1h TTL lands on the tools and system breakpoints only, with the message breakpoints left at 5m. Unset and 5m produce identical request bodies with no ttl. The 1h write count is read from usage.
  • Pricing: a 1h write is priced at its own rate (2x input). Without a rate it is priced at twice the input rate. Every Anthropic vendor entry and tier prices it at 2x input.
  • Usage and billing: Used() splits the two write meters. Request.Quantities and Stat.Use agree when the row is stored and priced again.
  • Config: store validation refuses 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 the agent.yaml key.

go test and go vet passed on the touched packages, plus -tags integration ./internal/api -testify.m PromptCacheTTL against a local Postgres.

Not done

  • Swift client not regenerated: its generator binary isn't in the repo, and these operations aren't client-accessible.
  • No docs page change.
  • The other SDKs' agent.yaml readers: see the note above.

🤖 Generated with Claude Code

https://claude.ai/code/session_015WLTK4LXi6LyXHjpnjuD4C

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
@adityaalifn

Copy link
Copy Markdown
Contributor Author

/jeffery review

@getstream-metis

Copy link
Copy Markdown

Thanks for asking Jeffery for review. I'll stay on this PR until I can approve it.

How it works

  • Findings come as inline comments, each starting with [Must Fix], [Should Fix], nit: or consider:.
  • Push fixes whenever you're ready. Once the branch has been quiet for 10 minutes I review the new head and resolve the threads it fixes. To skip the wait, comment /jeffery review.
  • If you disagree or have a question, reply in the thread. I'll answer there, and resolve it if you're right.
  • I approve once every must-fix and should-fix is closed and nobody has requested changes. CI isn't part of that; branch protection still holds the merge until it's green. From round 3 on, open should-fix findings stop blocking.
  • I don't approve release/* branches or changes to protected paths, and I'll say so when that applies.
  • If a must-fix is still open after round 8, I stop and hand the PR to a human. Draft PRs wait until they're marked ready.

Commands (each as a whole comment)

Command What it does
/jeffery review Review the current head now
/jeffery rereview Start over at round 1 on the current head and withdraw my approval. This redoes the whole review and costs as much as the first one, so use it only when you don't trust the earlier rounds. If nothing changed since my last round I say so instead; add force to redo it anyway
/jeffery pause / /jeffery resume Stop or restart automatic reviews on this PR
/jeffery status What I last reviewed, and why I have or haven't approved
/jeffery help This list

/review-agent or a mention of me works in place of /jeffery.

…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

@getstream-metis getstream-metis Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Round 1 on c40696168237 · 🆕 1 new
Still open: 1 should-fix. Full list on the checks tab.

Comment thread acceleration/internal/api/configs.go Resolved
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
@adityaalifn

Copy link
Copy Markdown
Contributor Author

/jeffery review

@getstream-metis getstream-metis Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Approved 43a0a46d653d after 2 rounds · 1 fixed

@adityaalifn
adityaalifn merged commit 3a94206 into main Nov 26, 2026
21 checks passed
@adityaalifn
adityaalifn deleted the cache-ttl-1h branch November 26, 2026 14:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant