Skip to content

The agent that grows with you

fix(agent): close() also releases a prompt-caching split system prompt from the token memo

kshitijk4poor1f502aded81a
diff --git a/run_agent.py b/run_agent.py
index a2220dc5bad..eda287031f8 100644
--- a/run_agent.py
+++ b/run_agent.py
@@ -955,9 +955,12 @@ class AIAgent(
self._streamed_assistant_text_parts = []
if isinstance(session_messages, list):
# The token-estimate memo pins message strings; a closed conversation's can never hit again. The
- # system prompt reaches the memo only through api_messages, never through the history.
+ # system prompt reaches the memo only through api_messages, never through the history, and a
+ # prompt-caching request splits it into the static prefix + a per-request suffix slice: the
+ # prefix's pin evicts that split entry.
from agent.model_metadata_token_memo import forget_message_token_memo
- _quietly(forget_message_token_memo, (session_messages, getattr(self, "_cached_system_prompt", None)))
+ _quietly(forget_message_token_memo, (session_messages, getattr(self, "_cached_system_prompt", None),
+ getattr(self, "_cached_system_prompt_static", None)))
_quietly(self._trim_process_memory)
_quietly(self._finalize_owned_session_row)
diff --git a/tests/agent/test_token_memo_release_on_close.py b/tests/agent/test_token_memo_release_on_close.py
index b80b40b1acf..cf2a27f135b 100644
--- a/tests/agent/test_token_memo_release_on_close.py
+++ b/tests/agent/test_token_memo_release_on_close.py
@@ -100,3 +100,22 @@ def test_compression_telemetry_does_not_pin_the_one_shot_summary_prompt():
assert recorder._active_compression_telemetry["aux_prompt_tokens"] > 0
assert id(prompt) not in _pinned_ids()
+
+
+def test_close_evicts_a_prompt_caching_split_system_prompt():
+ from agent.prompt_caching import apply_anthropic_cache_control
+
+ static = "the static, cacheable system prefix " * 2000
+ full = static + "\nper-session suffix: today's date and the active model"
+ request = apply_anthropic_cache_control([{"role": "system", "content": full}, {"role": "user", "content": "hi"}],
+ static_system_prefix=static)
+ assert isinstance(request[0]["content"], list) # the real split shape the memo sees
+ estimate_messages_tokens_rough(request)
+ agent = _closing_agent([{"role": "user", "content": "hi"}])
+ agent._cached_system_prompt = full
+ agent._cached_system_prompt_static = static
+ assert id(static) in _pinned_ids()
+
+ agent.close()
+
+ assert id(static) not in _pinned_ids()