AI SDK
Convert between AI SDK UI messages and the canonical message format, attribute spend in the AI Gateway, and keep the model catalog and costs current.
better-supabase/ai-sdk connects the AI SDK to the
AI chat block. The block stores messages in its own
format and never imports an SDK; this adapter converts AI SDK messages to
that format and back, and adds the AI Gateway helpers the assistant uses.
It owns no tables.
pnpm add ai @ai-sdk/reactai (7), @ai-sdk/react (4) and @ai-sdk/mcp (2) are optional peers: install them only in
the apps that use these subpaths.
| Subpath | What it gives you |
|---|---|
better-supabase/ai-sdk | Message converters and the AI Gateway helpers on this page, tenant keys and codeTool and metering |
better-supabase/ai-sdk/chat | createAssistant: the chat route over the block |
better-supabase/ai-sdk/files | aiFileDownload: file parts read as the caller, generated files and provider file references |
better-supabase/ai-sdk/embeddings | embedWith: embedders, a reranker and the knowledge search tool |
better-supabase/ai-sdk/memory | memoryTool: the memory tool, recall, core memory in the instructions and fact extraction |
better-supabase/ai-sdk/agents | createAgentRuntime: a ToolLoopAgent from an agent, tool approvals and moderation |
better-supabase/ai-sdk/mcp | connectTools: OAuth for MCP servers with tokens in Vault, sessions and approved tools |
better-supabase/ai-sdk/cache | cacheMiddleware: repeated model calls answered from the AI cache block |
better-supabase/ai-sdk/batches | aiBatches: provider batches recorded, polled and stored per tenant |
better-supabase/ai-sdk/react | useAssistant: useChat wired to that route |
better-supabase/ai-sdk/workflow, /workflow-react | durableChat: answers that run as Workflow SDK workflows, and useDurableAssistant |
Messages
import { fromUIMessage, toUIMessages } from "better-supabase/ai-sdk";
const stored = fromUIMessage(uiMessage); // an AiMessage for the block
const path = await aiChat.messages.path(chatId, { native: true }).orThrow();
const messages = toUIMessages(path); // UIMessage[] for useChattoUIMessage returns the stored AI SDK copy (native) when the adapter
wrote the message, so nothing is lost on the round trip. A message another
SDK wrote is converted from its parts:
| Canonical part | UI message part |
|---|---|
text, reasoning, file | the same part |
source (url or document) | source-url or source-document |
tool-call, tool-result | one tool-<name> part with its state, or one dynamic-tool part when the call was dynamic |
tool-approval | the approval of that tool part |
step | step-start |
data | data-<name> |
A tool message's results join the tool parts of the assistant message
before it, the way useChat expects them. A UI part with no canonical
counterpart is stored as a data part (ui.custom or ui.reasoning-file)
and comes back unchanged.
The tool parts keep the AI SDK 7 fields, so a message deep-equals itself after a round trip through the stored form:
- A
tool-callcarriesdynamic,title,toolMetadata, and, while the input is still arriving,state: "streaming"withrawInputandinputEncoding. A call stored withdynamic: truecomes back as adynamic-toolpart. - A
tool-resultcarriespreliminaryfor an interim output. - A
tool-approvalcarriessignature,isAutomatic,requestReason,descriptorandinputSchemaInputnext toreason.
toUIMessage, toUIMessages, fromUIMessage and validUIMessages take your
UIMessage type as a type argument, for example toUIMessages<MyMessage>(path).
These fields are part of version 2 of the message format
(schemas/ai-message-v2.json); stored v1 messages are still valid.
AI Gateway
Models are provider/model strings for the
AI Gateway. gatewayOptions attributes
spend to the user, the tenant and the chat:
import { streamText } from "ai";
import { gatewayOptions, usageOf } from "better-supabase/ai-sdk";
const result = streamText({
model: "openai/gpt-5-mini",
messages,
providerOptions: gatewayOptions({
userId,
organizationId,
chatId,
feature: "chat",
}),
});
const usage = usageOf({
usage: await result.usage,
finalStep: await result.finalStep,
});usage is the total of every step and finalStep.providerMetadata carries
the gateway's generation id and cost. usageOf still reads the 0.31 names
(totalUsage, providerMetadata) when the new ones are missing.
user is always userId, the caller from the verified session: a user
in the second argument (gateway options such as order) is dropped, so a
request body or a model can't change whom the spend is billed to. tags
in the context adds the app's tags after org:, chat: and feature:.
The agent runtime sets both
for every model call.
usageOf returns the token counts, the gateway's generationId and the
cost in micro-dollars when the gateway reported it. usageEntries(usage)
turns it into entries for the usage block
(ai.input_tokens and ai.output_tokens by default), and
usageQuota(usage, meter) is the quota check createAssistant takes: it
returns a quota_exceeded error with retryAfter when the meter is used up.
problem429(error) is the RFC 9457 response for it, with Retry-After. A third argument, { extra, headers, format }, adds members and headers to the answer.
Thread summaries
summarizeWith(model, options) is the summarizer
messaging.summarize takes. It
calls generateText with an AI Gateway model string and tags the spend with
the tenant and the summary feature:
import { summarizeWith } from "better-supabase/ai-sdk";
const summarizer = summarizeWith("openai/gpt-5-mini", {
providers, // the tenant's own keys as BYOK, when the app installs ai-keys
maxOutputTokens: 400,
});
const summary = await messaging
.summarize({ conversationId }, summarizer)
.orThrow();With providers, the AI providers block, each
summary goes through tenantGatewayOptions, so a
tenant with a saved key pays for its own summaries. instructions replaces
the default prompt and gateway adds AI Gateway options such as order.
Jobs
Two job handlers keep the block current:
import { gateway } from "ai";
import { costBackfill, modelCatalogRefresh } from "better-supabase/ai-sdk";
export const handlers = {
"ai.cost_backfill": costBackfill({
chats: aiChat,
gateway,
usage,
meter: "ai.cost",
}),
"ai.model_catalog": modelCatalogRefresh({
chats: aiChat,
gateway,
prune: true,
}),
};costBackfill reads the exact cost from gateway.getGenerationInfo once
the gateway has it, writes it to the run (runs.setCost) and, with
usage, records it once per generation. createAssistant enqueues it
when the answer arrives without a cost. modelCatalogRefresh copies
gateway.getAvailableModels() into ai_model_catalog, language models by
default, with plans deciding which entitlements may use each model. Run it
on a schedule.
Last updated on