Skip to content

feat(llm): support custom retry status codes via retry_codes config - #818

Merged
lizhengfeng101 merged 6 commits into
mainfrom
feat/retry-codes
Aug 10, 2026
Merged

lizhengfeng101 merged 6 commits into
mainfrom
feat/retry-codes

Conversation

@lizhengfeng101

@lizhengfeng101 lizhengfeng101 commented Aug 10, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Some self-hosted LLM clusters misuse HTTP status codes — e.g., returning 403 or 400 for rate limiting instead of the standard 429. This PR adds a retry_codes configuration field that lets users specify additional 4xx status codes to trigger exponential backoff retry.

  • Adds retry_codes field to provider config, legacy llm config, and CLI config set
  • Implements SDK middleware that injects x-should-retry: true response header on configured status codes, reusing the existing SDK retry mechanism (up to 5 retries with exponential backoff)
  • Redundant codes (408/409/429) are silently filtered at resolve time and emit a warning at config set time, rather than blocking startup
  • Fixes cloneProviderEntry to copy previously missing fields (TimeoutSec, ExtraHeaders, RetryCodes)

Configuration example

{
  "provider": "my-cluster",
  "custom_providers": {
    "my-cluster": {
      "api_key": "sk-xxx",
      "url": "https://internal.llm.corp/v1",
      "protocol": "openai",
      "model": "gpt-4o",
      "retry_codes": [403, 400]
    }
  }
}

Or via CLI:

ocr config set custom_providers.my-cluster.retry_codes 403,400

Bugfix: cloneProviderEntry data loss

cloneProviderEntry (used by TUI for rollback snapshots) was missing TimeoutSec, ExtraHeaders, and RetryCodes. This caused silent data loss on failed edits — rolling back would discard the user's timeout and extra headers settings. Now fixed with full test coverage for deep-copy isolation and nil preservation.

Test plan

  • make check passes (formatting, vet, license headers)
  • make test passes (all existing + new tests)
  • TestCloneProviderEntry_TimeoutAndRetryCodes — verifies deep-copy and mutation isolation
  • TestCloneProviderEntry_NilRetryCodesAndExtraHeaders — pins nil-stays-nil behavior
  • Manual test: configure retry_codes with a mock server returning the configured status code, verify retry behavior

Add a retry_codes configuration field that allows users to specify
additional HTTP status codes (4xx) that should trigger exponential
backoff retry. This targets self-hosted LLM clusters that misuse
status codes like 403 or 400 for rate limiting.

Implementation uses SDK middleware to inject the x-should-retry: true
response header when a configured status code is encountered, leveraging
the existing SDK retry mechanism (up to 5 retries with exponential
backoff) without any SDK modifications.

Configuration is supported via:
- Provider config: providers.<name>.retry_codes / custom_providers.<name>.retry_codes
- Legacy llm config: llm.retry_codes
- CLI: ocr config set providers.<name>.retry_codes 403,400

Also fixes cloneProviderEntry to copy previously missing fields
(TimeoutSec, ExtraHeaders, RetryCodes) for TUI rollback correctness.
@github-actions

Copy link
Copy Markdown
Contributor

✅ OpenCodeReview: Review complete: 0 finding(s) across 5 selected item(s).

@wu21-web wu21-web 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.

The approach is sound and the wiring is complete. The only real gap is missing tests for the new functions.

Cover ParseRetryCodes validation, retryCodesMiddleware behavior,
resolver integration (provider and legacy config paths, invalid codes),
and end-to-end retry via httptest for both OpenAI and Anthropic clients.
…d of erroring

Replace validateRetryCodes with sanitizeRetryCodes that filters out
SDK-default codes and returns warnings. The resolve phase silently
drops redundant codes without interrupting startup. The config set
command prints warnings to stderr so users are informed.

This makes the tool friendlier for users migrating from other tools
who habitually configure 429 and similar codes.
…c/RetryCodes/ExtraHeaders

Verify that the clone correctly copies these fields and that
mutations to the clone do not affect the original. Also pin the
nil-stays-nil behavior for RetryCodes and ExtraHeaders.
Verify that redundant codes (429, 408) emit a WARNING to stderr while
still writing the valid codes to config. Also verify that valid-only
codes produce no warning output.

@MuoDoo MuoDoo left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

@lizhengfeng101
lizhengfeng101 merged commit c89282f into main Aug 10, 2026
7 checks passed
@lizhengfeng101
lizhengfeng101 deleted the feat/retry-codes branch August 10, 2026 08:52
lizhengfeng101 added a commit that referenced this pull request Aug 10, 2026
…818)

* feat(llm): support custom retry status codes via retry_codes config

Add a retry_codes configuration field that allows users to specify
additional HTTP status codes (4xx) that should trigger exponential
backoff retry. This targets self-hosted LLM clusters that misuse
status codes like 403 or 400 for rate limiting.

Implementation uses SDK middleware to inject the x-should-retry: true
response header when a configured status code is encountered, leveraging
the existing SDK retry mechanism (up to 5 retries with exponential
backoff) without any SDK modifications.

Configuration is supported via:
- Provider config: providers.<name>.retry_codes / custom_providers.<name>.retry_codes
- Legacy llm config: llm.retry_codes
- CLI: ocr config set providers.<name>.retry_codes 403,400

Also fixes cloneProviderEntry to copy previously missing fields
(TimeoutSec, ExtraHeaders, RetryCodes) for TUI rollback correctness.

* test(llm): add unit tests for retry_codes feature

Cover ParseRetryCodes validation, retryCodesMiddleware behavior,
resolver integration (provider and legacy config paths, invalid codes),
and end-to-end retry via httptest for both OpenAI and Anthropic clients.

* feat(llm): silently filter redundant retry_codes (408/409/429) instead of erroring

Replace validateRetryCodes with sanitizeRetryCodes that filters out
SDK-default codes and returns warnings. The resolve phase silently
drops redundant codes without interrupting startup. The config set
command prints warnings to stderr so users are informed.

This makes the tool friendlier for users migrating from other tools
who habitually configure 429 and similar codes.

* test(tui): add coverage for cloneProviderEntry deep-copy of TimeoutSec/RetryCodes/ExtraHeaders

Verify that the clone correctly copies these fields and that
mutations to the clone do not affect the original. Also pin the
nil-stays-nil behavior for RetryCodes and ExtraHeaders.

* docs(llm): fix contradictory ParseRetryCodes comment

* test(config): cover retry_codes warning output in config set path

Verify that redundant codes (429, 408) emit a WARNING to stderr while
still writing the valid codes to config. Also verify that valid-only
codes produce no warning output.
@Qiyuanqiii Qiyuanqiii mentioned this pull request Aug 10, 2026
8 of 15 tasks
Githab-capibara added a commit to Githab-capibara/open-code-review that referenced this pull request Sep 17, 2026
…libaba#818)

* feat(llm): support custom retry status codes via retry_codes config

Add a retry_codes configuration field that allows users to specify
additional HTTP status codes (4xx) that should trigger exponential
backoff retry. This targets self-hosted LLM clusters that misuse
status codes like 403 or 400 for rate limiting.

Implementation uses SDK middleware to inject the x-should-retry: true
response header when a configured status code is encountered, leveraging
the existing SDK retry mechanism (up to 5 retries with exponential
backoff) without any SDK modifications.

Configuration is supported via:
- Provider config: providers.<name>.retry_codes / custom_providers.<name>.retry_codes
- Legacy llm config: llm.retry_codes
- CLI: ocr config set providers.<name>.retry_codes 403,400

Also fixes cloneProviderEntry to copy previously missing fields
(TimeoutSec, ExtraHeaders, RetryCodes) for TUI rollback correctness.

* test(llm): add unit tests for retry_codes feature

Cover ParseRetryCodes validation, retryCodesMiddleware behavior,
resolver integration (provider and legacy config paths, invalid codes),
and end-to-end retry via httptest for both OpenAI and Anthropic clients.

* feat(llm): silently filter redundant retry_codes (408/409/429) instead of erroring

Replace validateRetryCodes with sanitizeRetryCodes that filters out
SDK-default codes and returns warnings. The resolve phase silently
drops redundant codes without interrupting startup. The config set
command prints warnings to stderr so users are informed.

This makes the tool friendlier for users migrating from other tools
who habitually configure 429 and similar codes.

* test(tui): add coverage for cloneProviderEntry deep-copy of TimeoutSec/RetryCodes/ExtraHeaders

Verify that the clone correctly copies these fields and that
mutations to the clone do not affect the original. Also pin the
nil-stays-nil behavior for RetryCodes and ExtraHeaders.

* docs(llm): fix contradictory ParseRetryCodes comment

* test(config): cover retry_codes warning output in config set path

Verify that redundant codes (429, 408) emit a WARNING to stderr while
still writing the valid codes to config. Also verify that valid-only
codes produce no warning output.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants