Client Adapters
Connect AI SDK or TanStack AI to AI Gateway with Contextual's client adapters.
Contextual publishes optional adapters for AI SDK and TanStack AI. 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 |
Install with ai 7.x. |
| TanStack AI 1 | @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 with a working AI Connection and model. Use the Route ID when selecting the adapter's model.
- Your tenant ID 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. For an application, see Set up API Key access and 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:
{
"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. Without this option, streamText delivers each completed generation step together. Structured output arrives whole.
See the Contextual AI SDK provider README 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 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.