Cloud, Data & AI

Adding AI Features to an Existing Application: Where the Model Calls Belong

The model call is a few lines of code. Where it lives, what data it sees and what happens when it is slow or wrong decide whether an AI feature helps or hurts an existing application. These are the architectural choices we make first.

Colleagues gathered around a laptop in an office, one leaning in to share a comment while another talks

You have a working application, built over years in Laravel, Django, .NET or Node.js, and you want to add AI to it: summarise a case history, draft a reply, classify incoming requests, answer questions over your own records. The model call itself is a few lines of code. The hard decisions are around it. Where does that call live, what data does it see, what happens when it is slow or wrong, and how do you stop it spreading across fifty controllers?

Most of our AI integration and LLM development work is exactly this: adding model-powered features to applications that already exist and must keep working. These are the architectural choices we make early.

Never Call the Model From the Browser

It is tempting, especially in a single-page app, to call a model provider straight from the front end. Don't. The API key would be visible to anyone who opens the developer tools, and you would lose control over who calls it, how often and with what data. Model calls belong on the server, behind your existing authentication, where you can apply permissions, limits and logging. The browser calls your own API; your API calls the model.

Give AI Its Own Module With a Narrow Interface

The single most useful decision is to put every model call behind one internal interface. In practice that is a module or service with functions named after business tasks, such as summarising a ticket, classifying an enquiry or drafting a reply, rather than a generic "send prompt" helper used all over the codebase.

Inside that module live the things that change most often:

  • Prompts, kept in version control alongside the code, with the prompt version recorded against each output.
  • The provider and model, so moving between Claude, OpenAI, Gemini or a self-hosted model is a configuration change plus an evaluation run, not a rewrite.
  • Output schemas. Ask for structured output and validate it, for example with Pydantic in Python or Zod in TypeScript, before anything else in the application sees it.
  • Timeouts, retries and fallbacks, defined once rather than reinvented per feature.

Whether this is a module inside the existing codebase or a separate service depends on your stack. If the application is in PHP or .NET and the AI work needs Python libraries for documents or embeddings, a small FastAPI service beside the main application is often the cleanest boundary. If the stack is already Node.js or Python, a well-separated module is usually enough.

Synchronous, Streamed or Queued

Model calls take seconds rather than milliseconds, and their timing varies. Match the pattern to the feature:

  • Streamed responses suit interactive features where a person is waiting, such as an assistant panel or a drafted reply. Text appears as it is generated, so the wait feels short.
  • Queued background jobs suit everything that does not need an immediate answer: classifying incoming emails, summarising documents on upload, enriching records overnight. The request returns at once, a worker calls the model, and the result is saved and shown when ready.
  • Plain synchronous calls only for short tasks where a person can wait a moment and failure has an obvious fallback.

Your existing queue is usually the right home for background work, whether that is Laravel queues, Celery, BullMQ or Sidekiq. Queued jobs also make it easy to rate-limit calls to the provider and retry transient errors without users noticing.

Retrieval Must Respect Permissions

Many useful features answer questions over your own data using retrieval-augmented generation: find the relevant records or passages, then let the model answer from them. In an application with users, roles and tenants, the retrieval step is a security boundary. If a user cannot open a document in the application, the AI feature must not retrieve it for them either.

The safest approach is to filter by the same permission rules the application already uses, at query time, before anything reaches the model. Keeping embeddings in your existing PostgreSQL database with pgvector, next to the records they describe, makes those filters straightforward; a separate vector database needs the permission metadata copied across and kept in sync. Our database development work often starts here, and keeping the index current is covered in our post on keeping an AI assistant accurate as documentation changes.

Treat retrieved content as untrusted input. A document a customer uploaded can contain instructions aimed at the model, so model output should never trigger actions without the application's usual checks.

Data, Cost and Observability

  • Send only what is needed. Strip or mask personal data the task does not require, check the provider's data retention terms, and choose a processing region that suits your obligations.
  • Log every call with the feature, prompt version, model, token counts, latency and outcome, but think carefully before logging full inputs that contain personal data.
  • Budget per feature and per tenant, with limits enforced in your module rather than discovered on the provider's invoice.
  • Keep an evaluation set for each feature: real examples with known good outputs, run whenever a prompt or model changes.
  • Put features behind flags, so they can be rolled out to a few users first and switched off instantly.

Keep a Person Where It Matters

The features that work best in existing applications usually draft, suggest or sort, and leave the decision to a person: a suggested category the user can change, a drafted reply they edit before sending, a summary with links back to the source records. Show where the output came from, make accepting or correcting it one click, and record what users change, because those edits are the best evidence of whether the feature helps. Our article on AI use cases that actually pay off lists the kinds of features where this works well.

If you have an application and a feature in mind, tell us what it should do and what your stack looks like. We will show you where the model calls should sit and what it takes to ship the feature safely.

Frequently Asked Questions

Do we need to rebuild our application to add AI features?

No. Most AI features are added beside existing code: a module or small service the application calls, plus background jobs for slower tasks. The existing data model, authentication and permissions stay as they are, and the AI layer uses them rather than replacing them.

Will our data be used to train the provider's models?

That depends on the provider, the specific API and the terms of your plan, and terms change, so check the current data usage and retention policies for the exact service you use. Business API terms commonly exclude training on customer data by default. Where data must not leave your infrastructure at all, a self-hosted model is an option.

Which AI provider should we choose?

Choose on an evaluation with your own examples rather than general benchmarks, weighing quality, latency, cost, data terms and regions. Because we keep the provider behind a single internal interface, the choice is not permanent: you can test an alternative against the same evaluation set and switch with a configuration change.

How do we stop an AI feature producing wrong or harmful output?

Design for it. Validate structured output against a schema, ground answers in retrieved records with links to the source, keep a person in the loop for decisions that matter, and never let model output trigger actions without the normal permission checks. An evaluation set run on every change catches regressions before users do.

Can AI features work with a PHP or .NET application?

Yes. Provider APIs are plain HTTPS with official or community SDKs for most languages, so simple features can sit directly in PHP or C#. Where the work needs Python libraries for documents, embeddings or local models, we add a small Python service beside the main application with a clear API between them.

How We Can Help

Services Related to This Article

The work we do with the technologies discussed above — with pricing, process and real examples on each page.

  • AI Integration & LLM Development

    Practical AI in your product — assistants, document understanding, search over your own content, and OCR or classification pipelines built with Python and modern LLM APIs.

  • API Development & Integration

    Custom REST and GraphQL API development plus integrations with CRMs, ERPs, payment gateways, shipping carriers and marketing platforms.

  • Python, Django & FastAPI Development

    Python web applications and APIs built with Django, FastAPI or Flask — plus automation, data processing, OCR and document pipelines.

  • PHP & Laravel Development

    Laravel web applications, custom PHP development, admin panels, APIs and legacy PHP upgrades — secure, well-structured and easy to maintain.

In Practice

Case Studies on This Topic

Projects where we put these ideas to work, each with a live demo.

  • Man at a laptop in an office — Helm Software
    • Python
    • FastAPI
    • Vector database

    AI Integration

    Support Assistant Grounded in Product Documentation

    A retrieval-based assistant answering customer questions from the vendor’s own documentation, with citations.

  • Accounting Finance — Ledgerline Accountants
    • Python
    • FastAPI
    • Tesseract OCR

    Data Automation & OCR

    Invoice Processing & OCR Automation

    A Python pipeline that reads 18,400 supplier documents a month and posts 93% of them without a person keying anything.

  • Containers Bins — Stanmore Haulage
    • Azure Document Intelligence
    • Python
    • FastAPI

    Data Automation & OCR

    Proof-of-Delivery Capture with OCR for a Haulage Firm

    Photos of signed delivery notes turned into invoice-ready data automatically, cutting the gap between delivery and invoice from six days to one.

Keep Reading

Let’s Connect

Let’s Build Something Amazing Together

Share your project details and our experts will get back to you within one business day with the best solution — free consultation, no obligation.