---
title: "generateText — wait for the answer"
summary: "The smallest useful AI SDK call: pick a model, send a prompt, read text."
track: "The model"
day: 2
minutes: 16
author: "Akash Panchal"
url: https://ai-sdk-patterns.dev/learn/fundamentals/day-2/generate-text
dateModified: 2026-08-29
---

# generateText — wait for the answer

The smallest useful AI SDK call: pick a model, send a prompt, read text.

*The model · Day 2: Your first chat response · ~16 min. Written by [Akash Panchal](https://github.com/akashp1712).*

Canonical: https://ai-sdk-patterns.dev/learn/fundamentals/day-2/generate-text

## One prompt, one string back

This is the smallest useful call: one function on the server, one string back. Learn it before streaming or tools.

This is the smallest useful call in the AI SDK. Run it on the server — a script, a Route Handler, a server action. getModel() reads DEFAULT_MODEL. You wait. text is a string you can log, save, or return as JSON.

prompt is the short form: a single user turn, no history. Use it for one-shot jobs (summarize this, name this). Chat products almost always use messages instead, because they need a system prompt and a thread. Same function, two input shapes.

### generate-once.ts

```ts
import { generateText } from "ai";
import { getModel } from "@/lib/model";

const { text } = await generateText({
  model: getModel(),
  prompt: "Explain TypeScript in one sentence.",
});

console.log(text);
```

- **Import from ai, not from OpenAI.** generateText is the AI SDK. Importing a vendor SDK here locks you to one HTTP dialect and throws away streaming and tools later.
- **getModel() picks the provider.** DEFAULT_MODEL becomes a Gateway id. You do not hardcode openai('gpt-4o') in lesson code.
- **prompt is the short form.** One string, treated as a user message. No system instructions unless you stuff them into the same string — which you should not, once you have messages.
- **await means wait.** Nothing is returned until the model finishes. Slower to feel than ChatGPT, but you can reason about one value. Streaming is the same call with a different return.

## The same call, as a chat

Replace prompt with messages when you need roles or history. This is the shape every later lesson uses — streaming, tools, structured output. Learn it here while the result is still a quiet string.

After the call, if you want a thread, you append { role: 'assistant', content: text } yourself and save the array. Forget that step and the next request has no past.

### generate-chat.ts

```ts
import { generateText } from "ai";
import { getModel } from "@/lib/model";

const { text } = await generateText({
  model: getModel(),
  messages: [
    { role: "system", content: "You are a concise tutor. Short answers only." },
    { role: "user", content: "What is an LLM?" },
  ],
});
```

- **system is the rulebook.** Put durable instructions here, not in every user line. Short system prompts cost fewer tokens and are easier to audit.
- **user is this turn.** In a real app this string comes from the request body. It is data. Never eval it. Never concatenate it into SQL.
- **You would append the assistant reply.** Push { role: 'assistant', content: text } onto the array and persist it if the next turn should remember. The API will not.

## When to wait, when not to

Wait (generateText) is the right default for scripts, cron, classification, and anything you assert on in a test. You get one object. You can JSON.stringify it.

Do not wait in a chat UI. Users stare at a spinner for a paragraph they could have started reading. That is Day 3. The messages you learned here do not change.

## Common questions

### What is “generateText — wait for the answer”?

The smallest useful AI SDK call: pick a model, send a prompt, read text.

### How long does this lesson take?

About 16 minutes of reading. It is a free chapter in the AI SDK Patterns TypeScript course.

### When should I not use this?

Wait (generateText) is the right default for scripts, cron, classification, and anything you assert on in a test. You get one object. You can JSON.stringify it.
