Ai sdk

From diegosouzapw/awesome-omni-skill · View interactive version

Ai sdk is an AI agent skill from the GitHub repository diegosouzapw/awesome-omni-skill. Vercel AI SDK reference for building AI-powered applications. Use when implementing text/object generation (generateText, streamText, generateObject, streamObject), building chatbots with useChat/useCompletion hooks, defining tools with Zod schemas, creating agents with ToolLoopAgent, or integrating

How to use this skill

Skill file

AI SDK

The AI SDK is Vercel's TypeScript toolkit for building AI-powered applications with React, Next.js, Vue, Svelte, Node.js, and more.

When to Use This Skill

Use this skill when:

Documentation

See the docs/2025-12-02/ directory for complete AI SDK documentation:

Getting Started

AI SDK Core

Agents

AI SDK UI (React/Vue/Svelte Hooks)

AI SDK RSC (React Server Components)

Reference

Quick Reference

Core Functions

import { generateText, streamText, generateObject, streamObject } from 'ai';

// Generate text
const { text } = await generateText({
  model: anthropic('claude-sonnet-4-5-20241022'),
  prompt: 'Write a haiku about coding',
});

// Stream text
const result = streamText({
  model: anthropic('claude-sonnet-4-5-20241022'),
  prompt: 'Write a story',
});
for await (const chunk of result.textStream) {
  console.log(chunk);
}

// Generate structured data
const { object } = await generateObject({
  model: anthropic('claude-sonnet-4-5-20241022'),
  schema: z.object({
    name: z.string(),
    age: z.number(),
  }),
  prompt: 'Generate a person',
});

// Stream structured data
const { partialObjectStream } = streamObject({
  model: anthropic('claude-sonnet-4-5-20241022'),
  schema: z.object({ items: z.array(z.string()) }),
  prompt: 'List 5 fruits',
});

Tool Definition

import { tool } from 'ai';
import { z } from 'zod';

const weatherTool = tool({
  description: 'Get the weather for a location',
  inputSchema: z.object({
    location: z.string().describe('City name'),
  }),
  execute: async ({ location }) => {
    return { temperature: 72, condition: 'sunny' };
  },
});

// Use with generateText/streamText
const result = await generateText({
  model: anthropic('claude-sonnet-4-5-20241022'),
  tools: { weather: weatherTool },
  prompt: 'What is the weather in San Francisco?',
});

Agent (ToolLoopAgent)

import { ToolLoopAgent, stepCountIs, tool } from 'ai';

const agent = new ToolLoopAgent({
  model: anthropic('claude-sonnet-4-5-20241022'),
  tools: {
    search: tool({ /* ... */ }),
    calculate: tool({ /* ... */ }),
  },
  stopWhen: stepCountIs(10), // Max 10 steps
});

const result = await agent.generate({
  prompt: 'Research and calculate...',
});

useChat Hook (React)

import { useChat } from '@ai-sdk/react';
import { DefaultChatTransport } from 'ai';

function Chat() {
  const { messages, sendMessage, status, stop } = useChat({
    transport: new DefaultChatTransport({ api: '/api/chat' }),
  });

  return (
    <>
      {messages.map(m => (
        <div key={m.id}>
          {m.role}: {m.parts.map(p => p.type === 'text' ? p.text : null)}
        </div>
      ))}
      <form onSubmit={e => {
        e.preventDefault();
        sendMessage({ text: input });
      }}>
        <input disabled={status !== 'ready'} />
      </form>
    </>
  );
}

API Route (Next.js)

import { streamText, convertToModelMessages, UIMessage } from 'ai';
import { anthropic } from '@ai-sdk/anthropic';

export async function POST(req: Request) {
  const { messages }: { messages: UIMessage[] } = await req.json();

  const result = streamText({
    model: anthropic('claude-sonnet-4-5-20241022'),
    system: 'You are a helpful assistant.',
    messages: convertToModelMessages(messages),
  });

  return result.toUIMessageStreamResponse();
}

Providers

// Official providers
import { anthropic } from '@ai-sdk/anthropic';
import { openai } from '@ai-sdk/openai';
import { google } from '@ai-sdk/google';
import { mistral } from '@ai-sdk/mistral';

// Use models
const model = anthropic('claude-sonnet-4-5-20241022');
const model = openai('gpt-4o');
const model = google('gemini-1.5-flash');

Prompt Types

// Text prompt
await generateText({
  model,
  prompt: 'Hello!',
});

// System + prompt
await generateText({
  model,
  system: 'You are a helpful assistant.',
  prompt: 'Hello!',
});

// Message array
await generateText({
  model,
  messages: [
    { role: 'user', content: 'Hi!' },
    { role: 'assistant', content: 'Hello!' },
    { role: 'user', content: 'How are you?' },
  ],
});

// Multi-modal (images)
await generateText({
  model,
  messages: [{
    role: 'user',
    content: [
      { type: 'text', text: 'Describe this image' },
      { type: 'image', image: fs.readFileSync('./image.png') },
    ],
  }],
});

Status Values (useChat)

Stream Result Properties

const result = streamText({ model, prompt });

// Async iterables
result.textStream      // Stream of text chunks
result.fullStream      // Full event stream with types

// Promises (resolve when complete)
result.text            // Full generated text
result.toolCalls       // Tool calls made
result.toolResults     // Tool execution results
result.usage           // Token usage
result.finishReason    // Why generation stopped

// Response helpers
result.toUIMessageStreamResponse() // For useChat
result.toTextStreamResponse()      // Plain text stream

Source

Documentation downloaded from: https://github.com/vercel/ai/tree/main/content/docs

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.