[PR #8698] [MERGED] feat(packages): cache a package's stable prompt prefix on Anthropic (capability API 1.814) #8751

Closed
opened 2026-11-07 17:39:27 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/8698
Author: @SpicyMarinara
Created: 11/2/2026
Status: ✅ Merged
Merged: 11/2/2026
Merged by: @SpicyMarinara

Base: staging ← Head: feat/package-prompt-cache


📝 Commits (2)

  • 5bbe1f3 feat(packages): cache a package's stable prompt prefix on Anthropic (capability API 1.814)
  • e47264f fix(packages): keep caching when a marked package message is empty

📊 Changes

8 files changed (+411 additions, -49 deletions)

View changed files

📝 docs/development/optional-agent-packages.md (+42 -0)
📝 packages/server/src/services/capability-packages/capability-language-model.service.ts (+7 -0)
📝 packages/server/src/services/llm/providers/anthropic.provider.ts (+87 -45)
📝 packages/shared/src/schemas/capability-package.schema.ts (+5 -1)
📝 packages/shared/src/types/capability-runtime.ts (+17 -0)
📝 packages/shared/src/types/generation-integration.ts (+5 -0)
➕ scripts/regressions/capability-language-model-prompt-cache.regression.ts (+244 -0)
📝 scripts/regressions/capability-package-lifecycle.regression.ts (+4 -3)

📄 Description

Important

Contributions target staging. Only SpicyMarinara may promote this repository's staging branch or a same-repository hotfix/* branch to main.
Outside and first-time contributors also require an approving review from SpicyMarinara.

Linked issue

Closes #8695

Why this change

  • Capability packages such as Otome Hearth send the same large context with many calls in a short time: a chapter's plan and every scene repeat the instructions, cast and story memory.
  • Claude caches only what a request marks. The capability language-model path (capability-language-model.service.ts) passed no caching options, so package calls were never cached, even on connections with prompt caching on.
  • A cached read costs 0.1× and a cache write 1.25× once, so a chapter's calls could cost much less.

The maintainer decided that marked package calls are always cached, whatever the connection's caching switch.

What changed

  • Capability API 1.814. CapabilityLanguageModelMessage gains cacheBreakpoint?: boolean: "cache the prompt up to and including this message".
    • Works on any role, system included.
    • At most four count, Anthropic's limit. If more are marked, the last four in request order are kept.
    • Older Engines ignore the field, since nothing validates package messages at runtime.
    • Completion usage also reports cachedPromptTokens and cacheWritePromptTokens.
  • anthropic.provider.ts: both the plain and the tool-round request paths put cache_control on exactly the marked blocks.
    • System messages: a marked one becomes its own text block; before, only the last system block could carry cache_control.
    • Merging: a marked message keeps its own content block instead of being merged into its neighbour, so the breakpoint stays where the package put it.
    • Connection breakpoints: when a call has markers, those are the only breakpoints, and the connection's depth-based ones are left out.
    • Empty marked message: its marker moves to the message kept just before it, so the stable prefix is still cached.
    • Shared helper: the system-field code both paths duplicated is now one helper, anthropicSystemField.
  • capability-language-model.service.ts: a marked call is cached whatever the connection's caching switch, for the connection's cache time: 1 h with extended TTL on, else 5 minutes.
  • Unmarked calls are byte-identical to before. A review compared request bodies and usage on three Claude models and several message shapes.
  • Other providers:
    • Gemini, OpenAI, xAI and DeepSeek already cache repeated prefixes on their own. The marker never reaches them.
    • The Claude subscription (Agent SDK) caches by itself and ignores the marker. One change: a marked call there passes the connection's extended-TTL setting, as chat already does.
    • Not covered:
      • OpenRouter and other OpenAI-compatible routes to Claude: openai.provider.ts has no per-part cache_control, so markers do nothing there. That needs new message-part formatting in a follow-up.
      • /api/generate/raw (browser packages): its strict message schema doesn't accept the field.
  • Docs: docs/development/optional-agent-packages.md gains a 1.814 section. It explains how promptTokens relates to the cache counts: on Anthropic it's the uncached part only; on OpenAI-compatible routes and Gemini it already includes them. The type comments say the same. The translated packs are tracked in #8697.

Validation

  • pnpm check passes locally
  • Container (Docker / Podman) built and ran without issue
  • Ran the app, clicked through the changes manually
  • Checked edge cases (light + dark mode, mobile viewport, empty states, error paths)
  • Above manual verification completed (describe below)
  • Read and followed CONTRIBUTING.md

Manual verification notes

What the agent ran locally, rebased on staging 95f7a322b9:

  • node ./scripts/pnpm-runner.mjs check: exit 0, with only the existing client react-hooks warnings.

  • New regression: scripts/regressions/capability-language-model-prompt-cache.regression.ts, run over a fake Anthropic server. It checks that:

    • cache_control lands exactly on the marked system and user blocks, with a 5 m or 1 h TTL;
    • a marked message merged with the next keeps its own block;
    • the marker field is never sent;
    • an unmarked call sends no cache_control;
    • caching off still caches marked calls;
    • five markers keep the last four;
    • the tool-round path matches;
    • empty marked messages move their marker back;
    • cache read and write counts reach the package;
    • an OpenRouter call sends neither the marker nor cache_control.

    It fails on the pre-change server code at its first assertion.

  • Related regressions: provider-caching, capability-package-lifecycle (updated for 1.814), claude-subscription-cache, anthropic-safe-retry, provider-tool-history-shape, agent-connection-parameters, generation-usage, openai-prompt-cache-key and the Claude model regressions all pass.

  • Review: an independent review found two low-severity issues, both fixed above: the empty marked message, and the undocumented promptTokens meaning.

For a human to verify:

  • With a real Anthropic connection that has prompt caching off, send an Otome Hearth call with cacheBreakpoint: true on its stable system message. In the [debug/anthropic] final request debug log, manually verify cache_control is on that block. Then verify the provider reports a cache write on the first call and a cache read on the next one within 5 minutes.
  • With the connection's 1-hour cache setting on, manually verify the marked block carries ttl: "1h" and Anthropic accepts the request.
  • Manually verify a package call without markers on an Anthropic connection sends the same request as before (no cache_control while caching is off).
  • Manually verify a marked package call on an OpenRouter Claude connection, and on a Claude subscription connection, still completes normally.

Docs and release impact

  • No docs changes needed
  • Updated docs (README / CONTRIBUTING / android/README) as needed
  • Translated docs on the docs-i18n branch updated to match, or a [docs-i18n] follow-up issue opened (see CONTRIBUTING.md § Translated documentation)
  • Version/release files updated (only if this PR includes a version bump)

The English doc docs/development/optional-agent-packages.md is updated; the translations follow in #8697.

UI evidence (if applicable)

No UI change.

🤖 Generated with Claude Code


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Pasta-Devs/Marinara-Engine/pull/8698 **Author:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Created:** 11/2/2026 **Status:** ✅ Merged **Merged:** 11/2/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `staging` ← **Head:** `feat/package-prompt-cache` --- ### 📝 Commits (2) - [`5bbe1f3`](https://github.com/Pasta-Devs/Marinara-Engine/commit/5bbe1f378542f91869b42eed88eeba63877c5929) feat(packages): cache a package's stable prompt prefix on Anthropic (capability API 1.814) - [`e47264f`](https://github.com/Pasta-Devs/Marinara-Engine/commit/e47264f2ed4693d6b2855673c1bebb3a5b17632d) fix(packages): keep caching when a marked package message is empty ### 📊 Changes **8 files changed** (+411 additions, -49 deletions) <details> <summary>View changed files</summary> 📝 `docs/development/optional-agent-packages.md` (+42 -0) 📝 `packages/server/src/services/capability-packages/capability-language-model.service.ts` (+7 -0) 📝 `packages/server/src/services/llm/providers/anthropic.provider.ts` (+87 -45) 📝 `packages/shared/src/schemas/capability-package.schema.ts` (+5 -1) 📝 `packages/shared/src/types/capability-runtime.ts` (+17 -0) 📝 `packages/shared/src/types/generation-integration.ts` (+5 -0) ➕ `scripts/regressions/capability-language-model-prompt-cache.regression.ts` (+244 -0) 📝 `scripts/regressions/capability-package-lifecycle.regression.ts` (+4 -3) </details> ### 📄 Description > [!IMPORTANT] > Contributions target `staging`. Only `SpicyMarinara` may promote this repository's `staging` branch or a same-repository `hotfix/*` branch to `main`. > Outside and first-time contributors also require an approving review from `SpicyMarinara`. <!-- See CONTRIBUTING.md § Branches. --> ## Linked issue Closes #8695 ## Why this change - Capability packages such as Otome Hearth send the same large context with many calls in a short time: a chapter's plan and every scene repeat the instructions, cast and story memory. - Claude caches only what a request marks. The capability language-model path (`capability-language-model.service.ts`) passed no caching options, so package calls were never cached, even on connections with prompt caching on. - A cached read costs 0.1× and a cache write 1.25× once, so a chapter's calls could cost much less. The maintainer decided that marked package calls are always cached, whatever the connection's caching switch. ## What changed - **Capability API 1.814.** `CapabilityLanguageModelMessage` gains `cacheBreakpoint?: boolean`: "cache the prompt up to and including this message". - Works on any role, system included. - At most four count, Anthropic's limit. If more are marked, the last four in request order are kept. - Older Engines ignore the field, since nothing validates package messages at runtime. - Completion usage also reports `cachedPromptTokens` and `cacheWritePromptTokens`. - **`anthropic.provider.ts`:** both the plain and the tool-round request paths put `cache_control` on exactly the marked blocks. - **System messages:** a marked one becomes its own text block; before, only the last system block could carry `cache_control`. - **Merging:** a marked message keeps its own content block instead of being merged into its neighbour, so the breakpoint stays where the package put it. - **Connection breakpoints:** when a call has markers, those are the only breakpoints, and the connection's depth-based ones are left out. - **Empty marked message:** its marker moves to the message kept just before it, so the stable prefix is still cached. - **Shared helper:** the system-field code both paths duplicated is now one helper, `anthropicSystemField`. - **`capability-language-model.service.ts`:** a marked call is cached whatever the connection's caching switch, for the connection's cache time: 1 h with extended TTL on, else 5 minutes. - **Unmarked calls are byte-identical to before.** A review compared request bodies and usage on three Claude models and several message shapes. - **Other providers:** - Gemini, OpenAI, xAI and DeepSeek already cache repeated prefixes on their own. The marker never reaches them. - The Claude subscription (Agent SDK) caches by itself and ignores the marker. One change: a marked call there passes the connection's extended-TTL setting, as chat already does. - **Not covered:** - OpenRouter and other OpenAI-compatible routes to Claude: `openai.provider.ts` has no per-part `cache_control`, so markers do nothing there. That needs new message-part formatting in a follow-up. - `/api/generate/raw` (browser packages): its strict message schema doesn't accept the field. - **Docs:** `docs/development/optional-agent-packages.md` gains a 1.814 section. It explains how `promptTokens` relates to the cache counts: on Anthropic it's the uncached part only; on OpenAI-compatible routes and Gemini it already includes them. The type comments say the same. The translated packs are tracked in #8697. ## Validation - [ ] `pnpm check` passes locally - [ ] Container (Docker / Podman) built and ran without issue - [ ] Ran the app, clicked through the changes manually - [ ] Checked edge cases (light + dark mode, mobile viewport, empty states, error paths) - [ ] Above manual verification completed (describe below) - [ ] Read and followed `CONTRIBUTING.md` ### Manual verification notes What the agent ran locally, rebased on staging `95f7a322b9`: - **`node ./scripts/pnpm-runner.mjs check`:** exit 0, with only the existing client react-hooks warnings. - **New regression:** `scripts/regressions/capability-language-model-prompt-cache.regression.ts`, run over a fake Anthropic server. It checks that: - `cache_control` lands exactly on the marked system and user blocks, with a 5 m or 1 h TTL; - a marked message merged with the next keeps its own block; - the marker field is never sent; - an unmarked call sends no `cache_control`; - caching off still caches marked calls; - five markers keep the last four; - the tool-round path matches; - empty marked messages move their marker back; - cache read and write counts reach the package; - an OpenRouter call sends neither the marker nor `cache_control`. It fails on the pre-change server code at its first assertion. - **Related regressions:** provider-caching, capability-package-lifecycle (updated for 1.814), claude-subscription-cache, anthropic-safe-retry, provider-tool-history-shape, agent-connection-parameters, generation-usage, openai-prompt-cache-key and the Claude model regressions all pass. - **Review:** an independent review found two low-severity issues, both fixed above: the empty marked message, and the undocumented `promptTokens` meaning. For a human to verify: - With a real Anthropic connection that has prompt caching **off**, send an Otome Hearth call with `cacheBreakpoint: true` on its stable system message. In the `[debug/anthropic] final request` debug log, manually verify `cache_control` is on that block. Then verify the provider reports a cache write on the first call and a cache read on the next one within 5 minutes. - With the connection's 1-hour cache setting on, manually verify the marked block carries `ttl: "1h"` and Anthropic accepts the request. - Manually verify a package call without markers on an Anthropic connection sends the same request as before (no `cache_control` while caching is off). - Manually verify a marked package call on an OpenRouter Claude connection, and on a Claude subscription connection, still completes normally. ## Docs and release impact - [ ] No docs changes needed - [ ] Updated docs (README / CONTRIBUTING / android/README) as needed - [ ] Translated docs on the `docs-i18n` branch updated to match, or a `[docs-i18n]` follow-up issue opened (see CONTRIBUTING.md § Translated documentation) - [ ] Version/release files updated (only if this PR includes a version bump) The English doc `docs/development/optional-agent-packages.md` is updated; the translations follow in #8697. ## UI evidence (if applicable) No UI change. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-07 17:39:27 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#8751
No description provided.