# Vercel AI SDK (https://inference.net/docs/integrations/vercel-ai-sdk)

Connect the Vercel AI SDK (`ai`) to Inference.net with `@ai-sdk/openai-compatible` by setting `baseURL` to `https://api.inference.net/v1` and `apiKey` to your `INFERENCE_API_KEY`. You can call any hosted model on Inference.net with `generateText`, `streamText`, tool calling, and structured outputs using one key.

## Quick start

Install the AI SDK packages, export your key from [API keys](/dashboard/api-keys/) (or [create an account](/register/) first), and initialize the provider:

```bash
npm install ai @ai-sdk/openai-compatible
export INFERENCE_API_KEY="inf_sk_..."
```

```typescript
import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
import { generateText } from "ai";

const inference = createOpenAICompatible({
  name: "inference",
  baseURL: "https://api.inference.net/v1",
  apiKey: process.env.INFERENCE_API_KEY,
  includeUsage: true,
});

const { text, usage } = await generateText({
  model: inference("deepseek-v4.1-flash-1017"),
  prompt: "Explain speculative decoding in two sentences.",
});

console.log(text);
console.log(usage);
```

<Callout title="Use @ai-sdk/openai-compatible, not @ai-sdk/gateway" tone="info">

Use `createOpenAICompatible({ baseURL: "https://api.inference.net/v1" })`. Do not use `@ai-sdk/gateway` (`createGateway()`), which speaks Vercel's proprietary gateway protocol rather than standard OpenAI-compatible Chat Completions.

</Callout>

## Stream responses

Set `includeUsage: true` on `createOpenAICompatible` so streaming calls return token counts on completion:

```typescript
import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
import { streamText } from "ai";

const inference = createOpenAICompatible({
  name: "inference",
  baseURL: "https://api.inference.net/v1",
  apiKey: process.env.INFERENCE_API_KEY,
  includeUsage: true,
});

const result = streamText({
  model: inference("glm-5.3"),
  prompt: "Write a concise TypeScript retry helper with exponential backoff.",
});

for await (const chunk of result.textStream) {
  process.stdout.write(chunk);
}
```

## Tool calling

Pick a model with `supports_tools: true` in `GET https://api.inference.net/v1/models` (such as `deepseek-v4.1-flash-1017`, `glm-5.3`, or `moonshotai/kimi-k3.1-pro`). See [Function calling](/docs/api/function-calling/) for supported tool parameters.

```typescript
import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
import { generateText, tool } from "ai";
import { z } from "zod";

const inference = createOpenAICompatible({
  name: "inference",
  baseURL: "https://api.inference.net/v1",
  apiKey: process.env.INFERENCE_API_KEY,
  includeUsage: true,
});

const result = await generateText({
  model: inference("deepseek-v4.1-flash-1017"),
  prompt: "What is the status of order ORD-402?",
  tools: {
    getOrderStatus: tool({
      description: "Look up an order status by order ID.",
      inputSchema: z.object({
        orderId: z.string(),
      }),
      execute: async ({ orderId }) => ({
        orderId,
        status: "shipped",
      }),
    }),
  },
});

console.log(result.toolResults);
```

## Structured JSON outputs

Use `supportsStructuredOutputs: true` on `createOpenAICompatible` when passing a Zod schema to `generateText` with `Output.object`, or call [`inference-net/schematron-v2-turbo`](/models/schematron-v2-turbo/) for HTML-to-JSON extraction. See [Structured outputs](/docs/api/structured-outputs/) for JSON schema rules.

```typescript
import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
import { generateText, Output } from "ai";
import { z } from "zod";

const inference = createOpenAICompatible({
  name: "inference",
  baseURL: "https://api.inference.net/v1",
  apiKey: process.env.INFERENCE_API_KEY,
  supportsStructuredOutputs: true,
});

const { output } = await generateText({
  model: inference("deepseek-v4.1-flash-1017"),
  output: Output.object({
    schema: z.object({
      summary: z.string(),
      severity: z.enum(["low", "medium", "high"]),
    }),
  }),
  prompt: "Classify this incident: API latency increased by 40% in one region.",
});

console.log(output);
```

## Task IDs and regional routing

Pass `x-inference-task-id` and `x-inference-environment` at the provider level or per call, and switch `baseURL` to `https://us.api.inference.net/v1` for US-only models marked with the `US` badge in the [model catalog](/models/):

```typescript
const { text } = await generateText({
  model: inference("glm-5.3"),
  prompt: "Summarize the deployment diff.",
  headers: {
    "x-inference-task-id": "deploy-summary",
    "x-inference-environment": "production",
  },
});
```

## Verified model IDs

| Model ID | Context window | Tool calling | Model page |
| --- | --- | --- | --- |
| `deepseek-v4.1-flash-1017` | 1,040,000 | Yes | [DeepSeek V4.1 Flash](/models/deepseek-v4-1-flash-1017/) |
| `glm-5.3` | 1,048,576 | Yes | [GLM 5.3](/models/glm-5-3/) |
| `glm-5.3-flash` | 1,048,576 | Yes | [GLM 5.3 Flash](/models/glm-5-3-flash/) |
| `moonshotai/kimi-k3.1-pro` | 1,048,576 | Yes | [Kimi K3.1 Pro](/models/kimi-k3-1-pro/) |
| `inference-net/schematron-v2-turbo` | 128,000 | No | [Schematron V2 Turbo](/models/schematron-v2-turbo/) |

## Optional: bring your own provider key

If you want to proxy an external provider key through the Inference.net Gateway (requires a credit-billing API key), pass `x-inference-provider` and `x-inference-provider-api-key` in the `headers` object of `createOpenAICompatible`. See [API quickstart](/docs/api/quickstart/#2-proxy-through-the-inferencenet-gateway) for details.

## Troubleshooting

- **`401 Unauthorized`:** Check that `INFERENCE_API_KEY` is loaded in your runtime environment (`.env.local` in Next.js or shell export in Node/Bun).
- **`404 Not Found`:** Confirm `baseURL` is `https://api.inference.net/v1` (with `/v1`), not `https://inference.net/v1`.
- **Missing token usage on streams:** Set `includeUsage: true` in `createOpenAICompatible`.
- **Model not found:** Copy the exact `id` from `GET https://api.inference.net/v1/models`, including owner prefixes such as `moonshotai/kimi-k3.1-pro`.

Browse all supported editors, gateways, and SDKs in the [Integrations hub](/docs/integrations/).