Skip to content
AI & ML

Integrating AI Features into Modern Web Applications

A practical guide to shipping AI features that survive contact with real users — streaming, cost control, failure handling and prompt injection.

11 min readBy Charm Thiekshana Perera

Adding an AI feature to a web app is easy. Adding one that stays fast, affordable and safe under real traffic is the actual work. Here is what I have learned shipping them.

Keep API keys on the server. Always.

This should be obvious and it is still the most common mistake. A model API key in client-side code is a key anyone can extract from the network tab and spend on your account. Every model call goes through your own backend route. No exceptions.

That server route is also where you enforce everything else that follows.

Stream, or the feature will feel broken

A model response can take several seconds. A spinner for eight seconds reads as a hang; the same eight seconds with text appearing token by token reads as fast. Streaming is not a nice-to-have — it is the difference between a feature people use and one they abandon.

Control cost at the door

Usage-based pricing plus an unauthenticated endpoint is a bill waiting to happen. At minimum:

  • Rate limit per user and per IP. Cheap to add, saves you from both abuse and runaway loops.
  • Cap input length. Truncate or reject oversized inputs before they reach the model.
  • Cache deterministic results. Identical questions do not need to be re-answered from scratch.
  • Match the model to the task. Classification and extraction rarely need your most capable model. Reserve that for work that genuinely benefits.

Design for the failure cases

Model APIs time out, rate limit and occasionally return something unusable. Decide up front what the user sees in each case. A feature that silently does nothing on failure trains people to distrust it.

And if you need structured output, validate it. Ask for JSON, parse it defensively, and validate against a schema before it touches your database. Do not assume the shape is correct because it usually is.

Treat user input as hostile

Prompt injection is the genuinely new security problem here. If your prompt includes user-supplied text — or worse, text fetched from a URL the user provided — that content can attempt to override your instructions.

The mitigations that help: keep untrusted content clearly delimited and labelled as data rather than instructions; never grant the model authority it should not have (if it cannot be allowed to delete records, do not give it a delete tool); and validate every tool call server-side against the user's permissions, not the model's request.

The rule I work to: treat model output the way you treat form input from a stranger.

Be honest in the interface

Label AI-generated content. Make it correctable. Do not present a probabilistic answer with the visual confidence of a database lookup. Users forgive a system that is occasionally wrong and says so; they do not forgive one that was confidently wrong and hid it.

Start narrow

The AI features that work are specific: summarise this document, extract these fields, draft a reply in this tone. Broad open-ended assistants are much harder to make reliably good. Pick one task, make it genuinely excellent, then expand.

  • AI
  • Machine Learning
  • APIs

Share this article

Chat on WhatsApp