Module 1 · Lesson 1 of 6

One model call, end to end

Where the API key lives
Answer limit (max tokens)

Predict first: does the whole answer come back, and who can read the key? Step through to check.

User's browser, runs the page, NOWUser's browserruns the pagequestion sentNOWYour server, holds the API keyYour serverholds the API keyModel API, the providerModel APIthe provider
  • User's browser — runs the page — healthy — NOW — question sent
  • Your server — holds the API key — healthy
  • Model API — the provider — healthy
  • Request
  • Reply
Step 1 of 8

Step 1 of 8 · User's browser

Done: The page sends the question to your server

What happens
The user asks: "Can I return shoes I bought 40 days ago?" The page sends this to your own server, not to the provider.
Why
Your server holds the secret API key. The page runs on the user's device, where anyone can read its code.
What changes
Your server has the question. Nothing has gone to the provider yet.
Next
Your server builds the messages

Event log

7 more to go. Press a line to go back to that step.

Prompt tokens
–
Answer tokens
0
Cost of this call
–
API key
on your server

The request appears here once it has been built.

Step 1 of 8: User's browser. The page sends the question to your server. The user asks: "Can I return shoes I bought 40 days ago?" The page sends this to your own server, not to the provider.

What this simplifies. The shop, its policy and the answer are made up, and the model's reply is written in advance. Tokens are counted by this site's teaching splitter, so a real model counts a few more or fewer. Field names differ between providers: one says "finish_reason" where another says "stop_reason", and some take the instructions outside the message list. The shape is the same: messages and settings go in; text, a stop reason and token counts come back. Prices are examples.

Most AI features call a model through a provider's API. Your server sends an request with messages and settings, and gets back a reply with the answer.

The messages each have a role. A holds your instructions and context. User messages hold what the user typed, and earlier model replies go back in as assistant messages.

Settings travel with the messages: which model, the most the answer may use, and a . The reply holds the text, a , and the tokens that went in and out.

The tells the provider which account pays. Keep it in your server's secret settings. A key inside a web page can be read and copied by anyone who opens the page.

Example. In the run above, the is 78 tokens and the answer is 36, so the call costs $0.000774 at the example prices. With a 16-token limit, the answer stops after "If" and the stop reason says so.

In system design: Your server sits between users and the provider. It holds the key, builds the messages, sets limits, reads the stop reason and records token use per customer.

Check yourself

Why should the API key stay on your server, not in the web page?