Skip to content

fix(backend): answer a credit-exhausted BYOK OpenRouter account as a typed 429, not the generic 500 - #22630

Merged
Git-on-my-level merged 1 commit into
mainfrom
fix/errfix-1019-gcp-process-conversation-py-633-fastapi
Oct 21, 2026
Merged

Git-on-my-level merged 1 commit into
mainfrom
fix/errfix-1019-gcp-process-conversation-py-633-fastapi

Conversation

@Git-on-my-level

@Git-on-my-level Git-on-my-level commented Oct 19, 2026 •

Copy link
Copy Markdown
Member

fix(backend): answer a credit-exhausted BYOK OpenRouter account as a typed 429, not the generic 500

Error group

{"source": "gcp", "service": "backend", "group": "process_conversation.py:633 | fastapi.exceptions.HTTPException: 500: Error processing conversation, please try again later", "count": 12, "id": "gcp:backend-process-conversation-py-633-fastapi-exceptions-httpexception-500-error-processing-conversation-please-", "fingerprint": "fp:9dd85ca8e8f50576"}

Same fingerprint family as the py-632 predecessor fixed by #21779 (direct Anthropic credit exhaustion). That fix deployed; the group reopened on the new image (first line-633 event 2026-10-19T14:52Z, 68 events by 19:07Z).

Root cause

_get_structured's composition boundary maps every unmapped failure to the generic 500. The recurrence is driven by the direct OpenRouter BYOK notes lane: BYOK users whose key is OpenRouter's are routed to https://openrouter.ai/api/v1 (_get_or_create_openrouter_llm), and when that account's credits cannot cover the request, OpenRouter answers HTTP 402 and the openai SDK raises APIStatusError before generation. Retries can never succeed, so clients re-poll into the same 500. Prod evidence (project based-hardware, 2026-10-19):

  • 19 of the 68 line-633 events since the current image deployed carry this signature (69 occurrences since 2026-10-16); the remainder are provider transport failures (TimeoutError/APIConnectionError/InternalServerError, the family fix(backend): size notes loop deadline at the whole transport budget #22226 addresses) plus one interpreter-shutdown deploy artifact.
  • Full chained traceback for the exact group frame (/app/utils/conversations/process_conversation.py:633):
    File "/app/utils/conversations/process_conversation.py", line 610, in _get_structured
      structured = llm_input_dedupe.notes(
    File "/app/utils/llm/conversation_processing.py", line 466, in model_turn
      response = await call_model.ainvoke(messages)
    File "/opt/venv/.../openai/_base_client.py", line 1716, in request
      raise self._make_status_error_from_response(err.response) from None
    openai.APIStatusError: Error code: 402 - {'error': {'message': 'This request requires more
      credits, or fewer max_tokens. You requested up to 65535 tokens, but can only afford 65032.
      To increase, visit https://openrouter.ai/settings/credits and upgrade to a paid account',
      'code': 402, 'metadata': {'limit_source': 'openrouter_credits', ...}}}
    

This is the same account state the platform already answers as "throttled BYOK, not a server bug": the gateway classifies it byok_quota and answers 429, and the composition boundary already types the gateway envelope (#21779) and direct Anthropic's 400 "credit balance is too low" (#21779). The direct OpenRouter 402 spelling had no detection, so it fell into the generic 500 bucket — the user gets an unhelpful "try again later" for a state only they can fix, and this error group keeps reopening.

Fix

backend/utils/llm/gateway_error_contract.py:

  • Add _OPENROUTER_CREDIT_EXHAUSTED_MARKERS (requires more credits, openrouter_credits): OpenRouter's stable spellings of the credit-exhaustion account state, in the message and as the limit_source metadata value. Marker text is used only for classification — never logged or echoed.
  • is_byok_quota_error() now also classifies HTTP 402 carrying either marker as BYOK quota, mirroring the existing direct-Anthropic 400 branch. All other 402s and every other exception keep the privacy-safe generic 500.
  • conversation_processing_http_exception() is unchanged: the newly classified errors now map to the existing typed 429 {'code': 'byok_quota', ...} detail, matching the contract the gateway lane already enforces.

No fallback to the managed lane is introduced; the response only stops misreporting a fixable account state as a server error.

Verification

  • py_compile on both touched files: OK.
  • Extended the suite built from the live prod signature with real openai.APIStatusError instances (backend/tests/unit/test_conversation_structure_byok_quota.py):
    9 passed  (test_conversation_structure_byok_quota.py)
    17 passed (test_conversation_structure_byok_quota.py + test_conversation_structure_provider_timeout.py)
    57 passed (test_process_conversation_usage_context.py — contract consumers unchanged)
    
  • black --line-length 120 --skip-string-normalization --check: both files clean.

Failure-Class: none

View guided diff Turn on auto-fix

…typed 429

The direct OpenRouter BYOK notes lane answers a spent account with HTTP 402
('requires more credits' / limit_source openrouter_credits) before generation;
the composition boundary had no classifier for that spelling, so retries never
succeeded while clients re-polled the generic 500 at process_conversation.py:633
(same group family as the Anthropic credit fix in #21779). Classify 402 with
either OpenRouter credit marker as byok_quota so the boundary answers the
existing typed 429; every other 402 keeps the privacy-safe generic response.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-19T19:42:30.788313Z 8691b56 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@Git-on-my-level

Copy link
Copy Markdown
Member Author

Autonomous repair exhausted (review failed 3x (timeout)); 0 fixup rounds, attempt 3/3. PR left OPEN for human triage.\n\nLast findings excerpt:\nreview failed 3x (timeout)

@Git-on-my-level
Git-on-my-level merged commit 31756b9 into main Oct 21, 2026
62 checks passed
@Git-on-my-level
Git-on-my-level deleted the fix/errfix-1019-gcp-process-conversation-py-633-fastapi branch October 21, 2026 08:24
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.

1 participant