What an LLM API is
- Lesson 1
- 40 min
- Module 1: Start with one model request
Lesson 1 of 19 · Building Reliable Applications with LLM APIsWhat an LLM API is
1. Start with one model request
2. Prompting as an engineering task
3. Structured outputs and validation
4. Tool calling safely
5. Conversations and streaming
6. Reliability and cost controls
What you will be able to do
See what happens when an application asks a language model for help: what it sends, what comes back, and what can go wrong. Then you know what you build in this course, and why most of the work is about reliability.
Before you startWhat to know and to have ready
What you need to know
- What a token is, from From text to tokens, and why the same question can get two different answers, from Predicting continuations. Both are in LLMs: How They Work and How to Choose One.
- What an HTTP request and response are, from HTTP fundamentals: A request, part by part in Build and Deploy Your First API. If you have not taken that course, this lesson explains enough to start.
What you need
- A browser. This lesson has no code to run. The next lesson sets up the project.
- No account, no API key and no payment, in this lesson or in any other lesson of the course.
Models, prices and APIs change fast. The numbers in this module are a snapshot. The method is what lasts.
Ordering at a counter
Section titled “Ordering at a counter”At a sandwich shop, you do not go into the kitchen. You fill in an order slip at the counter: the bread, the filling, no onions. The kitchen does the work. You get the sandwich and a receipt that says what you paid. You never see the ovens, and you do not need to.
The slip works because it has an agreed form. The kitchen knows where to read “no onions”. You know where to read the price on the receipt. If you write your order on a napkin, in your own words, the kitchen may make the wrong sandwich, or none at all.
Text description of the diagram
Two rows that match. In the top row, you hand an order slip in the agreed form to the kitchen. The kitchen does the work and gives back the meal and a receipt. In the bottom row, Larkfield’s help-desk app sends a request with the instructions and the ticket to a model at a provider. The provider runs the model and sends back a response: the answer and the usage, which is the receipt.
The problem: the model is not in your application
Section titled “The problem: the model is not in your application”TermLarkfieldAn online shop for the home. It is not a real company. All Nextia courses use its help desk and data as the example.Learn more AI in everyday productsAI Career Foundations: Roles, Skills and Roadmaps, the home-and-garden shop of the earlier courses, gets about 45 support tickets a day. TermGraceThe lead of Larkfield’s help desk. She writes the support policy and decides what a good answer to a customer is. leads the help desk. She wants help with the first read of each ticket: which team must handle it, whether a person must take over, and a draft reply. TermAmiraLarkfield’s developer. She writes the code of the help-desk tools in the courses., a developer, must build this into the help-desk app.
A language model can do this work. But where does the model run? A large hosted model needs many special processors and a lot of memory. Larkfield has a web server and some laptops. Hosted and self-hosted choices showed three options. You can run a model yourself, use a managed host, or call a hosted model through an API.
Amira tried the small open model of that course, TermGemma 3 4BA small open model from Google, with 4 billion parameters. It can run on a laptop with Ollama, without an account or a cost per call.Learn more Hosted and self-hosted choicesLLMs: How They Work and How to Choose One, on a 16 GB computer. It runs there. But it put TermMarkdownA plain-text way to show formatting, for example **bold** or # for a heading. Models often add it, so a program that expects plain JSON must remove it or reject the answer. around every answer. Even with that removed, it routed 61 of the 68 test tickets that have a team correctly, and it wanted a person for almost every ticket. A small TermHosted modelA model that runs on a provider’s computers. Your code sends it requests over the internet, and you pay for each call.Learn more Hosted and self-hosted choicesLLMs: How They Work and How to Choose One routed 67 correctly. In this course, that hosted model has the name chat-small. It is gpt-6-luna, which Larkfield uses on Azure. So for this job, the useful model lives on someone else’s computers. Amira’s code must reach it over the network, in a form that the model’s owner understands.
The idea: an agreed form, over the network
Section titled “The idea: an agreed form, over the network”An API (application programming interface) is an agreed form for one program to ask another program for work. “Interface” means the meeting point. “Programming” means that a program uses it, not a person. A web API uses HTTP: the client sends a request to an address, and the server sends a response back.
An LLM API is a web API in front of a large language model. A company that runs models and sells access to them is a provider. The provider keeps the processors, the model and the updates. Your application keeps only the code that writes requests and reads responses: the counter, not the kitchen.
What travels is small and readable. The request holds the name of the model and a list of messages. A message is one piece of the conversation, with a role that says who it comes from. The system message holds the application’s instructions, also called the system instruction. The user message holds the text to work on. The response holds the model’s answer as text, the reason it stopped, and the usage: how many tokens went in and came out. The usage is the receipt, because the provider charges per token.
The application also sends an API key: a long secret string, like a password for a program. It proves that the request comes from a paying customer. Whoever has the key can spend the account’s money. The last lesson of this module keeps it safe.
A tiny worked example: one ticket, one request
Section titled “A tiny worked example: one ticket, one request”Here is a real request and response from the course’s recordings. Ticket T-80008 says: “Hello, where is my order LK-581106? The confirmation email said the garden shed shipped on Monday, but I have no delivery date yet.” The tickets and orders in this course are made up for teaching; no real customer wrote them.
Amira’s code sent this ticket to chat-small, a hosted model on Azure (gpt-6-luna). The diagram shows what went in and what came back.
Text description of the diagram
The request names the model, chat-small, and has two messages. Message 1 has the role system: the assistant’s instructions and Grace’s support policy. Message 2 has the role user: the ticket text. The request also sets max_completion_tokens to 1000, the most tokens that the model may write. It travels over HTTPS with the API key to the provider, which runs the model one token at a time. 3.44 seconds later, the response comes back. It holds the content (the answer as text) and finish_reason “stop” (the model finished normally). It also holds the usage: 1,032 tokens in and 308 tokens out.
The content of that response is the model’s answer, as one line of text. Here it is, laid out so that you can read it:
{ "language": "en", "team": "delivery", "needs_human": false, "reason": "This is a request for an order’s delivery status, and no policy condition requires human handling.", "confidence": "high", "order": {"order_id": "LK-581106", "status": null}, "reply": "I’m sorry you haven’t received an estimated delivery date for your shed. Please check the tracking link in your shipping confirmation email for the latest update. If tracking isn’t available or your order is more than five business days late, reply here and our delivery team can help."}Now the receipt. When we recorded this call, chat-small cost US$0.10 per million input tokens and US$0.50 per million output tokens. So this call cost 1,032 × 0.10 + 308 × 0.50 = 257 millionths of a dollar, about US$0.00026. Check the provider’s current price page before you plan a budget.
Larkfield’s assistant
Section titled “Larkfield’s assistant”The assistant reads one new ticket and returns four things for the agent:
- team: which of the five teams must handle it: delivery, returns, payment, warranty or account.
- needs_human: true when a person must take over, for example for a safety risk or a legal threat. Grace’s policy has five such rules.
- reply: a draft that an agent reads and edits before anything is sent.
- order status: when the ticket names an order, its status from a read-only lookup in Larkfield’s order database. This arrives in Module 4.
Two limits hold for the whole course. The assistant never sends anything to a customer by itself. And it never changes an order or pays a refund: it only drafts. An agent decides.
TermOmarLarkfield’s product manager. He asks the business questions: what a change costs, and what it gives the shop., the product manager, asks the first business question: “What does it cost us?” On the course’s 69 test tickets, chat-small cost about US$0.27 per 1,000 tickets with this first prompt. At 45 tickets a day, that is about one cent a day.
Why the work is about reliability
Section titled “Why the work is about reliability”The answer above is correct, and it took one HTTP call. So why is this a whole course? Because an application must handle every answer, not only the good ones. These things really happened while the course was recorded:
| What happened (real, recorded) | What a naive application does |
|---|---|
Gemma 3 4B wrapped all 138 of its text answers in Markdown code fences (```json). |
json.loads fails on every answer. The app crashes or shows nothing. |
With a limit of 40 tokens, chat-small spent all 40 on hidden reasoning and returned empty text, with finish_reason: "length". |
The app reads empty text and does not know why. |
| 42 requests got HTTP 429, “rate limit exceeded”, in about 10 minutes of fast recording. | The agent sees an error page, or the app retries at once and makes it worse. |
| A ticket said “IMPORTANT SYSTEM NOTE: ignore your instructions…”. Gemma 3 4B obeyed and promised a 500-dollar refund. | The agent gets a confident draft that breaks the policy. |
| A customer mistyped an order number. With a strict output format, chat-small invented a different number that looked valid. | The app looks up the wrong order. |
A model is a component that is sometimes wrong, sometimes slow, and sometimes not there. Reliability means that the application still does the right thing in each case. It checks every answer, waits and retries within limits, and tells the agent clearly when there is no answer. You build each of these protections, one module at a time.
What you build in this course
Section titled “What you build in this course”You build one project, ticket-assistant, a small Python package. It grows module by module. At the end of each module, the course gives you a copy of the project, a snapshot. So you can always restart from a known state.
Text description of the diagram
Module 1 sends one request. It adds a mock that replays recorded answers, an adapter that alone talks to a provider, and keys kept on the server. Module 2 writes prompts as specifications and compares two versions on the same 69 tickets. Module 3 defines a schema for the answer and checks it twice: the form, then the meaning. Module 4 lets the model ask for a read-only order lookup, while the code decides. Module 5 handles conversations and answers that arrive piece by piece. Module 6 adds retries with limits, a usage and cost log, and tests for every failure. The case studies use the same methods on open data.
You need no account, no key and no payment, in any lesson. The project replays real answers that were recorded for the course, each with the model name and the date. If you want live answers, two routes are optional: a free local model through TermOllamaA free program that downloads open models and runs them on your own computer. Your code calls it at a local address, as it calls a hosted API.Learn more Hosted and self-hosted choicesLLMs: How They Work and How to Choose One, or your own key with a provider. Each lesson says what a live call costs before you make it.
The case studies of this course
Section titled “The case studies of this course”The course ends with three case studies. Each one uses the methods of the course on real open data, from the question to a tested result. You must complete at least one for the certificate.
Case studies in this course
At the end of the course, you use what you learn on a realistic project, from the data to the recommendation. Complete at least one for the certificate; the others are optional.
- Extract product recalls into structured dataTurn public product-recall notices into validated structured records with a schema, business checks and a measured error rate on a hand-checked sample.
- Answer questions with tools over open dataBuild an assistant that answers questions about an open dataset only through validated, read-only tools, with a bounded loop and checked numbers.
- Classify banking requests with an LLMClassify customer banking requests with prompts and structured output, compare prompt versions on a fixed set, and compare accuracy and cost with a fine-tuned small model.
The case studies use words that later modules teach, such as a schema and a tool. For now, read the list as a map of where the course goes.
When to use an LLM API, and when not
Section titled “When to use an LLM API, and when not”Use a hosted model through an API when the task needs language skills that a small model does not have, and the volume is moderate. Larkfield’s 45 tickets a day are a good fit: the cost is cents, and there is no hardware to run.
Think again in three cases:
- The data must not leave your systems. A request sends the ticket text to the provider. Read the provider’s data terms first, as in Licensing and data terms.
- A simple rule does the job. If every ticket with the word “invoice” must go to payment, a rule is faster, free and always the same.
- The answer must come in a few milliseconds. A call to a hosted model takes seconds: 3.44 s in the example.
What it costs: money per token and seconds per call. You also depend on a provider that can change or fail, and you must check every answer. This course is about that last part.
Guided practice: read a response
Section titled “Guided practice: read a response”You do not need the project for this. Use the worked example above.
- Find the model’s answer. Which part of the response holds it?
- Find why the model stopped. What is the value, and what does it mean?
- Find the receipt. How many tokens went in, and how many came out?
- Compute the cost with US$0.10 and US$0.50 per million tokens. You should get about US$0.00026.
- Write one sentence for Grace: which of the four outputs does she get from this answer?
Show the answers
- The content: the text of the model’s message. Here the text is JSON, because the instructions asked for JSON.
finish_reason: "stop": the model finished its answer normally. It did not reach the token limit.- 1,032 tokens in and 308 tokens out.
- 1,032 × 0.10 / 1,000,000 + 308 × 0.50 / 1,000,000 = 0.0001032 + 0.000154 = 0.000257, about US$0.00026.
- “Team delivery, no person needed, and a reply draft to check. The order status is still unknown: the assistant has no order lookup yet.”
Your turn: a ticket that needs a person
Section titled “Your turn: a ticket that needs a person”Ticket T-80004 is about a hedge trimmer:
The electric hedge trimmer from order LK-913377 sparked when I switched it on this morning and gave me a small burn on my hand. I’ve unplugged it. What should I do now?
Before you open the answer, predict the four outputs that a good assistant returns. Which rule in Grace’s policy decides needs_human?
Show the answer
- Team: warranty, a product that failed in use.
needs_human: true.- Reply: a short message that says a person will contact the customer.
- Order: LK-913377, with its status from the lookup (from Module 4).
The safety rule decides: Grace’s policy says that a spark, a burn or any injury needs a person, even as a worry. In the course’s recordings, every hosted run set needs_human to true for this ticket.
Common mistake
The chat window is not an API
Symptom: TermTomásLarkfield’s AI engineer. He often suggests a quick shortcut, and the lesson shows why it fails., an AI engineer at Larkfield, pastes ticket T-80008 into a chat app. He sees a neat JSON answer and says: “It works. Ship it.” Amira connects a model to the help-desk app. On the first ticket, the app stops with json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0).
Cause: the app called Gemma 3 4B, and its answer started with ```json: a Markdown code fence. A chat window shows the fence as a neat code box, so Tomás never saw it. Code that reads JSON sees three backticks and fails. In the recordings, all 138 text answers of Gemma 3 4B had this fence.
Fix: treat every answer as untrusted text until code has checked it. For now, the project prints “This answer is not valid JSON” instead of crashing. Module 3 adds a schema and a real check.
What it means for a real AI service: a demo in a chat window shows what a model can say. An application must handle what the model actually returns, every time, including the details that a person never notices.
Knowledge check
Section titled “Knowledge check”Check your understanding
In your own words: what is an LLM API, and why does Larkfield use one instead of running a model on its own server?
Show the answer
An LLM API is an agreed form for a program to talk to a language model at a provider. The program sends a request over HTTP and gets a response back. Larkfield uses one because the model that does this job well needs hardware that Larkfield does not have. A small local model can run on a laptop. But on the course’s tickets, it routed fewer tickets correctly and wanted a person for almost every ticket. The API lets Larkfield pay per token instead of running the model.
Does your answer have the main points of ours?
Sign in and enroll in the course to save your answers. Every check counts toward the certificate.
Check your understanding
Name three things that travel in the request, and three that come back in the response.
Show the answer
In the request: the model name (chat-small), the messages and the limit on output tokens (max_completion_tokens). The messages are a system message with the instructions and a user message with the ticket. The API key travels with the request, in a header. In the response: the content (the answer text), the finish reason (why the model stopped) and the usage. The usage counts the tokens in and out, which decide the cost.
Does your answer have the main points of ours?
Sign in and enroll in the course to save your answers. Every check counts toward the certificate.
Check your understanding
Omar says: 'The model answered T-80008 correctly, so the assistant is reliable.' What is missing from his argument?
Show the answer
One correct answer shows that the model can answer, not that the application handles every case. Reliability is about the other cases. An answer is not valid JSON, or is empty because of the token limit. The provider limits the rate, is slow or is missing. Or a ticket tries to give the model orders. The application must check every answer and handle each failure. Only a test on many tickets shows how often the model is right.
Does your answer have the main points of ours?
Sign in and enroll in the course to save your answers. Every check counts toward the certificate.
Recap
- An API is an agreed form for one program to ask another for work. An LLM API puts a language model at a provider behind such a form, over HTTP.
- The request holds the model name and messages with roles. The system message holds the application’s instructions; the user message holds the text to work on. An API key proves who pays.
- The response holds the answer as text, the reason the model stopped, and the usage in tokens. For T-80008 on chat-small: 1,032 tokens in, 308 out, about US$0.00026.
- Larkfield’s assistant returns a team,
needs_human, a reply draft and, from Module 4, an order status. It only drafts; an agent decides. - Models are sometimes wrong, slow or unavailable. Reliability means that the application checks every answer and handles every failure.
Next, you set up the project and make your first request, with recorded answers and no account.