Integrating AI into existing SaaS: architecture that stays in your control.
Put AI behind your existing identity and permission system. Persist each operation before running it, let models propose typed actions, and validate those actions in ordinary application code. Release one bounded workflow before widening access.
Inside this article 7 sections +
Imagine a SaaS product where account managers prepare renewal summaries. They already use a React interface, a NestJS API, and PostgreSQL. The proposed AI feature should assemble recent activity, summarize open issues, and draft a next step. Replacing the application with an agent platform would put the most mature parts of the product back into question.
A more useful integration treats AI as another capability inside the existing product. Identity, billing, account ownership, and business rules continue to provide the foundation. The new work is to make a model-assisted operation durable, inspectable, and easy to stop. The architecture below is an illustrative design, not a report of a client deployment.
Follow a renewal summary from click to review
The user selects an account and requests a draft. The backend checks access to that account and creates an operation record with a requester, tenant, purpose, and input version. It returns an operation ID immediately. The screen can show a durable status instead of keeping a fragile browser request open until every model and data call finishes.
For a small application, Next.js route handlers may provide the necessary server endpoints. If the product already has a NestJS business API, keep that API responsible for the domain operation. The choice should follow existing ownership and deployment constraints. A second backend layer is useful only when it has a clear job, such as shaping data for the interface; it should not create a second copy of authorization rules.
Request + review→NestJS API
Identity + policy→PostgreSQL
Operation record
Authorized context→Model gateway
Budget + telemetry→Draft store
Review before action
Keep the operation durable across failures
A worker claims the operation, collects the permitted account information, and records the versions it used. A queue such as BullMQ or an AWS-managed queue can carry work to that worker. The operation record remains the business source of truth: queued, running, awaiting review, completed, or failed with an actionable reason. Those states should mean something to a person, not merely mirror infrastructure events.
Make saving the request and scheduling its work reliable. A database transaction can save both the operation and an outbox record; a dispatcher later publishes that record to the queue. Because dispatch can repeat, the worker must recognize a previously handled operation. This closes one failure gap without pretending the database and every downstream provider share a single transaction.
If the browser closes, the operation should still be discoverable when the user returns. If a worker restarts, it should either resume safely or leave enough evidence for a controlled retry. If the model provider times out after accepting the call, record the uncertain attempt and apply a bounded recovery policy. Repeated retries can multiply both latency and spend.
Keep business context separate from instructions
The worker builds a compact input from permitted records. It labels account facts, customer messages, and internal notes as data. A customer note that asks the assistant to ignore policy must not gain the authority of an application instruction. The application should also reject unexpected output fields and invalid account references before displaying or acting on a result.
Ask for a structured draft that the interface can explain: summary, supporting record IDs, unresolved questions, and proposed next step. Validate the shape, then verify references against the records actually supplied. A citation-shaped string is not proof. When evidence conflicts, show the conflict rather than requiring the model to produce a confident narrative.
Store only the diagnostic information needed for support and evaluation. Full prompts can contain customer material. Decide who can inspect them, how long they remain available, and what gets redacted. These choices belong in the feature design because debugging access can otherwise become broader than product access.
Use a gateway for visibility, not hidden business rules
A model gateway can centralize provider routing and usage telemetry. For example, Cloudflare AI Gateway exposes request and usage analytics; AWS deployments may instead organize model access through their existing cloud infrastructure. Keep the application contract independent of a particular provider so that a model change does not require rewriting the renewal workflow.
Provider independence has limits. Models can differ in output behavior, tool handling, latency, and failure modes. A fallback is a product change, not merely a network retry. Evaluate any alternative against the same renewal examples and record which configuration generated each draft. Do not silently switch to a model that has never passed the task’s acceptance checks.
Define what an approval actually approves
Extend the renewal example one step further: an account manager can approve a drafted email for sending. Saving a boolean called approved is not enough to describe that decision. The approval should refer to a specific draft revision, recipient, account, and proposed action. Otherwise, a draft can change after review while an old approval still appears valid.
Imagine that draft revision three proposes a renewal conversation with the account’s current contact. The manager reviews it, but another employee updates the contact before the send worker runs. The worker should compare the approved action with current account state. If the recipient or another material condition has changed, it should stop for a fresh review. It should not silently substitute the new contact and assume the original approval covers the change.
A practical operation record can retain the draft revision, the approved action payload, the approving user, an expiry time, and the result of the final policy check. The execution service still verifies that the approver is authorized. Record the application’s decision and the evidence needed to explain it; storing a model’s reasoning does not establish that the action was permitted.
Test the contract with deliberately awkward sequences. Approve a draft, then edit its content. Approve it, then remove the approver’s permission. Submit the same approval twice. Let the approval expire while the job waits. In each case, specify whether the action proceeds, requires another review, or becomes a duplicate of an already completed operation. Those expectations belong in application tests that do not depend on a model producing the same wording twice.
For an initial draft-only release, most of this action machinery can wait. The important decision is to keep the draft and execution contracts separate now, so adding an action later does not accidentally turn a text-generation feature into an unrestricted operator. This also gives the interface a straightforward story: the system prepared a draft, a named person approved a particular version, and the application checked the action before executing it.
Commission the first complete path through your product
A useful first engagement connects one user action to one reviewed outcome inside the application you already operate. For the renewal summary, that means selecting an account, retrieving permitted records, producing a draft, recording a review, and making the operation visible to support. Agree on this boundary before discussing how many agents or models the solution needs. Keep automatic account changes outside the initial scope unless they are essential to the business question.
Ask for an integration map, a versioned input contract, an executable evaluation set, and a short operating guide alongside the code. The evaluation should cover who can request a summary, what happens when a source is unavailable, and how a repeated request is handled. Your product owner supplies the definition of a useful renewal draft; the implementation team translates it into a reviewable workflow. The person who will operate the feature should take part in the handover.
A design review is a sensible starting point when identity, data ownership, or the write boundary is unresolved. A scoped implementation fits when those decisions are already clear. Ongoing engineering capacity becomes useful when the feature needs continued evaluation and integration work across several releases. These are different commitments, and the first deliverable should make the next commitment easier to judge.
Release a reversible slice
Start with internal users and draft generation only. Compare drafts with the underlying records, measure review effort, and collect reasons for rejection. A useful launch gate includes permission tests, duplicate-request tests, provider outage behavior, and a working disable switch. Turning off AI should leave the existing manual renewal process available.
Only then consider additional actions, each with its own authorization and recovery design. The knowledge assistant guide examines retrieval boundaries in more detail. If you need help connecting this approach to your current stack, explore AI integration services or software implementation.
The frontend and business-API boundary is the next architectural choice to examine in Does Next.js need a separate business API?.