How to Migrate an OpenRouter Integration to Another API Gateway
Plan a reversible OpenRouter migration with a requirements matrix, isolated credentials, protocol-specific endpoints, contract tests, a controlled canary, and explicit rollback triggers.
Migrating an OpenRouter integration is not a production-wide Base URL replacement. First capture the contract your application already depends on: protocol, SDK method, Model ID, response schema, streaming behavior, tool calls, errors, retries, and usage fields. Then test the candidate gateway with an isolated key and a small canary.
If the current OpenRouter integration works and its model catalog or routing behavior is important to the application, staying may be the correct decision. A second gateway can also be added as a tested backup without replacing the primary route. Full migration should happen only after the candidate passes the same workload-specific checks.
This tutorial uses BetterToken as a verifiable example of another gateway. It is not an OpenRouter clone, and an OpenAI-compatible label does not guarantee identical models, features, errors, or usage data.
Choose the migration scenario first
- Stay on OpenRouter when the current integration, billing path, model availability, and operational behavior meet the application's requirements.
- Add a tested backup when a second route is useful, but the primary integration does not need to change.
- Run a migration canary when the candidate gateway satisfies the protocol and model requirements and you are ready to compare real behavior on controlled traffic.
BetterToken does not sell OpenRouter accounts and cannot transfer OpenRouter keys or balance. To test it, create your own BetterToken account and API key, then obtain the current Model ID and key requirements from Workspace or the current documentation.
Build a requirements matrix before touching configuration
“OpenAI-compatible” describes part of an interface, not the complete behavior of a service. Two gateways can accept similar requests while differing in available models, streaming events, tool-call payloads, error bodies, retry headers, rate limits, or usage accounting.
Record the current behavior and the acceptance criterion for each requirement before you test a candidate.
Do not choose a gateway by model count alone. The important question is whether the current required Model ID and response contract work for this application. Model availability and prices are dynamic, so read the provider's current catalog and rate card during the migration.
Scenario 1: Stay on OpenRouter
Stay when there is no concrete gap to solve. If the application depends on OpenRouter's current catalog, routing, headers, or response behavior, a migration introduces new failure modes without a corresponding benefit.
You can still prepare for future portability:
- move the Base URL, API key, and Model ID into configuration;
- document the response and error fields the application consumes;
- separate provider-specific headers from shared request logic;
- add contract tests that can run against an isolated key;
- write down rollback ownership and the allowed retry policy.
This work reduces migration risk later without changing production traffic now.
Scenario 2: Add a tested backup
A backup route is useful only after it passes the same contract tests as the primary route. Keep provider configurations separate; do not overwrite the OpenRouter settings merely to prove that another request succeeds.
Define exactly which failures are eligible for fallback. Authentication failures, invalid Model IDs, unsupported methods, malformed requests, and most other client errors should not be retried through another provider automatically. For transient failures, fallback is still bounded by idempotency, retry limits, timeout budgets, and the candidate's feature support.
Never send the same mutating operation to two gateways unless the application has a verified idempotency mechanism and can confirm the destination state. A backup does not guarantee that every request will complete or that output will be identical.
Scenario 3: Migrate with a canary
Use a canary when the candidate has passed isolated tests and the objective is to move the primary route. Send only a small, controlled segment of non-critical traffic first. Keep the current route available until the observation window and rollback checks are complete.
Define success before the canary begins:
- expected response schema is parsed without fallback code;
- required streaming and tool-call behavior passes;
- error types remain classifiable;
- usage records reconcile within the application's accepted boundary;
- latency, timeout, and retry behavior meet the defined threshold;
- no duplicated or missing side effects appear.
If any required condition fails, stop expansion and route new traffic back to the known-good configuration.
Five-step migration procedure
Step 1: Inventory the current contract
Capture the exact protocol, SDK and method, OpenRouter Base URL, Model ID, authentication variable, provider-specific headers, streaming mode, tool use, timeout policy, retry policy, error fields, request ID, and usage fields.
Identify which items are hard requirements and which can change. A logging field may be replaceable; a tool-call contract used by production automation may not be.
Do not copy API keys into the worksheet. Record only the secret's variable name and storage location.
Step 2: Create an isolated candidate configuration
Create a separate test key at the candidate gateway. Do not reuse a production key or commit secrets to the repository.
For BetterToken, use your own account and select the current Model ID and key requirements shown in Workspace or the current API documentation. Do not hardcode a Model ID from an old tutorial.
Keep the candidate configuration alongside, not on top of, the current OpenRouter configuration. That separation makes comparison and rollback possible.
Step 3: Set the protocol-specific Base URL
For an OpenAI-compatible BetterToken client, use:
For an Anthropic-compatible client, the BetterToken Base URL is https://bettertoken.ai without /v1. Do not reuse the OpenAI-compatible Python request shape for an Anthropic-compatible client; follow the SDK and protocol documentation for that path.
Confirm whether your client expects a versioned Base URL or appends part of the path itself. A duplicated or missing /v1 is a configuration error, not evidence that the entire gateway is unavailable.
Step 4: Run the same minimal request and contract tests
Use the same SDK family and method as the application. The following Python example tests an OpenAI-compatible Chat Completions request with environment variables:
The placeholders are intentional. Load the real key through the project's secret mechanism, and obtain the current Model ID from the provider at test time.
After the basic request, run separate tests for streaming, tool calls, invalid authentication, and an invalid Model ID if the production application depends on those behaviors. Record non-sensitive evidence: timestamp, status, request ID when available, response shape, usage fields, and application result.
Step 5: Send a canary, compare, and decide
Route a controlled portion of non-critical traffic to the candidate. Compare both routes over a representative window:
- success and classified error counts;
- latency distribution and timeout behavior;
- retry count and
Retry-Afterhandling where present; - response and tool-call schemas;
- streaming completion;
- input, cached input, and output usage;
- provider-side status and charge records;
- duplicated, missing, or delayed side effects.
Expand only after every hard requirement passes. If a rollback trigger fires, return new traffic to OpenRouter and investigate the candidate offline.
Validate more than a successful response
A successful HTTP status proves only that one request returned. It does not prove protocol equivalence, production readiness, or correct routing.
Validate errors deliberately
Use the isolated test key to trigger controlled failures:
- an invalid Model ID;
- a revoked or intentionally invalid test key;
- an unsupported method, if safe to test;
- a timeout in a non-production environment.
Check the HTTP status, error body, request ID, retry metadata, and client behavior. Ensure terminal errors are not retried indefinitely and that logs redact credentials and sensitive request content.
The OpenRouter error reference documents OpenRouter's behavior. Treat the candidate's current documentation and observed response as a separate contract.
Reconcile usage
Capture the usage object returned to the SDK and compare it with the application log and the provider's account record. Required fields may include input tokens, cached input, output tokens, or provider-specific units.
For BetterToken, Dashboard can show request time, model, status, input, output, cache tokens, and the corresponding charge. It does not imply that complete prompts or responses are stored or displayed. Match the record by test time and other non-secret metadata; do not assume a successful reply proves which route handled it.
Test streaming and tool calls separately
For streaming, verify the first event, content deltas, finish reason, final usage when provided, disconnect handling, and whether the application can distinguish a partial answer from a completed one.
For tool calls, compare the tool name, call ID, serialized arguments, validation failures, and result submission flow. Use a read-only tool during the first test. A normal text response does not prove that the tool-call path is compatible.
Rollback boundaries
Keep rollback explicit and reversible before the canary starts.
Rollback immediately when a hard requirement fails, including:
- the application cannot parse the response or error schema;
- a required stream ends incompletely;
- tool-call arguments or IDs are corrupted;
- usage cannot be reconciled for required billing controls;
- timeout or error behavior breaches the agreed operational threshold;
- a mutating workflow produces an uncertain, duplicated, or missing result.
When rolling back:
- stop increasing candidate traffic;
- direct new requests to the known-good OpenRouter configuration;
- do not automatically replay mutating requests with an unknown outcome;
- preserve timestamps, request IDs, status codes, and redacted logs for diagnosis;
- keep the candidate key isolated until the cause is understood, then revoke it if it is no longer needed.
Do not delete the old configuration or credentials until the migration owner has confirmed the observation window, rollback test, and downstream reconciliation. If both routes remain active, document ownership, health checks, eligible fallback errors, and the maximum retry budget.
Cost and operational checks
Do not embed fixed prices in a migration runbook. Read each provider's current rate card on the test date and compare it with actual recorded usage.
Include these items in the decision:
- model and feature availability;
- input, cached input, and output rates;
- minimum funding or account requirements shown at payment time;
- request and concurrency limits;
- timeout and retry behavior;
- usage export or Dashboard visibility;
- key rotation and per-project control;
- support and incident escalation paths.
For BetterToken, use the current pricing page and Workspace rather than an old screenshot or copied rate. The service is one candidate gateway, not proof that every OpenRouter workload can migrate unchanged.
Final decision checklist
Stay on OpenRouter when:
- there is no concrete integration or operational gap;
- its current catalog and contract are required;
- the candidate has not passed a hard requirement.
Add a backup when:
- a second route has independent value;
- it has passed the same protocol, error, usage, and feature tests;
- fallback rules are bounded and observable.
Migrate when:
- the candidate meets every hard requirement;
- the canary stays within the accepted error and latency thresholds;
- usage and billing records reconcile;
- rollback has been tested and still works.
An OpenAI-compatible label is the start of a test plan, not evidence of complete equivalence. The safest migration is isolated, observable, incremental, and reversible.
If BetterToken is a candidate for your integration, start with its current API documentation, create a separate test key, and run the five-step canary before changing production traffic.
Before switching production traffic
For a concise comparison of requirements for an alternative route, see OpenRouter alternatives. It helps form a checklist before migration, but current model IDs, supported features, and payment options still need verification on the day you switch.