Deploying an AI app
An AI feature is still a web service. Most outages come from ordinary things, plus a few AI-specific ones.
Words on this page (6)
- repo
- : Repository: a project folder tracked by git, usually hosted on GitHub.
- SIGTERM
- : A signal that asks a program to shut down cleanly.
- latency
- : The time you wait for a response.
- token
- : A piece of text the model reads and writes, usually a word or part of a word. About 4 characters of English on average.
- prompt
- : The input text you give a model, including instructions, examples and data.
- eval
- : A repeatable test of AI output quality, run before and after every change.
Basics
Keep secrets in environment variables set on the host, not in the . Build in a clean environment with a pinned Node or Python version. Push to GitHub and let the host build from it. After each deploy run a smoke test: one real request through the whole path, including a model call. Use a staging environment with its own keys and spend cap, and deploy to production only what passed staging.
Health and signals
Add a health endpoint that returns 200 when the app can reach its database and its model provider's credentials work. Platforms use it to restart unhealthy instances. Handle : stop accepting new requests, finish in-flight ones including streaming responses, close connections and then exit. Without this, deploys cut users off mid-answer.
AI-specific
Set per-user and global rate limits and a daily spend cap, since a loop or an abuser can turn into a large bill. Alert on error rate, , usage and cost. Keep a fallback model. Store in version control and tag each deploy with the prompt and model versions. Expect provider outages and show a useful message.
Domains and data
Point a custom domain with DNS and HTTPS. Back up the database and test the restore. Run migrations before the new code needs them.
A worked example: a safe release
- Merge to main; the host builds.
- Deploy to staging with its own keys and a low spend cap.
- Smoke test: ask one real question.
- Run the set against staging.
- Promote to production.
- Watch error rate, latency and cost for fifteen minutes.
- If anything looks wrong, roll back to the previous version. Each step is boring and each has saved someone.
Long answers and time limits
A model answer can take 20 seconds or more. Hosts and proxies often cut requests that run longer than their time limit. The symptom is a feature that works on your laptop and, in production, long answers fail with a gateway timeout or stop mid-sentence. Confirm it by asking for a deliberately long answer on the deployed app, timing it, and reading the host's logs. Fixes are to stream the answer, raise the limit, or move the job to the background and notify the user.
What goes wrong: a spend cap that does not cap
Say each user gets a 5 dollar daily limit, counted in memory inside the app. You run 3 copies of the app, and each keeps its own count. A user can spend up to 3 × 5 = 15 dollars. Restarting a copy resets its count to zero as well. The symptom is a bill that is higher than the cap implies. Keep the counter in a shared database, or set a hard limit on the provider's side as well.
Rollback and the database
You deploy new code that needs a new column, and the migration drops an old one. The release goes wrong and you roll back the code. The old code expects the dropped column and crashes. To keep rollback safe, change the database in two releases. First add the new column and keep the old one. Remove the old one only after the new code has been stable for a while.
Common mistake
No cap on spending. A bug can turn a quiet day into an expensive one.
On launch day the team deploys on Friday afternoon, and the first real user triggers an error nobody saw in testing: the production key had no access to the model they used. A smoke test right after deploy, one real request through the whole path, would have shown it in thirty seconds.
Check yourself
What should a deploy do on SIGTERM?
Not quite. Otherwise users get cut off mid-response.
Why set a spend cap?
Not quite. Caps limit the damage of runaway usage.
Your first real user hits an error right after deploy. What step would have caught it?
Not quite. One real request through the whole path catches key and permission mistakes in seconds.
You got 0 of 3 right.
Go deeper
- Docs
Heroku · Config, logs, processes and disposability for deployable apps.
- Docs
Render · A typical build, deploy and health check flow.
- Docs
Node.js · Handling SIGTERM and exiting cleanly.
- Repo
BerriAI · A proxy with budgets, rate limits and model fallbacks.
Spotted a mistake or something unclear? Report it on GitHub.