$npx -y skills add jackspace/ClaudeSkillz --skill ai-sdk-coreBackend AI functionality with Vercel AI SDK v5 - text generation, structured output with Zod, tool calling, and agents. Multi-provider support for OpenAI, Anthropic, Google, and Cloudflare Workers AI. Use when: implementing server-side AI features, generating text/chat completion
| 1 | # AI SDK Core |
| 2 | |
| 3 | Production-ready backend AI with Vercel AI SDK v5. |
| 4 | |
| 5 | ## Quick Start (5 Minutes) |
| 6 | |
| 7 | ### Installation |
| 8 | |
| 9 | ```bash |
| 10 | # Core package |
| 11 | npm install ai |
| 12 | |
| 13 | # Provider packages (install what you need) |
| 14 | npm install @ai-sdk/openai # OpenAI (GPT-5, GPT-4, GPT-3.5) |
| 15 | npm install @ai-sdk/anthropic # Anthropic (Claude Sonnet 4.5, Opus 4, Haiku 4) |
| 16 | npm install @ai-sdk/google # Google (Gemini 2.5 Pro/Flash/Lite) |
| 17 | npm install workers-ai-provider # Cloudflare Workers AI |
| 18 | |
| 19 | # Schema validation |
| 20 | npm install zod |
| 21 | ``` |
| 22 | |
| 23 | ### Environment Variables |
| 24 | |
| 25 | ```bash |
| 26 | # .env |
| 27 | OPENAI_API_KEY=sk-... |
| 28 | ANTHROPIC_API_KEY=sk-ant-... |
| 29 | GOOGLE_GENERATIVE_AI_API_KEY=... |
| 30 | ``` |
| 31 | |
| 32 | ### First Example: Generate Text |
| 33 | |
| 34 | ```typescript |
| 35 | import { generateText } from 'ai'; |
| 36 | import { openai } from '@ai-sdk/openai'; |
| 37 | |
| 38 | const result = await generateText({ |
| 39 | model: openai('gpt-4-turbo'), |
| 40 | prompt: 'What is TypeScript?', |
| 41 | }); |
| 42 | |
| 43 | console.log(result.text); |
| 44 | ``` |
| 45 | |
| 46 | ### First Example: Streaming Chat |
| 47 | |
| 48 | ```typescript |
| 49 | import { streamText } from 'ai'; |
| 50 | import { anthropic } from '@ai-sdk/anthropic'; |
| 51 | |
| 52 | const stream = streamText({ |
| 53 | model: anthropic('claude-sonnet-4-5-20250929'), |
| 54 | messages: [ |
| 55 | { role: 'user', content: 'Tell me a story' }, |
| 56 | ], |
| 57 | }); |
| 58 | |
| 59 | for await (const chunk of stream.textStream) { |
| 60 | process.stdout.write(chunk); |
| 61 | } |
| 62 | ``` |
| 63 | |
| 64 | ### First Example: Structured Output |
| 65 | |
| 66 | ```typescript |
| 67 | import { generateObject } from 'ai'; |
| 68 | import { openai } from '@ai-sdk/openai'; |
| 69 | import { z } from 'zod'; |
| 70 | |
| 71 | const result = await generateObject({ |
| 72 | model: openai('gpt-4'), |
| 73 | schema: z.object({ |
| 74 | name: z.string(), |
| 75 | age: z.number(), |
| 76 | skills: z.array(z.string()), |
| 77 | }), |
| 78 | prompt: 'Generate a person profile for a software engineer', |
| 79 | }); |
| 80 | |
| 81 | console.log(result.object); |
| 82 | // { name: "Alice", age: 28, skills: ["TypeScript", "React"] } |
| 83 | ``` |
| 84 | |
| 85 | --- |
| 86 | |
| 87 | ## Core Functions |
| 88 | |
| 89 | ### generateText() |
| 90 | |
| 91 | Generate text completion with optional tools and multi-step execution. |
| 92 | |
| 93 | **Signature:** |
| 94 | |
| 95 | ```typescript |
| 96 | async function generateText(options: { |
| 97 | model: LanguageModel; |
| 98 | prompt?: string; |
| 99 | messages?: Array<ModelMessage>; |
| 100 | system?: string; |
| 101 | tools?: Record<string, Tool>; |
| 102 | maxOutputTokens?: number; |
| 103 | temperature?: number; |
| 104 | stopWhen?: StopCondition; |
| 105 | // ... other options |
| 106 | }): Promise<GenerateTextResult> |
| 107 | ``` |
| 108 | |
| 109 | **Basic Usage:** |
| 110 | |
| 111 | ```typescript |
| 112 | import { generateText } from 'ai'; |
| 113 | import { openai } from '@ai-sdk/openai'; |
| 114 | |
| 115 | const result = await generateText({ |
| 116 | model: openai('gpt-4-turbo'), |
| 117 | prompt: 'Explain quantum computing', |
| 118 | maxOutputTokens: 500, |
| 119 | temperature: 0.7, |
| 120 | }); |
| 121 | |
| 122 | console.log(result.text); |
| 123 | console.log(`Tokens: ${result.usage.totalTokens}`); |
| 124 | ``` |
| 125 | |
| 126 | **With Messages (Chat Format):** |
| 127 | |
| 128 | ```typescript |
| 129 | const result = await generateText({ |
| 130 | model: openai('gpt-4-turbo'), |
| 131 | messages: [ |
| 132 | { role: 'system', content: 'You are a helpful assistant.' }, |
| 133 | { role: 'user', content: 'What is the weather?' }, |
| 134 | { role: 'assistant', content: 'I need your location.' }, |
| 135 | { role: 'user', content: 'San Francisco' }, |
| 136 | ], |
| 137 | }); |
| 138 | ``` |
| 139 | |
| 140 | **With Tools:** |
| 141 | |
| 142 | ```typescript |
| 143 | import { tool } from 'ai'; |
| 144 | import { z } from 'zod'; |
| 145 | |
| 146 | const result = await generateText({ |
| 147 | model: openai('gpt-4'), |
| 148 | tools: { |
| 149 | weather: tool({ |
| 150 | description: 'Get the weather for a location', |
| 151 | inputSchema: z.object({ |
| 152 | location: z.string(), |
| 153 | }), |
| 154 | execute: async ({ location }) => { |
| 155 | // API call here |
| 156 | return { temperature: 72, condition: 'sunny' }; |
| 157 | }, |
| 158 | }), |
| 159 | }, |
| 160 | prompt: 'What is the weather in Tokyo?', |
| 161 | }); |
| 162 | ``` |
| 163 | |
| 164 | **When to Use:** |
| 165 | - Need final response (not streaming) |
| 166 | - Want to wait for tool executions to complete |
| 167 | - Simpler code when streaming not needed |
| 168 | - Building batch/scheduled tasks |
| 169 | |
| 170 | **Error Handling:** |
| 171 | |
| 172 | ```typescript |
| 173 | import { AI_APICallError, AI_NoContentGeneratedError } from 'ai'; |
| 174 | |
| 175 | try { |
| 176 | const result = await generateText({ |
| 177 | model: openai('gpt-4-turbo'), |
| 178 | prompt: 'Hello', |
| 179 | }); |
| 180 | console.log(result.text); |
| 181 | } catch (error) { |
| 182 | if (error instanceof AI_APICallError) { |
| 183 | console.error('API call failed:', error.message); |
| 184 | // Check rate |