One key, a dead provider, and a request that still lands.

We gave OpenCode a single ik- key, put a revoked Groq key first in its fallback chain, and recorded what happened. Below is the recording, then every message, check and rule the gateway used.

  1. 01Agent
    OpenCodeUnmodified, one config file
  2. 02Sends
    One ik- keyTo the local gateway
  3. 03Tried first
    Groq401, revoked key
  4. 04Answered
    Coarena200 in 1.4 seconds
  5. 05Never needed
    DeepSeek, Groq, OpenRouterWorking free-tier keys, kept in reserve

OpenCode got one normal answer and never saw the 401.

The recorded test, step by step

Each step in the video says what is being done, why, and what should happen, then shows the result. As it plays, a card pops up beside it with that step written out and what the gateway was doing underneath.

7 steps · 1:12

Press play. Each step pops up here as the video reaches it.

Every card says what was done, why, what should happen and what did, then what the gateway was doing underneath. Pick a number to jump to a step.

Both requests, message by message

What actually went over the wire. OpenCode only ever talks to the gateway on your computer; the gateway talks to the providers.

Request 1: the first question

  1. 1
    OpenCode → Local gatewayPOST /v1/chat/completions · model auto · key ik-••••
  2. 2
    Local gatewayChecks the ik- key, orders the chain. The revoked Groq key is Unchecked, so it goes first.
  3. 3
    Local gateway → Groq (revoked key)Same request, with the stored Groq key swapped in for ik-
  4. 4
    Groq (revoked key) → Local gateway401 Unauthorized
  5. 5
    Local gatewayRecords the attempt, marks Groq Invalid, keeps the request open.
  6. 6
    Local gateway → CoarenaSame request, Coarena key swapped in, its default model
  7. 7
    Coarena → Local gateway200 OK · the answer
  8. 8
    Local gateway → OpenCode200 OK · x-infinitykeys-attempts: 2 · logged as failover_served

Request 2: the second question

  1. 1
    OpenCode → Local gatewayPOST /v1/chat/completions · the haiku question
  2. 2
    Local gatewayOrders the chain again. Groq (revoked) is Invalid now, so it moves to the back.
  3. 3
    Local gateway → CoarenaSame request, straight to Coarena
  4. 4
    Coarena → Local gateway200 OK · the haiku
  5. 5
    Local gateway → OpenCode200 OK · x-infinitykeys-attempts: 1 · logged as served
Both requests from the recording, message by message. OpenCode only ever talks to the gateway; it sends one request and gets one answer each time. DeepSeek, Groq and OpenRouter were in the chain too but were never needed.

How the chain gets reordered

Why the second request skipped Groq without anyone touching a setting: before every request, the gateway sorts the chain by how each key has been doing.

  1. Ready, with roomHealthy keys in your chain order. With load spreading on, the ones with the most rate limit left go first.
  2. Close to a rate limitUnder 5% of the provider’s rate limit left, read from its rate-limit headers.
  3. Cooling downRecently out of credits, rate-limited or down. Still tried, after everything above.
  4. Marked InvalidThe provider rejected the key (401 or 403). Last resort only.

Order for request 1

  1. Groq (revoked demo key)Unchecked, so treated as ready
  2. Coarena
  3. DeepSeek AI
  4. Groq
  5. OpenRouter

Order for request 2

  1. Coarena
  2. DeepSeek AI
  3. Groq
  4. OpenRouter
  5. Groq (revoked demo key)Invalid, so tried last
The gateway sorts the chain into these four groups before every request, keeping your order inside each group. Your saved chain never changes; only the order for that request does. If an Invalid key ever works again, it is marked Valid and moves back up.

Inside one request

The eight checks every request passes through on your computer, in order, and the answer you get when one of them stops it.

  1. Who is calling

    Accepts the universal ik- key, a unique per-agent key, or the network key for Tailscale.

    Stops with Any other key gets 401. Ten failed tries in a minute from one address pause that address.

  2. What a shared key may send

    Unique and network keys may only send chat, completion, response, embedding and message requests, as a plain JSON body.

    Stops with 403 for other endpoints, 400 for a malformed body. The universal key on this computer is not limited here.

  3. Budgets and model limits

    Checks a unique key’s spend budget and its allowed models and providers.

    Stops with 429 when the budget is used up, 403 for a model the key may not use.

  4. Usage caps

    Checks your overall spend cap. Depending on your setting it blocks, or allows free-tier keys only.

    Stops with 429 when the cap blocks, or when no free-tier key is left.

  5. Build the candidate list

    Takes the fallback chain, or a smart-routing alias such as ik:cheap, and drops keys this caller may not use.

    Stops with 503 if the chain is empty.

  6. Order it

    Sorts the candidates into the four groups shown above.

    Stops with Nothing stops here.

  7. Cache (if you turned it on)

    Returns an identical recent answer without calling a provider.

    Stops with Off by default.

  8. Try each key in turn

    Swaps in the real key and the model (auto becomes that entry’s model), forwards the request, and on a failure records it and moves to the next key.

    Stops with Only when every key has failed: you get the last provider’s error, with any stored key redacted from it.

Everything the gateway does between receiving a request and answering it, in order. In the recording the universal key has no budget or model limits, no cap was reached and the cache (off by default) was not turned on, so the request went straight to stages 5, 6 and 8.

What each failure does

The gateway reads the provider’s answer and decides what it means for that key. The request itself always moves on.

What the gateway does with each kind of provider failure
Provider answersWhat it meansKey status becomesWaits before using the key again
401 or 403The provider rejected the key itself.Invalid, “Rejected during a gateway request”None. It moves to the back of the order.
402, or an out-of-credit message (400, 403 or 429)The key has no credits or quota left.Out of credits10 minutes, or the provider’s Retry-After (up to 1 hour)
429Rate-limited.Rate-limited60 seconds, or the provider’s Retry-After (up to 1 hour)
400, 404, 405 or 422This provider can’t take this particular request.Unchanged: the key is fineNone
5xx, no answer, or a timeoutThe provider is down or hung.Provider down15 seconds, or the provider’s Retry-After (up to 1 hour)
Every row moves on to the next key inside the same request; the kind of failure only decides the key’s status and how long it sits out. A provider gets 15 seconds to start streaming (60 for a normal reply) before it counts as hung.

What gets recorded

Three places show what happened, so you never have to take the agent’s word for it. None of them ever holds a secret.

The response headers

Sent back to the agent with every answer. The provider header names whichever key answered.

x-infinitykeys-provider: <provider>
x-infinitykeys-attempts: 2

Attempts counts the keys tried, including the one that answered. Unique and network keys see these two headers but never the provider’s account headers or your key names.

The gateway log

In Diagnostics, newest first. These are the two requests from the recording.

served  Coarena
  attempts: none

failover_served  Coarena
  tried: Groq (revoked demo key)
  status: 401

Each entry keeps key names, providers and status codes. If every key fails, the entry is all_failed with the full attempt list.

The key’s status

On the key’s row in the vault, until you act on it.

Groq (revoked demo key)
  Invalid
  Rejected during a gateway request
  Get a new Groq key ↗   Swap

Nothing is deleted for you. If the key works again later, the gateway marks it Valid on its own.

The big picture

The same diagrams as the GitHub README: where the gateway sits, how failover works in general, and what protects the keys at rest.

Architecture

How InfinityKeys fits together

Agents send requests to one local gateway with an ik- key. The gateway decrypts the real provider key in memory from vault.enc and your local keystore, and swaps it in on the way out.

Full size ↗
Diagram: Claude Code, Cursor, Hermes, and the Tailscale phone dashboard connect to the local InfinityKeys gateway on port 8790, which reads vault.enc via your local keystore and forwards requests with real provider keys.
Request failover

Failover inside one request

The general case of the recording: one key out of credits, the next rate-limited, the third answers. The agent sends once and receives once.

Full size ↗
Diagram: an agent sends one request; Key 1 (Groq) returns 402 and parks for 10 minutes, Key 2 (OpenRouter) returns 429 and cools down, and Key 3 (Cerebras) returns 200 OK without the agent retrying.
Security

What protects the keys

From the per-agent keys an agent holds, down through the gateway and the encrypted vault file, to the data key in the macOS Keychain or local key file.

Full size ↗
Diagram: five security layers from top to bottom — 1. Agent ik- keys, 2. Allow or Deny approval dialog, 3. Touch ID or passcode lock, 4. AES-256-GCM encrypted vault.enc, and 5. macOS Keychain or local key file.

Try it with your own agent

The recording used a throwaway vault on its own port. On your computer the gateway is at 127.0.0.1:8790. This is the whole OpenCode setup; any agent that speaks the OpenAI API works the same way.

opencode.json

{
  "provider": {
    "infinitykeys": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "InfinityKeys",
      "options": {
        "baseURL": "http://127.0.0.1:8790/v1",
        "apiKey": "{env:INFINITYKEYS_KEY}"
      },
      "models": { "auto": {} }
    }
  },
  "model": "infinitykeys/auto"
}

Or straight from a terminal

curl -s http://127.0.0.1:8790/v1/chat/completions \
  -H "Authorization: Bearer $INFINITYKEYS_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model":"auto","messages":[
        {"role":"user","content":"Hello"}]}' \
  -D - | grep -i x-infinitykeys

Copy the universal key from InfinityKeys → Universal Key into INFINITYKEYS_KEY. The last line prints which provider answered and how many keys it took.

Run the same test on your own keys.

Get the desktop app, or read the gateway code yourself.