[GH-ISSUE #3041] [Issue]: Anthropic cache breakpoint silently dropped when the cache-depth message has no text content (image/file-only turn) #1609

Closed
opened 2026-10-21 05:40:38 +07:00 by bsakaguchi · 2 comments
Owner

Originally created by @kolacheee on GitHub (Jun 29, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/3041

Originally assigned to: @SpicyMarinara on GitHub.

What's wrong

With prompt caching enabled on an Anthropic connection, the cache breakpoint can be silently lost when the message chosen for the breakpoint is an image-only or PDF-only turn (empty text content). The ephemeral cache_control marker is attached only inside if (m.content), i.e. only when a text block is created. The cache-depth message is selected purely by position, without checking whether it has text. When a content-less attachment turn lands at that position, no text block is created, cache_control is never attached, and the intended cache breakpoint vanishes — degrading prompt-cache hit rate (cost/latency) with no error or warning.

How to reproduce (try it yourself)

  1. Anthropic connection with enableCaching=true and a cachingAtDepth that resolves to a message which is image-only or PDF-only (empty content) — e.g. an older user turn that contained only an image.
  2. Send a request through the streaming chat() path.

You'll see: the content array for that turn contains only image/document blocks and no cache_control anywhere; the request goes out with no cache breakpoint at the configured depth, so caching does not apply where expected.

Expected: the breakpoint is carried by the trailing image/document block, so caching still lands at the configured depth.

Where it is

packages/server/src/services/llm/providers/anthropic.provider.ts: the streaming chat() body builder messages.map at lines 426-439 (the if (m.content) guard at 428-432 is the only place cache_control is attached). resolveCacheControlMessageIndex picks by position only (lines 25-28). The message filter that keeps content-less attachment turns is at lines 391-393.

What's happening

cache_control placement is coupled to the existence of a text block, but resolveCacheControlMessageIndex does not account for content-less messages, so the breakpoint can be assigned to a turn that produces no text block to carry it. The filter keeps a turn when it has images or files even if its content string is empty, so such a turn can sit at cacheControlMessageIndex.

Suggested fix

When the turn is the cache breakpoint but has no text content, attach cache_control to the trailing attachment block instead of dropping it — with an empty-array guard. Replace the map body (lines 426-439):

messages: mergedMessages.map((m, i) => {
  const parts: Array<Record<string, unknown>> = [...fileContentBlocks(m.files), ...imageContentBlocks(m.images)];
  const isCacheBreakpoint = i === cacheControlMessageIndex;
  if (m.content) {
    const textBlock: Record<string, unknown> = { type: "text", text: m.content };
    if (isCacheBreakpoint) textBlock.cache_control = { type: "ephemeral" };
    parts.push(textBlock);
  } else if (isCacheBreakpoint && parts.length > 0) {
    // Image/document-only turn at the cache depth: attach cache_control to the
    // trailing attachment block instead of silently dropping the breakpoint.
    parts[parts.length - 1]!.cache_control = { type: "ephemeral" };
  }
  if (m.images?.length || m.files?.length || isCacheBreakpoint) {
    return { role: m.role, content: parts };
  }
  return { role: m.role, content: m.content };
}),

Anthropic accepts cache_control on image/document blocks, so the breakpoint lands at the configured depth. Regression avoided: the parts.length > 0 guard is essential — the message filter keeps a turn whose only attachments are unparseable (imageContentBlocks silently drops non-data-URL images; fileContentBlocks skips non-PDF files), which can yield an empty parts array. Without the guard, parts[parts.length-1].cache_control = ... dereferences undefined and throws, crashing the generation request. With it, that corner falls back to the pre-existing content: [] behavior. resolveCacheControlMessageIndex is left unchanged (the alternative — skipping content-less messages there — would forfeit the breakpoint at the intended depth). The happy text-turn path runs exactly as before.


AI-investigated against v2.0.6; cited lines read and confirmed. Please verify before fixing.

Originally created by @kolacheee on GitHub (Jun 29, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/3041 Originally assigned to: @SpicyMarinara on GitHub. **What's wrong** With prompt caching enabled on an Anthropic connection, the cache breakpoint can be silently lost when the message chosen for the breakpoint is an image-only or PDF-only turn (empty text content). The ephemeral `cache_control` marker is attached only inside `if (m.content)`, i.e. only when a text block is created. The cache-depth message is selected purely by position, without checking whether it has text. When a content-less attachment turn lands at that position, no text block is created, `cache_control` is never attached, and the intended cache breakpoint vanishes — degrading prompt-cache hit rate (cost/latency) with no error or warning. **How to reproduce (try it yourself)** 1. Anthropic connection with `enableCaching=true` and a `cachingAtDepth` that resolves to a message which is image-only or PDF-only (empty content) — e.g. an older user turn that contained only an image. 2. Send a request through the streaming `chat()` path. You'll see: the content array for that turn contains only image/document blocks and no `cache_control` anywhere; the request goes out with no cache breakpoint at the configured depth, so caching does not apply where expected. Expected: the breakpoint is carried by the trailing image/document block, so caching still lands at the configured depth. **Where it is** `packages/server/src/services/llm/providers/anthropic.provider.ts`: the streaming `chat()` body builder `messages.map` at lines 426-439 (the `if (m.content)` guard at 428-432 is the only place `cache_control` is attached). `resolveCacheControlMessageIndex` picks by position only (lines 25-28). The message filter that keeps content-less attachment turns is at lines 391-393. **What's happening** `cache_control` placement is coupled to the existence of a text block, but `resolveCacheControlMessageIndex` does not account for content-less messages, so the breakpoint can be assigned to a turn that produces no text block to carry it. The filter keeps a turn when it has images or files even if its content string is empty, so such a turn can sit at `cacheControlMessageIndex`. **Suggested fix** When the turn is the cache breakpoint but has no text content, attach `cache_control` to the trailing attachment block instead of dropping it — with an empty-array guard. Replace the map body (lines 426-439): ```ts messages: mergedMessages.map((m, i) => { const parts: Array<Record<string, unknown>> = [...fileContentBlocks(m.files), ...imageContentBlocks(m.images)]; const isCacheBreakpoint = i === cacheControlMessageIndex; if (m.content) { const textBlock: Record<string, unknown> = { type: "text", text: m.content }; if (isCacheBreakpoint) textBlock.cache_control = { type: "ephemeral" }; parts.push(textBlock); } else if (isCacheBreakpoint && parts.length > 0) { // Image/document-only turn at the cache depth: attach cache_control to the // trailing attachment block instead of silently dropping the breakpoint. parts[parts.length - 1]!.cache_control = { type: "ephemeral" }; } if (m.images?.length || m.files?.length || isCacheBreakpoint) { return { role: m.role, content: parts }; } return { role: m.role, content: m.content }; }), ``` Anthropic accepts `cache_control` on image/document blocks, so the breakpoint lands at the configured depth. **Regression avoided:** the `parts.length > 0` guard is essential — the message filter keeps a turn whose only attachments are unparseable (`imageContentBlocks` silently drops non-data-URL images; `fileContentBlocks` skips non-PDF files), which can yield an empty `parts` array. Without the guard, `parts[parts.length-1].cache_control = ...` dereferences `undefined` and throws, crashing the generation request. With it, that corner falls back to the pre-existing `content: []` behavior. `resolveCacheControlMessageIndex` is left unchanged (the alternative — skipping content-less messages there — would forfeit the breakpoint at the intended depth). The happy text-turn path runs exactly as before. --- *AI-investigated against v2.0.6; cited lines read and confirmed. Please verify before fixing.*
Author
Owner

@SpicyMarinara commented on GitHub (Jun 29, 2026):

Claiming this for the current staging sweep in draft PR #3053 (branch codex/issue-sweep-3033-3052).

<!-- gh-comment-id:4834988340 --> @SpicyMarinara commented on GitHub (Jun 29, 2026): Claiming this for the current staging sweep in draft PR #3053 (branch `codex/issue-sweep-3033-3052`).
Author
Owner

@SpicyMarinara commented on GitHub (Jun 29, 2026):

Fixed in PR #3053 on branch codex/issue-sweep-3033-3052 (commit bfb1bc76). Closing this issue as addressed; the PR remains open for staging review/merge.

<!-- gh-comment-id:4835088220 --> @SpicyMarinara commented on GitHub (Jun 29, 2026): Fixed in PR #3053 on branch `codex/issue-sweep-3033-3052` (commit bfb1bc76). Closing this issue as addressed; the PR remains open for staging review/merge.
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#1609
No description provided.