Repository navigation
feat(llm,agent): opt-in 1h prompt-cache TTL for Claude (cache_ttl) - #246
Open
collen-steffen wants to merge 1 commit into
Open
collen-steffen wants to merge 1 commit into
collen-steffen wants to merge 1 commit into
Conversation
Claude prompt-cache markers were always sent as {type="ephemeral"}, the
5-minute default. An agent step that thinks or runs a tool for longer than
5 minutes then re-sends its whole prefix uncached on the next request.
Measured on a document-drafting agent (Claude Sonnet 5.5, ~300k-token
context): two steps of 6m13s and 5m41s were each followed by a request with
cache_read_tokens = 0, while a 2m21s step was followed by a full cache hit.
- llm (Claude driver): options.cache_ttl "5m" | "1h". "1h" marks every
cache_control block with ttl="1h"; any other value is invalid_request.
Omitted keeps today's payload byte-for-byte.
- agent: an agent definition may set cache_ttl; it passes through discovery
and the compiler into the step's LLM options, like thinking_effort.
- tests: mapper (ttl applied / default unchanged / validation) and agent
(passed only when configured).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0139v7159kcEYoZk3JYhKwtK
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.
Problem
The Claude driver always marks cache breakpoints
{type = "ephemeral"}, which uses Claude's 5-minute default. The cache lifetime runs from the start of the request that wrote or read it, so the time spent generating counts against it. An agent step that thinks or runs a tool for a few minutes therefore loses its cache. The next request then re-sends the whole prefix uncached.Measured on a Fortress document-drafting agent on DEV (Claude Sonnet 5.5, ~300k-token context, one delegated dataflow run):
cache_read_tokensEach miss re-prefills about 300k tokens. That is slower (time to first token) and about 10× the input cost of a cache read.
Change
options.cache_ttl, either"5m"or"1h". With"1h", everycache_controlblock the mapper writes getsttl = "1h". Every marker shares one TTL, so the API's rule that longer TTLs come first is always met. Any other value returnsinvalid_request. When the option is omitted, the payload is byte-for-byte what it is today.mapper.map_messages(messages, { cache_ttl = ... }), plus helpersmapper.cache_control()andmapper.valid_cache_ttl().generate.luaandstructured_output.luapass the option through.cache_ttl: 1h. It flows through discovery and the compiler into each step's LLM options, the same waythinking_effortdoes. Other drivers already ignore unknown options (map_optionsis an allow-list).The 1-hour TTL is GA on the Claude API with no beta header. Writes cost 2× the base input price instead of 1.25×, and reads cost the same. It is opt-in per agent because it only pays off when consecutive requests start more than about 5 minutes apart.
Tests
src/llm/test/claude/mapper_test.lua: with"1h", the TTL lands on the system and message markers; without it, there is no TTL field; validation accepts only nil,"5m"and"1h".src/agent/test/agent_test.lua:cache_ttlreaches the LLM options when configured and is absent otherwise.I could not run the suite locally (no verified runtime on this machine), so I'm relying on CI here.
Out of scope: Bedrock (
cachePoint) is unchanged.🤖 Generated with Claude Code
https://claude.ai/code/session_0139v7159kcEYoZk3JYhKwtK