npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@dmmop/opencode-key-rotator

v2.2.1

Published

OpenCode v2 plugin that automatically rotates provider API keys on rate limits and quota errors.

Downloads

601

Readme

opencode-key-rotator

npm version npm downloads CI code style: prettier License: MIT

OpenCode v2 plugin that automatically rotates provider API keys on rate limits and quota errors.

Automatically swaps to the next saved key when OpenCode encounters rate limits (429), quota exhaustion, or resource errors during sessions.

Features

  • Automatic rotation — detects rate-limit and quota errors and swaps to the next available key on retry
  • OpenCode v2 plugin — subscribes to public session events with one native v2 entrypoint
  • Persistent storage — keys and rotation logs stored in the XDG data directory
  • Configurable — customize rotation patterns via a sidecar config
  • Secure — transactional SQLite writes, restrictive permissions, and credential log sanitization

Installation

npx -y @dmmop/opencode-key-rotator init

Or install it globally:

npm install -g @dmmop/opencode-key-rotator
opencode-key-rotator init

Runtime compatibility

OpenCode runs the plugin with Bun and does not need additional flags. Node.js 22.5 or newer is also supported. In Node.js 22.5 through 22.12, node:sqlite is experimental: the installed opencode-key-rotator CLI enables --experimental-sqlite through its shebang, while direct package imports require:

NODE_OPTIONS=--experimental-sqlite node your-script.mjs

Node.js 22.13 and newer do not require this option.

Setup

Automatic (recommended)

Run the init command to register both OpenCode V2 entrypoints and create a default sidecar config:

npx -y @dmmop/opencode-key-rotator init

Manual

The server plugin and local TUI plugin use separate OpenCode V2 configuration files. Add both entries, then restart OpenCode:

opencode.json:

{
  "plugins": ["opencode-key-rotator"],
}

cli.json:

{
  "plugins": ["opencode-key-rotator/tui"],
}

init creates or updates ~/.config/opencode/opencode.json, ~/.config/opencode/cli.json, and ~/.config/opencode/opencode-key-rotator.json idempotently. Use --config-dir to target another OpenCode config directory. A direct path such as ./dist/tui.js is only needed when developing the plugin locally.

Uninstall

npx -y @dmmop/opencode-key-rotator uninstall

Saved keys and rotation logs are preserved on removal.

CLI

opencode-key-rotator init [--config-dir <dir>]
opencode-key-rotator uninstall [--config-dir <dir>]
opencode-key-rotator switch [--provider <id>] [--alias <alias> | --credential-id <id>] [--data-dir <dir>]
opencode-key-rotator manage [--provider <id>] [--data-dir <dir>]
opencode-key-rotator status [--provider <id>] [--data-dir <dir>]

Key management

Use opencode-key-rotator switch for an interactive provider and connection selector. Pass --provider and a unique --alias to use it non-interactively, or --credential-id to select an exact native connection. The interactive selector distinguishes duplicate labels.

Use opencode-key-rotator manage for an interactive local workflow that switches the active OpenCode connection. It does not use the agent. Add, rename, and remove connections exclusively through OpenCode's native connection flow.

Use opencode-key-rotator status to list aliases, synchronization health, and the latest automatic rotation recorded in rotation.log.jsonl.

The server only translates OpenCode events. Rotation policy, cooldowns, switching, and automatic rotation logs live in rotation.ts, while SQLite CRUD lives in key-store.ts; the CLI and TUI adapter call the same core operations.

The ./tui adapter registers /key-switch and /key-status. Connections are added exclusively through OpenCode's native connection flow.

Configuration

Create a sidecar config file at ~/.config/opencode/opencode-key-rotator.json:

{
  "rotation": {
    "enabled": true,
    "patterns": ["\\b429\\b", "rate\\s*limit", "quota", "resource exhausted", "usage limit", "insufficient quota"]
  }
}

The config file supports JSONC (comments and trailing commas). Resolution order:

  1. ${configDir}/opencode-key-rotator.json
  2. Built-in defaults

How rotation works

  1. The plugin handles session.retry.scheduled events and evaluates every retry whose status is 429 or whose message matches the configured patterns.
  2. session.error is a fallback and rotates on HTTP 429 or matching messages.
  3. The provider comes from the event payload or the session model.
  4. If at least two native connections exist for the provider, the plugin activates the next connection in a round-robin cycle. It tracks each connection by its native credential ID, so duplicate labels remain distinct.
  5. If a rotated key fails on the next attempt, that connection enters a 2-minute cooldown and is skipped during subsequent rotations.
  6. Switching only changes OpenCode's native active connection marker; credential values and rows are preserved.
  7. Equivalent rotatable incidents with a session and operation ID are coalesced in memory for 10 seconds; restarting OpenCode clears this short-lived state.
  8. Every decision is recorded in ~/.local/share/opencode/keys/rotation.log.jsonl.

Data layout

~/.local/share/opencode/
  opencode.db                 # OpenCode native credentials/connections
  keys/
    rotation.log.jsonl             # Rotation decisions (JSON Lines)

Troubleshooting

| Symptom | Cause | Fix | | ----------------------- | ----------------------------------------------------------- | ---------------------------------------- | | Provider unknown | The v2 event did not expose a provider ID | Check the provider event data | | No fallback key | Only one connection exists for the provider | Add another OpenCode connection | | All keys cooling down | All connection labels are in cooldown after failed attempts | Wait 2 minutes or add another connection |

Development

npm install
npm run typecheck
npm run build
npm test
npm run format

Releasing

A maintainer can trigger a fully automated release from GitHub Actions:

  1. Go to ActionsReleaseRun workflow
  2. Choose patch, minor, or major
  3. The workflow will:
    • Run the full test suite
    • Bump the version in package.json
    • Create and push a Git tag
    • Create a GitHub Release with auto-generated notes
    • Publish the package to npm

License

MIT