> ## Documentation Index
> Fetch the complete documentation index at: https://docs.ai-stats.phaseo.app/llms.txt
> Use this file to discover all available pages before exploring further.

# Migration from OpenRouter

> Move from OpenRouter to AI Stats Gateway with a controlled rollout, explicit parity checks, and minimal request-shape changes.

If your app already uses OpenRouter through the OpenAI SDK or direct OpenAI-compatible HTTP calls, the safest migration path is usually:

1. Keep payload shape unchanged.
2. Swap base URL and API key source.
3. Verify model IDs and any OpenRouter-only headers.
4. Roll traffic gradually while comparing latency, output, and cost behavior.

## Before you start

* Access to the current OpenRouter integration code and deployment config.
* `AI_STATS_API_KEY` available in dev, staging, and production.
* A short list of production model IDs and representative prompts.

## 1) Inventory current OpenRouter usage

Start by identifying every place OpenRouter is referenced: endpoint URLs, keys, model IDs, and any provider-specific headers.

* Find `openrouter.ai` endpoint references.
* Find `OPENROUTER_API_KEY` usage in code, CI, and hosting env vars.
* Find OpenRouter-only headers such as `HTTP-Referer` and `X-Title`.
* Document active model IDs and any fallback-chain logic.
* Document any reusable prompt, provider, or parameter defaults that should move into Gateway presets instead of staying duplicated in application code.

## 2) Swap base URL and credentials

Keep request payload shape unchanged first. Do behavior parity before optimization.

<CodeGroup>
  ```typescript TypeScript theme={null}
  // Before
  import OpenAI from "openai";

  const before = new OpenAI({
    apiKey: process.env.OPENROUTER_API_KEY,
    baseURL: "https://openrouter.ai/api/v1",
  });

  const response = await before.chat.completions.create({
    model: "openai/gpt-4.1-mini",
    messages: [{ role: "user", content: "Summarize our migration plan." }],
  });
  ```

  ```typescript TypeScript theme={null}
  // After
  import OpenAI from "openai";

  const after = new OpenAI({
    apiKey: process.env.AI_STATS_API_KEY,
    baseURL: "https://api.phaseo.app/v1",
  });

  const response = await after.chat.completions.create({
    model: "openai/gpt-4.1-mini",
    messages: [{ role: "user", content: "Summarize our migration plan." }],
  });
  ```

  ```bash cURL theme={null}
  # Before
  curl -s "https://openrouter.ai/api/v1/chat/completions" \
    -H "Authorization: Bearer $OPENROUTER_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "openai/gpt-4.1-mini",
      "messages": [{"role":"user","content":"Say hello"}]
    }'

  # After
  curl -s "https://api.phaseo.app/v1/chat/completions" \
    -H "Authorization: Bearer $AI_STATS_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "openai/gpt-4.1-mini",
      "messages": [{"role":"user","content":"Say hello"}]
    }'
  ```
</CodeGroup>

## 3) Validate model IDs and remove OpenRouter-only behavior

Do not assume every previous alias is valid. Query `/v1/models` and verify each production model ID.

<CodeGroup>
  ```bash cURL theme={null}
  curl -s "https://api.phaseo.app/v1/models" \
    -H "Authorization: Bearer $AI_STATS_API_KEY" | jq '.data[0:10] | map(.id)'
  ```

  ```typescript TypeScript theme={null}
  const MODEL_ALIASES: Record<string, string> = {
    "openai/gpt-4.1-mini": "openai/gpt-4.1-mini",
    "anthropic/claude-3.5-sonnet": "anthropic/claude-3.5-sonnet",
  };

  export function resolveModelId(input: string): string {
    return MODEL_ALIASES[input] ?? input;
  }
  ```
</CodeGroup>

* Keep `Authorization: Bearer` format unchanged.
* Remove legacy gateway-specific headers unless you still need them.
* If callers depend on OpenRouter-only response fields, adapt them in one compatibility layer instead of changing every caller.
* If your OpenRouter setup relies on provider allow/deny lists or routing defaults, map those into [Presets](../guides/presets.mdx) and [Routing and Fallbacks](../guides/routing-and-fallbacks.mdx) instead of scattering them across callers.

## 4) OpenRouter parity checklist

Use this checklist before shifting meaningful traffic:

* Base URL updated to `https://api.phaseo.app/v1`.
* `OPENROUTER_API_KEY` replaced with `AI_STATS_API_KEY` in all environments.
* All production model IDs verified against `/v1/models`.
* One non-streaming request validated through `/v1/chat/completions` or `/v1/responses`.
* One streaming request validated through the same app-level integration path used in production.
* Generation lookups rechecked through `GET /v1/generations?id=<request_id>` so failed requests can be replayed from the stored `replay_request` payload when `replay_supported=true`.
* Tool-calling and structured-output flows rechecked with real prompts.
* Invalid-key and invalid-model failures verified in staging.
* Any OpenRouter-only headers or response fields either removed or explicitly normalized.
* Shared prompt/routing defaults moved into presets where appropriate.

## 5) Roll out safely

Use a staged rollout: dev first, then a small production slice, then full traffic once metrics are stable.

1. Start with internal traffic only.
2. Move to 5-10% production traffic and compare quality, latency, and cost metrics.
3. Promote to 100% only after parity is confirmed.
4. Keep rollback as a config-only endpoint/key switch until the cutover is stable.

## Validation commands

```bash theme={null}
curl -s "https://api.phaseo.app/v1/health"
curl -s "https://api.phaseo.app/v1/models" -H "Authorization: Bearer $AI_STATS_API_KEY"
curl -s "https://api.phaseo.app/v1/chat/completions" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $AI_STATS_API_KEY" \
  -d '{"model":"openai/gpt-4.1-mini","messages":[{"role":"user","content":"Say hello"}]}'
```

Then:

* Run one streaming request through your app-level integration test.
* Run one negative test for invalid key or invalid model.
* Replay a small golden prompt set and compare outputs to baseline.

## Next steps

* [Quickstart](../quickstart.mdx)
* [API Reference: Models](../api-reference/endpoint/models.mdx)
* [Examples](../guides/examples.mdx)
* [Error Handling](../developers/error-handling.mdx)
