The replacement API key works in a terminal. You update the CI secret, revoke the old credential, and close the ticket. An hour later, a scheduled code review fails. Another job still runs, but its second model is unavailable. Your application has not lost its code, configuration, or prompts. It has lost a consistent connection between credentials and the model routes those credentials must access.
This is an illustrative failure pattern, not a reported customer incident. It explains why OpenCode API key rotation needs a rollout procedure rather than a single secret edit.
The practical sequence is straightforward: identify every consumer, test the candidate credential against the required model routes, start fresh workers with that credential, verify the actual OpenCode workload, and retire the previous key only after the migration is complete. If the old key has been compromised, revoke it promptly and accept an interruption if necessary; a planned overlap is not appropriate for a known leak.
This guide is for developers operating unattended model calls through OpenCode, including teams evaluating an AI API relay such as 4SAPI. It covers planned credential changes. If your immediate problem is an already expired key, begin with the OpenCode API key expiration fix. If a replacement appears to have no effect, use the separate guide to tracing an active stale key.
Before the change: define what “working” means
A credential is not an application health check. A successful response from one endpoint establishes only that this particular request succeeded under those conditions. It does not establish access to every model, every protocol, or every workspace your jobs use.
Write a small dependency record before touching production secrets. For each consumer, identify its owner, runtime, secret source, provider configuration, exact model identifier, and required request type. Include scheduled jobs and infrequent release tasks. They are easy to overlook because they may not run during the deployment window.
For example, a review workflow might use one model to summarize a patch and another to produce a structured finding. A separate job might use a native Claude endpoint. A text response from the first model cannot certify the other two operations. The useful unit of validation is the required model-and-protocol combination.
Keep the test small enough to repeat. Use a synthetic prompt with no repository secrets, a fixed expected output shape, and a clear finish condition. Do not turn credential rotation into a model benchmark. Its purpose is to establish continued access to the behavior the application already depends on.
The 4SAPI multi-model integration guide provides broader integration context. Here, the important distinction is narrower: changing a credential should not silently change the model, endpoint, or payload being tested.
A rotation record that can survive a handoff
Use a record like this in your deployment issue. The labels are application records, not provider API fields.
| Field | Example meaning |
|---|---|
| Credential revision | A non-secret label such as candidate-b |
| Consumer | Nightly review worker |
| Required route | The documented API route used by that worker |
| Required model | Exact identifier from the account configuration |
| Canary result | Response completed and application checks passed |
| Worker revision | Fresh process started with the candidate secret |
| Retirement condition | Every required consumer has passed |
Never place the key itself in this record. A label is enough to connect deployment evidence to a secret-manager revision. A partial key screenshot is unnecessary and can spread sensitive material into issue trackers, chat transcripts, and exported reports.
Also record who can stop the rollout. If a model fails its canary, the operator should know whether to retain the current deployment, restore the previous secret revision, or pause a particular workload. A rollback decision should not depend on remembering which person created the new key.
Stage one: separate secret delivery from model access
OpenCode supports environment and file substitutions in its configuration. Its configuration documentation explains the supported syntax and configuration locations. Match the configuration to your installed OpenCode version; copying an old provider block into a newer release introduces an unrelated variable during rotation.
Verify secret presence without printing it. A missing environment variable is a deployment problem. A present variable followed by an authorization failure is a different problem. Neither should be diagnosed by repeatedly pasting credentials into command history.
For GitHub-hosted automation, review the official GitHub Actions secrets guide. Repository, environment, and organization secrets have different scopes. A successful manual run in one environment does not prove that a scheduled workflow receives the same secret.
For container workloads, use the documented Docker Compose secrets mechanism where appropriate. Delivery through a mounted file and delivery through an environment variable are different runtime arrangements. Your process must actually read the configured source.
Treat the secret store update and consumer refresh as separate milestones. Existing processes do not generally acquire a new environment just because an external secret value changed. Start a fresh canary process and verify the resulting workload. Avoid assuming that a dashboard showing the new value proves that every worker has adopted it.
Stage two: run a narrowly scoped API canary
The following Python script checks a non-streaming, OpenAI-compatible Chat Completions route. It does not validate Responses, native Claude Messages, or native Gemini requests. Those routes need equivalent checks using their own response contracts.
Save a previously working, synthetic request as canary_request.json. It must contain the exact account-enabled model identifier and an appropriate output limit for that model. Obtain the complete endpoint from your current integration documentation; do not derive it by guessing a suffix.
Use a prompt that asks for exactly READY, and run python canary.py canary_request.json in the protected runtime that receives the candidate key. The example deliberately refuses redirects so a credential is not forwarded through an unexpected location. Python documents the request and handler behavior in its urllib.request reference.
A failed exact-answer check is a conservative canary failure, not proof that authentication failed. Inspect the response privately with your normal debugging controls. A successful check establishes a minimal text path; it does not establish tool execution, streaming, billing accuracy, or model identity beyond the evidence reported by the endpoint.
Stage three: test OpenCode with the candidate
The direct canary narrows the problem. The next test must use OpenCode itself because its provider adapter, model selection, and runtime configuration are part of the real application.
Consult the current OpenCode provider documentation and CLI documentation for your release. Run a small, read-only task in an isolated fixture repository. Keep the installed version, model selection, and provider settings fixed while changing only the credential source.
A useful fixture might contain a short function and a known defect. Ask the workflow to explain the defect without editing files. The acceptance record should show that the expected model route was requested, the task completed, and the output reached the workflow artifact. Do not accept an empty artifact merely because the process returned a success code.
If direct access works but OpenCode fails, investigate the client path. If both fail on the same model, investigate the credential and route before reinstalling the client. This separation prevents a routine rotation from expanding into speculative configuration changes across the entire team.
For application selection questions, the existing OpenCode and Codex CLI comparison is a different decision. A credential rollout is not the moment to change clients just because one canary fails.
Stage four: expand by consumer, then retire the old key
Promote the candidate to one low-impact consumer first. Observe actual completions and authentication errors for a representative run. Then move the remaining consumers in a recorded order. For an infrequent job, trigger its approved test path rather than waiting several days and assuming it is covered.
If overlapping credentials are permitted, keep the overlap brief and deliberate. Do not describe overlap as guaranteed: issuance rules, permissions, expiration, and revocation behavior depend on the account and service. If overlap is unavailable, schedule a controlled cutover and make interruption handling explicit.
Retirement requires positive evidence. Every required consumer should have a fresh-process result under the new revision. Check forgotten development runners, scheduled workflows, and manual release tools. Revoke the old key only when the planned migration is complete, unless compromise requires earlier revocation.
Keep the old key out of fallback code. An automatic “try every credential” loop hides migration failures and can make audit records misleading. Rollback should be an explicit deployment operation with a documented reason, not an invisible branch on every request.
Read failures as evidence, not instructions to retry forever
An authorization error, a missing model, a rate limit, and an invalid request need different responses. Classify the outcome before repeating the canary. Repeatedly submitting a request with a revoked key will not restore access, and retrying a malformed payload will not repair its schema.
Model-specific failures deserve model-specific investigation. If a Gemini route is involved, the 4SAPI Gemini API error guide can help organize the next checks. Keep that diagnosis separate from evidence that the secret was delivered correctly.
Likewise, an account may expose a model alias that differs from a provider's public model identifier. Preserve the requested name and reported response metadata. Do not quietly substitute another model simply to make the rotation dashboard green. The model-version verification guide explains why those identifiers deserve explicit records.
Turn a successful canary into a usable integration
If you are evaluating 4SAPI for these workloads, create a 4SAPI account and confirm the required models, protocols, credential controls, and pricing before connecting production automation. Start with one synthetic request, then one isolated OpenCode job. Registration is the beginning of evaluation; a successful application-level call is the useful milestone.
Ask whether the account supports your actual workflow rather than whether it advertises a familiar model name. Can the credential access the required route? Does the request return the expected format? Can your team identify which worker used which revision? These questions turn an API relay evaluation into evidence you can act on.
The finished rotation should leave behind three things: a working deployment, a retired credential, and a repeatable record. The next rotation can reuse that record with new values. That is a more durable result than a one-time fix that depends on the person who happened to know where the old key was stored.




