A simple Ruby script to switch between different OpenCode configuration profiles, manage provider API keys per profile, and run connectivity tests and token-generation benchmarks.
OpenCode stores its active configuration in ~/.config/opencode/opencode.json. When working across multiple providers (e.g., OpenRouter, local Ollama/vLLM endpoints, cloud APIs, or private clusters), updating credentials and JSON configurations manually can be repetitive.
opencode-switcher provides:
- Profile isolation under
~/.config/opencode-switcher/<number>/(containingconfig.jsonandAPI_key.sh). - A single wrapper command
octo run OpenCode using any profile. - A subcommand
oc confto inspect, switch, test, and benchmark profiles. - Command alias
opencode-switcherfor direct profile management.
- Ruby 3.0+ (standard library only; no external gems required)
- OpenCode (
opencodebinary inPATHor~/.opencode/bin/opencode) bash(used internally to safely sourceAPI_key.shscripts)
Copy oc to a directory in your PATH (such as ~/.local/bin) and make it executable:
mkdir -p ~/.local/bin
cp oc ~/.local/bin/oc
chmod +x ~/.local/bin/oc
ln -sf oc ~/.local/bin/opencode-switcherEnsure ~/.local/bin is in your shell PATH:
export PATH="$HOME/.local/bin:$PATH"Profiles are stored in ~/.config/opencode-switcher/:
~/.config/opencode-switcher/
├── default.json # Tracks active default profile number (e.g. {"default": "01"})
├── 01/
│ ├── config.json # OpenCode JSON configuration for Profile 01
│ └── API_key.sh # Environment export script (e.g. export OPENROUTER_API_KEY="...")
├── 02/
│ ├── config.json
│ └── API_key.sh
└── 03/
├── config.json
└── API_key.sh
When you invoke oc [profile], the script:
- Sources
API_key.shfor the selected profile and exports the variables to the execution environment. - Copies
config.jsonto~/.config/opencode/opencode.json(and cleans up any conflictingopencode.jsonc). - Forwards the profile's default model (via
-m <model>) if none is explicitly provided. - Executes the
opencodebinary with any supplied arguments.
oc# Run profile 02 in interactive mode
oc 02
# Run profile 01 and pass a prompt
oc 01 "Write a binary search implementation in Ruby"
# Run profile 03 with additional flags
oc 03 -m anthropic/claude-3-7-sonnetUse -- to prevent oc from interpreting arguments:
oc -- --version
oc 02 -- -m custom/model -f main.rboc conf
# or
oc conf listExample output:
OpenCode Profiles (~/.config/opencode-switcher/):
01 "OpenRouter DeepSeek direct" — Provider: openrouter [OPENROUTER_API_KEY: SET]
Model: openrouter/deepseek/deepseek-v4-flash | URL: https://openrouter.ai/api/v1
* 02 [default] "Lumen Remote Qwen 3.6 coder" — Provider: remote-coder [REMOTE_CODER_API_KEY: SET]
Model: remote-coder/qwen3.6-35b-a3b | URL: https://lumen.ncsa.illinois.edu/v1
oc conf 02
# or
oc conf set_default 02Display full provider settings, base URLs, configured models, and API key status:
# Inspect profile 01
oc conf info 01
# Inspect all profiles
oc conf info all# Set model for profile 02
oc conf set_model 02 anthropic/claude-3-7-sonnet
# or
oc conf 02 set_model anthropic/claude-3-7-sonnet
# Set model for the current default profile
oc conf set_model anthropic/claude-3-7-sonnetoc conf set_desc 02 "Primary coding profile with local LLM"
# or
oc conf 02 set_desc "Primary coding profile with local LLM"oc conf set_api 02 "sk-your-api-key-here"
# or
oc conf 02 set_api "sk-your-api-key-here"oc conf create path/to/my-config.jsonThis allocates the next available numeric directory (e.g. 04), copies the JSON file, replaces literal API keys with {env:...} placeholders, and creates the corresponding API_key.sh file.
Sends a short request to verify API keys, access points, and model responses:
# Test profile 01
oc conf test 01
# Test all profiles
oc conf test allMeasures latency and output tokens per second across multiple endpoints:
# Benchmark current default profile
oc conf bench
# Compare profile 01 and profile 02
oc conf bench 01 02
# Benchmark all profiles and display a speed summary
oc conf bench allRuns generation benchmarks for every model listed in a profile's configuration:
oc conf test_models 01The full usage reference can be triggered with any of the following standard help flags:
oc help
oc -h
oc -H
oc -?
oc /?
oc /h
oc /H
oc --helpMIT