Skip to content

Add with_prompt_cache_key for provider-side prompt caching - #53

Merged
anchpop merged 3 commits into
mainfrom
prompt-cache-key
Aug 1, 2026
Merged

anchpop merged 3 commits into
mainfrom
prompt-cache-key

Conversation

@anchpop

@anchpop anchpop commented Jul 31, 2026

Copy link
Copy Markdown
Owner

Adds ChatClient::with_prompt_cache_key, exposing OpenAI's prompt_cache_key.

Requests sharing a key are routed to the same cache, which is what makes long
prefix hits reliable. On GPT-5.6+ setting it is required for dependable
matching — without it a shared system prompt re-tokenizes at full price on most
requests.

Cache behaviour: the key is stripped from the local response-cache key, the
same way service_tier already is. It's a routing hint that can't change
response content, so setting or renaming a key never invalidates cached
responses. Both cache_key and legacy_cache_key strip it, so existing caches
keep hitting.

Motivation: yap's clean-nlp-data sends a ~10k-token tokenization policy as
the system prompt on every Japanese request; without a stable key the prefix
cache misses often enough to matter.

Verified by using it to generate data — cargo check is clean and the 11
offline tests pass. The 5 failing tests are pre-existing and environmental
(OpenAiApiKeyError(NotPresent) — they make live API calls).

🤖 Generated with Claude Code

https://claude.ai/code/session_01FgmaWYGPXGHGermM2JKUvQ

OpenAI's `prompt_cache_key` routes requests sharing a key to the same cache,
which is what makes long-prefix cache hits reliable. On GPT-5.6+ it is required
for dependable matching — without it a shared system prompt re-tokenizes at full
price on most requests.

Excluded from the local response-cache key, like service_tier: it is a routing
hint that cannot change response content, so setting or renaming a key never
invalidates cached responses. Both the current and legacy cache-key functions
strip it, so existing caches keep hitting.

Motivated by yap's clean-nlp-data, where every Japanese request shares a ~10k
token tokenization policy as its system prompt.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FgmaWYGPXGHGermM2JKUvQ
anchpop added a commit to yaptown/yap that referenced this pull request Jul 31, 2026
The previous commit carried a [patch] pointing tysm at ../tysm, which built
here and nowhere else. The change it needed — with_prompt_cache_key — is now
pushed as anchpop/tysm#53, so the dependency pins that rev directly and the
patch section is gone.

Drop the `rev` once the PR merges.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FgmaWYGPXGHGermM2JKUvQ
@anchpop
anchpop merged commit e560cd3 into main Aug 1, 2026
5 checks passed
@anchpop
anchpop deleted the prompt-cache-key branch August 1, 2026 09:53
anchpop added a commit to yaptown/yap that referenced this pull request Aug 1, 2026
with_prompt_cache_key landed on tysm main as anchpop/tysm#53 and shipped in
0.20.0, so the pin is no longer needed. The rev we were on diffs empty against
tysm's main, so this is a no-op in terms of code.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FgmaWYGPXGHGermM2JKUvQ
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant