Skip to content

fix(agent): make Anthropic prompt caching work - #706

Open
jedrzejsieracki wants to merge 1 commit into
BaaRai:mainfrom
jedrzejsieracki:fix/prompt-cache
Open

jedrzejsieracki wants to merge 1 commit into
BaaRai:mainfrom
jedrzejsieracki:fix/prompt-cache

Conversation

@jedrzejsieracki

Copy link
Copy Markdown

Problem

A deployment with Anthropic profiles (claude-sonnet-5-5, thinking type "not sent") recorded 0 cache reads and 0 cache writes in llm_usage across all 49 calls (about 1.2M input tokens, all four lanes). With zero writes, no cache entry was ever created. That means no breakpoint was being sent at all; the cache wasn't being invalidated.

The cause in ARTEX: deferredSystem returned boundary = 0 unless the agent had globally available MCP tools. Norma reads a boundary of 0 as "no cache breakpoint". For an agent without such tools, which is the common case, the planner, workers, chat and main agent sent their tools and system prompt (11–16k tokens per turn) uncached on every turn. The goal decomposer never set a boundary at all.

Changes

System prompt breakpoint

  • deferredSystem always returns len(system), so the whole session-fixed system prompt (with the tools before it) is cached, with or without the deferred-tools block.
  • The goal decomposer sets DynamicBoundary: 1 for its single static segment.

Bedrock gateway

  • Norma's companion change fix(anthropic): automatic prompt caching and a fallback for refused thinking "disabled" Norma#14 sends a top-level cache_control (automatic caching), so the conversation history is cached too.
  • The legacy InvokeModel integration rejects that field but accepts explicit breakpoints on every version. So the gateway removes the field and puts a breakpoint on the last cacheable block of the last message, walking back past thinking blocks, which is where automatic caching would place it.
  • It leaves the messages unchanged when that block already has a breakpoint or the request already uses all 4 slots.
  • Breakpoints are counted only where the API reads them (tools, system, message blocks, tool_result content), so a cache_control key inside tool input isn't counted.

Docs: CHANGELOG.md, and docs/context-telemetry.md, which now explains that system_dynamic holds the constant untrusted-output note on the lanes that send it.

Order of landing

Testing

Tests changed or added:

  • TestDeferredSystem_NoGlobal and TestSkillIndexBlock_Empty now expect boundary 1.
  • TestDecomposeGoalsValidStructuredToolCallAccepted checks the decomposer's boundary on every request. It needs Postgres and skips without ARTEX_PG_DSN; I ran it against a throwaway Postgres.
  • TestBedrockGatewayMovesTopLevelCacheControl is table-driven and covers:
    • text, string and tool_result placement;
    • walking back past a thinking block;
    • an existing marker being kept;
    • the last free slot being used;
    • no slot left;
    • markers on tools and inside a tool_result being counted;
    • cache_control keys inside tool input being ignored.

Strength of the tests: each fails when its fix is reverted. The gateway logic was also checked by mutation (removing the move, walk-back, existing-marker check, string handling or any part of the count, and each direction of off-by-one).

Test runs: the full test suite (all Go packages; ./... itself trips over a local data directory) passes against the pinned Norma, and against BaaRai/Norma#14 via a local replace.

Not tested: no live API calls were made.

🤖 Generated with Claude Code

@BaaRai

BaaRai commented Dec 1, 2026

Copy link
Copy Markdown
Owner

The zero cache writes diagnosis is useful. Please rebase on main and re-run CI; I will review the provider changes after that. Keep the cache breakpoints out of the volatile parts of the system prompt.

On a profile talking to the Anthropic API directly, every call recorded
0 cache reads and 0 cache writes.

System prompt: deferredSystem returned boundary 0 unless the agent had
globally available MCP tools, and Norma reads a boundary of 0 as "no
cache breakpoint". For an agent without such tools, the common case,
the planner, workers, chat and main agent sent tools and system prompt
uncached on every turn; the goal decomposer never set a boundary at
all. deferredSystem now always returns len(system), and the decomposer
sets 1 for its single static segment.

Bedrock gateway: newer Norma versions send a top-level cache_control
(automatic caching) so the conversation history is cached too. The
legacy InvokeModel integration rejects that field but accepts explicit
breakpoints on every version, so the gateway now removes it and puts
the breakpoint on the last cacheable block of the last message, walking
back past thinking blocks, where automatic caching would put it. It
leaves the messages unchanged when that block already has a breakpoint
or the request already uses all four slots, counting markers only where
the API reads them (tools, system, message blocks, tool_result content),
so a "cache_control" key inside tool input is not mistaken for one.

docs/context-telemetry.md now describes system_dynamic with the
always-on boundary: it holds the constant untrusted-output note on the
lanes that send it.

Tests: TestDeferredSystem_NoGlobal and TestSkillIndexBlock_Empty expect
boundary 1; TestDecomposeGoalsValidStructuredToolCallAccepted checks the
decomposer's boundary (needs Postgres); the table in
TestBedrockGatewayMovesTopLevelCacheControl covers placement, string
content, walk-back, existing markers, the last free slot, a full
request and keys inside tool input. Each fails when its fix is reverted,
and the gateway logic was checked by mutation.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jedrzejsieracki

Copy link
Copy Markdown
Author

Rebased on main; CI is green.

On volatile parts: the breakpoint sits at the end of the system prompt, which is fixed for the whole session (a planner round, a main agent message, a worker intent) and is resent unchanged on every tool-loop turn, so it is read from cache on every turn after the first. The per-intent worker text stays after the breakpoint when ARTEX_CACHE_PREFIX=1 splits the prompt. The one case where the cached block varies between sessions is a custom DB prompt that uses {{.Now}}: it still caches within a session but cannot be shared across sessions. Dropping the breakpoint for such prompts would remove the only cache breakpoint Norma v0.4.13 sends, so those sessions would go back to zero cache reads.

BaaRai/Norma#14 adds automatic caching of the whole conversation; once ARTEX builds against it, this breakpoint mainly serves sharing the system prompt across sessions. #706 works without it.

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.

2 participants