T1 · Beginner · 7 minStable · Last reviewed: 2026-08-19Early draft

The First Call — Reaching the Model From Code

In one line · You send a request with your messages and the model you want, and the API returns the model's reply inside JSON — just like any HTTP call.

Glossary:LLMModelPromptToken
Why it matters

This is the line between playing in a playground and building an actual product. Everything later in this domain builds on a first call that works.

Like ordering food online: you send the order (your messages + the model), wait a moment, and the reply arrives. The API endpoint is the kitchen and the response is your order coming back.

An analogy

The model is a chef in a distant kitchen; you don't speak to it directly — you send a written order (request) and wait for the dish (response) to come back.

You POST messages: [{role:'user', content:'greet me'}] plus the model, and it returns JSON with the assistant reply inside choices — you pull the text out and show it.

How it works

A call has three parts: an endpoint you POST to, a header carrying your key (Authorization), and a body with the model name and a messages array. The server runs inference on your input and returns JSON holding the model's reply plus a usage block counting the tokens you spent. You first check the status is OK, then pull the text out of the response and use it.

const res = await fetch("https://api.example.com/v1/chat/completions", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    Authorization: `Bearer ${process.env.API_KEY}`, // never hardcode the key
  },
  body: JSON.stringify({
    model: "model-name",
    messages: [{ role: "user", content: "Say hello!" }],
  }),
});
if (!res.ok) throw new Error(`API error ${res.status}: ${await res.text()}`);
const data = await res.json();
console.log(data.choices[0].message.content);
Practice

🧪 Try it yourself: write a small script that reads the key from an env var, POSTs a single message, and prints the model's reply plus the token count from the usage block. If the status isn't 200, print the full error message instead of failing silently.

Common mistakes

(1) Hardcoding the key in code and pushing it to GitHub — a real disaster that happens every day. (2) Calling res.json() immediately without checking the status — so you wrestle undefined instead of reading the actual error message. (3) Assuming the model 'remembers' the conversation — every call is fully independent; you must resend the whole history in messages each time.

When to use it

The moment you move past experimenting and build a real feature: a script, a backend endpoint, or any automation that needs a model reply inside code.

When NOT to use it

If you're still iterating on prompt wording, the playground is faster and cheaper. And never call the API straight from browser code — the key will leak; route the call through your own backend.

LET'S BUILD ITDone reading — now build it yourself.Send your first call and print the reply
The First Call — Reaching the Model From Code · BuildAI