# Vercel AI SDK

Use eWest models with generateText, streamText, tools and embed.

The AI SDK reaches eWest through its OpenAI-compatible provider, so `generateText`, `streamText`, tools, `embed` and the UI hooks work with every `ewest/*` chat and embedding model. Examples are plain JavaScript and need Node 22 or newer (the AI SDK packages require it); they work unchanged in TypeScript and in Next.js route handlers.

## Setup

Install the AI SDK, the OpenAI-compatible provider and zod (for tool inputs):

npm:

```bash
npm install ai @ai-sdk/openai-compatible zod
```

[Create an API key](https://ewest.ai/keys) and put it in the `EWEST_KEY` environment variable (`export EWEST_KEY=sk-…`). The examples read it from there; never put the key in browser code.

Create the provider once with the base URL `https://api.ewest.ai/v1` and your key. `includeUsage` asks for token counts on streamed responses as well.

JavaScript:

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

const ewest = createOpenAICompatible({
  name: "ewest",
  baseURL: "https://api.ewest.ai/v1",
  apiKey: process.env.EWEST_KEY,
  includeUsage: true, // token usage on streamed responses too
});

export default ewest;
```

Already using the OpenAI or Anthropic provider? Both work with eWest too: set `baseURL` to `https://api.ewest.ai/v1` in either. With `@ai-sdk/openai`, `ewest.chat(id)` sends chat completions and `ewest(id)` uses the Responses API.

@ai-sdk/openai:

```javascript
import { createOpenAI } from "@ai-sdk/openai";
import { generateText } from "ai";

const ewest = createOpenAI({ baseURL: "https://api.ewest.ai/v1", apiKey: process.env.EWEST_KEY });

// ewest.chat() uses /v1/chat/completions; ewest() uses /v1/responses.
const { text } = await generateText({
  model: ewest.chat("ewest/claude-haiku"),
  prompt: "Say hello in one short sentence.",
  maxOutputTokens: 256,
});
console.log(text);
```

@ai-sdk/anthropic:

```javascript
import { createAnthropic } from "@ai-sdk/anthropic";
import { generateText } from "ai";

// This provider adds /messages to the base URL, so keep /v1 here.
const ewest = createAnthropic({ baseURL: "https://api.ewest.ai/v1", apiKey: process.env.EWEST_KEY });

const { text } = await generateText({
  model: ewest("ewest/claude-haiku"),
  prompt: "Say hello in one short sentence.",
  maxOutputTokens: 256,
});
console.log(text);
```

## Choose a model

Use the full model ID, including the `ewest/` prefix. The table has good starting points; [Models](https://ewest.ai/docs/models) lists every model with its context window and limits, and `GET /v1/models` returns the models your key may use. Chat models go in `ewest(id)`, embedding models in `ewest.embeddingModel(id)`.

| Use | Model ID |
| --- | --- |
| Fast and cheap chat, tools | `ewest/claude-haiku` |
| Stronger reasoning and coding | `ewest/claude-sonnet` |
| Long context (1M tokens), images, audio and video input | `ewest/gemini-flash` |
| Embeddings for search and RAG | `ewest/gemini-embedding` |

## Verify it works

A first request with `generateText`. It prints the answer and the token usage.

JavaScript:

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

const ewest = createOpenAICompatible({
  name: "ewest",
  baseURL: "https://api.ewest.ai/v1",
  apiKey: process.env.EWEST_KEY,
  includeUsage: true, // token usage on streamed responses too
});

const { text, usage } = await generateText({
  model: ewest("ewest/claude-haiku"),
  prompt: "Say hello in one short sentence.",
  maxOutputTokens: 256,
});
console.log(text);
console.log(usage);
```

The request shows up in the console under **Requests** within a few seconds, with its tokens and cost. Every response carries an `x-request-id` header, its [trace ID](https://ewest.ai/docs/traces).

## Streaming and tools

`streamText` returns text as it is generated. Read `textStream` in Node, or return `result.toUIMessageStreamResponse()` from a Next.js route for `useChat`.

JavaScript:

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

const ewest = createOpenAICompatible({
  name: "ewest",
  baseURL: "https://api.ewest.ai/v1",
  apiKey: process.env.EWEST_KEY,
  includeUsage: true, // token usage on streamed responses too
});

const result = streamText({
  model: ewest("ewest/claude-haiku"),
  prompt: "Write a haiku about the sea.",
  maxOutputTokens: 256,
});
for await (const text of result.textStream) process.stdout.write(text);
console.log("\n", await result.usage);
```

Tools are defined with a zod schema and an `execute` function. With `stopWhen`, the SDK sends the tool result back to the model so it can answer in the same call.

JavaScript:

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

const ewest = createOpenAICompatible({
  name: "ewest",
  baseURL: "https://api.ewest.ai/v1",
  apiKey: process.env.EWEST_KEY,
  includeUsage: true, // token usage on streamed responses too
});

const { text, steps } = await generateText({
  model: ewest("ewest/claude-haiku"),
  tools: {
    getWeather: tool({
      description: "Get the current weather for a city",
      inputSchema: z.object({ city: z.string() }),
      execute: async ({ city }) => ({ city, temperatureC: 18, sky: "cloudy" }),
    }),
  },
  stopWhen: stepCountIs(3), // call the tool, then answer with its result
  prompt: "What is the weather in Yerevan?",
  maxOutputTokens: 256,
});
console.log(steps.flatMap((s) => s.toolCalls.map((c) => c.toolName)));
console.log(text);
```

Embeddings use `embed` (one value) or `embedMany` (a list) with an embedding model:

JavaScript:

```javascript
import { embed } from "ai";
import { createOpenAICompatible } from "@ai-sdk/openai-compatible";

const ewest = createOpenAICompatible({
  name: "ewest",
  baseURL: "https://api.ewest.ai/v1",
  apiKey: process.env.EWEST_KEY,
  includeUsage: true, // token usage on streamed responses too
});

const { embedding } = await embed({
  model: ewest.embeddingModel("ewest/gemini-embedding"),
  value: "How do I reset my password?",
});
console.log(embedding.length);
```

## Troubleshooting

- **404 `model_not_found`:** the model name needs the `ewest/` prefix (`ewest/claude-haiku`, not `claude-haiku`). Check the spelling against [Models](https://ewest.ai/docs/models).
- **Empty answer from `ewest/gemini-flash`:** `ewest/gemini-flash` does not think unless reasoning is on (low, medium or high); then thinking uses output tokens and a small output limit can be used up. Allow at least 256 output tokens, or turn reasoning off (below). This is a known limitation.
- **Follow-up turns forget the conversation:** on the Responses API, pass the earlier response's `id` as `previous_response_id` (conversations are stored by default), or send the full message history with every request; chat completions and Messages are always stateless. See [Responses](https://ewest.ai/docs/responses).
- **401, 402 or 403:** the key is missing or wrong, out of budget, or not allowed to use this model. [Errors](https://ewest.ai/docs/errors) explains each code; a [trace ID](https://ewest.ai/docs/traces) finds the request in the console.
- **No usage on streamed responses:** set `includeUsage: true` on the provider.

Turning reasoning off: pass `reasoningEffort: "none"` in `providerOptions`, under the provider's `name`. Most chat models accept `none`; a few don't (their lowest is `low`) and answer 400 `unsupported_value`. Check the model's `reasoning_efforts` in `GET /v1/models/{id}` or the catalog.

JavaScript:

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

const ewest = createOpenAICompatible({
  name: "ewest",
  baseURL: "https://api.ewest.ai/v1",
  apiKey: process.env.EWEST_KEY,
  includeUsage: true, // token usage on streamed responses too
});

const { text } = await generateText({
  model: ewest("ewest/gemini-flash"),
  prompt: "Say hello in one short sentence.",
  maxOutputTokens: 256,
  providerOptions: { ewest: { reasoningEffort: "none" } }, // answer without thinking first
});
console.log(text);
```

Still stuck? Follow [Troubleshoot a request](https://ewest.ai/docs/troubleshooting).

Source: https://ewest.ai/docs/guides/vercel-ai-sdk
