Vercel AI SDK
Moth DocsTutorials
The AI SDK reaches Moth through its OpenAI-compatible provider, with streaming, tools and the chat hooks for React.
npm install ai @ai-sdk/openai-compatible @ai-sdk/react zod
This page uses version 7 of the AI SDK. Put MOTH_API_KEY=moth_your_key_here in .env.local so it stays on the server, then add the route below to a Next.js app.
import { streamText, UIMessage, convertToModelMessages, createUIMessageStreamResponse, toUIMessageStream } from "ai";
import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
const moth = createOpenAICompatible({
name: "moth",
baseURL: "https://api.runmoth.si/v1",
apiKey: process.env.MOTH_API_KEY,
includeUsage: true,
});
export async function POST(req: Request) {
const { messages }: { messages: UIMessage[] } = await req.json();
const result = streamText({
model: moth("gemini-3.8-flash"),
messages: await convertToModelMessages(messages),
});
return createUIMessageStreamResponse({ stream: toUIMessageStream({ stream: result.stream }) });
}On the page, useChat from @ai-sdk/react talks to this route as in the AI SDK's own Next.js guide. includeUsage adds the token counts to each streamed reply.
Tools
import { generateText, tool, isStepCount } from "ai";
import { z } from "zod";
const { text } = await generateText({
model: moth("deepseek-v4.1-flash"),
prompt: "Where is order A-1042?",
stopWhen: isStepCount(3),
tools: {
order_status: tool({
description: "Look up where an order is",
inputSchema: z.object({ order_id: z.string() }),
execute: async ({ order_id }) => (order_id === "A-1042" ? "shipped on 12 November" : "no such order"),
}),
},
});The SDK runs the tool, hands the result back and asks again, up to three steps here, and text holds the final answer.
Use the OpenAI-compatible provider. @ai-sdk/openai calls OpenAI's Responses API by default, which Moth doesn't serve. If you use it anyway, create the model with .chat("model-id") so it calls chat completions.