Skip to content

fix(quota): read the Ollama Cloud quota from /api/balance - #6786

Merged
lidge-jun merged 8 commits into
devfrom
codex/ollama-balance-quota-carry
Oct 9, 2026
Merged

lidge-jun merged 8 commits into
devfrom
codex/ollama-balance-quota-carry

Conversation

@lidge-jun

@lidge-jun lidge-jun commented Oct 9, 2026 •

Copy link
Copy Markdown
Owner

Summary

Ollama changed the payload of GET https://ollama.com/api/usage to a request-count timeseries, so parseOllamaCloudQuota finds no limits and the Ollama Cloud row silently disappears from the quota dashboard. This carries #6739 by @xingqi-gif onto current dev and adds the failure-handling fixes found in review.

  • Read GET https://ollama.com/api/balance first (shipped upstream in server: proxy cloud usage and balance APIs ollama/ollama#18829, tracked in Cloud API: expose credit balance & true spend after pay-as-you-go migration ollama/ollama#18653) and keep /api/usage as a fallback for older deployments. included.{session,weekly,monthly} map to the 5-hour/weekly/monthly windows; allowance plans map included.{allowance_usd,balance_usd,period.until} to creditsUsd. Purchased credit is ignored, so creditsUsd describes the included allowance only. For routing, an exhausted included allowance vetoes Ollama Cloud only when purchased.balance_usd is known to be zero or negative; with positive or unknown purchased credit the credits stay display-only, so a paid balance is never blocked.
  • A transport rejection on one endpoint now falls through to the next instead of rejecting the whole /api/provider-quotas response.
  • Terminal precedence keeps today's rule (hard 4xx other than 404/408/429). A terminal status from either endpoint wins unless the other endpoint returns a parsed report; an unreadable second body after a terminal status now clears the row instead of keeping stale quota. Unread non-success bodies are cancelled. An oversized /api/balance body still stops without spending a second request.
  • Ollama tests move to tests/providers/provider-quota-ollama-cloud.test.ts (registered in both layout files) so provider-quota.test.ts stays under its ratchet cap.
  • Adds the Ollama quota contract to structure/providers-and-adapters.md and a short paragraph to the Providers guide.

Carries #6739. Plan and review record: devlog/_plan/261009_L5-auth-quota/.

Co-authored-by: xingqi-gif [email protected]

Verification

  • bun test tests/providers/provider-quota-ollama-cloud.test.ts tests/providers/provider-quota.test.ts tests/providers/provider-api-keys.test.ts tests/server/bounded-body.test.ts tests/test-layout.test.ts tests/test-layout-tooling.test.ts tests/ci-workflows/file-size-ratchet.test.ts → 304 pass, 0 fail at 4d88dea; after the review fix the quota/key/layout/ratchet subset reran at 237 pass, 0 fail.
  • New regression cases failed on the contributor head before the review fixes (31 pass / 8 fail) and pass now: balance transport rejection → usage fallback, 408/429/5xx fallback, balance 401 → unreadable usage clears the row, balance 401 → usage 200 publishes, missing/invalid resets_at, allowance expiry, mixed windows + credits, percent clamping, Ollama missing-key/disabled/non-key exclusion, URL-specific legacy mocks with endpoint-sequence assertions, and combo-routing cases for exhausted included credit with positive / unknown / empty purchased credit (the first two fail without the routing projection fix).
  • bun run typecheck, bun run privacy:scan, bun run structure:check → pass.
  • Full bun run test and the docs-site build were not run locally (lane runs minimal local tests by instruction; several lanes share this machine). Both are left to exact-head hosted CI, including the docs site build job.
  • No live Ollama call was made in this carry; the /api/balance shape relies on the upstream PR, third-party SDK documentation, and the original author's live check with a Pro key.

Checklist

  • Scope stays focused and avoids unrelated cleanup.
  • Docs or release notes were updated when needed.
  • Security-sensitive changes were reviewed for secrets, auth, and unsafe defaults. (The key is still sent only to https://ollama.com through the existing quotaFetch guard: Bearer, redirect: "error", request timeout.)

Summary by CodeRabbit

  • New Features

    • Ollama Cloud quota details now use balance data first, with usage data as a fallback. Quota displays include available usage windows and reset times when reported.
    • Included credits are distinguished from purchased credits when showing allowance and determining whether routing can continue.
  • Documentation

    • Updated provider guidance to explain quota reporting, fallback behavior, and how included and purchased credits affect routing.

@lidge-jun
lidge-jun requested a review from Ingwannu as a code owner October 9, 2026 01:30
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 9, 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-09T01:33:20.574099Z 4d88dea 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.

@github-actions github-actions Bot added the intake: hygiene-blocked Deterministic PR hygiene checks failed label Oct 9, 2026
@github-actions

github-actions Bot commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

⚠️ Deterministic hygiene checks failed.

  • missing_coauthor_credit — This pull request says it reimplements, supersedes, carries, or rebases another author's pull request, but no Co-authored-by trailer names that author. Prose in a commit body is not read by anything; the trailer is what GitHub counts. Add it to the description or a commit, or obtain attribution-approved. Paths: #6745.

@github-actions github-actions Bot added the bug Something isn't working label Oct 9, 2026
@github-actions

github-actions Bot commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

✅ READY

  • all PR quality gates passed.

Hygiene

✅ Deterministic PR hygiene checks passed.

@github-actions
github-actions Bot marked this pull request as draft October 9, 2026 01:31
@coderabbitai

coderabbitai Bot commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 20 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 10 included reviews currently available.

Learn how review limits work.

Review configuration:

⚙️ Run configuration
  • Configuration used: Repository: lidge-jun/opencodex/.coderabbit.yaml
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 623fe1df-79f4-4c42-a162-1280294f72bc
📥 Commits

Reviewing files that changed from the base of the PR and between 175e1e3 and 3014954.

📒 Files selected for processing (11)
  • devlog/_plan/261009_L5-auth-quota/000_roadmap.md
  • devlog/_plan/261009_L5-auth-quota/010_ollama_balance_quota.md
  • docs-site/src/content/docs/guides/providers.md
  • scripts/test-layout/layout.json
  • src/providers/quota.ts
  • src/providers/quota/vendor-probes-key.ts
  • structure/providers-and-adapters.md
  • tests/fixtures/test-layout-expected.json
  • tests/providers/provider-api-keys.test.ts
  • tests/providers/provider-quota-ollama-cloud.test.ts
  • tests/providers/provider-quota.test.ts

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: lidge-jun/opencodex/.coderabbit.yaml
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 5b374f57-e45e-4514-a958-2c6816257f3e
📥 Commits

Reviewing files that changed from the base of the PR and between 4d88dea and 175e1e3.

📒 Files selected for processing (4)
  • docs-site/src/content/docs/guides/providers.md
  • src/providers/quota/vendor-probes-key.ts
  • structure/providers-and-adapters.md
  • tests/providers/provider-quota-ollama-cloud.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.


📝 Walkthrough

Walkthrough

The PR adds balance-first Ollama Cloud quota reporting with usage fallback, updates its tests and documentation, and adds planning documents for two carry PRs. The ChatGPT OAuth hardening document describes planned work; it does not implement that work.

Changes

Ollama Cloud quota reporting

Layer / File(s) Summary
Balance parsing and endpoint fallback
src/providers/quota/vendor-probes-key.ts, src/providers/quota.ts
The quota probe tries /api/balance before /api/usage. The new balance parser maps included quota windows and eligible USD allowance data. Network failures and 404 responses continue to the next attempt; other 4xx responses except 408 and 429 record a terminal failure.
Probe tests and layout
tests/providers/provider-quota-ollama-cloud.test.ts, tests/providers/provider-api-keys.test.ts, tests/providers/provider-quota.test.ts, scripts/test-layout/layout.json, tests/fixtures/test-layout-expected.json
The new suite covers parsing, fallback, failure handling, report retention, eligibility, and request options. The provider API-key test now expects /api/balance. The prior Ollama Cloud cases are removed from the shared quota test, and the new suite is registered in the test layout and fixture.
Quota documentation and implementation plan
docs-site/src/content/docs/guides/providers.md, structure/providers-and-adapters.md, devlog/_plan/261009_L5-auth-quota/010_ollama_balance_quota.md
The guides describe endpoint order, report-retention behavior, bounded-body handling, and included-credit display. The plan records implementation, test, and verification details. The MiniMax documentation is reflowed without changing its stated behavior.

Carry PR planning

Layer / File(s) Summary
Roadmap and OAuth refresh plan
devlog/_plan/261009_L5-auth-quota/000_roadmap.md, devlog/_plan/261009_L5-auth-quota/020_chatgpt_refresh_hardening.md
The roadmap records sequencing, review requirements, verification, and escalation conditions for two carry PRs. The OAuth plan records accepted decisions and acceptance cases; it describes planned work rather than implementation.

Priority: ➖ Normal

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant QuotaProbe as Ollama Cloud quota probe
  participant BalanceAPI as /api/balance
  participant BalanceParser as parseOllamaCloudBalance
  participant UsageAPI as /api/usage
  participant UsageParser as parseOllamaCloudQuota
  QuotaProbe->>BalanceAPI: Request balance
  BalanceAPI-->>QuotaProbe: Return response
  QuotaProbe->>BalanceParser: Parse successful balance body
  BalanceParser-->>QuotaProbe: Return quota or null
  alt No balance quota
    QuotaProbe->>UsageAPI: Request usage
    UsageAPI-->>QuotaProbe: Return response
    QuotaProbe->>UsageParser: Parse successful usage body
    UsageParser-->>QuotaProbe: Return quota or null
  end
Loading

Merge Risk: ⚪ Minimal · up to 175e1

The quota change has no identified merge-blocking issue; complete the usual checks before merging.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 27.27% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 11 functions across 4 files. (2 skipped: … Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely identifies the main change: updating Ollama Cloud quota retrieval to use /api/balance. This matches the implementation and PR objectives.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

Docstring coverage is 27.27% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 11 functions across 4 files. (2 skipped: 2 unsupported.)

✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4d88dea7c3

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@lidge-jun
lidge-jun force-pushed the codex/ollama-balance-quota-carry branch from 2a7a811 to 175e1e3 Compare October 9, 2026 01:40
@github-actions github-actions Bot removed the intake: hygiene-blocked Deterministic PR hygiene checks failed label Oct 9, 2026
@github-actions
github-actions Bot marked this pull request as ready for review October 9, 2026 01:40
lidge-jun and others added 8 commits October 9, 2026 11:07
Ollama replaced the /api/usage quota payload with request-count data.
Read /api/balance first, retaining /api/usage for older deployments.
Handle transport failures per endpoint and retain terminal verdicts from review.

Carries #6739.

Co-authored-by: xingqi-gif <[email protected]>
Purchased credit is not part of creditsUsd, so an exhausted included allowance vetoes routing only when purchased balance is known to be zero.

Co-authored-by: xingqi-gif <[email protected]>
@lidge-jun
lidge-jun force-pushed the codex/ollama-balance-quota-carry branch from 175e1e3 to 3014954 Compare October 9, 2026 02:08
@lidge-jun
lidge-jun merged commit 042b3ac into dev Oct 9, 2026
31 checks passed
@lidge-jun
lidge-jun deleted the codex/ollama-balance-quota-carry branch October 9, 2026 02:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant