Skip to content

[Feature Request] Subagent model fallback chain with quota-aware auto-switching #374

Description

@genglintong

Problem

When spawning subagents with a specific agent_type (e.g. executor), the model is hardcoded in the agent TOML file (e.g. model = "gpt-5.6-sol"). If that model hits its usage limit (e.g. ChatGPT Plus quota exhausted), the subagent fails immediately with no recovery path:

Spawned 019f91d5-... (gpt-5.6-sol medium)
  └ errored: "You've hit your usage limit..."

Currently there is no fallback mechanism — the model parameter in spawn_agent is a single string, and the agent TOML schema has no fallback_model or model_chain field. The autoSwitchThreshold only rotates between multiple accounts within the same provider, not across different models/providers.

Proposed Solution

A quota-aware model fallback chain for subagent spawning:

1. Model fallback chain in config

Allow subagentModels (or a new subagentModelFallback) to define a priority-ordered fallback list:

{
  "subagentModelFallback": [
    "gpt-5.6-sol",
    "alibaba-token-plan/qwen3.8-max-preview",
    "kimi/k3"
  ]
}

When the primary model fails with a quota/rate-limit error, automatically retry with the next model in the chain.

2. Periodic quota polling

A background task (or pre-flight check before each spawn) that queries usage/quota for each configured provider:

  • OpenAI Codex account usage (already available via getAccountQuota)
  • Third-party provider health (e.g. /v1/models reachability, recent error rates from usage.jsonl)

Cache the results with a short TTL (e.g. 60s) to avoid excessive polling.

3. Priority-based routing

When spawning a subagent:

  1. Check the agent TOML's model field (current behavior)
  2. If that model's provider is known to be quota-exhausted or unhealthy, skip to the next available model in the fallback chain
  3. Optionally override the agent TOML model entirely if subagentModelFallback is configured and the TOML model is unavailable

4. Agent TOML extension (optional)

Allow per-role fallback in the TOML itself:

name = "executor"
model = "gpt-5.6-sol"
model_fallback = ["alibaba-token-plan/qwen3.8-max-preview"]

Use Case

Users running opencodex with a ChatGPT Plus account (limited quota) plus third-party providers (Alibaba MaaS, Codebuddy, Kimi, etc.) as alternatives. When the Plus quota is exhausted, subagents should seamlessly fall back to available third-party models instead of failing.

Environment

  • opencodex v2.7.31
  • macOS (aarch64)
  • Providers: openai (Plus), alibaba-token-plan, kimi, sports
  • autoSwitchThreshold: 0 (only one OpenAI account, so account rotation is not applicable)

Alternatives Considered

  • Manually editing agent TOML files to change the model — works but fragile, overwritten by omx setup
  • Always passing model explicitly in spawn_agent — puts the burden on the caller, no auto-recovery
  • Not specifying agent_type to inherit leader model — loses role-specific system prompts

Activity

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

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions