# Vercel AI SDK (/docs/guides/sdks/vercel-ai-sdk)

Your `ai` code keeps `streamText`, `generateText`, its tools and its structured output. You build the provider with your `AiConnect` client's `fetch`, and every call then answers through the visitor's wallet, on the account the visitor picked for your site. That account can be a ChatGPT plan, a Claude plan, a Mistral key or any other account the wallet holds. The [AI SDK guide](/docs/guides/existing-sdks) explains how this works for every SDK.

## Install [#install]

```bash
npm install ai @ai-sdk/openai @tensorpass/aiconnect
```

Until `@tensorpass/aiconnect` is published on npm, the [JavaScript quickstart](/docs/quickstart/javascript#install-the-package) shows how to point your project at a checkout of the repository.

## The change [#the-change]

```ts
// Before
import { openai } from '@ai-sdk/openai';

// After
import { createOpenAI } from '@ai-sdk/openai';

const openai = createOpenAI({ apiKey: aiconnect.sdkOptions.apiKey, fetch: aiconnect.fetch });
```

`aiconnect` is your `AiConnect` client. The provider requires a key, so the placeholder `aiwallet` of `sdkOptions.apiKey` fills it, and the wallet never reads it. `openai(model)` speaks the Responses API and `openai.chat(model)` speaks chat completions, and both answer.

`createAnthropic` from `@ai-sdk/anthropic` and `createGoogleGenerativeAI` from `@ai-sdk/google` take the same two options. The provider you pick only sets the protocol between the page and the wallet: whichever you keep, the visitor's account answers.

```ts
import { createAnthropic } from '@ai-sdk/anthropic';
import { createGoogleGenerativeAI } from '@ai-sdk/google';

const anthropic = createAnthropic({ apiKey: aiconnect.sdkOptions.apiKey, fetch: aiconnect.fetch });
const google = createGoogleGenerativeAI({ apiKey: aiconnect.sdkOptions.apiKey, fetch: aiconnect.fetch });
```

The call runs in the page, where the wallet answers. Show your connect entry point first, as the [React quickstart](/docs/quickstart/react) does: a request from a site the visitor has not connected fails with status 401.

## streamText with tools [#streamtext-with-tools]

The bookshop demo in `apps/demo` of the AI Connect repository runs this in its SDK mode. `search_books` stands for your own tools, `runTool` runs one and returns its result as text, and `showText` writes the answer as it streams.

```ts
import { createOpenAI } from '@ai-sdk/openai';
import { jsonSchema, stepCountIs, streamText, tool } from 'ai';

const openai = createOpenAI({ apiKey: aiconnect.sdkOptions.apiKey, fetch: aiconnect.fetch });
const result = streamText({
  model: openai('gpt-6-luna'),
  system,
  prompt: question,
  tools: {
    search_books: tool({
      description: 'Find books in the catalog by mood and maximum length.',
      inputSchema: jsonSchema(searchBooksSchema),
      execute: (args) => runTool('search_books', args),
    }),
  },
  stopWhen: stepCountIs(4),
  abortSignal: signal,
});
for await (const part of result.fullStream) {
  if (part.type === 'text-delta') {
    showText(part.text);
  } else if (part.type === 'error') {
    throw part.error;
  }
}
```

The SDK runs the tool loop: each `execute` result goes back to the wallet in the next step. When your tools live on your MCP server, the [MCP guide](/docs/guides/mcp-tools#with-an-ai-sdk) builds this `tools` object from them. `toolChoice`, `temperature`, `topP`, `maxOutputTokens` and `stopSequences` reach the accounts that take them.

## Structured output [#structured-output]

`Output.object` sends a JSON schema to the account in its own provider's format, and the SDK parses the answer:

```ts
import { generateText, jsonSchema, Output } from 'ai';

const { output } = await generateText({
  model: openai.chat('gpt-6-luna'),
  prompt: question,
  output: Output.object({ schema: jsonSchema<{ title: string; reason: string }>(pickSchema) }),
});
```

A Zod schema works in place of `jsonSchema`, as with any provider.

## A route on your server [#a-route-on-your-server]

`aiconnect.fetch` answers in the browser only, so a `streamText` call in a server route has to move into the page. The tools the route ran on your data stay on your server, served over MCP. The [AI SDK guide](/docs/guides/existing-sdks#move-a-call-from-your-server-into-the-page) shows the route before and after.

## The model that answers [#the-model-that-answers]

The model name you pass is ignored, since the visitor chooses the model in the wallet. `(await result.response).modelId` names the model that answered, and the provider's first event carries the model of the account that answers. The [model names section](/docs/guides/existing-sdks#model-names) tells which event carries which name.

## Errors and retries [#errors-and-retries]

A failure before any text reaches `fullStream` as an `error` part. It holds an `APICallError` whose `statusCode` is the wallet's status, or a `RetryError` around that error once the retries are spent: 401 when the site is not connected, 403 when the visitor declined or left the wallet locked, 429 when the account's plan is spent. The [errors section](/docs/guides/existing-sdks#errors) lists every status.

`streamText` and `generateText` retry a 429 and the 5xx statuses twice. On Auto the wallet already moves to the visitor's next account, so you can answer sooner with `maxRetries: 0`.

`abortSignal` stops the answer in the wallet.

## What a request can carry [#what-a-request-can-carry]

Images as `data:` URLs or bytes reach every account. Files other than images, several answers per request and a provider's own tools, such as OpenAI's web search, are refused with a 400. The [AI SDK guide](/docs/guides/existing-sdks#what-the-wallet-takes-from-a-request) lists what the wallet passes on to each account.
