A Magpie routing group failed over 429, 403 and 5xx without touching the agent's config
A reviewable pack format and runner for Magpie failover on long Gemini CLI sessions. Seven keyless runs on the real Magpie binary and a mock upstream kept the agent config byte-identical every time.
By David Discua
Measured in the lab
- 7/7 runs
- agent config unchanged
- 10/10
- long-horizon turns ok
- 3/3
- CLI turns kept across 403 + 429
- 0
- real Google 429 observed
- Gemini CLI settings.json and .env had the same sha256 before and after every run
- a 429 sent the session to the second key with its history intact
- a 503 on the only usable member was retried once and answered
- with every member at 429 the agent got the 429
- packs with inline keys or stays strict are refused
From reports/e2e.json · run on October 21, 2026. Magpie e297a8ab2746 built with go1.27.2, Python 3.12.14, Gemini CLI 0.65.0, Darwin x86_64; mock upstream that fails on cue
How it is wired
01/03The agent is pointed at one Magpie routing group, once.
A long agent session on Gemini stalls when a key runs out of quota (429 RESOURCE_EXHAUSTED), hits a paid-tier gate (403) or gets a server error, and the usual fix is editing the agent's model or key by hand mid-session. Magpie already fails over inside its gateway, so this lab adds no proxy. It adds a pack format you can review, a runner that compiles the pack into real magpie commands against an isolated Magpie, a keyless proof on the real binary, and a command that turns Magpie's routing log into one line per failover. Across seven runs, the agent's config files never changed. The catch is that every error came from a mock upstream, and a real Google 429 or 403 was never observed.
The catch
The mock uses Google's documented status names, RESOURCE_EXHAUSTED and PERMISSION_DENIED, with message text of its own. Magpie classifies some errors by their wording (quotaWords and unservedWords in fallback.go), so Google's real wording could make Magpie rest one model instead of the whole key. A live call through Magpie after a forced 429 reached Google but did not return a completion in this lab, so no live result is claimed.
Two more gaps remain. The agy binary for Antigravity was not run, although its binding is set and checked in the configs. Claude Code is covered at the API level only, through one Anthropic Messages turn on the same group. Only routing: order is exercised, and the numbers come from one macOS machine.
The runs also showed a cost in Magpie's behavior. A 403 rests its key for one minute. When another model on the same key then answers, the key counts as healthy again, so the gated member is tried first on the next turn too. In long-horizon that meant one extra failed upstream call per turn after turn 6. The turns never failed, but each one paid a round trip.
What we ran (keyless)
git clone https://github.com/SkilledDragonCorp/magpiegemini-failover.git
cd magpiegemini-failover
uv sync --frozen --group dev
bash scripts/build-magpie.sh
bash scripts/install-gemini-cli.sh
uv run mgf validate profiles/gemini-long-horizon.yaml
uv run mgf demo
bash scripts/check.sh
From reports/e2e.json, run on October 21, 2026 with Magpie built from pin e297a8ab2746 and a mock upstream:
| Scenario | Turns ok | Faults hit | Log lines (failover / retry / exhausted) | Upstream calls | Agent config identical |
|---|---|---|---|---|---|
| quota-429 | 3/3 | 429 | 1 / 0 / 0 | 4 | yes |
| paid-gate-403 | 2/2 | 403 | 3 / 0 / 0 | 5 | yes |
| server-5xx | 3/3 | 500, 502, 503 | 3 / 0 / 0 | 6 | yes |
| long-horizon | 10/10 | 403, 429, 503 | 6 / 1 / 0 | 17 | yes |
| claude-code-api | 1/1 | 429 | 1 / 0 / 0 | 2 | yes |
| all-exhausted | 0/1 | 429 | 2 / 0 / 1 | 2 | yes |
| gemini-cli 0.65.0 (headless, --resume) | 3/3 | 403, 429 | 2 / 0 / 0 | 5 | yes |
After key A's 429, both of key A's members went to the back of the order for 15 minutes, Magpie's default when the vendor names no reset time. The session stayed on key B with its full history. With every member at 429, the agent got the 429, and Magpie refused the last try itself without calling upstream. The real Gemini CLI 0.65.0, in headless mode with --resume latest, kept its chat across a 403 and a 429, and each request carried more history than the one before.
How it is wired
A pack names providers by key_env, never by key, and lists the members of one routing group. mgf apply turns it into magpie provider add, magpie group add ... routing=order and magpie gemini group/<id>, writes keys into Magpie's providers.json with mode 0600, and binds the gateway to 127.0.0.1 inside an isolated home. Magpie writes ~/.gemini/settings.json and .env once. From then on, failover happens in the gateway and the runner reads each try from Magpie's routing log.
Minilab (open source)
The lab builds yetone/magpie (20,377 stars, MIT, Go) from a pinned, sha256-checked tarball and tests google-gemini/gemini-cli (107,573 stars, Apache-2.0, TypeScript), both as of October 24, 2026. To use it with your own Magpie, run uv run mgf commands profiles/gemini-long-horizon.yaml and apply the printed commands yourself.
Run it yourself
SkilledDragonCorp/magpiegemini-failover
Clone, install and run the report with no API keys. The numbers above are what that run writes.
uv sync --frozen --group dev && bash scripts/build-magpie.sh && uv run mgf demoSkilledDragon is not affiliated with Google (Gemini, Gemini CLI, Antigravity), Anthropic (Claude Code) or the Magpie project. Numbers are from our own keyless lab run against a mock upstream; re-run the report before relying on them. Marks belong to their owners.
All labs