> ## Documentation Index
> Fetch the complete documentation index at: https://docs.atlaso.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# AI SDK

> Use Atlaso as an AI SDK model, with a user on each request and a receipt on each result.

The Atlaso SDK includes an adapter for the AI SDK (the `ai` package). It gives
you a model for one user, and puts the call's [receipt](https://docs.atlaso.ai/gateway/sdk.md#example-3-which-memories-a-reply-used)
on the result as `providerMetadata.atlaso`.

```bash theme={null}
npm install atlaso ai @ai-sdk/openai-compatible
```

`ai` 6 and 7 both work. `ai` 7 itself needs Node 22 or newer. For Responses
models, also install `@ai-sdk/openai`.

## A model per request

Call `createAtlaso()` once, at module scope: it holds your key and no user.
Inside each request, after your sign-in check, call `forUser(userId).chat(model)`.

```typescript theme={null}
import { generateText } from "ai";
import type { Receipt } from "atlaso";
import { createAtlaso } from "atlaso/ai-sdk";

const atlaso = createAtlaso();   // reads ATLASO_KEY; no user here

export async function answer(userId: string, question: string) {
  const result = await generateText({
    model: atlaso.forUser(userId).chat("openai/gpt-6.1-terra"),   // per request
    prompt: question,
  });
  const receipt = result.providerMetadata?.atlaso as Receipt | undefined;
  return { text: result.text, memoryIds: receipt?.memory?.memoryIds ?? [] };
}
```

With `streamText`, the receipt is on each step's `finish-step` part. The
[streaming route example](https://docs.atlaso.ai/gateway/sdk.md#example-5-the-ai-sdk) sends it to
`useChat`.

* `forUser(id).responses(model)` gives a Responses model (needs `@ai-sdk/openai`).
  If your bundler cannot load that package on demand, pass its `createOpenAI`
  to `createAtlaso({ createOpenAI })`.
* `forUser(id, { memory: "off", tags: { feature: "chat" } })` sets the same
  per-call options as the core SDK.
* `forUser(id).memory` is the [memory API](https://docs.atlaso.ai/gateway/sdk.md#example-2-save-list-and-delete) for that user.
* Stream cost: `createAtlaso({ includeUsage: true })` asks the stream for usage,
  so the receipt has its cost. Left unset, nothing is added to your request and a
  stream receipt says `usage_not_requested`.

## Memory tools for agents

`forUser(id).tools.aiSdk()` gives `remember`, `list_memories`, and the read-only
`search_memories` tool bound to that user. Search takes a query and an optional
limit of 1 to 20. There is no user argument for the model to fill in. `forget` is
added only with `{ allowForget: true }`, and it can delete only facts returned by
`list_memories` or named in a receipt you pass with `{ receipt }`.

```typescript theme={null}
import { generateText, stepCountIs } from "ai";
import { createAtlaso } from "atlaso/ai-sdk";

const atlaso = createAtlaso();

export async function chatWithTools(userId: string, text: string) {
  const user = atlaso.forUser(userId);
  const result = await generateText({
    model: user.chat("openai/gpt-6.1-terra"),
    tools: user.tools.aiSdk(),   // remember, list_memories, search_memories
    stopWhen: stepCountIs(3),
    prompt: text,
  });
  return result.text;
}
```

Every step of the run, model calls and tool calls alike, carries the same user.

## Your own provider, per-call headers

If you keep your own `@ai-sdk/openai-compatible` provider, build it once
without a user and pass the user's headers on each call. You get no receipt this
way; use `forUser(id).chat()` for receipts.

```typescript theme={null}
import { streamText } from "ai";
import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
import { Atlaso } from "atlaso";

const atlaso = new Atlaso();
const gateway = createOpenAICompatible({ name: "atlaso", ...atlaso.clientOptions() });   // no user

export async function answer(userId: string, question: string) {
  const result = streamText({
    model: gateway.chatModel("openai/gpt-6.1-terra"),
    prompt: question,
    headers: atlaso.forUser(userId).headers(),   // this call only
  });
  return await result.text;
}
```

> [!WARNING]
>
> **The dangerous pattern: a user in the provider settings.** A provider built at
> module scope with an `x-atlaso-user` header sends every request, for every user,
> as that user. Whoever signs in next gets the first user's facts.

```typescript theme={null}
import { generateText } from "ai";
import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
import { Atlaso } from "atlaso";

const atlaso = new Atlaso();
// Don't: every user of this function is sent as demo-user-42.
const shared = createOpenAICompatible({
  name: "atlaso", ...atlaso.clientOptions(), headers: { "x-atlaso-user": "demo-user-42" },
});

export async function answer(userId: string, question: string) {
  const result = await generateText({ model: shared.chatModel("openai/gpt-6.1-terra"), prompt: question });
  return result.text;   // userId was never sent
}
```

## Choose providers

`routing()` from `atlaso/ai-sdk` sets which providers may answer, in what
order, and whether a backup may answer. Pass it as `providerOptions` on a
`chat()` model. It takes the same options and runs the same checks as the
[core `routing()`](https://docs.atlaso.ai/gateway/sdk#choose-providers), and sends nothing when no
option is set. Responses models (`responses()`) do not carry it: the adapter
throws `routing_not_supported` instead of dropping it.

```typescript theme={null}
import { generateText } from "ai";
import { answeredBy, type Receipt } from "atlaso";
import { createAtlaso, routing } from "atlaso/ai-sdk";

const atlaso = createAtlaso();

export async function answer(userId: string, question: string) {
  const result = await generateText({
    model: atlaso.forUser(userId).chat("anthropic/claude-sonnet-5.5"),
    prompt: question,
    providerOptions: routing({ order: ["anthropic", "bedrock"], allowFallbacks: false }),
  });
  const receipt = result.providerMetadata?.atlaso as Receipt | undefined;
  return { text: result.text, answeredBy: receipt ? answeredBy(receipt) : null };
}
```

To add other options for this provider in the same call, spread the inner
object: `providerOptions: { atlaso: { ...routing(options).atlaso, reasoningEffort: "low" } }`.

## Limits

* There is no receipt before the first token arrives, and none for a stream you
  abort: the AI SDK sends no finish part then.
* `providerMetadata.atlaso` replaces any metadata under the same key from the
  provider package.
* If you pass your own core client with `createAtlaso({ atlaso })`, also pass the
  same `fetch` there if you gave it a custom one.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.