Adding an LLM feature to a mobile or web app

By Kallistesoft · · 3 min read

Illustration: an app, a server and a language model
Illustration: an app, a server and a language model

A feature powered by a large language model (LLM) is quick to demo and slow to ship: in between sit security, cost, latency, outages and privacy.

A server between the app and the LLM

The API key must never live in the app: mobile apps can be decompiled and websites expose their traffic. An embedded key eventually gets extracted, and you pay the bill.

The app never talks to the provider directly: the key stays on your server.AppYour serverLLM APISecret key: only here
The app never talks to the provider directly: the key stays on your server.

That server (a cloud function is often enough) authenticates the user, rate-limits, keeps the system prompt out of reach and lets you switch models without shipping a new app version. Keep the key in a secrets manager, never in the repository. The OWASP Top 10 for LLM Applications lists the typical risks, including prompt injection.

Cost: token budgets

Billing generally follows the volume of text processed. Without guardrails, a buggy loop can drain a budget overnight. Cap output and input size, set a per-user quota, configure spending alerts, summarize history instead of resending it, and keep the large model for hard tasks. Log the tokens of every call: without measurement you cannot tell what is expensive.

Latency and caching

A few seconds of empty screen feels endless. Stream the response (streaming documentation), show an indicator right away and allow cancelling. If you expect JSON, parse it only at the end.

Cache repeated requests (key = normalized request + prompt version), and use the provider's context caching by putting stable parts first. Never use a shared cache for personal data.

Failures and quality

Plan a timeout, retries with exponential backoff for temporary errors only, and a fallback (another model, a cached answer, or the non-AI behavior). Validate output against a schema: an LLM can return malformed JSON.

An LLM is non-deterministic: "it works on my example" proves nothing. Collect 30 to 100 realistic examples, including hostile ones, describe what a good answer must contain, rerun the set on every prompt or model change and read a sample by hand (detailed approach).

Privacy and store rules

As soon as text goes to a provider you are sending data to a third party: send only what is needed, tell users at the moment of use, read the provider's terms and update your privacy policy. Treat output as untrusted: never raw HTML, never executed. Label generated content and add a disclaimer for health, legal or financial topics.

Stores change: reread the rules before each submission. Apple's App Review Guidelines require accurate privacy disclosures and consent before sharing personal data; Google Play has its own rules for AI-generated content (check the Play Console help). Plan filters and a way to report content.

Pre-launch checklist

ItemDone
No key in the app or the repository☐
Authentication and rate limiting on the server☐
Caps, quotas and spending alerts☐
Streaming, timeout, fallback☐
Output validation and evaluation set☐
Consent, privacy policy, store declarations☐

Conclusion

Calling the model is the easy part; the rest is ordinary engineering applied with rigor. A reliable AI feature beats an impressive one that falls over on a Friday night.

Sources

Related reading