Module 5 · Deployment and Career

Putting an AI online safely

Lesson 13 of 15 9 min lesson Updated on October 30, 2026
Progress in the course87%

In this lesson

Putting AI online with no scares.

Putting a system online is the easy part. The hard part is doing it in a way you can measure, limit and undo. With AI, you need more care: every request takes time, costs money and may bring back a different answer.

What changes with AI

An ordinary API answers in milliseconds, at a fixed cost, and always with the same answer. An AI breaks all three certainties.

What changesProtection
Variable time: from 1 to 60 secondsA maximum time in each layer
Cost per use: a bug that repeats requests spends the month's budget in hoursA daily spending cap
The provider's request limitA queue and a refusal with your own error
Different answers for the same inputValidate the output before using it

The API is the door

An API is the way one system asks another for something. Here, customer service sends a text and gets back a category. Three precautions protect the door. The text has a limit, from 1 to 8 thousand characters; without it, someone sends a whole book with every request, and every word costs money. The access key should be compared with secrets.compare_digest, which takes the same time whether the key is right or wrong. And the list of sites allowed to call the API (CORS) should be short, address by address, with no wildcard.

Secrets outside the code

Code is like a shop door with a note taped to it: colleagues read it, copy it, and sometimes it ends up on the internet. Whatever changes from one environment to another goes in an environment variable. The code reads only the name; the value stays on the server. The .env file never goes into the repository, and a secret never has a default value: without the variable, the service does not start.

A test: could the repository go public tomorrow without leaking a password? If not, something needs to come out of the code.

Timeouts in order

Every call to the AI needs a maximum time. The rule: the one that gives up first is always the one deepest inside. The provider gives up at 40 seconds, the API answers with its own error, the gatekeeper (the proxy, such as nginx) waits 50, and the client gives up last. In the reverse order, the gatekeeper returns an error while the API keeps working and paying.

What the pros know

The nginx timeout can fool you. The documentation says that proxy_read_timeout, 60 seconds by default, applies between two successive reads of the response, not to the whole response. So a response that arrives little by little can last much longer than 60 seconds without timing out. People who put AI into production do not rely on the gatekeeper alone: they also set a total maximum time inside the API itself, so a request never gets stuck and keeps costing money.

Observing: logs, metrics and alerts

Three questions guide what you observe: what happened in this request, how is the service doing right now, and is it spending more than agreed? Log one line per event, in JSON, with the request number. Count the errors, measure how long 95 out of every 100 requests take to finish and add up the tokens used. Three alerts cover almost everything: a high error rate for a few minutes, prolonged slowness and spending above the agreed amount in 24 hours. Test each alert on purpose: an alert that has never fired may not work.

Rolling out little by little, and going back

Releasing everything at once is the riskiest way. The new version comes in next to the old one, in steps.

  1. It goes up with no traffic, at its own address for testing.
  2. It gets 5% of the requests for one hour, while you watch the alerts.
  3. If things are calm, it gets all the traffic.
  4. If something goes wrong, the traffic goes back to the previous version.

Careful

Do not put the service on the internet without a key, a rate limit and an error log. Turn off the API documentation page in production. And rehearse going back before you need it.

Practice · 5 min

  1. List every secret in your system and note where it is. Mark the ones written inside a program file.
  2. Draw the path of a request: client, gatekeeper, API and provider. Write the maximum time of each one, in the right order.
  3. Write three alerts for the middle of the night: errors, slowness and spending, and what you would do for each one.
Deliverable: the list of secrets, the drawing with the four times in order and the three alerts.

Summary

Sources

Read on October 30, 2026.

  1. nginx. ngx_http_proxy_module: proxy_read_timeout.
  2. Python. secrets: compare_digest.
  3. The Twelve-Factor App. Config: separating configuration from code.

Lesson quiz

Three situations to apply what you learned.

Lesson 13 of 15

Before moving on to the next lesson

Put what you saw into practice. The boxes stay checked in this browser, so you can come back later.

Have a question about this lesson? The IV Help team answers personally on WhatsApp.

Ask on WhatsApp