πŸ”‘
πŸ”’ browser only Β· simulates AI SDK Core behaviour
AI SDK Codeserver.js
import { streamText } from 'ai'; import { openai } from '@ai-sdk/openai'; const result = await streamText({ model: openai('gpt-4o-mini'), system: 'You are a frontend tutor.', messages: req.body.messages, }); for await (const chunk of result.textStream) res.write(chunk); res.end(); // Browser calls with plain fetch: const res = await fetch('/api/chat', { method: 'POST', body: JSON.stringify({ messages }) }); const reader = res.body.getReader(); while (true) { const {done, value} = await reader.read(); if (done) break; output.textContent += decode(value); }
Live Democalls OpenAI directly
Enter your API key above and ask a question.
Tokens stream back one by one β€” same as AI SDK streamText.
AI SDK Codegenerate-object.js
import { generateObject } from 'ai'; import { openai } from '@ai-sdk/openai'; import { z } from 'zod'; const { object } = await generateObject({ model: openai('gpt-4o-mini'), schema: z.object({ title: z.string(), slug: z.string(), description:z.string(), tags: z.array(z.string()), difficulty: z.enum([ 'beginner', 'intermediate', 'advanced' ]), readingTime:z.number(), }), prompt: `Generate SEO metadata for: ${content}`, }); // object is fully typed β€” no JSON.parse console.log(object.tags); // string[]
Live β€” Generate Article Metadata
Article topic or title
// structured JSON will appear here // guaranteed schema β€” no JSON.parse needed
AI SDK Codeproviders.js
import { generateText } from 'ai'; // Change THIS line β€” nothing else changes // OpenAI import { openai } from '@ai-sdk/openai'; const model = openai('gpt-4o-mini'); // Anthropic import { anthropic } from '@ai-sdk/anthropic'; const model = anthropic('claude-haiku-4-5'); // Google import { google } from '@ai-sdk/google'; const model = google('gemini-2.5-flash'); // Ollama (local, free) import { ollama } from '@ai-sdk/ollama'; const model = ollama('llama3.2'); // IDENTICAL call for all providers: const { text } = await generateText({ model, prompt });
Live β€” Ask Across Providers
Select provider (demo uses OpenAI for all)
Prompt
// Response will appear here // In production: swap provider import, same generateText() call
AI SDK Codetools.js
import { generateText, tool } from 'ai'; import { z } from 'zod'; const result = await generateText({ model, prompt: 'What is 15% of 840?', maxSteps: 3, // auto tool loop! tools: { calculate: tool({ description: 'Evaluate a math expression', parameters: z.object({ expression: z.string() }), execute: async ({ expression }) => ({ result: new Function( `return ${expression}` )() }) }) } }); console.log(result.text); // "15% of 840 is 126." // maxSteps handles the loop β€” no while needed
Live β€” Tool Calling Demo
Prompt β€” try math or weather questions
Available tools: calculate(expression) isPrime(n) getWeather(city)
// AI SDK calls tools automatically via maxSteps // No while loop, no manual tool dispatch needed
Read the tutorial