# Client Adapters

These instructions apply to the legacy Admin Console.

Contextual publishes optional adapters for [AI SDK](https://ai-sdk.dev/docs/ai-sdk-core/overview) and [TanStack AI](https://tanstack.com/ai/latest/docs/getting-started/overview). Use one when your application already uses that library, or when you want its generation and tool-call helpers.

Both adapters call AI Gateway's native `POST /api/v1/generate` endpoint. Your AI Route selects the provider and model and applies its configured retries and fallbacks. Provider credentials stay in the Route's AI Connections. Your application supplies any tool implementations.

## Choose an adapter

| Application library | Contextual package | Requirements |
| --- | --- | --- |
| AI SDK 7 | [`@contextual-io/ai-sdk-provider`](https://www.npmjs.com/package/@contextual-io/ai-sdk-provider) | Install with `ai` 7.x. |
| TanStack AI 1 | [`@contextual-io/tanstack-ai-adapter`](https://www.npmjs.com/package/@contextual-io/tanstack-ai-adapter) | Install with `@tanstack/ai` 1.x. For Zod tool or output schemas, use Zod 4.2 or later. |

Both packages require **Node.js 22 or later** and use **ES modules**. Run them on your server. Keep access tokens and Client Secrets there, and have browser applications call your server.

The package READMEs linked above provide installation commands, code examples, credential options, and current limitations.

## Before you start

You need:

* A saved [AI Route](https://docs.contextual.io/legacy/components-and-data/ai-routes.md) with a working AI Connection and model. Use the Route ID when selecting the adapter's model.
* Your [tenant ID](https://docs.contextual.io/legacy/tenants/tenant-apis/authentication-and-permissions.md#identify-the-tenant) and Gateway origin, such as `https://ai-gateway.<tenant-id>.my.contextual.io`.
* A current Contextual access token, with [permission to use the Route and its Connections](https://docs.contextual.io/legacy/tenants/tenant-apis/ai-gateway-api.md#authentication-and-permissions). For an application, see [Set up API Key access](https://docs.contextual.io/legacy/tenants/tenant-apis/authentication-and-permissions.md#set-up-api-key-access) and [Get and use an access token](https://docs.contextual.io/legacy/tenants/tenant-apis/authentication-and-permissions.md#get-and-use-an-access-token).

## Configure the adapter

Give the adapter the Gateway **origin**, without a path. It appends `/api/v1/generate` itself.

| Setting | AI SDK provider | TanStack AI adapter |
| --- | --- | --- |
| Gateway origin | `aiGatewayApiUrl` | `url` |
| Tenant ID | `tenantId` | `tenantId` |
| Caller credentials | `credentials` | `credentials` |

For a fixed access token, the `credentials` value has this shape:

```json
{
  "kind": "bearer",
  "token": "<current Contextual access token>"
}
```

The adapters send the token as `Authorization: Bearer <token>` and set `x-org-id` from `tenantId`. The token authorizes your application to use Contextual. AI Connections hold the upstream provider credentials.

Access tokens expire. A fixed bearer credential sends the same token on every call. For a long-running application, use a `provider` credential with `getBearerToken` as described in the package README. That provider manages token caching and renewal; the adapter asks it for a token before each Gateway request.

### AI SDK

Create the provider with `createContextual` and the settings above. Select a model by passing your Route ID to the returned provider, then use that model with AI SDK's `generateText` or `streamText`.

For text and reasoning to arrive during generation, set `streaming: true` when creating the HTTP provider. This requires a Gateway that supports [native streaming](https://docs.contextual.io/legacy/tenants/tenant-apis/ai-gateway-api/streaming-and-tool-calling.md#stream-text-over-http). Without this option, `streamText` delivers each completed generation step together. Structured output arrives whole.

See the [Contextual AI SDK provider README](https://www.npmjs.com/package/@contextual-io/ai-sdk-provider) for setup and streaming examples.

### TanStack AI

Create the HTTP client with `createGatewayClient` and the settings above. Pass your Route ID and that client to `contextualText`, then use the returned adapter with TanStack AI's `chat()`.

The adapter returns each generated answer as one text delta after generation finishes. A TanStack chat stream does not mean that this adapter delivers tokens during generation.

See the [Contextual TanStack AI adapter README](https://www.npmjs.com/package/@contextual-io/tanstack-ai-adapter) for setup, tool use, errors, and supported message types.

## Check access and errors

For `401` or `403`, check the token's expiry, tenant, and Route and Connection permissions. Neither adapter automatically resends a failed Gateway request. Handle the reported error before deciding whether to try again; the AI Route already handles its configured provider retries and fallbacks.
