1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
17 small wins to finish your pathNext lesson →
Prompts, system instructions and messages
A call takes either a prompt string or a messages array, plus an optional system instruction. The result includes the new messages to append for the next turn.
import { generateText, type ModelMessage } from "ai";
import { shopModel } from "./shop-model.ts";
const messages: ModelMessage[] = [
{ role: "user", content: "Hi, I have a question." },
{ role: "assistant", content: "Of course, what is it?" },
{ role: "user", content: "My parcel never arrived." },
];
const result = await generateText({ model: shopModel, system: "You answer support tickets.", messages });
console.log(result.text);
console.log(result.response.messages.map((m) => m.role));
console.log(shopModel.doGenerateCalls[0].prompt.map((m) => m.role));npx tsx messages.tsmessages is the conversation so far, in the ModelMessage type: user, assistant and, later, tool messages. system is separate. doGenerateCalls, recorded by the mock, shows the model received the system message first, then the three turns.
result.response.messages holds what this call added: one assistant message. To continue the chat, push it onto your array with the next user message. The SDK keeps no conversation state between calls; your app does.
Try it yourself
- Continue the conversation: push
result.response.messagesand a new user message, and call again. - Print
shopModel.doGenerateCalls[0].prompt[3]to see how a string content is sent. - Pass both
promptandmessagesand read the error.
Every expert started right here.