Adding AI to an existing SaaS app: the architecture that keeps it boring
How to fit LLM calls into an existing SaaS product: background jobs, timeouts, retries, rate limits, provider outages and storing results, without slowing down or breaking the rest of the app.
Adding an AI feature to a SaaS product that already has paying customers is different from building a demo. The rest of the app has to keep working. Pages have to stay fast. And a slow or failing model provider can’t be allowed to take your product down with it.
Most of that comes down to a few architecture decisions made before the first prompt is written. Here’s how we’d fit LLM calls into an existing SaaS app.
Treat the model as a slow, unreliable external service
An LLM call behaves more like a call to a payment provider or an email service than a database query. It can be slow, it can fail, and it can be rate-limited.
- Put every model call behind one module in your code, with a clear interface. The rest of the app shouldn’t know which provider you use.
- Set timeouts on every call. Decide what happens when one runs out, rather than letting a request hang.
- Retry carefully: a small number of retries with backoff, only for errors that are worth retrying.
- Never call the model inside a database transaction. Holding locks while waiting on an external service causes problems far away from the AI feature.
Move slow work into background jobs
If the user doesn’t need the answer in the next second or two, don’t make them wait for it in a web request.
- Queue the work with whatever job system you already run, and return to the user straight away.
- Show progress honestly, such as “Drafting your summary…”, and notify them or update the page when it’s ready.
- Make jobs safe to run twice, so a retry doesn’t send two emails or create two records.
- Store the result with the inputs, the prompt version and the model, so you can show it again without paying for it twice and investigate it later.
Summaries, document processing, enrichment and bulk actions all fit this pattern. Chat and autocomplete usually don’t, and our post on why your LLM feature is slow covers how to make those feel quick.
Protect the rest of the app
One busy customer or one bad loop shouldn’t affect everyone else.
- Limit usage per account and per user, so one tenant can’t use up your provider’s rate limit or your budget.
- Use a separate queue or worker pool for AI jobs, so a backlog doesn’t delay password resets and invoices.
- Set a spending alert and a hard cap you can enforce in code. What an AI feature costs to run shows how to estimate it.
Plan for the provider being down
Every external service has bad days. Decide in advance what your product does when the model is unavailable.
- Degrade gracefully: hide the AI button, fall back to the manual flow, or queue the work for later with a clear message.
- Keep a kill switch so you can turn the feature off without a deploy. Our post on rolling out AI features safely covers flags and switches.
- Consider a second provider only once you’ve measured that your feature works acceptably on it. Switching models without testing swaps one risk for another.
Keep tenant data where it belongs
In a multi-tenant app, the biggest risk isn’t a wrong answer. It’s one customer’s data appearing in another customer’s result.
- Scope every retrieval and tool call to the current account in code, not in the prompt.
- Keep caches per tenant, or make sure cache keys include the account.
- Log carefully and apply your usual retention rules. See keeping customer data safe in LLM features.
The short checklist
- One module for all model calls, with timeouts and careful retries
- No model calls inside database transactions
- Background jobs for anything that can wait, safe to run twice
- Store results with inputs, prompt version and model
- Per-account limits, a separate queue and a spending cap
- A tested fallback and kill switch for provider outages
- Every lookup, cache and log scoped to the tenant
If you’re adding AI to a product that customers already rely on, see our fixed-scope offers or book a 30-minute call.