$npx -y skills add vercel/ai --skill develop-ai-functions-exampleDevelop examples for AI SDK functions. Use when creating, running, or modifying examples under examples/ai-functions/src to validate provider support, demonstrate features, or create test fixtures.
| 1 | ## AI Functions Examples |
| 2 | |
| 3 | The `examples/ai-functions/` directory contains scripts for validating, testing, and iterating on AI SDK functions across providers. |
| 4 | |
| 5 | ## Example Categories |
| 6 | |
| 7 | Examples are organized by AI SDK function in `examples/ai-functions/src/`: |
| 8 | |
| 9 | | Directory | Purpose | |
| 10 | | ------------------ | ---------------------------------------------------- | |
| 11 | | `generate-text/` | Non-streaming text generation with `generateText()` | |
| 12 | | `stream-text/` | Streaming text generation with `streamText()` | |
| 13 | | `generate-object/` | Structured output generation with `generateObject()` | |
| 14 | | `stream-object/` | Streaming structured output with `streamObject()` | |
| 15 | | `agent/` | `ToolLoopAgent` examples for agentic workflows | |
| 16 | | `embed/` | Single embedding generation with `embed()` | |
| 17 | | `embed-many/` | Batch embedding generation with `embedMany()` | |
| 18 | | `generate-image/` | Image generation with `generateImage()` | |
| 19 | | `generate-speech/` | Text-to-speech with `generateSpeech()` | |
| 20 | | `transcribe/` | Audio transcription with `transcribe()` | |
| 21 | | `rerank/` | Document reranking with `rerank()` | |
| 22 | | `middleware/` | Custom middleware implementations | |
| 23 | | `registry/` | Provider registry setup and usage | |
| 24 | | `telemetry/` | OpenTelemetry integration | |
| 25 | | `complex/` | Multi-component examples (agents, routers) | |
| 26 | | `lib/` | Shared utilities (not examples) | |
| 27 | | `tools/` | Reusable tool definitions | |
| 28 | |
| 29 | ## File Naming Convention |
| 30 | |
| 31 | Examples follow the pattern: `{provider}-{feature}.ts` |
| 32 | |
| 33 | | Pattern | Example | Description | |
| 34 | | ---------------------------------------- | ------------------------------------------ | -------------------------- | |
| 35 | | `{provider}.ts` | `openai.ts` | Basic provider usage | |
| 36 | | `{provider}-{feature}.ts` | `openai-tool-call.ts` | Specific feature | |
| 37 | | `{provider}-{sub-provider}.ts` | `amazon-bedrock-anthropic.ts` | Provider with sub-provider | |
| 38 | | `{provider}-{sub-provider}-{feature}.ts` | `google-vertex-anthropic-cache-control.ts` | Sub-provider with feature | |
| 39 | |
| 40 | ## Example Structure |
| 41 | |
| 42 | All examples use the `run()` wrapper from `lib/run.ts` which: |
| 43 | |
| 44 | - Loads environment variables from `.env` |
| 45 | - Provides error handling with detailed API error logging |
| 46 | |
| 47 | ### Basic Template |
| 48 | |
| 49 | ```typescript |
| 50 | import { providerName } from '@ai-sdk/provider-name'; |
| 51 | import { generateText } from 'ai'; |
| 52 | import { run } from '../lib/run'; |
| 53 | |
| 54 | run(async () => { |
| 55 | const result = await generateText({ |
| 56 | model: providerName('model-id'), |
| 57 | prompt: 'Your prompt here.', |
| 58 | }); |
| 59 | |
| 60 | console.log(result.text); |
| 61 | console.log('Token usage:', result.usage); |
| 62 | console.log('Finish reason:', result.finishReason); |
| 63 | }); |
| 64 | ``` |
| 65 | |
| 66 | ### Streaming Template |
| 67 | |
| 68 | ```typescript |
| 69 | import { providerName } from '@ai-sdk/provider-name'; |
| 70 | import { streamText } from 'ai'; |
| 71 | import { printFullStream } from '../lib/print-full-stream'; |
| 72 | import { run } from '../lib/run'; |
| 73 | |
| 74 | run(async () => { |
| 75 | const result = streamText({ |
| 76 | model: providerName('model-id'), |
| 77 | prompt: 'Your prompt here.', |
| 78 | }); |
| 79 | |
| 80 | await printFullStream({ result }); |
| 81 | }); |
| 82 | ``` |
| 83 | |
| 84 | ### Tool Calling Template |
| 85 | |
| 86 | ```typescript |
| 87 | import { providerName } from '@ai-sdk/provider-name'; |
| 88 | import { generateText, tool } from 'ai'; |
| 89 | import { z } from 'zod'; |
| 90 | import { run } from '../lib/run'; |
| 91 | |
| 92 | run(async () => { |
| 93 | const result = await generateText({ |
| 94 | model: providerName('model-id'), |
| 95 | tools: { |
| 96 | myTool: tool({ |
| 97 | description: 'Tool description', |
| 98 | inputSchema: z.object({ |
| 99 | param: z.string().describe('Parameter description'), |
| 100 | }), |
| 101 | execute: async ({ param }) => { |
| 102 | return { result: `Processed: ${param}` }; |
| 103 | }, |
| 104 | }), |
| 105 | }, |
| 106 | prompt: 'Use the tool to...', |
| 107 | }); |
| 108 | |
| 109 | console.log(JSON.stringify(result, null, 2)); |
| 110 | }); |
| 111 | ``` |
| 112 | |
| 113 | ### Structured Output Template |
| 114 | |
| 115 | ```typescript |
| 116 | import { providerName } from '@ai-sdk/provider-name'; |
| 117 | import { generateObject } from 'ai'; |
| 118 | import { z } from 'zod'; |
| 119 | import { run } from '../lib/run'; |
| 120 | |
| 121 | run(async () => { |
| 122 | const result = await generateObject({ |
| 123 | model: providerName('model-id'), |
| 124 | schema: z.object({ |
| 125 | name: z.string(), |
| 126 | items: z.array(z.string()), |
| 127 | }), |
| 128 | prompt: 'Generate a...', |
| 129 | }); |
| 130 | |
| 131 | console.log(JSON.stringify(result.object, null, 2)); |
| 132 | console.log |