Skip to content

Commit 8eab506

Browse files
committed
fix(agent): a retried request warms from its own send time, not the retry loop start
api_start_time marks the start of the whole retry loop, so after a slow failed attempt or a long backoff a fresh cache entry looked old and the warmer refreshed at once or stopped as expired. build_request_payload (once per attempt) now records the attempt's send time and the warmer anchors on it.
1 parent 8f7782a commit 8eab506

2 files changed

Lines changed: 7 additions & 1 deletion

File tree

‎agent/turn_api_request.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
from dataclasses import dataclass
1212
import logging
13+
import time
1314
from typing import Any
1415

1516
from agent.message_sanitization import sanitize_outbound_kwargs, strip_images_for_rejecting_model
@@ -120,6 +121,9 @@ def build_request_payload(
120121

121122
# The real request about to go out refreshes the cache entry itself.
122123
cancel_prompt_cache_warming(agent, "new request in flight")
124+
# This attempt's send time: a retried request's cache entry dates from here, not from the
125+
# start of the retry loop (the warmer schedules its first refresh from it).
126+
agent._last_api_attempt_sent_at = time.time()
123127
agent._reset_stream_delivery_tracking()
124128
# Per-attempt first-chunk timestamp so a stale value never leaks into post_api_request.
125129
agent._last_api_first_chunk_at = None

‎agent/turn_response_check.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,9 @@ def _verdict(action: str, result: Optional[dict[str, Any]] = None) -> ResponseCh
204204
_last_preflight_pressure = None
205205
# Arm a one-token replay of THIS request before its Anthropic cache entry expires, so a tool
206206
# round longer than the TTL does not turn the next request into a full-price cache write.
207-
start_prompt_cache_warming(agent, api_kwargs, response, sent_at=api_start_time)
207+
start_prompt_cache_warming(
208+
agent, api_kwargs, response, sent_at=getattr(agent, "_last_api_attempt_sent_at", None) or api_start_time,
209+
)
208210

209211
_retry.has_retried_429 = False
210212
# Clearing Nous rate-limit state proves the limit reset so other sessions may resume.

0 commit comments

Comments
 (0)