Skip to content

[codex] feat: add Codex quota-aware OpenAI scheduling#3317

Open
chaochaoweb3 wants to merge 1 commit into
Wei-Shaw:mainfrom
chaochaoweb3:codex/openai-codex-quota-aware-scheduler
Open

[codex] feat: add Codex quota-aware OpenAI scheduling#3317
chaochaoweb3 wants to merge 1 commit into
Wei-Shaw:mainfrom
chaochaoweb3:codex/openai-codex-quota-aware-scheduler

Conversation

@chaochaoweb3

Copy link
Copy Markdown

Summary

  • Add Codex quota headroom signals to the existing OpenAI advanced scheduler score.
  • Use both 5-hour and 7-day Codex usage snapshots so the scheduler can gradually prefer accounts with more remaining short-window and weekly capacity.
  • Keep the existing auto-pause, sticky-session, TopK, load, queue, error-rate, and TTFT behavior intact.

Algorithm

For each OpenAI candidate in the load-balance layer, the scheduler now adds two soft score terms:

score += quota_5h * headroom(codex_5h_used_percent)
score += quota_7d * headroom(codex_7d_used_percent)
headroom = 1 - used_percent / 100
  • A lower 5h usage percentage receives a higher 5h score, reducing the chance that one account hits the short rolling window too quickly.
  • A lower 7d usage percentage receives a higher 7d score, spreading weekly Codex quota consumption across accounts more evenly.
  • Missing, reset, or stale quota snapshots are treated as neutral 0.5, reusing the existing reset/staleness guards instead of trusting old usage data.
  • Set gateway.openai_ws.scheduler_score_weights.quota_5h or quota_7d to 0 to disable either quota signal.

Why

This is a softer version of quota-aware routing than hard threshold switching: accounts are gradually demoted as their Codex windows fill up, while already-exhausted accounts are still handled by the existing auto-pause logic.

Related: #979, #477, #372

Tests

go test -tags unit ./internal/service -run 'TestOpenAIAccountScheduler_CodexQuotaWeights|TestOpenAIGatewayService_SchedulerWrappersAndDefaults'
go test -tags unit ./internal/config -run 'TestLoadDefaultOpenAIWSConfig|TestValidateConfig_OpenAIWSRules'
go test -tags unit ./internal/service -run 'TestOpenAI.*Scheduler|TestDefaultOpenAIAccountScheduler|TestBuildOpenAIWeightedSelectionOrder|TestOpenAIAccountCandidateHeap|TestClamp01|TestCalcLoadSkewByMoments|TestDeriveOpenAISelectionSeed|TestOpenAISelectionRNG'
go test -tags unit ./internal/config ./internal/service

@github-actions

github-actions Bot commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

All contributors have signed the CLA. ✅
Posted by the CLA Assistant Lite bot.

@chaochaoweb3

Copy link
Copy Markdown
Author

I have read the CLA Document and I hereby sign the CLA

github-actions Bot added a commit that referenced this pull request Jun 17, 2026
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