Building AI-Powered Apps · Unit 1 of 8
Unit 1: How an AI app works
The three parts of an AI app, what a model API request and response contain, keeping API keys secret on the server, and why conversations must be re-sent every turn.
Opens the first lesson of this unit in Building AI-Powered Apps. Every lesson is open from the start.
Lesson 1: The three parts of an AI app
Goal: Describe the front end, back end and model provider and what each must do.
Almost every AI-powered app has the same three parts. The front end is what the user sees: a web page or a mobile app. The back end is your server: your code, your database and your rules. The model provider runs the large language model and offers it through an API (application programming interface), a web address that your server sends requests to.
Browser (front end) Your server (back end) Model provider API
------------------- ---------------------- ------------------
user types a question ---> checks the user, adds the system ---> runs the model
prompt, context and the secret key
shows the answer <--- validates, logs and returns it <--- text + token usage
Why not let the browser call the provider directly? Because the request needs your API key, and anything sent to a browser can be read by anyone who opens its developer tools. Your server is also the only place where you can check who the user is, enforce limits, add the system prompt users must not change, fetch only the data this user may see, validate outputs and keep logs.
Worked example: a support assistant for a bike shop
- A customer types "Where is my order?" into the chat box on the Harbor Bikes website.
- The browser sends it to the shop's own server at /api/chat, along with the customer's login session.
- The server looks up this customer's orders, builds a request with the system prompt and the order details, adds the secret key and calls the provider.
- The provider returns a reply and a count of tokens used. The server checks the reply, records the cost and sends the text back to the browser.
The browser never sees the key, the system prompt or any other customer's data.
- Front end
- The part of an app the user sees and interacts with, such as a web page.
- Back end
- Your server-side code, data and rules, which users cannot see or change.
- API
- Application programming interface: a defined way for one program to send requests to another.
- Model provider
- The company or service that runs a language model and offers it through an API.
Remember: Browser talks to your server; only your server talks to the model, with the key it keeps secret.
Lesson 2: Anatomy of a model request
Goal: Read a model API request and response, including stop reason and usage.
A model API is called with an HTTP POST request whose body is JSON. Field names differ between providers, so always check the documentation, but the core is the same everywhere.
{
"model": "example-model",
"max_tokens": 400,
"temperature": 0.2,
"system": "You answer questions about Harbor Bikes orders. Be brief.",
"messages": [
{ "role": "user", "content": "Where is order 5512?" }
]
}
| Field | What it does |
|---|---|
| model | Which model to run: larger models are usually more capable, slower and more expensive |
| max_tokens | The most output tokens the reply may use |
| temperature | How random the output is: low for extraction, higher for ideas |
| system | Standing instructions for the conversation |
| messages | The conversation so far, oldest first, each with a role and content |
The response comes back as JSON too: the generated text (often as a list of content blocks), a stop reason saying why it stopped (it finished, it hit max_tokens, or it wants to call a tool), and usage, the number of input and output tokens counted. Usage is what you are billed for.
{
"content": [{ "type": "text", "text": "Order 5512 shipped on Monday." }],
"stop_reason": "end_turn",
"usage": { "input_tokens": 52, "output_tokens": 9 }
}
- HTTP request
- A message a program sends to a web server, such as a POST with a JSON body.
- Stop reason
- The part of a response that says why the model stopped writing.
- Usage
- The input and output token counts for a request, which are what you are billed for.
Remember: Request: model, max_tokens, temperature, system, messages. Response: content, stop reason, usage.
Lesson 3: API keys and secrets
Goal: Keep API keys on the server, out of code, and respond correctly to a leak.
An API key is a long secret string that proves a request comes from your account. Treat it like a password that can spend your money: anyone who has it can run up your bill or abuse the service in your name.
- Keep it on the server, read from an environment variable, never typed into the code.
- Never put it in front-end code, a mobile app, a public repository, a screenshot or a support ticket.
- Use separate keys for development and production, so a leaked test key cannot touch the live app.
- Rotate it (replace it with a new one) immediately if it might have leaked, and set spending limits and alerts in the provider's dashboard.
- Least privilege: if the provider offers keys with limited permissions, give each key only what it needs.
// The key lives in the hosting settings as MODEL_API_KEY, never in the code.
const key = process.env.MODEL_API_KEY;
if (!key) throw new Error('MODEL_API_KEY is not set');
Worked example: a leaked key
- A developer pushes a test project to a public code repository with the key typed into a settings file.
- Automated scanners search public repositories for exposed keys around the clock and can find one within minutes.
- By morning the account has a large bill for requests the developer never made.
- The fix: revoke the key, create a new one stored in an environment variable, keep settings files out of the repository, and set a monthly spending cap.
A key written in code ends up in public sooner or later.
- API key
- A secret string that identifies your account to an API and lets requests be billed to you.
- Environment variable
- A named setting provided to a program by its server, used to keep secrets out of code.
- Key rotation
- Replacing a key with a new one and disabling the old one.
Remember: Keys live in server environment variables. If a key may have leaked, revoke and replace it now.
Lesson 4: Conversations are stateless
Goal: Explain statelessness and manage conversation history and its cost.
Model APIs are stateless: each request is independent, and the model remembers nothing between calls. The "memory" in a chat app is your app sending the earlier messages again with every new request.
Worked example: turn three of a chat
- Turn 1: the app sends the system prompt plus "Hi, I need help with my order": about 60 input tokens.
- Turn 2: it sends the system prompt, turn 1, the reply and the new message: about 180 input tokens.
- Turn 3: everything so far again, plus the new message: about 320 input tokens.
- Input tokens, and with them cost and delay, grow with every turn.
Long chats get expensive because the whole history is sent again each time.
Apps manage that growth. Common strategies: keep only the last few turns; replace older turns with a short summary; store facts the app needs (order number, user name) in your database and add them as context instead of relying on old messages; and set a maximum conversation length. Some providers also offer prompt caching, which makes re-sending the same beginning of a prompt cheaper and faster.
Statelessness is also a feature. Because the model keeps nothing between calls, your app decides exactly what it sees each time, which matters for privacy: if a message is deleted from your database and never sent again, the model does not have it. (The provider's own logging is a separate question, covered in Unit 8.)
- Stateless
- Keeping no memory between requests: each call must include everything needed.
- Conversation history
- The earlier messages an app re-sends so the model can follow the conversation.
- Prompt caching
- A provider feature that makes re-sending an unchanged start of a prompt cheaper and faster.
Remember: The model remembers nothing. Your app re-sends history, so trim, summarize and store facts yourself.
Practise it in Building AI-Powered Apps
Each lesson ends with three questions that check you can use the idea, not just recognise it. Wrong answers explain why and come back in Review. The unit ends with a 6-question quiz; pass it with 80% or more to master the unit.
Every lesson is free: no ads, no lives and no paywall.