agentmemory icon indicating copy to clipboard operation
agentmemory copied to clipboard

fix(provider): honor Retry-After and avoid circuit-breaker amplification on HTTP 429

Open geek111 opened this issue 4 months ago • 1 comments

Summary

AgentMemory currently treats provider HTTP 429 responses as ordinary failures. On bursty LLM workloads this can open the circuit breaker immediately, discard the provider's Retry-After guidance, and turn a short rate-limit window into a long run of circuit_breaker_open failures.

Observed behavior

Validated on AgentMemory 0.9.27 with an OpenAI-compatible hosted provider while backfilling a historical knowledge graph:

  • 7 graph-extraction calls completed successfully
  • 3 calls then returned HTTP 429
  • those failures were followed by 113 circuit_breaker_open results
  • restarting the process closed the breaker, but the unfinished work had no provider-level retry path

No credentials or request contents are included in this report.

Root cause

  1. The OpenAI-compatible provider throws a generic error for non-2xx responses, so callers lose the structured HTTP status and Retry-After header.
  2. ResilientProvider records every 429 as a circuit-breaker failure immediately.
  3. Summarization and graph workloads can issue several LLM calls close together, which amplifies a provider's rate limit.

Proposed behavior

  • Preserve provider error metadata, at minimum status and parsed retryAfterMs.
  • Parse both supported Retry-After forms: delta-seconds and HTTP-date.
  • For HTTP 429, keep the call in a provider queue and retry using Retry-After, falling back to configurable delays.
  • Record a circuit-breaker failure only after the configured 429 retries are exhausted.
  • Make provider concurrency configurable (for example AGENTMEMORY_LLM_CONCURRENCY) rather than forcing global serialization for high-throughput providers.
  • Keep retry delays and maximum retries configurable.

Suggested defaults for low-rate hosted endpoints:

AGENTMEMORY_LLM_MAX_RETRIES=3
AGENTMEMORY_LLM_RETRY_DELAYS_MS=10000,30000,60000
AGENTMEMORY_LLM_CONCURRENCY=1

Test coverage

  • concurrent calls never exceed configured concurrency
  • a 429 with Retry-After succeeds on retry
  • both delta-seconds and HTTP-date parsing
  • exhausted retries count as one breaker failure, not one failure per attempt
  • a rejected queued call does not block later calls

Relationship to #766

#766 proposes durable, operation-level auditing and delayed retry after consolidation/extraction failures. This proposal is complementary and lower-level: handle a short provider rate-limit window immediately before the operation is declared failed and handed to a durable retry sweep.

geek111 avatar Jul 19 '26 11:07 geek111

Complementary data point from a different trigger — same amplification, but the error is permanent rather than transient, which I think argues for a second rule alongside the Retry-After handling proposed here.

Setup: agentmemory 0.9.28, local OpenAI-compatible endpoint, backfilling summaries across ~900 historical sessions. No credentials or request contents included.

What happened: a small number of chunks exceeded the loaded model's context window and returned HTTP 400 ("Context size has been exceeded"). Measured over one ~6-hour window:

  • 24 genuine 400s
  • 2,404 subsequent circuit_breaker_open results
  • 133 distinct sessions failed wholesale as a result, each reported to the caller as too_many_chunks_skipped: N/N chunks failed to parse after retry

The ratio is the point: ~100 rejected calls per real error, and the sessions taken out were entirely healthy — they simply arrived while the breaker was open.

Why the fix proposed here would not cover it: a 400 for an oversized prompt is deterministic. Retrying it after Retry-After fails identically, and there is no Retry-After header to honor. Queue-and-retry is the right answer for 429; it is the wrong answer for this class.

Suggested additional rule: distinguish provider-health failures from per-request failures when deciding whether to trip the breaker. The breaker exists to protect against the provider being unavailable — but a prompt that is too large for the configured context says nothing about provider health. A 400-class response caused by request shape (context exceeded, malformed request, unsupported parameter) should fail that chunk only and not count toward the global breaker at all. Otherwise one badly-sized request poisons every unrelated call for the duration of the cooldown.

Two smaller observations from the same incident, in case they are useful:

  1. The user-visible error is misleading. All three of these surface to the caller as too_many_chunks_skipped: N/N chunks failed to parse after retry — (a) a genuine XML parse failure, (b) a network/timeout failure (fetch failed), and (c) circuit_breaker_open. Only the chunk-level log distinguishes them, so the session-level error sends you looking at the parser when the cause is the breaker. Surfacing the dominant underlying error in the session-level message would save a lot of misdiagnosis.

  2. Fail-fast callers amplify it further. My backfill script advanced immediately on an error response instead of waiting — which meant it sprinted through the work queue while the breaker was open, converting a cooldown window into dozens of lost sessions. Anything that retries or advances quickly on failure will make this worse, so the breaker not opening on per-request errors matters more than it might first appear.

Partial-failure summaries produced by this mechanism can also silently lose fields — filed separately as #1114.

tr3lane avatar Jul 26 '26 22:07 tr3lane