# OpenRouter

Use OpenRouter as a model provider or render items from its Agent SDK.

## Use OpenRouter as a model provider

Add the OpenRouter provider to your AI SDK server.

```bash
npm install @openrouter/ai-sdk-provider@3.1.0
```

Configure the model ID and the server-side key. Continue using the same [Vercel AI SDK view](/libraries/ai-runtime-vercel).

```tsx
import { createOpenRouter } from "@openrouter/ai-sdk-provider";
import { streamText } from "ai";
export function streamOpenRouter(apiKey: string, modelId: string, prompt: string, signal: AbortSignal) {
  const provider = createOpenRouter({ apiKey });
  return streamText({ model: provider.chat(modelId), prompt, abortSignal: signal }).toUIMessageStreamResponse();
}

```

Return that response from your authenticated route. Use the [Vercel React recipe](/libraries/ai-runtime-vercel) unchanged. Validate input and own history/tool policy in your server. See [OpenRouter's provider docs](https://openrouter.ai/docs/quickstart?api=ai-sdk).

## Agent SDK with native items

For the Agent SDK path, install the tested package.

```bash
npm install @openrouter/agent@0.11.0
```

getItemsStream() sends updated items. Replace each item by ID and keep its original position in the list.

```tsx
import type { OpenRouter } from "@openrouter/agent";
import { normalizeOpenRouterItems } from "@collectivecreation/ghost-ui/chat-normalizers";
function itemId(item: unknown): string {
  if (typeof item !== "object" || item === null) throw new TypeError("Invalid item.");
  if ("id" in item && typeof item.id === "string" && item.id) return item.id;
  if ("callId" in item && typeof item.callId === "string") return `result:${item.callId}`;
  throw new TypeError("A durable item ID is required.");
}
export async function* openRouterMessages(client: OpenRouter, model: string, input: string, signal: AbortSignal) {
  const result = client.callModel({ model, input, signal });
  const items = new Map<string, unknown>();
  for await (const item of result.getItemsStream()) {
    items.set(itemId(item), item);
    yield normalizeOpenRouterItems([...items.values()], { getId: itemId });
  }
}

```

Run this on the server or an existing authenticated application owner; deliver its canonical snapshots to the [plain-data React view](/libraries/ai#installation) or an [external store](/libraries/ai-runtime#external-store). Keep the native conversation state for follow-up runs; the projected UI messages do not replace it.

## Tools, usage and cancellation

A completed function_call item means its input is ready, not that execution succeeded. A function_call_output is a separate result. Tool execution and approval policy stay with the Agent SDK/application configuration. Unknown items remain explicit. Reasoning summaries require deliberate classification.

The items stream excludes usage metadata. Use native usage/full-response APIs when your application needs cost or round accounting; do not invent totals from displayed text. Pass cancellation through the native signal and preserve its outcome. See [Agent item streaming](https://openrouter.ai/docs/agent-sdk/call-model/working-with-items) and [stability](/libraries/ai-stability).
