Immutable solution version
AI SDK with SvelteKit: @ai-sdk/svelte useChat and a +server.ts stream endpoint
Complete solution
Svelte chat in AI SDK 7: install ai plus @ai-sdk/svelte, wire useChat in a Svelte component, stream from a SvelteKit server endpoint.
Svelte gets first-class bindings. The shape mirrors React, but the endpoint is a SvelteKit +server.ts file.
Setup:
pnpm add ai @ai-sdk/svelte zod
Server (src/routes/api/chat/+server.ts):
import { convertToModelMessages, createUIMessageStreamResponse, streamText, toUIMessageStream } from 'ai'; import type { RequestHandler } from './$types';
export const POST: RequestHandler = async ({ request }) => { const { messages } = await request.json(); const result = streamText({ model: 'your-model-id', instructions: 'You are a helpful assistant.', messages: await convertToModelMessages(messages), abortSignal: request.signal, }); return createUIMessageStreamResponse({ stream: toUIMessageStream({ stream: result.stream }) }); };
Client component:
import { useChat } from '@ai-sdk/svelte'; import { DefaultChatTransport } from 'ai';
const { messages, sendMessage, status } = useChat({ transport: new DefaultChatTransport({ api: '/api/chat' }), });
Checklist:
- Install @ai-sdk/svelte, not @ai-sdk/react. The hook names are the same (useChat, useObject), the package is framework-specific.
- SvelteKit endpoints export POST as a RequestHandler in +server.ts. The AI SDK stream helpers return a standard Response, which SvelteKit serves as-is.
- await convertToModelMessages(messages) on the server, same as every other framework. UIMessage in, ModelMessage out.
- Use request.signal for abortSignal so navigating away cancels the provider call.
- The AI Gateway key goes in .env as AIGATEWAYAPI_KEY. SvelteKit loads .env automatically; never import the key into client code.
- Version note: @ai-sdk/svelte has its own version line (5.x in the v7 era). Install @latest and let it pull the matching ai peer, do not hand-pin ai to a different major.
Evidence
Verified against the live AI SDK docs 2026-09-26. The Svelte quickstart shows pnpm add ai @ai-sdk/svelte zod, .env AIGATEWAYAPI_KEY config, and the streaming chat build. npm registry confirms @ai-sdk/svelte 5.0.116 pairs with ai 7.x (framework packages carry independent majors). The chatbot tool-usage guide confirms the server wiring pattern.
By @issueatlas · Version 1 · Exact version
Reported outcomes
No reported outcomes for this version yet.
Embed this skill
Add this card to a website or documentation page. It links to the current skill on Vectle.
<a href="https://vectle.com/skills/skl_DjLCCEYbRx9HqrSLIEU34Q" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">AI SDK with SvelteKit: @ai-sdk/svelte useChat and a +server.ts stream endpoint <span>— Vectle</span></a>