Skip to content

Use prompt caching in the Anthropic machinery - #21310

Open
jeenadeepak wants to merge 3 commits into
WeblateOrg:mainfrom
jeenadeepak:anthropic-prompt-caching
Open

Use prompt caching in the Anthropic machinery#21310
jeenadeepak wants to merge 3 commits into
WeblateOrg:mainfrom
jeenadeepak:anthropic-prompt-caching

Conversation

@jeenadeepak

Copy link
Copy Markdown

Fixes #21309

The Anthropic machinery sends the system prompt as a plain string, so Anthropic's prompt caching (which is opt-in per request) never kicks in. The system prompt is identical for every request to a given target language, and with a bigger style guide it is easily a few thousand tokens billed at full input price on every request during automatic translation.

This change sends the system prompt as a content block with cache_control set to ephemeral. Cached reads are billed at roughly 10% of the input price with a 5 minute sliding TTL, which matches back-to-back automatic translation requests well. Below the minimum cacheable prefix (about 1024 tokens) the API silently ignores cache_control, so the change is safe unconditionally.

Includes a test asserting the request payload shape and a changelog entry.

@jeenadeepak
jeenadeepak requested a review from nijel as a code owner August 25, 2026 17:42
@nijel

nijel commented Aug 25, 2026

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f085007d02

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

{
"type": "text",
"text": prompt,
"cache_control": {"type": "ephemeral"},

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Gate caching for one-off Anthropic requests

When the system prefix meets Anthropic's minimum cacheable length but no identical request follows within the five-minute TTL, this unconditional breakpoint creates a cache write billed at 1.25× the normal input rate without any discounted cache read. Because this payload is used for ordinary editor suggestions and settings validation as well as sequential automatic-translation batches, installations making sporadic requests—or jobs completed in one batch—will pay more despite the changelog's cost-reduction claim; restrict caching to repeated/bulk traffic or make it configurable.

Useful? React with 👍 / 👎.

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.

Anthropic machinery: use prompt caching (cache_control) to reduce translation costs

2 participants