Skip to content

fix(realtime): treat credit_balance_exhausted as a fatal OpenAI error - #7819

Merged
longcw merged 3 commits into
livekit:mainfrom
jipeng6036-del:fix/7805-credit-balance-exhausted-fatal
Oct 20, 2026
Merged

longcw merged 3 commits into
livekit:mainfrom
jipeng6036-del:fix/7805-credit-balance-exhausted-fatal

Conversation

@jipeng6036-del

@jipeng6036-del jipeng6036-del commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #7805

OpenAI Realtime can send an insufficient_quota error whose code is credit_balance_exhausted:

RealtimeError(message='You have no credits remaining. ...', type='insufficient_quota', code='credit_balance_exhausted', ...)

_is_fatal_error reads code before type, and credit_balance_exhausted is not in _FATAL_ERROR_CODES, so this error is treated as recoverable. RealtimeModelFallbackAdapter then leaves it to the plugin's reconnect instead of switching to the next model, which is the loop described in the issue.

This adds credit_balance_exhausted to the fatal code set in llm/_realtime/openai.py (also used by the inference realtime session through super()._is_fatal_error) and to the matching set in the GPT-Live session.

It also changes _is_fatal_error to match either field (code in the set or type in the set) instead of code or type, in llm/_realtime/openai.py, llm/_realtime/gpt_live.py and inference/realtime/openai.py, so a fatal type still stops the reconnect if OpenAI changes the code again. A missing or empty code/type never matches.

Tests:

  • test_is_fatal_error_matches_known_codes now covers the code="credit_balance_exhausted", type="insufficient_quota" shape.
  • New test_an_exhausted_credit_balance_is_not_recoverable in tests/test_gpt_live_model.py checks that the GPT-Live error event raises a non-retryable APIError.
  • New test_is_fatal_error_matches_either_code_or_type, test_fatal_errors_match_either_code_or_type (GPT-Live) and test_gateway_fatal_errors_match_either_code_or_type (inference) check that a fatal type with an unlisted code is fatal, and that unlisted, None or empty values on both fields are not.

Each of these tests fails without the change (the new ones on their fatal-type-with-unlisted-code cases) and passes with it.

Ran locally: uv run pytest tests/test_realtime/ --unit (125 passed), uv run pytest tests/test_gpt_live_model.py --unit (111 passed), ruff check / ruff format --check on the changed files, uv run mypy --platform linux -p livekit.agents (no issues). Before the either-field change, a full uv run pytest --unit --audio_eot -n 4 here had failures only in test_room.py (no docker), test_vad_buffer_resize.py, test_tokenizer.py (missing nltk data) and an import error in test_plugin_azure_realtime.py (azure.ai.voicelive not installed); the room and VAD tests fail the same way on main in this environment. I did not test against a real OpenAI account with no credits.

OpenAI Realtime can report an insufficient_quota error with code
credit_balance_exhausted. The fatal check looks at the code before the type,
so that error stayed recoverable: RealtimeModelFallbackAdapter left it to the
plugin's reconnect instead of switching to the next model. Add the code to the
fatal sets of both OpenAI realtime sessions (Realtime and GPT-Live).
@jipeng6036-del
jipeng6036-del requested a review from a team as a code owner October 14, 2026 13:45

@devin-ai-integration devin-ai-integration 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.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Devin Review

main (livekit#7673) moved the GPT-Live session into
livekit-agents/livekit/agents/llm/_realtime/gpt_live.py, so the
credit_balance_exhausted fatal code now goes into that file's
_FATAL_ERROR_CODES instead of the plugin's gpt_live_model.py.
_FATAL_ERROR_CODES = frozenset(
{
"insufficient_quota",
"credit_balance_exhausted",

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 root cause is a bit wider than the missing code. _is_fatal_error checks code or type, so when code is set, type is never looked at. insufficient_quota is already fatal, but here it is the type, not the code. In #6352 this same condition arrived as code='insufficient_quota'; OpenAI has since changed the code and kept the type. Adding credit_balance_exhausted covers today's code, and checking type as well would also cover any later change.

Could you change the check to match either field, e.g. code in _FATAL_ERROR_CODES or type in _FATAL_ERROR_CODES, in all three places that use the pattern?

  1. llm/_realtime/openai.py (_is_fatal_error)
  2. llm/_realtime/gpt_live.py (_is_fatal_error)
  3. inference/realtime/openai.py (_is_fatal_error)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in d1e5b3a: all three now match on either code or type, with tests for both cases.

_is_fatal_error used `code or type`, so the type was never looked at once
a code was set. OpenAI has changed the code of the insufficient_quota error
(livekit#6352 sent code='insufficient_quota', livekit#7805 sends
code='credit_balance_exhausted') while keeping the type, so check both
fields in llm/_realtime/openai.py, llm/_realtime/gpt_live.py and
inference/realtime/openai.py.
@longcw
longcw merged commit 72e0217 into livekit:main Oct 20, 2026
17 checks passed
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.

Mistakenly treat no credits remaining error as "recoverable" error

2 participants