By the end of this lesson you will know the three parts of every AI chat app, why the API key must stay on the server, what one request to a model contains, and what a chat really costs.
You need: nothing installed yet, only a notebook or notes app for your bot idea · Time: 8 minutes
In this course you build your own AI chat web app. Two examples we use: Exam Buddy, a study helper for a coaching class's Class 10 students, and Rangrez Crafts Helper, a FAQ bot for a Jaipur handicraft shop that answers "Do you ship to Pune?" at 11 pm.
Every chat app like these has three boxes:
- The browser. The chat page: a text box, a Send button, the messages. It runs on the user's phone or laptop.
- Your server. A small piece of code you own, called a route (an address like
/api/chatthat runs code on a server instead of in the browser). It receives the message, adds your secret API key and your instructions, and calls the model. - The model API. The LLM (large language model, the AI that writes text) runs at a provider. In this course that is AllModelAPI, at
https://api.allmodelapi.com/v1. It writes the answer and bills the key that called it.
The browser talks only to your server. Only your server talks to the model, with the key. Why?When something breaks later, the first question is always "which box?". A blank page is the browser, a 401 error is the key on your server, a slow answer is usually the model. Knowing the boxes turns panic into a short search.
It is tempting to skip box 2 and call the model straight from the chat page. Fewer files, it works on day one. It is also the most common way people lose money with AI apps.
Anything in the browser is public. Anyone can press
F12, open the Network tab and read every request, including the key. On the OpenAI developer forum, one builder whose app called the API directly found thousands of requests in two days on a stolen key, and a bill of hundreds of dollars (forum thread). Their fix was the one you will build from the start: a server in the middle.Hiding the key in a
.envfile does not help if the browser code reads it. Names starting withNEXT_PUBLIC_orVITE_are copied into the browser on purpose. The full rules are in Keep secrets secret.Why?Your key is like your UPI PIN: whoever has it spends your money. The server is the only box you control, so it is the only safe home for the key.
Put in order Put what happens after a student taps Send in the right order
Tap the steps in the order you would do them. Tap a placed step to remove it.
- 1
Your server sends the model a small JSON message (text in a fixed format of names and values). It has two main parts: which model to use, and the messages so far. Each message has a role:
- system: your instructions for the bot. Who it is, who it talks to, what it must refuse. Users never see this.
- user: what the person typed.
- assistant: what the bot answered earlier.
JSON{ "model": "glm-5.3-flash", "max_tokens": 600, "messages": [ { "role": "system", "content": "You are Exam Buddy, a patient study helper for Class 10 CBSE students." }, { "role": "user", "content": "What is photosynthesis?" }, { "role": "assistant", "content": "Photosynthesis is how green plants make food from sunlight, water and carbon dioxide." }, { "role": "user", "content": "Ek example do" } ] }max_tokensis the longest answer you allow. You will use it in lesson 4.Match them Match each role with what it holds
Tap an item on the left, then its meaning on the right.
Why?Once you can read this small block, you can check what the AI coding agent built. If the system message is missing or the key appears in it, you will spot it.
The model API is stateless: it remembers nothing between requests. When a student asks "Ek example do", the model only knows what "it" means because your server sends the whole conversation again: the system message, every earlier question and every earlier answer.
So a chat is not one request. A 10-message chat is 10 requests, and each one is a little longer than the one before.
Why?This one fact explains most of your bill and most "the bot forgot what I said" bugs. If your app forgets context, the history is not being sent. If your bill grows fast, the history is too long.
Models count text in tokens, small pieces of words. In English, 1,000 tokens is roughly 750 words; text in Hindi (Devanagari) script often needs more tokens for the same idea. You pay per million tokens, with one price for input (everything you send) and a higher one for output (what the model writes).
Worked example for Exam Buddy on
glm-5.3-flash($0.15 per million input tokens, $0.50 per million output tokens). Assume a 300-token system prompt, 50-token questions and 250-token answers:Turn 1 Turn 10 Whole 10-turn chat Input sent 350 tokens 3,050 tokens 17,000 tokens Output written 250 tokens 250 tokens 2,500 tokens Cost $0.00018 $0.00058 about $0.0038, roughly ₹0.34 (Using $1 ≈ ₹90 to keep the maths easy.) Turn 10 costs about three times turn 1, for the same short question, only because of the history.
The question stays short. The history is what grows. Now scale it. 100 students, one 10-turn chat a day, 30 days = 3,000 chats. On
glm-5.3-flashthat is about $11 (≈ ₹1,000) a month. The same chats onkimi-k3.1-pro($2 in, $10 out) cost about $177 (≈ ₹16,000). Same app, 15 times the bill. Many models also "think" before answering, and thinking tokens are billed as output, so check your real numbers in Usage once the app runs.Before any code, decide what you are building. Keep it small: one audience, one job.
Do it now Write these 5 lines in your notes
Why?A bot that does one job for one group is cheaper, safer and easier to test than "a ChatGPT for everyone". These five lines become your system prompt in lesson 2.
- Every chat app has three boxes: browser, your server, the model API.
- The API key lives only on the server. Anything in the browser is public.
- A request is a model ID plus a messages list with system, user and assistant roles.
- The API forgets everything, so your server sends the history every time, and long chats cost more per turn.
- Next: Your first chat, with the key on the server.
Create a free account to save your progress and open every lesson. Create free account