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.
- List what changes when an AI goes into production
- Take secrets out of the code
- Measure errors and spending, and roll out with a way back
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 changes | Protection |
|---|---|
| Variable time: from 1 to 60 seconds | A maximum time in each layer |
| Cost per use: a bug that repeats requests spends the month's budget in hours | A daily spending cap |
| The provider's request limit | A queue and a refusal with your own error |
| Different answers for the same input | Validate 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.
- It goes up with no traffic, at its own address for testing.
- It gets 5% of the requests for one hour, while you watch the alerts.
- If things are calm, it gets all the traffic.
- 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
- List every secret in your system and note where it is. Mark the ones written inside a program file.
- Draw the path of a request: client, gatekeeper, API and provider. Write the maximum time of each one, in the right order.
- Write three alerts for the middle of the night: errors, slowness and spending, and what you would do for each one.
Summary
- AI in production takes time, costs money and varies: each point calls for a protection.
- Secrets outside the code, with no default value.
- Timeouts: whoever is deepest inside gives up first.
- Roll out in steps, with the way back ready.
Sources
Read on October 30, 2026.
- nginx. ngx_http_proxy_module: proxy_read_timeout.
- Python. secrets: compare_digest.
- The Twelve-Factor App. Config: separating configuration from code.
Lesson quiz
Three situations to apply what you learned.
Isaque Victor free courses