|
| 1 | +--- |
| 2 | +title: Get started with GPT-5 |
| 3 | +description: Get started with GPT-5 using the AI SDK. |
| 4 | +tags: ['getting-started'] |
| 5 | +--- |
| 6 | + |
| 7 | +# Get started with OpenAI GPT-5 |
| 8 | + |
| 9 | +With the [release of OpenAI's GPT-5 model](https://openai.com/index/introducing-gpt-5), there has never been a better time to start building AI applications with advanced capabilities like verbosity control, web search, and native multi-modal understanding. |
| 10 | + |
| 11 | +The [AI SDK](/) is a powerful TypeScript toolkit for building AI applications with large language models (LLMs) like OpenAI GPT-5 alongside popular frameworks like React, Next.js, Vue, Svelte, Node.js, and more. |
| 12 | + |
| 13 | +## OpenAI GPT-5 |
| 14 | + |
| 15 | +OpenAI's GPT-5 represents their latest advancement in language models, offering powerful new features including verbosity control for tailored response lengths, integrated web search capabilities, reasoning summaries for transparency, and native support for text, images, audio, and PDFs. The model is available in three variants: `gpt-5`, `gpt-5-mini` for faster, more cost-effective processing, and `gpt-5-nano` for ultra-efficient operations. |
| 16 | + |
| 17 | +### Prompt Engineering for GPT-5 |
| 18 | + |
| 19 | +Here are the key strategies for effective prompting: |
| 20 | + |
| 21 | +#### Core Principles |
| 22 | + |
| 23 | +1. **Be precise and unambiguous**: Avoid contradictory or ambiguous instructions. GPT-5 performs best with clear, explicit guidance. |
| 24 | +2. **Use structured prompts**: Leverage XML-like tags to organize different sections of your instructions for better clarity. |
| 25 | +3. **Natural language works best**: While being precise, write prompts as you would explain to a skilled colleague. |
| 26 | + |
| 27 | +#### Prompting Techniques |
| 28 | + |
| 29 | +**1. Agentic Workflow Control** |
| 30 | + |
| 31 | +- Adjust the `reasoning_effort` parameter to calibrate model autonomy |
| 32 | +- Set clear stop conditions and define explicit tool call budgets |
| 33 | +- Provide guidance on exploration depth and persistence |
| 34 | + |
| 35 | +```ts |
| 36 | +// Example with reasoning effort control |
| 37 | +const result = await generateText({ |
| 38 | + model: openai('gpt-5'), |
| 39 | + prompt: 'Analyze this complex dataset and provide insights.', |
| 40 | + providerOptions: { |
| 41 | + openai: { |
| 42 | + reasoning_effort: 'high', // Increases autonomous exploration |
| 43 | + }, |
| 44 | + }, |
| 45 | +}); |
| 46 | +``` |
| 47 | + |
| 48 | +**2. Structured Prompt Format** |
| 49 | +Use XML-like tags to organize your prompts: |
| 50 | + |
| 51 | +``` |
| 52 | +<context_gathering> |
| 53 | +Goal: Extract key performance metrics from the report |
| 54 | +Method: Focus on quantitative data and year-over-year comparisons |
| 55 | +Early stop criteria: Stop after finding 5 key metrics |
| 56 | +</context_gathering> |
| 57 | +
|
| 58 | +<task> |
| 59 | +Analyze the attached financial report and identify the most important metrics. |
| 60 | +</task> |
| 61 | +``` |
| 62 | + |
| 63 | +**3. Tool Calling Best Practices** |
| 64 | + |
| 65 | +- Use tool preambles to provide clear upfront plans |
| 66 | +- Define safe vs. unsafe actions for different tools |
| 67 | +- Create structured updates about tool call progress |
| 68 | + |
| 69 | +**4. Verbosity Control** |
| 70 | + |
| 71 | +- Use the `textVerbosity` parameter to control response length programmatically |
| 72 | +- Override with natural language when needed for specific contexts |
| 73 | +- Balance between conciseness and completeness |
| 74 | + |
| 75 | +**5. Optimization Workflow** |
| 76 | + |
| 77 | +- Start with a clear, simple prompt |
| 78 | +- Test and identify areas of ambiguity or confusion |
| 79 | +- Iteratively refine by removing contradictions |
| 80 | +- Consider using OpenAI's Prompt Optimizer tool for complex prompts |
| 81 | +- Document successful patterns for reuse |
| 82 | + |
| 83 | +## Getting Started with the AI SDK |
| 84 | + |
| 85 | +The AI SDK is the TypeScript toolkit designed to help developers build AI-powered applications with React, Next.js, Vue, Svelte, Node.js, and more. Integrating LLMs into applications is complicated and heavily dependent on the specific model provider you use. |
| 86 | + |
| 87 | +The AI SDK abstracts away the differences between model providers, eliminates boilerplate code for building chatbots, and allows you to go beyond text output to generate rich, interactive components. |
| 88 | + |
| 89 | +At the center of the AI SDK is [AI SDK Core](/docs/ai-sdk-core/overview), which provides a unified API to call any LLM. The code snippet below is all you need to call OpenAI GPT-5 with the AI SDK: |
| 90 | + |
| 91 | +```ts |
| 92 | +import { generateText } from 'ai'; |
| 93 | +import { openai } from '@ai-sdk/openai'; |
| 94 | + |
| 95 | +const { text } = await generateText({ |
| 96 | + model: openai('gpt-5'), |
| 97 | + prompt: 'Explain the concept of quantum entanglement.', |
| 98 | +}); |
| 99 | +``` |
| 100 | + |
| 101 | +### Generating Structured Data |
| 102 | + |
| 103 | +While text generation can be useful, you might want to generate structured JSON data. For example, you might want to extract information from text, classify data, or generate synthetic data. AI SDK Core provides two functions ([`generateObject`](/docs/reference/ai-sdk-core/generate-object) and [`streamObject`](/docs/reference/ai-sdk-core/stream-object)) to generate structured data, allowing you to constrain model outputs to a specific schema. |
| 104 | + |
| 105 | +```ts |
| 106 | +import { generateObject } from 'ai'; |
| 107 | +import { openai } from '@ai-sdk/openai'; |
| 108 | +import { z } from 'zod'; |
| 109 | + |
| 110 | +const { object } = await generateObject({ |
| 111 | + model: openai('gpt-5'), |
| 112 | + schema: z.object({ |
| 113 | + recipe: z.object({ |
| 114 | + name: z.string(), |
| 115 | + ingredients: z.array(z.object({ name: z.string(), amount: z.string() })), |
| 116 | + steps: z.array(z.string()), |
| 117 | + }), |
| 118 | + }), |
| 119 | + prompt: 'Generate a lasagna recipe.', |
| 120 | +}); |
| 121 | +``` |
| 122 | + |
| 123 | +This code snippet will generate a type-safe recipe that conforms to the specified zod schema. |
| 124 | + |
| 125 | +### Verbosity Control |
| 126 | + |
| 127 | +One of GPT-5's new features is verbosity control, allowing you to adjust response length without modifying your prompt: |
| 128 | + |
| 129 | +```ts |
| 130 | +import { generateText } from 'ai'; |
| 131 | +import { openai } from '@ai-sdk/openai'; |
| 132 | + |
| 133 | +// Concise response |
| 134 | +const { text: conciseText } = await generateText({ |
| 135 | + model: openai('gpt-5'), |
| 136 | + prompt: 'Explain quantum computing.', |
| 137 | + providerOptions: { |
| 138 | + openai: { |
| 139 | + textVerbosity: 'low', // Produces terse, minimal responses |
| 140 | + }, |
| 141 | + }, |
| 142 | +}); |
| 143 | + |
| 144 | +// Detailed response |
| 145 | +const { text: detailedText } = await generateText({ |
| 146 | + model: openai('gpt-5'), |
| 147 | + prompt: 'Explain quantum computing.', |
| 148 | + providerOptions: { |
| 149 | + openai: { |
| 150 | + textVerbosity: 'high', // Produces comprehensive, detailed responses |
| 151 | + }, |
| 152 | + }, |
| 153 | +}); |
| 154 | +``` |
| 155 | + |
| 156 | +### Web Search Integration |
| 157 | + |
| 158 | +GPT-5 can access real-time information through the integrated web search tool when accessed via the [Responses API](/providers/openai#responses-models): |
| 159 | + |
| 160 | +```ts |
| 161 | +import { generateText } from 'ai'; |
| 162 | +import { openai } from '@ai-sdk/openai'; |
| 163 | + |
| 164 | +const result = await generateText({ |
| 165 | + model: openai.responses('gpt-5'), |
| 166 | + prompt: 'What are the latest developments in AI this week?', |
| 167 | + tools: { |
| 168 | + web_search_preview: openai.tools.webSearchPreview({ |
| 169 | + searchContextSize: 'high', |
| 170 | + }), |
| 171 | + }, |
| 172 | + toolChoice: { type: 'tool', toolName: 'web_search_preview' }, |
| 173 | +}); |
| 174 | + |
| 175 | +// Access URL sources |
| 176 | +const sources = result.sources; |
| 177 | +``` |
| 178 | + |
| 179 | +### Reasoning Summaries |
| 180 | + |
| 181 | +For transparency into GPT-5's thought process, enable reasoning summaries: |
| 182 | + |
| 183 | +```ts |
| 184 | +import { openai } from '@ai-sdk/openai'; |
| 185 | +import { streamText } from 'ai'; |
| 186 | + |
| 187 | +const result = streamText({ |
| 188 | + model: openai.responses('gpt-5'), |
| 189 | + prompt: |
| 190 | + 'Solve this logic puzzle: If all roses are flowers and some flowers fade quickly, do all roses fade quickly?', |
| 191 | + providerOptions: { |
| 192 | + openai: { |
| 193 | + reasoningSummary: 'detailed', // 'auto' for condensed or 'detailed' for comprehensive |
| 194 | + }, |
| 195 | + }, |
| 196 | +}); |
| 197 | + |
| 198 | +// Stream reasoning and text separately |
| 199 | +for await (const part of result.fullStream) { |
| 200 | + if (part.type === 'reasoning') { |
| 201 | + console.log(part.textDelta); |
| 202 | + } else if (part.type === 'text-delta') { |
| 203 | + process.stdout.write(part.textDelta); |
| 204 | + } |
| 205 | +} |
| 206 | +``` |
| 207 | + |
| 208 | +### Using Tools with the AI SDK |
| 209 | + |
| 210 | +GPT-5 supports tool calling out of the box, allowing it to interact with external systems and perform discrete tasks. Here's an example of using tool calling with the AI SDK: |
| 211 | + |
| 212 | +```ts |
| 213 | +import { generateText, tool } from 'ai'; |
| 214 | +import { openai } from '@ai-sdk/openai'; |
| 215 | +import { z } from 'zod'; |
| 216 | + |
| 217 | +const { toolResults } = await generateText({ |
| 218 | + model: openai('gpt-5'), |
| 219 | + prompt: 'What is the weather like today in San Francisco?', |
| 220 | + tools: { |
| 221 | + getWeather: tool({ |
| 222 | + description: 'Get the weather in a location', |
| 223 | + inputSchema: z.object({ |
| 224 | + location: z.string().describe('The location to get the weather for'), |
| 225 | + }), |
| 226 | + execute: async ({ location }) => ({ |
| 227 | + location, |
| 228 | + temperature: 72 + Math.floor(Math.random() * 21) - 10, |
| 229 | + }), |
| 230 | + }), |
| 231 | + }, |
| 232 | +}); |
| 233 | +``` |
| 234 | + |
| 235 | +### Building Interactive Interfaces |
| 236 | + |
| 237 | +AI SDK Core can be paired with [AI SDK UI](/docs/ai-sdk-ui/overview), another powerful component of the AI SDK, to streamline the process of building chat, completion, and assistant interfaces with popular frameworks like Next.js, Nuxt, and SvelteKit. |
| 238 | + |
| 239 | +AI SDK UI provides robust abstractions that simplify the complex tasks of managing chat streams and UI updates on the frontend, enabling you to develop dynamic AI-driven interfaces more efficiently. |
| 240 | + |
| 241 | +With four main hooks — [`useChat`](/docs/reference/ai-sdk-ui/use-chat), [`useCompletion`](/docs/reference/ai-sdk-ui/use-completion), and [`useObject`](/docs/reference/ai-sdk-ui/use-object) — you can incorporate real-time chat capabilities, text completions, streamed JSON, and interactive assistant features into your app. |
| 242 | + |
| 243 | +Let's explore building a chatbot with [Next.js](https://nextjs.org), the AI SDK, and OpenAI GPT-5: |
| 244 | + |
| 245 | +In a new Next.js application, first install the AI SDK and the OpenAI provider: |
| 246 | + |
| 247 | +<Snippet text="pnpm install ai @ai-sdk/openai @ai-sdk/react" /> |
| 248 | + |
| 249 | +Then, create a route handler for the chat endpoint: |
| 250 | + |
| 251 | +```tsx filename="app/api/chat/route.ts" |
| 252 | +import { openai } from '@ai-sdk/openai'; |
| 253 | +import { convertToModelMessages, streamText, UIMessage } from 'ai'; |
| 254 | + |
| 255 | +// Allow responses up to 30 seconds |
| 256 | +export const maxDuration = 30; |
| 257 | + |
| 258 | +export async function POST(req: Request) { |
| 259 | + const { messages }: { messages: UIMessage[] } = await req.json(); |
| 260 | + |
| 261 | + const result = streamText({ |
| 262 | + model: openai('gpt-5'), |
| 263 | + messages: convertToModelMessages(messages), |
| 264 | + }); |
| 265 | + |
| 266 | + return result.toUIMessageStreamResponse(); |
| 267 | +} |
| 268 | +``` |
| 269 | + |
| 270 | +Finally, update the root page (`app/page.tsx`) to use the `useChat` hook: |
| 271 | + |
| 272 | +```tsx filename="app/page.tsx" |
| 273 | +'use client'; |
| 274 | + |
| 275 | +import { useChat } from '@ai-sdk/react'; |
| 276 | +import { useState } from 'react'; |
| 277 | + |
| 278 | +export default function Page() { |
| 279 | + const [input, setInput] = useState(''); |
| 280 | + const { messages, sendMessage } = useChat({}); |
| 281 | + |
| 282 | + return ( |
| 283 | + <> |
| 284 | + {messages.map(message => ( |
| 285 | + <div key={message.id}> |
| 286 | + {message.role === 'user' ? 'User: ' : 'AI: '} |
| 287 | + {message.parts.map((part, index) => { |
| 288 | + if (part.type === 'text') { |
| 289 | + return <span key={index}>{part.text}</span>; |
| 290 | + } |
| 291 | + return null; |
| 292 | + })} |
| 293 | + </div> |
| 294 | + ))} |
| 295 | + <form |
| 296 | + onSubmit={e => { |
| 297 | + e.preventDefault(); |
| 298 | + if (input.trim()) { |
| 299 | + sendMessage({ text: input }); |
| 300 | + setInput(''); |
| 301 | + } |
| 302 | + }} |
| 303 | + > |
| 304 | + <input |
| 305 | + name="prompt" |
| 306 | + value={input} |
| 307 | + onChange={e => setInput(e.target.value)} |
| 308 | + /> |
| 309 | + <button type="submit">Submit</button> |
| 310 | + </form> |
| 311 | + </> |
| 312 | + ); |
| 313 | +} |
| 314 | +``` |
| 315 | + |
| 316 | +The useChat hook on your root page (`app/page.tsx`) will make a request to your AI provider endpoint (`app/api/chat/route.ts`) whenever the user submits a message. The messages are then displayed in the chat UI. |
| 317 | + |
| 318 | +## Get Started |
| 319 | + |
| 320 | +Ready to get started? Here's how you can dive in: |
| 321 | + |
| 322 | +1. Explore the documentation at [ai-sdk.dev/docs](/docs) to understand the full capabilities of the AI SDK. |
| 323 | +2. Check out practical examples at [ai-sdk.dev/cookbook](/cookbook) to see the SDK in action and get inspired for your own projects. |
| 324 | +3. Dive deeper with advanced guides on topics like Retrieval-Augmented Generation (RAG) and multi-modal chat at [ai-sdk.dev/cookbook/guides](/cookbook/guides). |
| 325 | +4. Check out ready-to-deploy AI templates at [vercel.com/templates?type=ai](https://vercel.com/templates?type=ai). |
0 commit comments