Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.
Python agentagent-osaiai-agentsai-partnerchatopsclaudeclaude-codecodexdevtoolsdiscord-botlark-botllmlocal-firstlong-term-memoryopencodeslack-botvibe-codingvibeywechat
fix(ai): align Claude conversation cache lifetime (#3113)
Call Whitec52aec7fadc9
diff --git a/core/agent_core/agent/loop.py b/core/agent_core/agent/loop.pyindex 532cfc42d..2ba6bf775 100644--- a/core/agent_core/agent/loop.py+++ b/core/agent_core/agent/loop.py@@ -1694,7 +1694,7 @@ class Agent:# C-10 section 7), ``_commit_context`` (3), ``self._attempts`` with ``_settle_attempts`` and ``side_turn``'s# one audit (4), ``harness.context.budget`` (5), and ``_model`` (6, admission).- def _cache_cold(self, endpoint: ModelEndpoint) -> bool:+ def _cache_cold(self, request: ModelRequest) -> bool:"""The provider cache of the request's route has gone unused longer than it stays warm (section 3).Its use is the latest model call's end or response row's commit served by the route's upstream cache@@ -1708,8 +1708,8 @@ class Agent:if row.kind == "response" and row.created_at is not None:cache = _cache_of(row.message.origin)used[cache] = max(used.get(cache, row.created_at), row.created_at)- last = used.get(_cache_of(endpoint_origin(endpoint)), max(used.values(), default=None))- return last is not None and self.context.clock() - last > cache_ttl_s(endpoint)+ last = used.get(_cache_of(endpoint_origin(request.endpoint)), max(used.values(), default=None))+ return last is not None and self.context.clock() - last > cache_ttl_s(request.endpoint, request=request)async def _prepare_conversation(self, system: str, selected: ModelSelection) -> _PreparedConversation:"""Compose and budget before any context transition; also used by dry_tick."""@@ -1729,7 +1729,7 @@ class Agent:if (not plan.can_fit and not minimal_checked) or ladder.refused:return "context_relief"if self.context.clear_tool_results and (- plan.est >= CLEAR_SOFT_RATIO * plan.threshold or self._cache_cold(request.endpoint)+ plan.est >= CLEAR_SOFT_RATIO * plan.threshold or self._cache_cold(request)) and clearable_results(view):return "clear_tool_results"if plan.est >= plan.threshold and not ladder.compacted and not self._stopped:diff --git a/core/agent_core/ai/anthropic.py b/core/agent_core/ai/anthropic.pyindex f439f0295..e20c76d43 100644--- a/core/agent_core/ai/anthropic.py+++ b/core/agent_core/ai/anthropic.py@@ -83,9 +83,8 @@ PAUSE_UNSERVED_MESSAGE = "the Source that served the paused turn cannot continue#: A continuation goes out only as the first send plus the paused response exactly as served (§4.3).PAUSE_ALTERED_MESSAGE = "the paused turn cannot be sent back as it was served"_MAX_CACHE_BREAKPOINTS = 4-# A Claude route caches its static prefix, the tools and then the system prompt, for an hour (``_prefix_ttl``); the-# conversation's breakpoints keep the default five minutes. Anthropic evaluates breakpoints as tools, system, messages-# and requires every longer lifetime to come before every shorter one, which this placement satisfies.+# A Claude route caches its static prefix and the conversation breakpoints for an hour (``_prefix_ttl``); every+# longer lifetime precedes every shorter one because Anthropic evaluates breakpoints as tools, system, messages._PREFIX_TTL = "1h"# Routes whose endpoint rejected the field. They keep the default lifetime for the rest of the process._TTL_REJECTED_ROUTES: set[tuple[str, str | None, str, str]] = set()@@ -1071,7 +1070,7 @@ def build_messages_payload() -> dict[str, Any]:"""Build the Anthropic Messages request body.- ``prefix_ttl`` is the lifetime of the tool and system breakpoints; message breakpoints keep the default.+ ``prefix_ttl`` is the lifetime of every cache breakpoint in the request: tools, system, and messages."""payload_messages: list[dict[str, Any]] = []@@ -1095,7 +1094,7 @@ def build_messages_payload(if request.cache == "default" and request.system:system = [{"type": "text", "text": request.system, "cache_control": _cache_control(prefix_ttl)}]if request.cache == "default":- _mark_message_breakpoints(payload_messages)+ _mark_message_breakpoints(payload_messages, prefix_ttl)payload: dict[str, Any] = {"model": request.endpoint.model_id,"max_tokens": request.max_tokens,@@ -1232,7 +1231,7 @@ def _anthropic_user_content(return result-def _mark_message_breakpoints(messages: list[dict[str, Any]]) -> None:+def _mark_message_breakpoints(messages: list[dict[str, Any]], ttl: str | None = None) -> None:candidates = [indexfor index, message in enumerate(messages)@@ -1241,11 +1240,11 @@ def _mark_message_breakpoints(messages: list[dict[str, Any]]) -> None:for index in candidates[-2:]:content = messages[index]["content"]if isinstance(content, str):- messages[index]["content"] = [{"type": "text", "text": content, "cache_control": _cache_control()}]+ messages[index]["content"] = [{"type": "text", "text": content, "cache_control": _cache_control(ttl)}]elif content:last = content[-1]if isinstance(last, dict) and last.get("type") in {"text", "image", "tool_result"}:- last["cache_control"] = _cache_control()+ last["cache_control"] = _cache_control(ttl)def _cache_control(ttl: str | None = None) -> dict[str, str]:@@ -1254,6 +1253,17 @@ def _cache_control(ttl: str | None = None) -> dict[str, str]:return {"type": "ephemeral", "ttl": ttl} if ttl else {"type": "ephemeral"}+def cache_ttl_s(request: ModelRequest) -> float:+ """The cache lifetime of this request's Anthropic breakpoints, in seconds.++ This is the owner of the route predicate used by both the wire builder and C-9's cold-cache clock: a supported+ Claude route sends one-hour breakpoints, while a rejected route or any other request keeps Anthropic's five-minute+ default.+ """++ return 3_600.0 if _prefix_ttl(request) == _PREFIX_TTL else 300.0++def _prefix_ttl(request: ModelRequest) -> str | None:"""The static prefix's lifetime: an hour where the field is known to work, else the default (``None``).diff --git a/core/agent_core/harness/context.py b/core/agent_core/harness/context.pyindex 0f1a017d9..310eee8bf 100644--- a/core/agent_core/harness/context.py+++ b/core/agent_core/harness/context.py@@ -88,14 +88,23 @@ COMMAND_TOOLS = frozenset({"bash", "powershell"})CLEARABLE_TOOLS = frozenset({"read"}) | COMMAND_TOOLS#: How long a route's provider prompt cache stays warm after its last use, in seconds, by cache family (section 3):#: the documented lifetime of the cache mode Vibey requests for the conversation, where clearable results live.-#: ``anthropic``: the message breakpoints' ``cache_control`` is ephemeral with no ``ttl``, 5 minutes, refreshed on use.-#: ``openai-gpt-5.6``: OpenAI Responses GPT-5.6+ defaults to a 30-minute prefix cache; a validated official route may opt into ``12h`` via ``prompt_cache_options.ttl``. The latter has a 2.5x cache-write rate versus 1.25x for 30m, while reads use the same cached-input rate.+#: ``anthropic``: official Claude routes send one-hour breakpoints; the adapter's shared route predicate falls back to+#: the five-minute default for rejected or otherwise non-one-hour requests.+#: ``anthropic-compatible``: non-Claude models and other vendors on the Anthropic protocol use the five-minute+#: default, because Avibe does not send the one-hour field there.+#: ``openai-gpt-5.6``: OpenAI Responses GPT-5.6+ defaults to a 30-minute prefix cache; a validated official route may+#: opt into ``12h`` via ``prompt_cache_options.ttl``. The latter has a 2.5x cache-write rate versus 1.25x for 30m,+#: while reads use the same cached-input rate.#: Route overrides are intentionally closed: they are persisted as operator-visible#: duration labels, not arbitrary provider claims. ``12h`` is admitted only for#: official OpenAI Responses GPT-5.6+ routes by Model Hub validation.CACHE_TTL_OVERRIDES: Mapping[str, float] = {"5m": 300.0, "30m": 1_800.0, "1h": 3_600.0, "12h": 43_200.0}#: Earlier supported OpenAI models may expose the separate Chat Completions ``prompt_cache_retention`` modes ``in_memory`` or ``24h`` depending on model and organization policy; Avibe does not infer or send that policy-dependent option, so those routes stay on the conservative built-in estimate unless explicitly supported later.-CACHE_TTLS: Mapping[str, float] = {"anthropic": 300.0, "openai-gpt-5.6": 1_800.0}+CACHE_TTLS: Mapping[str, float] = {+ "anthropic": 3_600.0,+ "anthropic-compatible": 300.0,+ "openai-gpt-5.6": 1_800.0,+}#: Any other route, or one not identified unambiguously: clearing a warm cache costs a full-price re-read of the#: context, while leaving a cold one only keeps some old output, so an unknown lifetime counts as an hour.CACHE_TTL_UNKNOWN_S = 3_600.0@@ -646,23 +655,34 @@ def budget(_GPT_MODEL = re.compile(r"gpt-(\d+)(?:\.(\d+))?(?:-[a-z0-9]+(?:[.-][a-z0-9]+)*)?", re.ASCII)-def cache_ttl_s(endpoint: ModelEndpoint) -> float:+def cache_ttl_s(endpoint: ModelEndpoint, *, request: ModelRequest | None = None) -> float:"""How long the provider cache of a request's route stays warm (section 3).- A validated Model Hub route override wins. Otherwise use the documented- family lifetime, with OpenAI GPT-5.6+ recognized only on Responses; unknown- routes keep the conservative one-hour estimate.+ A validated Model Hub route override wins, except that a remembered Anthropic rejection downgrades an explicit 1h+ override to the five-minute wire lifetime. Otherwise use the documented family lifetime; Anthropic requests use the+ adapter's wire predicate when the complete request is available, so C-9 clears on the lifetime actually sent."""+ family = _cache_family(endpoint)+ if family in {"anthropic", "anthropic-compatible"} and request is not None:+ # Import lazily: the adapter owns both the wire predicate and its per-route rejection memory.+ from core.agent_core.ai.anthropic import cache_ttl_s as anthropic_cache_ttl_s++ actual = anthropic_cache_ttl_s(request)+ # An explicit official-Claude 1h override describes the normal wire mode, but a remembered provider+ # rejection downgrades this request to Anthropic's five-minute default before C-9 decides whether it is cold.+ if endpoint.cache_ttl == "1h" and actual != CACHE_TTL_OVERRIDES["1h"]:+ return actualif endpoint.cache_ttl is not None:return CACHE_TTL_OVERRIDES.get(endpoint.cache_ttl, CACHE_TTL_UNKNOWN_S)- family = _cache_family(endpoint)+ if family in {"anthropic", "anthropic-compatible"} and request is not None:+ return actualreturn CACHE_TTL_UNKNOWN_S if family is None else CACHE_TTLS[family]def _cache_family(endpoint: ModelEndpoint) -> Optional[str]:"""The route's cache family from its provider, protocol, and whole model id; None unless unambiguous."""- if endpoint.provider == "anthropic" and endpoint.protocol == "anthropic":- return "anthropic"+ if endpoint.protocol == "anthropic":+ return "anthropic" if endpoint.provider == "anthropic" and endpoint.model_id.startswith("claude-") else "anthropic-compatible"gpt = (_GPT_MODEL.fullmatch(endpoint.model_id)if endpoint.provider == "openai" and endpoint.protocol == "openai_responses"diff --git a/core/handlers/model_hub/resolver.py b/core/handlers/model_hub/resolver.pyindex 3f5db1f34..771f0e6e1 100644--- a/core/handlers/model_hub/resolver.py+++ b/core/handlers/model_hub/resolver.py@@ -341,9 +341,9 @@ def validate_cache_ttl_override(and source.base_url is Nonefor hop in route.hops)- and ttl != "5m"+ and ttl != "1h"):- raise ValueError("official Anthropic conversation breakpoints support only 5m")+ raise ValueError("official Anthropic Claude routes support only 1h")def effective_model_route(diff --git a/docs/plans/agent-core-contracts/context.md b/docs/plans/agent-core-contracts/context.mdindex 82682467c..d3178c511 100644--- a/docs/plans/agent-core-contracts/context.md+++ b/docs/plans/agent-core-contracts/context.md@@ -154,9 +154,10 @@ route override (`harness/context.py`, `CACHE_TTLS`):| Route | Lifetime || --- | --- |-| provider `anthropic` on the Anthropic protocol: the message breakpoints' `cache_control` is ephemeral, with no `ttl` | 300 s, refreshed on use |+| provider `anthropic` on the Anthropic protocol: official Claude requests carry `ttl: 1h` on the tool, system, and message breakpoints; a rejected route or a request without a static prefix carries the default ephemeral breakpoints | 3,600 s when the one-hour fields are sent; 300 s otherwise, refreshed on use |+| non-Claude models and other vendors on the Anthropic protocol use the default ephemeral breakpoints | 300 s, refreshed on use || provider `openai`, protocol `openai_responses`, and a model id `gpt-<major>[.<minor>][-<suffix>]` from GPT-5.6: `prompt_cache_options.ttl` defaults to 30 minutes; an explicit route override may request `30m` or the opt-in `12h` mode | 1,800 s by default |-| any other route, or one not identified unambiguously (older OpenAI models, `openai_chat`, other vendors, relays) | 3,600 s |+| any other route, or one not identified unambiguously (older OpenAI models, `openai_chat`, relays) | 3,600 s || a nonempty route's validated `cache_ttl` override | the configured 5m, 30m, 1h, or 12h estimate; official OpenAI Responses GPT-5.6+ routes accept explicit `30m` or `12h` wire modes |Earlier supported OpenAI models may expose a separate policy-dependent `prompt_cache_retention` mode (`in_memory` or `24h`), but Avibe does not infer or send that option; those routes remain on the conservative built-in estimate unless a route capability explicitly supports it. The official 12-hour Responses mode has a 2.5x cache-write rate versus 1.25x for the 30-minute default, while reads use the same cached-input rate; Avibe sends that opt-in only on confirmed official GPT-5.6+ Responses routes and meters the two write lifetimes separately. Clearing a warm cache forces a full-price re-read of the context, while skipping a clear on a cold one only keepsdiff --git a/docs/plans/agent-core-contracts/conversion-tables.md b/docs/plans/agent-core-contracts/conversion-tables.mdindex 64c1edf7e..c31da533e 100644--- a/docs/plans/agent-core-contracts/conversion-tables.md+++ b/docs/plans/agent-core-contracts/conversion-tables.md@@ -517,7 +517,7 @@ credential-free stubs and did not alter the Pi source or Avibe dependencies.| Tool result | A user message containing `tool_result` with `tool_use_id`, `content`, and `is_error`. || Tools | `tools[]` with `name`, `description`, and `input_schema`; the final tool carries the prefix breakpoint (Cache, below) for `cache="default"`. || Reasoning effort | `none/off/disabled` → `thinking.type="disabled"`; other values → adaptive thinking and `output_config.effort` (`minimal` maps to `low`, `xhigh` to `max`). |-| Cache | For `cache="default"`, at most four breakpoints, in Anthropic's evaluation order: the final tool, the system block, and the two latest eligible user-message tails. On a Claude route (the hop's `provider` is exactly `anthropic` and its model id starts with `claude-`), the tool and system breakpoints are `{"type":"ephemeral","ttl":"1h"}`. Message tails are always `{"type":"ephemeral"}`, the five-minute default, so no longer lifetime follows a shorter one. Every other route, including another vendor's Anthropic-compatible endpoint even for a `claude-` id, sends `{"type":"ephemeral"}` everywhere. `cache="none"` sends no breakpoint. |+| Cache | For `cache="default"`, at most four breakpoints, in Anthropic's evaluation order: the final tool, the system block, and the two latest eligible user-message tails. On an official Claude route (the hop's `provider` is exactly `anthropic` and its model id starts with `claude-`), every breakpoint is `{"type":"ephemeral","ttl":"1h"}` when a static prefix exists; without a static prefix, or after the route rejects `ttl`, all breakpoints use `{"type":"ephemeral"}` (the five-minute default). Every other route, including another vendor's Anthropic-compatible endpoint even for a `claude-` id, sends `{"type":"ephemeral"}` everywhere. `cache="none"` sends no breakpoint. || Cache TTL rejection | A 4xx `invalid_request` whose message names `ttl` or `cache_control`, answering a request that carried `ttl`, is resent once without it before any event reaches the loop. The route (`base_url`, `provider`, model id) then sends no `ttl` for the rest of the process; the downgrade is logged once per route. |Anthropic stream events:diff --git a/docs/plans/system-prompt-cache-tiers.md b/docs/plans/system-prompt-cache-tiers.mdindex 63613d394..070769812 100644--- a/docs/plans/system-prompt-cache-tiers.md+++ b/docs/plans/system-prompt-cache-tiers.md@@ -213,7 +213,8 @@ cannot observe it.| Route | Sent | Lifetime Avibe assumes (`CACHE_TTLS`) || --- | --- | --- |-| Official Anthropic, Claude model | tools and system prompt breakpoints with `ttl: 1h` (since #2484); the last two user-message breakpoints with the default 5 minutes | 300 s |+| Official Anthropic, Claude model | tools, system, and the last two eligible user-message breakpoints with `ttl: 1h` when a static prefix is present; a route that rejects `ttl`, or a request without a static prefix, uses Anthropic's default ephemeral breakpoints | 3,600 s when the one-hour fields are sent; 300 s otherwise |+| Other Anthropic-protocol routes (non-Claude models and other vendors) | default ephemeral breakpoints; Avibe does not send the one-hour field | 300 s || Official OpenAI Responses, GPT-5.6 and later | nothing by default (30 minutes); a validated route override may send `prompt_cache_options.ttl` of `30m` or `12h` | 1,800 s || Every other route | no cache fields | 3,600 s (errs toward warm) |@@ -222,7 +223,7 @@ No route sends `prompt_cache_key` or a conversation header.## Decisions and follow-ups1. **Claude: tools and system prompt stay at 1 hour; the conversation moves- from 5 minutes to 1 hour** (owner decision, 2026-11-01).+ from 5 minutes to 1 hour** (owner decision, 2026-11-01; implemented in this PR).- The conversation is almost all of the prefix. Measured above, 55% of allClaude writes were re-writes after 5 to 60 minute gaps.- At Opus 5.5 rates (read 0.05x), one hour turns those 306 M re-written@@ -234,10 +235,12 @@ No route sends `prompt_cache_key` or a conversation header.bills a 1-hour write only beyond what was read, so it never extends thatcontent. History has one-hour coverage only if it was written at onehour.- - Update `CACHE_TTLS["anthropic"]` to 3,600 s and the route-override- validation that limits official Anthropic conversation breakpoints to- `5m`. Review with the metrics below after two weeks; split by Session- kind (for example unattended Task runs) only if the data asks for it.+ - Implemented by setting `CACHE_TTLS["anthropic"]` to 3,600 s and the+ route-override validation to admit only `1h` for official Anthropic+ Claude routes. C-9 derives its lifetime from the adapter's actual+ breakpoint predicate. Review with the metrics below after two weeks;+ split by Session kind (for example unattended Task runs) only if the+ data asks for it.2. **OpenAI: keep 30 minutes.** Gaps of 30 minutes to 12 hours missed 48 Mtokens; a 12-hour lifetime would save about 55 M token-equivalents but makeevery other write cost 2.5x instead of 1.25x, roughly 400 M more. OpenAIdiff --git a/tests/agent_core/agent/test_compaction.py b/tests/agent_core/agent/test_compaction.pyindex ba7fc5ffa..a7a80b5cd 100644--- a/tests/agent_core/agent/test_compaction.py+++ b/tests/agent_core/agent/test_compaction.py@@ -1533,13 +1533,14 @@ async def test_one_compaction_is_in_flight_per_session():assert len(model.checkpoint_requests) == 1-#: An official OpenAI GPT model (a 30-minute prompt cache, section 3) and Anthropic, whose message breakpoints Vibey-#: caches for 5 minutes; ``ENDPOINT``'s test provider is a route of unknown lifetime, an hour.+#: An official OpenAI GPT model (a 30-minute prompt cache, section 3) and Anthropic, whose Claude breakpoints Vibey+#: caches for 1 hour; ``ENDPOINT``'s Anthropic-compatible test provider uses the default five-minute lifetime.GPT = ModelEndpoint("openai_responses", "http://model.invalid", "gpt-6-astra", "", provider="openai")CLAUDE = ModelEndpoint("anthropic", "http://model.invalid", "claude-opus-5-5", "", provider="anthropic")+UNKNOWN = ModelEndpoint("openai_chat", "http://model.invalid", "test-model", "", provider="test-provider")[email protected]("endpoint,lifetime", [(CLAUDE, 300), (GPT, 1_800), (ENDPOINT, 3_600)])[email protected]("endpoint,lifetime", [(CLAUDE, 3_600), (GPT, 1_800), (ENDPOINT, 300)])async def test_clearing_runs_once_the_route_cache_has_gone_cold_and_spares_recent_turns(endpoint, lifetime):now = [1_000.0]store = InMemoryTranscriptStore(clock=lambda: now[0])@@ -1609,15 +1610,15 @@ async def routed_turn(store, now, endpoint, scripts, row, served=None):async def test_a_route_cache_counts_only_its_own_use():# Ten minutes after Claude's last reply, GPT answers for the first time: a route with no use yet takes the latest- # use of any route, well inside GPT's 30 minutes, so nothing clears. A minute later the Session is back on Claude,- # whose own cache went unused for eleven minutes: cold, although another route answered a minute ago.+ # use of any route, well inside GPT's 30 minutes. After another 50 minutes, the Session is back on Claude,+ # whose own cache went unused for just over an hour: cold, although GPT answered 50 minutes ago.now = [1_000.0]store = InMemoryTranscriptStore(clock=lambda: now[0])await routed_turn(store, now, CLAUDE, [*_long_context(9), [Done(assistant("one"))]], "turn-1")await routed_turn(store, now, CLAUDE, [[Done(assistant("two"))]], "turn-2")now[0] += 600assert not await routed_turn(store, now, GPT, [[Done(assistant("three"))]], "turn-3")- now[0] += 60+ now[0] += 3_001assert len(await routed_turn(store, now, CLAUDE, [[Done(assistant("four"))]], "turn-4")) == 4@@ -1632,7 +1633,7 @@ async def test_a_converted_route_cache_is_its_upstream_provider_and_model():now[0] += 3_000assert not await routed_turn(store, now, GEMINI, [[Done(assistant("three"))]], "turn-3", GEMINI_SERVED)now[0] += 3_000- assert not await routed_turn(store, now, ENDPOINT, [[Done(assistant("four"))]], "turn-4")+ assert not await routed_turn(store, now, UNKNOWN, [[Done(assistant("four"))]], "turn-4")now[0] += 700assert len(await routed_turn(store, now, GEMINI, [[Done(assistant("five"))]], "turn-5", GEMINI_SERVED)) == 4diff --git a/tests/agent_core/agent/test_context.py b/tests/agent_core/agent/test_context.pyindex e5f560926..86f283b83 100644--- a/tests/agent_core/agent/test_context.py+++ b/tests/agent_core/agent/test_context.py@@ -496,7 +496,10 @@ def test_a_checkpoint_or_another_route_invalidates_the_anchor():@pytest.mark.parametrize("protocol,provider,model,lifetime",[- ("anthropic", "anthropic", "claude-opus-5-5", 300), # message breakpoints: ephemeral, no ttl+ ("anthropic", "anthropic", "claude-opus-5-5", 3_600), # official Claude breakpoints: ttl 1h+ ("anthropic", "anthropic", "gpt-5", 300), # non-Claude Anthropic route: default ephemeral+ ("anthropic", "custom", "claude-opus-5-5", 300), # other vendor on Anthropic protocol+ ("anthropic", "moonshot", "kimi-k2", 300), # other vendor on Anthropic protocol("openai_responses", "openai", "gpt-6-astra", 1_800),("openai_responses", "openai", "gpt-6.1-sol", 1_800),("openai_chat", "openai", "gpt-5.6-mini", 3_600), # OpenAI cache retention is official Responses-only@@ -506,8 +509,8 @@ def test_a_checkpoint_or_another_route_invalidates_the_anchor():("openai_responses", "openai", "my-gpt-6-astra", 3_600), # parsed whole, never searched for("openai_chat", "openrouter", "openai/gpt-6-astra", 3_600), # a relay("openai_responses", "xai", "grok/grok-4.6", 3_600),- ("anthropic", "moonshot", "kimi-k2", 3_600), # another vendor on the Anthropic protocol- ("anthropic", "test-provider", "test-model", 3_600),+ ("anthropic", "moonshot", "kimi-k2", 300), # another vendor on the Anthropic protocol+ ("anthropic", "test-provider", "test-model", 300),],)def test_a_route_cache_lives_as_long_as_its_family_documents_else_an_hour(protocol, provider, model, lifetime):diff --git a/tests/agent_core/ai/test_anthropic_prompt_cache.py b/tests/agent_core/ai/test_anthropic_prompt_cache.pyindex 8b4ba5d73..9f470ad85 100644--- a/tests/agent_core/ai/test_anthropic_prompt_cache.py+++ b/tests/agent_core/ai/test_anthropic_prompt_cache.py@@ -1,7 +1,7 @@"""Anthropic prompt-cache breakpoints, read on the wire at the adapter's HTTP boundary.-A Claude route caches its static prefix (tools, then system) for an hour and the conversation for the default five-minutes; every other route keeps the default everywhere. An endpoint that rejects the field never fails a turn+A Claude route caches its static prefix and conversation breakpoints for one hour; every other route keeps Anthropic's+five-minute default. An endpoint that rejects the field never fails a turn(C-2 ``conversion-tables.md``, Anthropic "Cache" row). Usage parsing of the TTL breakdown is pinned by``test_provider_adapters.py`` (``test_anthropic_usage_*``)."""@@ -16,9 +16,10 @@ from typing import Anyimport httpximport pytest-from core.agent_core.ai.anthropic import AnthropicAdapter+from core.agent_core.ai.anthropic import AnthropicAdapter, cache_ttl_s as anthropic_cache_ttl_sfrom core.agent_core.ai.provider import Done, ModelEndpoint, ModelRequest, ProviderErrorfrom core.agent_core.cancel import CancelToken+from core.agent_core.harness.context import cache_ttl_s as context_cache_ttl_sfrom core.agent_core.messages import AssistantMessage, Origin, TextBlock, ToolCallBlock, ToolResultMessage, UserMessagefrom core.agent_core.tools.base import ToolSpecfrom core.handlers.model_hub.provenance import produce_turn_outcome, render_turn_outcome_copy@@ -74,6 +75,7 @@ def _request(messages: tuple[Any, ...] = CONVERSATION,base_url: str | None = None,source_id: str | None = None,+ cache_ttl: str | None = None,) -> ModelRequest:# A fresh endpoint per request unless one is given: a route's TTL memory lasts the process.return ModelRequest(@@ -84,6 +86,7 @@ def _request("token",provider=provider,source_id=source_id,+ cache_ttl=cache_ttl,),system=system,messages=messages,@@ -164,8 +167,8 @@ async def test_only_a_claude_route_caches_its_static_prefix_for_an_hour(assert _breakpoints(body) == [("tools.1", prefix),("system.0", prefix),- ("messages.0.content.0", DEFAULT),- ("messages.2.content.0", DEFAULT),+ ("messages.0.content.0", prefix),+ ("messages.2.content.0", prefix),]if prefix is not HOUR:# Today's request, unchanged: no route but a Claude one ever sees the field.@@ -186,16 +189,14 @@ async def test_only_a_claude_route_caches_its_static_prefix_for_an_hour(async def test_a_longer_cache_lifetime_never_follows_a_shorter_one(system: str, tools: tuple[ToolSpec, ...], messages: tuple[Any, ...],) -> None:- # Anthropic rejects a 1h breakpoint evaluated after a 5m one; tools, system, messages is its evaluation order.+ # A 1h breakpoint must never follow a shorter one. Supported Claude requests use one hour for all breakpoints;+ # requests without a static prefix use Anthropic's default everywhere._raw, body = await _wire(_request(system=system, tools=tools, messages=messages))-+ expected = HOUR if system or tools else DEFAULTlifetimes = [LIFETIME_S[cache_control.get("ttl")] for _, cache_control in _breakpoints(body)]assert lifetimes == sorted(lifetimes, reverse=True)assert len(lifetimes) <= 4- assert all(- where.startswith(("tools.", "system.")) == (cache_control == HOUR)- for where, cache_control in _breakpoints(body)- )+ assert all(cache_control == expected for _, cache_control in _breakpoints(body))@pytest.mark.parametrize("rejection", [ANTHROPIC_REJECTION, GATEWAY_REJECTION], ids=["endpoint", "model-hub"])@@ -215,7 +216,7 @@ async def test_a_rejected_ttl_is_retried_once_without_it_and_remembered_for_the_caplog.set_level(logging.WARNING, logger="core.agent_core.ai.anthropic")# Two Sources behind one gateway URL, as every Vibey hop is.gateway = f"https://{uuid.uuid4().hex}.test/vibey/v1"- request = _request(base_url=gateway, source_id="src_relayaaaa01")+ request = _request(base_url=gateway, source_id="src_relayaaaa01", cache_ttl="1h")async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:runs = [await _run(client, request),@@ -237,6 +238,7 @@ async def test_a_rejected_ttl_is_retried_once_without_it_and_remembered_for_the_assert other_retried == _without_ttl(other_rejected)assert _carries_ttl(source_rejected)assert source_retried == _without_ttl(source_rejected)+ assert context_cache_ttl_s(request.endpoint, request=request) == 300for events in runs:# One terminal per run: a rejected attempt never reaches the consumer.terminals = [event for event in events if isinstance(event, (Done, ProviderError))]@@ -276,3 +278,22 @@ async def test_a_rejection_the_ttl_did_not_cause_is_surfaced_without_a_retry(proassert events[-1].kind == "invalid_request"# The route was not downgraded: its next request asks for the hour again.assert _carries_ttl(sent[1]) == (provider == "anthropic")+++ ("provider", "model_id", "cache", "expected"),+ [+ ("anthropic", CLAUDE, "default", 3_600),+ ("anthropic", "gpt-5", "default", 300),+ ("custom", CLAUDE, "default", 300),+ ("anthropic", CLAUDE, "none", 300),+ ],+)+async def test_assumed_cache_lifetime_matches_the_breakpoints_on_the_wire(+ provider: str, model_id: str, cache: str, expected: int,+) -> None:+ request = _request(provider, model_id, cache=cache)+ _raw, body = await _wire(request)+ assert (3_600 if _carries_ttl(body) else 300) == expected+ assert anthropic_cache_ttl_s(request) == expected+ assert context_cache_ttl_s(request.endpoint, request=request) == expecteddiff --git a/tests/agent_core/ai/test_hosted_pause_turn.py b/tests/agent_core/ai/test_hosted_pause_turn.pyindex 71820d7ab..8dfa58fc6 100644--- a/tests/agent_core/ai/test_hosted_pause_turn.py+++ b/tests/agent_core/ai/test_hosted_pause_turn.py@@ -329,7 +329,9 @@ async def test_a_continuation_whose_route_refuses_the_cache_lifetime_is_sent_agafirst, refused, retried = (json.loads(wire.content) for wire in sent)assert '"ttl"' in json.dumps(refused) and '"ttl"' not in json.dumps(retried)- assert refused["messages"] == retried["messages"]+ assert refused["messages"] != retried["messages"]+ assert refused["messages"][0]["content"][0]["cache_control"] == {"type": "ephemeral", "ttl": "1h"}+ assert retried["messages"][0]["content"][0]["cache_control"] == {"type": "ephemeral"}message = events[-1].messageassert [block.text for block in message.content if isinstance(block, TextBlock)] == ["part 1", "part 3"]assert message.usage == Usage(input_tokens=10, output_tokens=1)diff --git a/tests/test_model_hub_routing_modes.py b/tests/test_model_hub_routing_modes.pyindex 5333e2961..de3e4a8ad 100644--- a/tests/test_model_hub_routing_modes.py+++ b/tests/test_model_hub_routing_modes.py@@ -95,6 +95,27 @@ def test_prompt_cache_override_rejects_unsupported_official_values():validate_cache_ttl_override(config, "vibey", route)+def test_prompt_cache_override_rejects_unsupported_official_anthropic_values():+ source = _source("src_anthropic001", ("claude-opus-4-6",), vendor="anthropic")+ config = ModelHubConfig(sources=[source])+ route = ModelHubRouteConfig(+ hops=(ModelHubRouteHopConfig(source.id, "claude-opus-4-6"),),+ cache_ttl="5m",+ )+ with pytest.raises(ValueError, match="only 1h"):+ validate_cache_ttl_override(config, "vibey", route)+++def test_prompt_cache_override_accepts_the_official_anthropic_one_hour_mode():+ source = _source("src_anthropic001", ("claude-opus-4-6",), vendor="anthropic")+ config = ModelHubConfig(sources=[source])+ route = ModelHubRouteConfig(+ hops=(ModelHubRouteHopConfig(source.id, "claude-opus-4-6"),),+ cache_ttl="1h",+ )+ validate_cache_ttl_override(config, "vibey", route)++def test_prompt_cache_override_is_rejected_for_non_vibey_routes():source = _source("src_openai001", ("gpt-6-astra",), vendor="openai")config = ModelHubConfig(sources=[source])