Skip to content

feat: Add LLM retry mechanism with exponential backoff - #38640

Closed
fanzhidongyzby wants to merge 3 commits into
openclaw:mainfrom
fanzhidongyzby:feat/llm-retry-mechanism
Closed

fanzhidongyzby wants to merge 3 commits into
openclaw:mainfrom
fanzhidongyzby:feat/llm-retry-mechanism

Conversation

@fanzhidongyzby

@fanzhidongyzby fanzhidongyzby commented Mar 7, 2026 •

Copy link
Copy Markdown

Changes

  • Add llm-retry.ts implementing exponential backoff retry logic
  • Wrap prompt() method in attempt.ts with retry handler
  • Support HTTP 429, 500, 502, 503, 504 auto-retry
  • Max 7 retries with delays: 0s, 1s, 2s, 4s, 8s, 16s, 32s

Motivation

The API quota limit (HTTP 429) is triggered when the Subagent concurrently calls LLM. OpenClaw's pi-ai provider has no built-in retry and needs to be implemented at the framework layer.

Copilot AI review requested due to automatic review settings March 7, 2026 06:29
@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: S labels Mar 7, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7906c394e8

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@greptile-apps

greptile-apps Bot commented Mar 7, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR adds an exponential-backoff retry wrapper (withLlmRetry) around the prompt() call in the pi-embedded-runner to handle transient LLM API failures (429, 5xx, network errors). The approach — plugging into the existing retryAsync infrastructure, extracting Retry-After hints from headers, and wrapping the session's prompt method — is sound.

Issue found: The message-pattern regex in llm-retry.ts (lines 121–128 and 131–136) captures numeric values but fails to capture the unit separately. The code always multiplies by 1000, causing millisecond hints (e.g., "retry in 500ms") to be interpreted as 500-second delays instead of 500 ms. This is a concrete logic bug that will cause excessive retry delays when error messages use millisecond units.

Confidence Score: 2/5

  • PR contains a unit-parsing logic bug that will cause excessively long retry delays (up to 1000x) when errors mention millisecond timeouts.
  • The ms/seconds parsing bug is a straightforward logic error with a concrete, reproducible trigger. When an error message contains "retry in X ms", the code will multiply X by 1000, resulting in X-second delays instead of X-millisecond delays. This is a real bug that affects retry behavior, though the fallback exponential backoff will still work. Recommend fixing before merge.
  • src/agents/pi-embedded-runner/llm-retry.ts (lines 121–128 and 131–136: unit parsing logic)

Last reviewed commit: 7906c39

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a framework-level retry/backoff wrapper for embedded Pi agent LLM calls to mitigate transient provider failures (notably HTTP 429 quota/rate limits) during concurrent subagent execution.

Changes:

  • Introduces withLlmRetry() helper using shared retryAsync() exponential backoff logic.
  • Wraps activeSession.prompt() in the embedded runner attempt loop with the retry handler.
  • Adds retryability heuristics (HTTP 429/5xx, some network/timeout signals) and Retry-After extraction.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 7 comments.

File Description
src/agents/pi-embedded-runner/run/attempt.ts Wraps the embedded session prompt() method with retry/backoff before prompting the model.
src/agents/pi-embedded-runner/llm-retry.ts New retry helper: retryable-error classification, retry-after parsing, and retryAsync wiring.

You can also share your feedback on Copilot code review. Take the survey.

- Add llm-retry.ts with 7-retry exponential backoff (1-32s)
- Wrap prompt() method in attempt.ts with retry logic
- Support HTTP 429/500/502/503/504 auto-retry
- Extract Retry-After delay from headers and error messages
- Configurable attempts, delays, and jitter
…fety

- Add comprehensive unit tests for isRetryableLlmError() covering 429, 5xx, network errors
- Add comprehensive unit tests for extractRetryAfterMs() covering ms/s parsing, statusCode formats
- Fix regular expressions to support:
  - DEADLINE_EXCEEDED (underscore variant)
  - Whitespace in retry/reset patterns
- Use type-safe wrapper for prompt method in attempt.ts (typeof activeSession.prompt)
- All tests passing (31/31)

Fixes review comments for PR openclaw#38640
@fanzhidongyzby
fanzhidongyzby force-pushed the feat/llm-retry-mechanism branch from 2b06ec4 to bb24a1a Compare March 7, 2026 07:09

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: bb24a1a943

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

- Fix P1: Make unit capture optional in regex to handle messages without explicit units (defaults to seconds)
- Fix P2: Reorder error checks - check status codes first, then only check timeout/message when not 4xx
- Fix P2: Add comprehensive JSDoc comments for isRetryableLlmError and extractRetryAfterMs
- Fix P2: Simplify type-safe prompt wrapper using Parameters<> and typeof

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a6951d27e7

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@openclaw-barnacle

Copy link
Copy Markdown

This pull request has been automatically marked as stale due to inactivity.
Please add updates or it will be closed.

@openclaw-barnacle openclaw-barnacle Bot added the stale Marked as stale due to inactivity label Apr 22, 2026
@clawsweeper

clawsweeper Bot commented Apr 26, 2026 •

Copy link
Copy Markdown
Contributor

Thanks for the context here. I swept through the related work, and this is now duplicate or superseded.

Close as superseded: the remaining LLM retry feature is already tracked by #49376 and the active provider-level retry PR #61443, while this branch retries at the prompt wrapper in a way that conflicts with current failover/auth-profile rotation and still has concrete correctness issues.

So I’m closing this here and keeping the remaining discussion on the canonical linked item.

Review details

Best possible solution:

Keep #49376/#61443 as the canonical path for configurable provider-call retry, and fold any useful retry-after parsing coverage into that design without adding a prompt-level wrapper that delays auth-profile rotation or model fallback.

Do we have a high-confidence way to reproduce the issue?

Yes for the proposed patch defects: static review shows the PR passes a signal that the retry primitive cannot honor, compares status values without string normalization, and retries same-session prompt failures before the current failover path can rotate profiles or models.

Is this the best way to solve the issue?

No, this is not the best way to solve the issue as-is. The retry policy should be coordinated with the existing provider/failover classifier and configurable retry surface being tracked in #49376/#61443.

Security review:

Security review cleared: The diff adds no dependencies, workflows, package scripts, lockfile changes, secret handling, or permission changes; the extra-call behavior is a functional retry/failover concern rather than a concrete supply-chain issue.

What I checked:

Likely related people:

  • Shakker: Local blame in the current checkout points the central runner, retry, and model-failover documentation lines to commit a4fd45c; the local history is grafted, so this is strongest as recent maintainer routing rather than original authorship. (role: recent maintainer; confidence: medium; commits: a4fd45ca3186; files: src/agents/pi-embedded-runner/run.ts, src/infra/retry.ts, docs/concepts/model-failover.md)
  • @aaron-he-zhu: Current changelog history credits related agents/failover classifier work in fix(agents): normalize provider errors for better failover #58856, which is adjacent to the string-status and structured-provider-error behavior this PR would duplicate. (role: adjacent failover-history contributor; confidence: low; files: CHANGELOG.md, src/agents/failover-error.ts, src/agents/pi-embedded-helpers/errors.ts)

Codex review notes: model gpt-5.5, reasoning high; reviewed against 757af70bf7ca.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling size: M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants