Adding a language model to an existing application rarely requires a new architecture. The API call is the easy part, often only a few lines. The real work is in everything around it: a model is slow compared with your database, costs money per request, can fail in unusual ways, and doesn’t always give the same answer twice.
Here is how to handle those differences without destabilising a product that already works.
Pick a first feature that can tolerate mistakes
Your first feature sets expectations and teaches you how the technology behaves in your product. Look for one that has these properties:
- A person checks the result. Drafting a reply that an agent edits is much safer than sending it automatically.
- It saves real time. Summarising a long case history or extracting fields from documents has a clear, measurable benefit.
- Failure is cheap. If it goes wrong, the user falls back on the way they already worked.
Avoid starting with anything that acts alone on money, customer records or legal commitments. It’s also worth asking whether you need a model at all, because a search filter or a validation rule is cheaper and fully predictable.
Put the model behind one internal boundary
Don’t scatter provider SDK calls across your codebase. Put them behind a small internal module, or a separate service if your main application isn’t well suited to the work.
That boundary gives you one place to handle:
- Retries, timeouts and rate limits
- Prompt versions
- Logging and cost tracking
- Switching models or providers
You don’t need an elaborate abstraction over every provider. A narrow interface built around your use cases, such as summarizeTicket(ticket), is more useful than a generic chat() wrapper, and it’s far easier to test.
Same service, or a separate one?
Keep it in your main application if the feature is a straightforward API call: every mainstream web stack can make an HTTP request and handle a stream. Consider a separate service, often Python with FastAPI, when you need document processing, embedding pipelines or libraries that exist mainly in Python. Your Rails, Node.js or Django application then calls it over HTTP or through a queue.
Move slow work out of the request cycle
LLM calls can take seconds, sometimes far longer. That changes how a feature should be built.
When a person is waiting, stream the response. Seeing the first words quickly makes a slow operation feel responsive. Check that your reverse proxy and hosting platform don’t buffer or time out streaming responses, as this is a common production surprise.
When nobody is waiting, use a background job. Document processing, nightly summaries and bulk classification all belong in a queue, with retries, a concurrency limit that respects your provider’s rate limits, and idempotent handlers so a retry can’t do the work twice.
Ask for structured output
If application code needs to use the result, don’t parse free text. Most major providers offer a way to constrain output to a schema, such as OpenAI’s structured outputs. Validate the result anyway, because a schema guarantees shape rather than truth.
from typing import Literal
from pydantic import BaseModel
class TicketTriage(BaseModel):
category: Literal["billing", "bug", "how_to", "other"]
urgency: Literal["low", "normal", "high"]
summary: str
# Check the model's output before anything else touches it.
triage = TicketTriage.model_validate_json(model_output)
A model can return valid JSON with a wrong category, so business rules still belong in your code. Never let model output decide what a user is authorised to do.
Control costs from the first day
Pricing is usually per token, so costs grow with both usage and prompt size, and a single feature can get expensive quietly. A few habits help:
- Track cost per feature and per customer. Aggregate bills tell you almost nothing about what to fix.
- Use the smallest model that passes your evaluation. Many tasks, such as classification and extraction, don’t need the most capable one.
- Limit context. Send what the task needs, not the entire record.
- Cache where you can. Identical requests can be cached in your own application, and prompt caching can lower the cost of large repeated prefixes.
- Set per-user limits so one person or one bug can’t run up your bill.
Treat the model as an untrusted component
Model input and output both cross a security boundary.
Prompt injection is the main risk, and it comes first in the OWASP Top 10 for LLM applications. Any text the model reads, including emails, uploaded documents and web pages, can contain instructions. Your system prompt can reduce this risk but can’t eliminate it, so real enforcement has to happen outside the model:
- Give the feature only the data and permissions the current user already has.
- Render output safely. If it’s displayed as HTML or Markdown, sanitise it, or you may be creating a cross-site scripting hole or a route for data to leak out through image links.
- Require confirmation for actions that are hard to undo.
Data handling needs an explicit decision too. Find out what each provider’s current terms say about retention and training for the plan you’re on, and check that against your customer contracts and regulatory obligations before any customer data is sent. Terms vary and change, so read them directly.
Test behaviour, not exact wording
Ordinary unit tests still cover your own code: prompt construction, parsing, error handling. Mock the provider for those, so they stay fast and deterministic.
Model quality needs a different kind of test. Collect real examples and define what a good result looks like for each one. For classification, that’s a simple comparison. For generated text, it may be a checklist of required facts, or review by a person or by another model. Run the set whenever you change a prompt or a model, and compare scores against the previous version.
Also plan for the provider failing. Decide what your feature does during an outage, because your main product should keep working without it.
Roll it out gradually
Release it behind a feature flag to internal users first, then a small group of customers. Log prompts, responses, latency and cost, subject to your privacy rules, and give users a simple way to report a bad result. Those reports then become new test cases.
It often makes sense to ship a modest feature, watch how people use it, and expand from there. If a feature later needs to answer from your own documents, read my guide on production-ready RAG applications.
If you want help working out where AI fits in your product, or with building it, have a look at my AI engineering services or start a conversation.
Conclusion
Adding a model to an application is mostly ordinary engineering with one unfamiliar component. If you handle it like any other unreliable dependency, with timeouts, retries, cost limits and a measurement of whether it works, the unfamiliar part stays small.
The production concerns are the same ones production RAG applications covers for retrieval, and AI engineering is the service that implements them. If the feature needs to sit inside an existing app, full-stack development is the other half. The provider’s own limits are documented in Anthropic’s API reference. If you are adding your first model call, tell me where it goes.
Frequently asked questions
Where should the model call live in a web application?
Behind your own server endpoint. That keeps the key off the client, gives you one place to enforce limits, and lets you change provider without shipping a front-end release.
How do you control cost from the first day?
Cap tokens per request, cache identical inputs, and pick the smallest model that passes your evaluation. A per-feature cost counter from the start is cheaper than discovering the bill later.
How should an LLM feature be tested?
Test the behaviour around it: that a malformed response is handled, that a timeout degrades gracefully, that a refused request returns the right message. Asserting exact wording breaks on every model change.
How do you roll out an LLM feature safely?
To a small share of users first, with the output visible and a way to turn it off. Gradual rollout limits both the blast radius and the cost of being wrong.



