Back to course
1 / 2

Module 2 · Lesson 1

The simplest call

Send one prompt to a model and get a response back, with a single function.

This is the lesson where your app starts talking to a model. We will make one call, see a real response, and understand exactly what each piece does. It is smaller than you might expect, and that is the good news.

Learn: what a model call actually is

A language model call is simpler than it sounds. You send some text in. You get some text back. That is the whole shape of it.

The model does not remember anything between calls on its own. Each call is self-contained: you hand it the input, it returns its best continuation. Everything fancy you will build later (chat history, tools, structured data) is built on top of this one plain idea. So it is worth meeting it directly before we add anything else.

In the SDK, that single call is the generateText function. You give it a model and a prompt, and it gives you back the text. No streaming, no UI, no moving parts yet. Just in, and out.

Build: your first generateText call

We will put the call in a route handler so you can trigger it from the browser. Create a new file at app/api/chat/route.ts:

import { generateText } from "ai";
import { anthropic } from "@ai-sdk/anthropic";

export async function GET() {
  const { text } = await generateText({
    model: anthropic("claude-opus-4-8"),
    prompt: "Explain what a language model is, in one friendly sentence.",
  });

  return Response.json({ text });
}

Three things are happening here:

  • anthropic("claude-opus-4-8") picks the model. The provider reads your ANTHROPIC_API_KEY from .env.local automatically.
  • prompt is the text you send in.
  • generateText makes the call and returns an object. We pull text out of it and send it back as JSON.

Make sure your dev server is running (npm run dev), then visit http://localhost:3000/api/chat. After a short pause, you will see a JSON response with the model's answer in the text field.

That pause is the model thinking and the response coming back over the network. It is real. You just called a language model from your own app.

Tip

If you get an authentication error, double-check that .env.local has ANTHROPIC_API_KEY set and that you restarted npm run dev after creating the file. The server only reads env files on startup.

Remix: change the prompt, then change the model

First, change the prompt to a question you actually want answered. Reload the route and read the new response. Notice how much the output depends on exactly what you asked. Prompting is mostly this: asking clearly.

Then try the one-line swap that shows off the SDK's real strength. The model is the only provider-specific line. If you installed another provider, you could change just that line and nothing else:

// from this
model: anthropic("claude-opus-4-8"),
// to another provider, same everything-else
// model: openai("gpt-..."),

You do not have to actually install another provider right now. The point is to see it: your code is written against the SDK, not against one vendor. That is what "model-agnostic" buys you.

When you have a prompt and response you like, drop a screenshot of it in the community. First real model call is worth sharing.

Key takeaways

  • A model call is text in, text out. generateText is that call.
  • The provider reads your API key from the environment, so you never hardcode it.
  • The model line is the only provider-specific part, which is what makes swapping easy.