Build · Step 8 of 14

Integrate AI into your app

A chat window is the demo. The real work is wiring a model into software your users already use — as a feature, not a destination.

It's just an API call

Strip away the mystique and integrating AI is one HTTP request: you send text (and instructions), you get text back. Every SDK wraps the same thing.

import Anthropic from "@anthropic-ai/sdk";

const client = new Anthropic(); // reads ANTHROPIC_API_KEY

const res = await client.messages.create({
  model: "claude-opus-5-5",
  max_tokens: 16000,
  system: "You write concise, friendly release notes.",
  messages: [{ role: "user", content: rawCommitLog }],
});

// The reply is a list of blocks; print the text ones.
for (const block of res.content) {
  if (block.type === "text") console.log(block.text);
}
The API key is a server-side secret. Never ship it to the browser. The model call belongs in your backend, a serverless function, or a CLI — never client-side JavaScript.

Design the feature like a function

An AI feature is a function with a fuzzy contract: a known input, a known shape of output, and a model in the middle. Pin down the edges and the middle becomes manageable.

  • Input — what real data goes in (a comment, a document, a row, a diff)?
  • Output — what does your code do next with the result? That dictates the format you ask for.
  • The prompt — the stable system prompt is the function body. See Prompting.

Get structured output you can trust

If your code consumes the result, you need a predictable shape — not prose. Don't ask for JSON in the prompt and hope: use the API's structured output feature, which constrains the response to a schema you define.

import { z } from "zod";
import { zodOutputFormat } from "@anthropic-ai/sdk/helpers/zod";

const Review = z.object({
  sentiment: z.enum(["positive", "neutral", "negative"]),
  summary: z.string(),
});

const res = await client.messages.parse({
  model: "claude-opus-5-5",
  max_tokens: 16000,
  messages: [{ role: "user", content: customerComment }],
  output_config: { format: zodOutputFormat(Review) },
});

const review = res.parsed_output; // typed; null if it didn't parse
if (!review) throw new Error("no usable output");

The schema guarantees the shape, not the truth. A well-formed answer can still be wrong, so keep treating model output like any untrusted input.

Give it your data

The model knows the world; it doesn't know your world. Two ways to fix that:

  • Just include it. If the relevant data is small — a document, a record, a page — put it straight in the prompt. Simplest, and usually enough.
  • Retrieve it (RAG). When the data is too big to send wholesale, store it, search for the few relevant pieces at request time, and include only those. Reach for this only once "put it in the prompt" genuinely stops fitting.
  • Let the model fetch it. Give the model tools — a search function, a database query, an MCP server — and it looks up what it needs as it goes. More moving parts, but it handles questions you could not predict.

"Just include it" goes further than it used to: current models accept around a million tokens, and a long prompt prefix you send repeatedly can be cached, which cuts its cost by about 90% on later calls. Put the stable part (instructions, reference documents) first and the part that changes last — Context explained why.

It reads more than text

Current models take images and PDFs directly — screenshots, charts, scanned forms, a photographed whiteboard, a fifty-page contract. You don't need an OCR step first; send the file as part of the message, before the question.

import { readFileSync } from "node:fs";

const pdf = readFileSync("invoice.pdf").toString("base64");

const res = await client.messages.create({
  model: "claude-opus-5-5",
  max_tokens: 16000,
  messages: [
    {
      role: "user",
      content: [
        {
          type: "document",
          source: { type: "base64", media_type: "application/pdf", data: pdf },
        },
        { type: "text", text: "What is the total, and when is it due?" },
      ],
    },
  ],
});

Images work the same way with type: "image". Combine this with structured output and "turn this pile of PDFs into rows" becomes a short script.

Stream for anything user-facing

A model can take several seconds to finish. If a person is waiting, stream the response so words appear as they're generated — the same answer feels dramatically faster. For backend jobs where nobody is watching, skip it.

The production non-negotiables

  • Cost — every call is metered. Estimate tokens × volume before you ship, and pick the right tier and effort (see the AI Picker). Cache repeated prompt prefixes, and send work nobody is waiting on through the batch API at half price.
  • Latency — model calls are slow by API standards. Stream, run them in the background, or cache repeated results.
  • Failure — the API can rate-limit or error. The official SDKs already retry with backoff; decide what your feature does when they give up.
  • Incomplete answers — check why the model stopped before using the reply. It may have hit the length limit, or declined the request; in both cases the text you got is not the finished answer.
  • Verification — keep a set of real inputs and re-check them when you change the prompt or model. A small eval set catches regressions a demo never will; building one is its own step, Evals.
  • Security — the model's output is untrusted input to the rest of your program, and anything it reads may contain someone else's instructions. See Security before you ship.
Ready to do it for real? The Projects page walks through building a complete AI tool end to end.