SlouchyQuill on Discord asked for a Tavily key pool after
dsh-tavily-pool: several keys, each key's quota shown, keys rotated.
Before this, the comma-separated keys (#1477) always started from the
key that last answered, so one key took every search until it was
refused, a refused key kept no rest of its own, and nothing showed what
a key had left.
What changed:
- Turns (internal/gateway/search_keys.go). Searches take turns over the
keys by default (round robin, a counter per vendor). pick=order (CLI)
or In order (Settings) always starts from the first key; the pick is
saved in SearchAPI.Pick.
- Rests. A refused key rests: 401/403 for 10m ("turned away"), 402 for
1h ("out of credit"), 429 for its Retry-After held to 30s-10m (1m
without one). 432/433 count as out of credit from Tavily only; from
any other vendor they rest as a rate limit. A resting key is asked
after the others, soonest back first, so failover is kept, and it
wakes when it answers. Rests are keyed by a hash of the key.
- Quota (internal/gateway/search_quota.go). Each Tavily key's quota is
read from GET /usage with that key. The fixture is the documented
reply from docs.tavily.com. The key's own limit is used when it has
one; otherwise its plan's plan_limit/plan_usage, which the account's
keys share. A failed read, or a reply missing those fields, says
unknown and why, never 0. A read is kept 2m unless asked fresh. A key
found used up with no pay-as-you-go credit rests 1h; a later read
with credit wakes it.
- Keys never appear in full. The vendor's error has the key masked
(tvly-…abcd), and errors name "key N (tvly-…abcd)".
- GUI. Settings › Models › Web search adds a "Keys of <API>" row under
an API with several keys or with Tavily's: In turn / In order, each
key masked with what it has used and why it is asked last, and Check
again. There is an API for it (POST /api/settings/search-quota), and
13 strings in zh, zh-TW, ja and de.
- CLI. `magpie search` lists the pick and each Tavily key's quota, read
fresh; `magpie search add … pick=turns|order`.
- Docs. gateway-routing.md has a new section "A search API's keys";
both READMEs have a line on web search.
What was run:
- go test -tags nogui ./internal/gateway ./internal/provider
./internal/cli ./internal/gui: ok. After the last rebase, provider
and cli again, and gateway -race -run Search.
- New tests: TestReadTavilyUsage, TestSearchKeysTakeTurnsRoundRobin,
TestSearchKeyRefusedRests, TestSearchRestFor, TestSearchQuotas and
TestSearchKeyNeverInFull. Each guarantee was broken by hand and its
tests went red:
- turns pinned to the first key: RoundRobin and RefusedRests;
- rests ignored: TestSearchAPIKeysTakeTurns, RefusedRests and
TestSearchQuotas;
- key masking removed: NeverInFull;
- a failed read reported as known: TestSearchQuotas.
- gofmt -l; go vet -tags nogui for darwin, linux and windows on the
four packages.
- GUI: search-keys.test.cjs covers en, zh, zh-TW, ja and de ×
Chromium and WebKit × 1100 and 420px: 20/20. Without the new
.search-keys .val wrap rule, de at 420px fails ("Erneut prüfen" went
past the window's edge).
- search-api, search-first, search-instead, search-mcp,
searcher-pick, settings-search, provider-search, gui-de, gui-ja and
i18n-keys: 56/56 in each engine.
- Full suite in Chromium (concurrency 6, load ~40): 4498/4504. The
files that failed (gateway-key-groups, rest-try-now,
routing-wb-restricted, text-size, usage-sum-whole) were rerun
alone. All pass except usage-sum-whole, which fails now and then
on an origin/main worktree too under the same load (#1864, #2023;
rest-try-now and routing-wb-restricted are #1905). None of them
touch search.
- Real Tavily API with two invalid keys: /search returns 401
{"detail":{"error":"Unauthorized: missing or invalid API key."}},
which counts as a refusal. Both keys rest 10m as turned away, the
error names them only masked, and both quotas read "unknown · 401".
Checked through a test and through the nogui CLI binary in a sandbox
HOME.
Not tried: a valid key's /usage against the real API. No Tavily key
was at hand, so the quota parsing is checked against the documented
bytes only.