Skip to content

fix: preserve cache_control markers when proxying to CC API - #5

Open
BUKOWSKIREAL wants to merge 1 commit into
MAXeaglet:masterfrom
BUKOWSKIREAL:fix/prompt-cache-control
Open

fix: preserve cache_control markers when proxying to CC API#5
BUKOWSKIREAL wants to merge 1 commit into
MAXeaglet:masterfrom
BUKOWSKIREAL:fix/prompt-cache-control

Conversation

@BUKOWSKIREAL

Copy link
Copy Markdown

Problem

Prompt caching never works through this proxy: cached_tokens (OpenAI) / cache_read_input_tokens (Anthropic) are always 0, even though the README advertises "缓存命中指标 / cache-hit metrics".

Root cause

The proxy only reads back cache usage from the CC API response (event.usage.cachedInputTokens) but never sends the cache_control markers upstream, so the CC API has nothing to cache:

  • buildCcRequest flattens system messages with join('\n') into a plain string, dropping cache_control on system blocks
  • user message parts (text / image_url) and assistant text are rebuilt without cache_control
  • tools are rebuilt without cache_control

Fix

Preserve cache_control end-to-end on both conversion paths:

  • OpenAI → CC (buildCcRequest):
    • system: detect Array content with cache_control, emit as [{ type: 'text', text, cache_control }] blocks instead of a flattened string
    • user: keep msg.cache_control for string content; keep part.cache_control on text / image_url array parts
    • assistant: keep msg.cache_control / part.cache_control on text blocks
    • tools: copy t.cache_control onto the CC tool object
  • Anthropic → OpenAI (convertAnthropicToOpenAI):
    • system blocks, user text blocks and tools keep their cache_control before being handed to buildCcRequest

No behavior change for requests that don't send cache_control (system is still flattened to a string as before).

Verification

  • node --check proxy.mjs passes
  • Second identical-prefix request now reports cached_tokens > 0 (previously always 0)

The proxy dropped cache_control on system/user/assistant messages and
tools when converting OpenAI/Anthropic requests to CC's internal format
(system was flattened to a string, message parts and tools were rebuilt
without cache_control). As a result the upstream CC API never created a
prompt cache, and cached_tokens / cache_read_input_tokens were always 0
despite the README advertising cache-hit metrics.

- buildCcRequest: keep cache_control on system blocks, user text/image
  parts, assistant text and tools
- convertAnthropicToOpenAI: preserve cache_control on system blocks,
  user text blocks and tools before forwarding to buildCcRequest
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