When must an instance abort a retry storm it cannot observe: wall-clock latency budget exhaustion versus per-tool cost attribution divergence in self-reporting agent loops
**Date of Record:** 2026 December 2 **Scope:** Applies to agent loops whose runtime observability is derived from the agent's own emitted telemetry (tool-call spans, retry counters, token/cost accounting) rather than an out-of-band supervisor. Does not apply to externally instrumented loops where a sidecar proxy owns the span lifecycle and can attribute cost independently of the model's self-report.
Core Distinction
The failure is not that retries happen. It is that the retry counter and the wall-clock latency budget are *observed through the same channel that retries corrupt*. An agent loop typically attributes cost and latency per tool by having the model emit a structured record after each tool call: `{tool, attempt, elapsed_ms, tokens_in, tokens_out, cost_usd, status}`. When a tool call fails and the loop retries, the retry itself consumes the very budget it is trying to report against, and the reporting emission is *also* a tool call (a logging/emit action) that can fail, be delayed, or be dropped. The agent therefore cannot distinguish three states that are observationally identical from inside the loop:
- The tool is slow but succeeding; budget is fine.
- The tool is failing fast and retrying; budget is being burned but each attempt looks cheap.
- The observability channel itself is saturated, so the last known span is stale and the loop is blind.
The invariant that must hold: **the latency budget consumed by retries plus the latency budget consumed by telemetry emission must be accounted against a single monotonic clock the agent does not own.** When the agent owns the clock (uses `time.monotonic()` inside its own process and reports deltas), a retry storm inflates apparent elapsed time per attempt while the *aggregate* remains underreported because dropped emit calls never contribute. The loop concludes it has budget remaining when it does not.
What Was Observed
A tool-calling agent was wired to a flaky upstream (`search_index`) that returned `503` under load with a ~40% failure rate during a 90-second window. The loop had a 120s wall-clock budget and a per-tool retry cap of 5 with exponential backoff (100ms base, jitter). Telemetry was emitted via a `log_span` tool after every attempt.
Trace reconstruction from the out-of-band proxy (which the agent did not consult):
- Attempts 1–3 on `search_index`: 503, 503, 503. Each emitted a span. Agent's internal elapsed estimate: 100+200+400ms backoff + ~15ms per call ≈ 745ms.
- Attempt 4: succeeded at 1.9s (upstream cold cache). Span emitted. Agent's internal elapsed: ~2.6s.
- Attempt 5: the *next* task's first `search_index` call began retrying. This time the `log_span` tool call itself hit a rate limit on the telemetry sink and returned `429`. The agent's loop treated the emit failure as a non-fatal warning and continued without recording the span. The retry counter for `search_index` incremented, but no span was persisted.
- Attempts 6–9: three more 503s and one success, of which two `log_span` emissions were dropped (429) and one was delayed by 8s in the sink queue.
From the agent's perspective at the 90s mark: elapsed ≈ 61s (sum of recorded spans + backoff), retries on `search_index` = 4 of 5, cost attributed to `search_index` = $0.11. From the proxy: true elapsed = 118s (budget nearly exhausted), true retry count = 9 (cap exceeded, because dropped spans did not increment the persisted counter), true cost = $0.34 (unrecorded emissions still cost tokens).
The agent then spent its remaining budget on a *new* tool (`summarize`) because it believed it had 59s left. It did not. The loop terminated mid-summarize at the hard 120s cutoff, losing the partial result and the unflushed spans.
This is the non-obvious part: **the retry cap and the latency budget diverged because they were updated by different, lossy paths.** The retry counter was in-memory and authoritative; the latency budget was reconstructed from persisted spans that could be dropped. The loop's decision to continue was correct given its observations and catastrophically wrong given ground truth.
Condition of Validity
The failure requires all of:
- **Self-reported telemetry.** The agent's view of elapsed time and cost is assembled from spans it emits, and emission is itself a fallible tool call.
- **Lossy emit path.** The telemetry sink can drop, delay, or reject (429/queue-full) emissions without the agent treating that as a budget event.
- **Retry backoff that is not charged to the budget.** Backoff sleeps are wall-clock time but are not tool calls, so they generate no span and are invisible to a span-summing budget estimator.
- **A single shared wall-clock deadline** across tools, so budget burned by one tool's retries starves another's execution.
It does *not* occur when: the supervisor owns the deadline and kills the loop; when emission failures are escalated to budget deductions; or when backoff is charged against a monotonic deadline the agent reads directly rather than reconstructs.
Decision Boundary
The loop must treat **every emit failure as a budget debit of unknown magnitude**, and must never reconstruct remaining budget from span sums alone. Concretely, the decision boundary is:
- Maintain a monotonic deadline `t_deadline` captured at loop start from a clock the loop reads directly (not from span deltas).
- On any `log_span` failure, debit a *floor* equal to the last successful inter-span interval, and mark the budget estimate as *degraded*.
- When degraded, the loop may continue only if `now < t_deadline - safety_margin`, where `safety_margin` ≥ the maximum observed emit latency (queue delay included).
- When the retry counter's persisted value and the in-memory value disagree (because a span was dropped), the in-memory value wins for capping, but the persisted value is flagged for reconciliation. Never let a dropped span *reduce* the retry count.
The boundary is crossed the moment the loop decides to start a new tool while `degraded == True` and the remaining budget is within one `safety_margin`. At that point it must abort or checkpoint, not proceed.
Operational Effect
Loops that ignore this boundary exhibit a characteristic signature: they finish "under budget" by their own accounting while the supervisor records a timeout, and their per-tool cost attribution undercounts the tools that triggered the most retries—precisely the tools an operator most needs to see. The corrective is architectural, not a prompt fix: the deadline and the retry cap must be owned by a component that does not depend on the loop's own emit path, and any divergence between in-memory and persisted counters must be treated as evidence of blindness, not as noise to be smoothed. The guardrail is a hard invariant: *a dropped span is a debit, never a discount.*
import time
from dataclasses import dataclass, field
class BudgetBlindnessError(RuntimeError):
"""Raised when the loop's self-reported budget diverges from the monotonic deadline."""
pass
@dataclass
class LoopBudget:
deadline_monotonic: float
max_emit_latency_s: float = 0.0
degraded: bool = False
last_span_interval_s: float = 0.0
retries_in_memory: int = 0
retries_persisted: int = 0
retry_cap: int = 5
safety_margin_s: float = 2.0
def remaining(self, now: float) -> float:
return self.deadline_monotonic - now
def on_emit_failure(self, now: float, observed_interval_s: float) -> None:
# A dropped span is a debit, never a discount.
self.degraded = True
self.last_span_interval_s = max(self.last_span_interval_s, observed_interval_s)
# Charge the floor: the interval we could not observe.
self.deadline_monotonic -= observed_interval_s
def on_emit_success(self, now: float, emit_latency_s: float, interval_s: float) -> None:
self.max_emit_latency_s = max(self.max_emit_latency_s, emit_latency_s)
self.last_span_interval_s = interval_s
if self.max_emit_latency_s <= self.safety_margin_s:
self.degraded = False
def on_retry(self) -> None:
self.retries_in_memory += 1
# Persisted counter only advances when the span lands; never let it regress.
if self.retries_persisted < self.retries_in_memory:
self.retries_persisted = self.retries_in_memory
def guard_new_tool(budget: LoopBudget, now: float) -> None:
if budget.retries_in_memory != budget.retries_persisted:
# Divergence means at least one span was dropped: we are blind.
budget.degraded = True
if budget.retries_in_memory >= budget.retry_cap:
raise BudgetBlindnessError(
f"retry cap hit: in_memory={budget.retries_in_memory} "
f"persisted={budget.retries_persisted}"
)
remaining = budget.remaining(now)
if budget.degraded and remaining <= budget.safety_margin_s + budget.max_emit_latency_s:
raise BudgetBlindnessError(
f"degraded observability with remaining={remaining:.2f}s "
f"<= margin={budget.safety_margin_s}s + max_emit={budget.max_emit_latency_s:.2f}s; "
f"abort or checkpoint"
)
if remaining <= 0:
raise BudgetBlindnessError(f"hard deadline exceeded: remaining={remaining:.2f}s")