What: an ollama-cloud key (any provider whose base is on ollama.com) is
read at GET https://ollama.com/api/balance with Authorization: Bearer
<key>. included.{balance_usd, allowance_usd, period} becomes a plan card
(PlanQuotas, planQuotaSourceOf) with one window in USD: spent of the
allowance, named Month when the period is a month long, resetting at
period.until. purchased.balance_usd becomes the key's balance card
(KeyBalances, balanceSourceOf). Routing weighs the key by that window as
it does other plan keys (keyWindows), and the window is Aside while bought
credits are left, since spending past the allowance draws on them.
Absent fields are unknown, never $0: no included, no allowance, an
allowance of 0 or no balance_usd gives no window (and, as for MiniMax and
Command Code, no plan card); no purchased gives no balance card (new
errNoBalance, which KeyBalances leaves out instead of showing an error);
null period gives a window with no reset. A reply with neither object
says its shape. A refused key's balance card says Ollama's reason.
Why: #1888 asked for Ollama Cloud's balance and plan quota beside the
other key-bought plans.
Run:
- go test ./internal/provider -run 'Ollama|TestPlanQuotaSource|TestBalanceSourceByHost|TestKeyBalances'
in a sandbox HOME: pass. Broken by hand five ways, each red:
no ollama.com plan source (TestOllamaCloudCards, TestPlanQuotaSource),
missing purchased read as $0.00 (TestReadOllamaBalanceMissing,
TestOllamaCloudCards), KeyBalances not skipping errNoBalance
(TestOllamaCloudCards), missing included.balance_usd read as 0
(TestReadOllamaBalanceMissing: a $20 allowance shown 100% used), Aside
dropped (TestReadOllamaBalance).
- The built binary in a sandbox HOME with an ollama-cloud provider and a
fake key: `magpie quota` asked the real ollama.com and showed
`ollama-cloud balance 401 Unauthorized: {"error":"invalid credentials"}`,
no plan card.
- gofmt -l, go vet (darwin), GOOS=linux go vet -tags nogui, GOOS=windows
go vet, go build for darwin, linux (nogui) and windows: clean.
- go test -tags nogui ./... in a sandbox HOME at load ~27: 48 packages
ok; internal/agent and internal/gateway hit the 10m timeout, and in
gateway TestIntentClassifierGroup (#1813, 3.63s against its 3s bound)
and TestHistoryKeepsDaysAndDropsOld (filed #1892) failed. Rerun with
-timeout 40m both packages pass (1032s, 1138s). Neither package reads
ollama.com or these readers.
Not tried against the real API with a real key: the owner's magpie has
no ollama-cloud provider, so the fixtures are the issue's literal JSON
and the free-plan and missing-field shapes are guesses from it. Whether a
spent allowance really falls through to bought credits (Aside) is not
verified either.
Reported-by: yangmingyuan380