content/docs/02-foundations/03-prompts.mdx
Prompts are instructions that you give a large language model (LLM) to tell it what to do. It's like when you ask someone for directions; the clearer your question, the better the directions you'll get.
Many LLM providers offer complex interfaces for specifying prompts. They involve different roles and message types. While these interfaces are powerful, they can be hard to use and understand.
In order to simplify prompting, the AI SDK supports text, message, and system prompts.
Text prompts are strings. They are ideal for simple generation use cases, e.g. repeatedly generating content for variants of the same prompt text.
You can set text prompts using the prompt property made available by AI SDK functions like streamText or generateText.
You can structure the text in any way and inject variables, e.g. using a template literal.
const result = await generateText({
model: __MODEL__,
prompt: 'Invent a new holiday and describe its traditions.',
});
You can also use template literals to provide dynamic data to your prompt.
const result = await generateText({
model: __MODEL__,
prompt:
`I am planning a trip to ${destination} for ${lengthOfStay} days. ` +
`Please suggest the best tourist activities for me to do.`,
});
System prompts are the initial set of instructions given to models that help guide and constrain the models' behaviors and responses.
You can set system prompts using the system property.
System prompts work with both the prompt and the messages properties.
const result = await generateText({
model: __MODEL__,
system:
`You help planning travel itineraries. ` +
`Respond to the users' request with a list ` +
`of the best stops to make in their destination.`,
prompt:
`I am planning a trip to ${destination} for ${lengthOfStay} days. ` +
`Please suggest the best tourist activities for me to do.`,
});
A message prompt is an array of user, assistant, and tool messages.
They are great for chat interfaces and more complex, multi-modal prompts.
You can use the messages property to set message prompts.
Each message has a role and a content property. The content can either be text (for user and assistant messages), or an array of relevant parts (data) for that message type.
const result = await generateText({
model: __MODEL__,
messages: [
{ role: 'user', content: 'Hi!' },
{ role: 'assistant', content: 'Hello, how can I help?' },
{ role: 'user', content: 'Where can I buy the best Currywurst in Berlin?' },
],
});
Instead of sending a text in the content property, you can send an array of parts that includes a mix of text and other content parts.
You can pass through additional provider-specific metadata to enable provider-specific functionality at 3 levels.
Functions like streamText or generateText accept a providerOptions property.
Adding provider options at the function call level should be used when you do not need granular control over where the provider options are applied.
const { text } = await generateText({
model: azure('your-deployment-name'),
providerOptions: {
openai: {
reasoningEffort: 'low',
},
},
});
For granular control over applying provider options at the message level, you can pass providerOptions to the message object:
import { ModelMessage } from 'ai';
const messages: ModelMessage[] = [
{
role: 'system',
content: 'Cached system message',
providerOptions: {
// Sets a cache control breakpoint on the system message
anthropic: { cacheControl: { type: 'ephemeral' } },
},
},
];
Certain provider-specific options require configuration at the message part level:
import { ModelMessage } from 'ai';
const messages: ModelMessage[] = [
{
role: 'user',
content: [
{
type: 'text',
text: 'Describe the image in detail.',
providerOptions: {
openai: { imageDetail: 'low' },
},
},
{
type: 'image',
image:
'https://github.com/vercel/ai/blob/main/examples/ai-functions/data/comic-cat.png?raw=true',
// Sets image detail configuration for image part:
providerOptions: {
openai: { imageDetail: 'low' },
},
},
],
},
];
Text content is the most common type of content. It is a string that is passed to the model.
If you only need to send text content in a message, the content property can be a string,
but you can also use it to send multiple content parts.
const result = await generateText({
model: __MODEL__,
messages: [
{
role: 'user',
content: [
{
type: 'text',
text: 'Where can I buy the best Currywurst in Berlin?',
},
],
},
],
});
User messages can include image parts. An image can be one of the following:
string with base-64 encoded contentstring, e.g. data:image/png;base64,...ArrayBufferUint8ArrayBufferstring, e.g. https://example.com/image.pngURL object, e.g. new URL('https://example.com/image.png')const result = await generateText({
model,
messages: [
{
role: 'user',
content: [
{ type: 'text', text: 'Describe the image in detail.' },
{
type: 'image',
image: fs.readFileSync('./data/comic-cat.png'),
},
],
},
],
});
const result = await generateText({
model: __MODEL__,
messages: [
{
role: 'user',
content: [
{ type: 'text', text: 'Describe the image in detail.' },
{
type: 'image',
image: fs.readFileSync('./data/comic-cat.png').toString('base64'),
},
],
},
],
});
const result = await generateText({
model: __MODEL__,
messages: [
{
role: 'user',
content: [
{ type: 'text', text: 'Describe the image in detail.' },
{
type: 'image',
image:
'https://github.com/vercel/ai/blob/main/examples/ai-functions/data/comic-cat.png?raw=true',
},
],
},
],
});
User messages can include file parts. A file can be one of the following:
string with base-64 encoded contentstring, e.g. data:image/png;base64,...ArrayBufferUint8ArrayBufferstring, e.g. https://example.com/some.pdfURL object, e.g. new URL('https://example.com/some.pdf')You need to specify the MIME type of the file you are sending.
import { google } from '@ai-sdk/google';
import { generateText } from 'ai';
const result = await generateText({
model: google('gemini-2.5-flash'),
messages: [
{
role: 'user',
content: [
{ type: 'text', text: 'What is the file about?' },
{
type: 'file',
mediaType: 'application/pdf',
data: fs.readFileSync('./data/example.pdf'),
filename: 'example.pdf', // optional, not used by all providers
},
],
},
],
});
import { openai } from '@ai-sdk/openai';
import { generateText } from 'ai';
const result = await generateText({
model: openai('gpt-4o-audio-preview'),
messages: [
{
role: 'user',
content: [
{ type: 'text', text: 'What is the audio saying?' },
{
type: 'file',
mediaType: 'audio/mpeg',
data: fs.readFileSync('./data/galileo.mp3'),
},
],
},
],
});
You can use custom download functions to implement throttling, retries, authentication, caching, and more.
The default download implementation automatically downloads files in parallel when they are not supported by the model.
Custom download function can be passed via the experimental_download property:
const result = await generateText({
model: __MODEL__,
experimental_download: async (
requestedDownloads: Array<{
url: URL;
isUrlSupportedByModel: boolean;
}>,
): PromiseLike<
Array<{
data: Uint8Array;
mediaType: string | undefined;
} | null>
> => {
// ... download the files and return an array with similar order
},
messages: [
{
role: 'user',
content: [
{
type: 'file',
data: new URL('https://api.company.com/private/document.pdf'),
mediaType: 'application/pdf',
},
],
},
],
});
Assistant messages are messages that have a role of assistant.
They are typically previous responses from the assistant
and can contain text, reasoning, and tool call parts.
const result = await generateText({
model: __MODEL__,
messages: [
{ role: 'user', content: 'Hi!' },
{ role: 'assistant', content: 'Hello, how can I help?' },
],
});
const result = await generateText({
model: __MODEL__,
messages: [
{ role: 'user', content: 'Hi!' },
{
role: 'assistant',
content: [{ type: 'text', text: 'Hello, how can I help?' }],
},
],
});
const result = await generateText({
model: __MODEL__,
messages: [
{ role: 'user', content: 'How many calories are in this block of cheese?' },
{
role: 'assistant',
content: [
{
type: 'tool-call',
toolCallId: '12345',
toolName: 'get-nutrition-data',
input: { cheese: 'Roquefort' },
},
],
},
],
});
const result = await generateText({
model: __MODEL__,
messages: [
{ role: 'user', content: 'Generate an image of a roquefort cheese!' },
{
role: 'assistant',
content: [
{
type: 'file',
mediaType: 'image/png',
data: fs.readFileSync('./data/roquefort.jpg'),
},
],
},
],
});
For models that support tool calls, assistant messages can contain tool call parts, and tool messages can contain tool output parts. A single assistant message can call multiple tools, and a single tool message can contain multiple tool results.
const result = await generateText({
model: __MODEL__,
messages: [
{
role: 'user',
content: [
{
type: 'text',
text: 'How many calories are in this block of cheese?',
},
{ type: 'image', image: fs.readFileSync('./data/roquefort.jpg') },
],
},
{
role: 'assistant',
content: [
{
type: 'tool-call',
toolCallId: '12345',
toolName: 'get-nutrition-data',
input: { cheese: 'Roquefort' },
},
// there could be more tool calls here (parallel calling)
],
},
{
role: 'tool',
content: [
{
type: 'tool-result',
toolCallId: '12345', // needs to match the tool call id
toolName: 'get-nutrition-data',
output: {
type: 'json',
value: {
name: 'Cheese, roquefort',
calories: 369,
fat: 31,
protein: 22,
},
},
},
// there could be more tool results here (parallel calling)
],
},
],
});
Tool results can be multi-part and multi-modal, e.g. a text and an image.
You can use output: { type: 'content', value: [...] } to specify multi-part tool results.
const result = await generateText({
model: __MODEL__,
messages: [
// ...
{
role: 'tool',
content: [
{
type: 'tool-result',
toolCallId: '12345', // needs to match the tool call id
toolName: 'get-nutrition-data',
// for models that do not support multi-part tool results,
// you can include a regular output part:
output: {
type: 'json',
value: {
name: 'Cheese, roquefort',
calories: 369,
fat: 31,
protein: 22,
},
},
},
{
type: 'tool-result',
toolCallId: '12345', // needs to match the tool call id
toolName: 'get-nutrition-data',
// for models that support multi-part tool results,
// you can include a multi-part content part:
output: {
type: 'content',
value: [
{
type: 'text',
text: 'Here is the nutrition data for the cheese:',
},
{
type: 'image-data',
data: fs
.readFileSync('./data/roquefort-nutrition-data.png')
.toString('base64'),
mediaType: 'image/png',
},
],
},
},
],
},
],
});
System messages are messages that are sent to the model before the user messages to guide the assistant's behavior.
You can alternatively use the system property.
const result = await generateText({
model: __MODEL__,
messages: [
{ role: 'system', content: 'You help planning travel itineraries.' },
{
role: 'user',
content:
'I am planning a trip to Berlin for 3 days. Please suggest the best tourist activities for me to do.',
},
],
});