> ## 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.

# Migrating from Vercel AI Gateway

> Replace Vercel AI Gateway routing with AI Stats Gateway while preserving AI SDK or OpenAI-compatible application behavior.

If you already use Vercel AI Gateway through the Vercel AI SDK or an OpenAI-compatible client, the safest migration path is to keep your app logic the same and swap only the provider boundary first.

## Before you start

* Current Vercel AI Gateway base URL and key configuration.
* `AI_STATS_API_KEY` available in local, staging, and production environments.
* A small prompt set or integration suite that covers non-streaming, streaming, and any tool-calling paths you rely on.

## 1) Document your current gateway boundary

Find the single place where your app creates model providers or API clients. That is the preferred migration point.

* Locate the provider/client factory used by your app.
* List the model IDs currently used in production.
* Record retry, timeout, and fallback defaults.
* Note whether both edge and server runtimes need the same config change.
* Identify any shared prompt or parameter defaults that should become Gateway presets instead of remaining embedded in individual AI SDK call sites.

## 2) Swap endpoint and key

For most OpenAI-compatible client paths, this is a base URL and key replacement only.

<CodeGroup>
  ```typescript TypeScript theme={null}
  // OpenAI-compatible client before
  import OpenAI from "openai";

  const before = new OpenAI({
    apiKey: process.env.VERCEL_AI_GATEWAY_API_KEY,
    baseURL: "https://ai-gateway.vercel.sh/v1",
  });
  ```

  ```typescript TypeScript theme={null}
  // OpenAI-compatible client after
  import OpenAI from "openai";

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

  ```typescript TypeScript theme={null}
  // Official AI Stats provider for the Vercel AI SDK
  import { generateText } from "ai";
  import { createAIStats } from "@ai-stats/ai-sdk-provider";

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

  const { text } = await generateText({
    model: aiStats("openai/gpt-4.1-mini"),
    prompt: "Generate a migration checklist.",
  });
  ```

  ```bash cURL theme={null}
  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":"Hello"}]
    }'
  ```
</CodeGroup>

## 3) Confirm behavior parity

Run the same prompt set against old and new paths, then compare latency, output format, and token usage.

* Verify non-streaming text generation.
* Verify streaming chunk handling through the same code path your app uses in production.
* Verify tool-calling paths if your app depends on them.
* Confirm application-level error mapping is unchanged.
* Move durable routing defaults and provider restrictions into [Presets](../guides/presets.mdx) or [Routing and Fallbacks](../guides/routing-and-fallbacks.mdx) instead of rebuilding them ad hoc in every model factory.

## 4) Vercel AI SDK and gateway checklist

Use this checklist before promoting traffic:

* Base URL updated to `https://api.phaseo.app/v1`.
* `AI_STATS_API_KEY` configured in every runtime that previously used the Vercel gateway key.
* Official `@ai-stats/ai-sdk-provider` wired in if your app uses the Vercel AI SDK directly.
* Primary AI SDK text generation path passes in staging.
* One app-level streaming test passes unchanged.
* Tool-calling and structured-output flows rechecked if used.
* Old/new outputs compared for a small prompt suite.
* Rollback remains possible through config or feature-flag change only.
* Shared prompt/routing defaults moved into presets where appropriate.
* 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`.

## 5) Release plan for low risk

1. Ship behind a feature flag or percentage rollout.
2. Start with internal traffic or a very small production slice.
3. Monitor latency, error rate, and token/cost drift.
4. Keep both configs available for at least one release window.

## 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"
```

Then:

* Run your app's primary text-generation integration test.
* Run one streaming test in staging.
* Compare old/new outputs for your top prompts before full rollout.

## Next steps

* [Migration from OpenRouter](./from-openrouter.mdx)
* [Quickstart](../quickstart.mdx)
* [Examples](../guides/examples.mdx)
