-
Notifications
You must be signed in to change notification settings - Fork 2.1k
fix(backend): set Anthropic prompt cache TTL to 1h (was 5m default) #7953
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -820,6 +820,50 @@ def test_page_context_in_dynamic_section(): | |
| assert "Meeting with team" in dynamic_suffix | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Tests: Anthropic cache_control includes TTL | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| def test_anthropic_cache_control_has_ttl(): | ||
| """ | ||
| The cache_control dict in _run_anthropic_agent_stream must include | ||
| ttl="1h" so that interactive chat sessions (with gaps >5min between | ||
| turns) get cache hits instead of re-writing on every request. | ||
|
|
||
| Regression: Anthropic changed default TTL from 1h→5m on 2026-03-06. | ||
| """ | ||
| agentic_mod = _get_agentic_module() | ||
|
|
||
| # Inspect the source to find the system_blocks construction | ||
| import inspect | ||
|
|
||
| src = inspect.getsource(agentic_mod._run_anthropic_agent_stream) | ||
| assert '"ttl": "1h"' in src or "'ttl': '1h'" in src, ( | ||
| "cache_control must include ttl='1h' to avoid 5-min default " | ||
| f"(source excerpt: ...{src[src.find('cache_control'):src.find('cache_control')+120]}...)" | ||
|
Comment on lines
+843
to
+844
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Context Used: Backend Python import rules - no in-function impor... (source) Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||
| ) | ||
| assert "ephemeral" in src, "cache type must be ephemeral" | ||
|
|
||
|
|
||
| def test_anthropic_cache_control_not_5min_default(): | ||
| """ | ||
| Guard against regression: ensure we are NOT relying on the 5-minute | ||
| default TTL that Anthropic introduced in March 2026. | ||
| """ | ||
| agentic_mod = _get_agentic_module() | ||
| import inspect | ||
|
|
||
| src = inspect.getsource(agentic_mod._run_anthropic_agent_stream) | ||
| # The old (broken) pattern was just {"type": "ephemeral"} with no ttl field | ||
| # Find the cache_control line(s) | ||
| lines_with_cache_ctrl = [l for l in src.splitlines() if "cache_control" in l] | ||
| for line in lines_with_cache_ctrl: | ||
| # Must NOT be the bare {"type": "ephemeral"} form | ||
| if '"type": "ephemeral"' in line or "'type': 'ephemeral'" in line: | ||
| assert "ttl" in line, f"cache_control line missing ttl field: {line.strip()}" | ||
|
|
||
|
Comment on lines
+857
to
+865
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Utility | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -362,7 +362,9 @@ async def _run_anthropic_agent_stream( | |
| and feeds results back until the model stops requesting tools. | ||
| """ | ||
| # System prompt with cache_control for Anthropic prompt caching | ||
| system_blocks = [{"type": "text", "text": system_prompt, "cache_control": {"type": "ephemeral"}}] | ||
| # TTL=1h: Anthropic changed default from 1h→5m on 2026-03-06; interactive chat | ||
| # sessions have gaps >5min between turns, so the 5-min default kills cache hit rate. | ||
| system_blocks = [{"type": "text", "text": system_prompt, "cache_control": {"type": "ephemeral", "ttl": "1h"}}] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The Anthropic API docs confirm |
||
|
|
||
| loop_iteration = 0 | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move this
inspectimport (and the duplicate one added in the next test) to the module imports.backend/AGENTS.mdapplies to this file and explicitly requires “No in-function imports — all imports at module top level,” so these new tests currently violate the backend import policy.Useful? React with 👍 / 👎.