Skip to content

feat: support apiKey arrays with round-robin rotation per provider - #31596

Closed
gautamkrishnar wants to merge 2 commits into
anomalyco:devfrom
gautamkrishnar:feat/multi-apikey-rotation
Closed

feat: support apiKey arrays with round-robin rotation per provider#31596
gautamkrishnar wants to merge 2 commits into
anomalyco:devfrom
gautamkrishnar:feat/multi-apikey-rotation

Conversation

@gautamkrishnar

Copy link
Copy Markdown

Issue for this PR

Closes #29085

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Adds support for apiKey as an array of strings in provider options for round-robin rotation across multiple API keys.

When apiKey is an array in opencode.json:

{
  "provider": {
    "openrouter": {
      "options": {
        "apiKey": ["sk-key-1", "sk-key-2", "sk-key-3"]
      }
    }
  }
}

Each request rotates to the next key in the array (key1 → key2 → key3 → key1 → ...). This works for both the AI SDK provider path (via a fetch wrapper that swaps auth headers) and the native LLM path (via a new Auth.pool() credential that round-robins on each resolve). Single string apiKey continues to work identically — the array check only triggers for actual arrays.

I also added defensive type guards in Snowflake Cortex, Cloudflare Workers AI, and GitHub Copilot providers to prevent array coercion into malformed auth headers, and updated the v1 config schema to accept the new type.

How did you verify your code works?

  • All 278 existing LLM package tests pass, plus 3 new tests for pool credential rotation
  • Type checking passes across all 23 packages (core, llm, opencode, and all others via turbo)
  • Tested live with Nvidia provider using two keys (one valid, one invalid) — confirmed round-robin alternation: valid key returns response, invalid key returns empty, next request uses valid key again

Screenshots / recordings

N/A — no UI changes

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@github-actions

Copy link
Copy Markdown
Contributor

Automated PR Cleanup

Thank you for contributing to opencode.

Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions.

This PR was closed because it matched the following cleanup criteria:

  • The PR was created more than 1 month ago
  • The PR had fewer than 2 positive reactions
  • Positive reactions are counted as thumbs-up, heart, celebration, or rocket reactions on the PR

PRs created within the last month are not affected by this cleanup.

If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate.

Thanks again for taking the time to contribute.

@droid1-git

Copy link
Copy Markdown

Could you please reopen automatically closed PR.
Feature is needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE]: Support multiple API keys/credentials with round-robin rotation per provider

2 participants