Repository navigation
fix(agent): make Anthropic prompt caching work - #706
jedrzejsieracki wants to merge 1 commit into
Conversation
|
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. |
b780e49 to
5bdb472
Compare
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>
5bdb472 to
2e77377
Compare
|
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 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. |
Problem
A deployment with Anthropic profiles (claude-sonnet-5-5, thinking type "not sent") recorded 0 cache reads and 0 cache writes in
llm_usageacross 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:
deferredSystemreturnedboundary = 0unless 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
deferredSystemalways returnslen(system), so the whole session-fixed system prompt (with the tools before it) is cached, with or without the deferred-tools block.DynamicBoundary: 1for its single static segment.Bedrock gateway
cache_control(automatic caching), so the conversation history is cached too.tool_resultcontent), so acache_controlkey inside tool input isn't counted.Docs:
CHANGELOG.md, anddocs/context-telemetry.md, which now explains thatsystem_dynamicholds the constant untrusted-output note on the lanes that send it.Order of landing
Config.DisableAutoCache, for Anthropic-compatible endpoints that reject the top-level field;NoAutoCacheon ARTEX's own one-shot requests (agent/compaction.gocompress,server/intercept.go,sidequestion).thinking: "disabled"is refused. That fixes the 400 thatserver/intercept.goandsidequestioncurrently get on Sonnet/Opus 5.5.Testing
Tests changed or added:
TestDeferredSystem_NoGlobalandTestSkillIndexBlock_Emptynow expect boundary 1.TestDecomposeGoalsValidStructuredToolCallAcceptedchecks the decomposer's boundary on every request. It needs Postgres and skips withoutARTEX_PG_DSN; I ran it against a throwaway Postgres.TestBedrockGatewayMovesTopLevelCacheControlis table-driven and covers:tool_resultplacement;tool_resultbeing counted;cache_controlkeys 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 localreplace.Not tested: no live API calls were made.
🤖 Generated with Claude Code