Your First API Call: Chat and Streaming

AI Navigate Original / 5/16/2026

共有:

Key Points

  • Implement 3 basics: sync chat, streaming, multi-turn
  • Add system prompts; same patterns for OpenAI and Vercel AI SDK
  • Handle errors (429/401), retry with exponential backoff
  • Track token cost; complex patterns covered in practice chapter

Your First API Call

Once setup is done, implement the 3 basic patterns (sync chat, streaming, multi-turn).

1. Sync Chat (Minimal)

Send a question, receive the whole response, and return it.

import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic();

const res = await client.messages.create({
  model: "claude-opus-4-7",
  max_tokens: 1024,
  messages: [
    { role: "user", content: "Difference between TypeScript and JavaScript?" }
  ],
});

console.log(res.content[0].text);
console.log("Tokens used:", res.usage);

2. Streaming

Receive the response token by token. Better UX.

const stream = await client.messages.create({
  model: "claude-opus-4-7",
  max_tokens: 1024,
  messages: [{ role: "user", content: "5 things to see in Tokyo" }],
  stream: true,
});

for await (const event of stream) {
  if (event.type === "content_block_delta") {
    process.stdout.write(event.delta.text);
  }
}

3. Multi-Turn Conversation

Stack past exchanges in messages.

Sign up to read the full article

Create a free account to access the full content of our original articles.