Integrating LLMs into a Mobile App: Architecture and Costs
By the apprat.io AI Code Engineering Team 7 min read
In short: Integrate an LLM into a mobile app through your own backend, never by embedding provider keys in the app. The backend handles authentication, rate limits, prompt logic and cost control, while the app focuses on streaming responses, offline behavior and clear privacy disclosures.
Adding an AI feature to a mobile app is easy to demo and surprisingly hard to run well. Keys leak, costs spike, answers arrive slowly on a train, and the store asks how you handle user data. This article lays out an architecture that avoids the common failures.
The golden rule: do not call the model from the app directly
A mobile app is distributed to users' phones, and anything inside it can be extracted. If a provider API key ships in the app, someone will eventually find it and use it at your expense. The safe pattern is a thin backend of your own that sits between the app and the model provider.
Reference architecture
- App: collects user input and displays streamed output.
- Your backend: verifies the user's identity, applies rate limits and builds the final prompt.
- Model provider: receives the request from your server using a key stored only there.
- Logging and analytics: usage per user and feature, with care for personal data.
- Optional data layer: documents or records the model may reference, filtered by the user's permissions.
Design decisions that shape the experience
- Streaming: showing text as it arrives makes slow responses feel faster.
- Latency and failure: design for timeouts, retries and clear error messages instead of endless spinners.
- Offline behavior: decide what the feature does without a connection and say so in the interface.
- Guardrails: constrain topics, validate outputs and avoid showing unchecked model output as fact in high-stakes contexts.
- On-device options: smaller models can run on the device for some tasks, which helps privacy and offline use but has quality and size limits; evaluate carefully.
What drives cost
Model usage is typically billed by the amount of text processed, and providers change prices and models frequently, so check current vendor pricing when you budget. Your levers are mostly independent of the vendor:
- Keep prompts concise and send only the context that is needed.
- Cache answers to repeated questions where it is appropriate.
- Use smaller or cheaper models for simple tasks and reserve stronger ones for hard cases.
- Set per-user limits and monitor for abuse.
- Track cost per feature so you can see what is worth it.
Privacy, store review and compliance
Sending user text to a model provider is data processing. You need to tell users, reflect it in your privacy policy and store privacy disclosures, and have the right agreements with the provider. Apple and Google both have guidelines that touch AI-generated content and user data; verify the current requirements before you submit. If your app serves EU users, the GDPR applies, and the EU AI Act may add transparency duties depending on the use case. Features that look like thin wrappers around a chatbot can also draw review attention, a point covered in App Store Guideline 4.2 and AI-built apps.
Testing an AI feature properly
Unlike ordinary code, model output varies. Create a set of representative inputs and review the results by hand, including bad inputs, long texts and attempts to make the feature misbehave. Test on real phones with weak connectivity, because streaming and timeouts behave differently there than on office Wi-Fi.
Prompts are code too
Prompts, system instructions and tool definitions determine how the feature behaves, so manage them like code: keep them in version control on the backend, review changes, and test them against your set of example inputs before release. Keeping them on the server also lets you adjust behavior without waiting for store review of a new app version, which is a practical advantage. Avoid putting business rules only in a prompt when ordinary code can enforce them, since code is deterministic and prompts are not. For example, validate that a model-suggested value falls within an allowed range in your backend rather than trusting the model to stay within bounds.
How apprat.io helps
AI features are built by senior AI Code Engineers at jocapps GmbH with coding agents doing much of the implementation, while engineers design the backend, review every change and test on iOS and Android hardware. Store submission, including privacy disclosures, is part of the service, and GDPR and EU hosting options are available. If you want to talk through an AI feature for your app, book a call for free, or read how AI agents in business apps extend the pattern.
Frequently asked questions
Can I call an LLM API directly from my mobile app?
It is not recommended, because keys inside the app can be extracted. Route requests through your own backend, which holds the key, checks users and enforces limits.
How do I keep LLM costs under control?
Keep prompts short, cache repeated answers, use cheaper models for simple tasks, set per-user limits and track cost per feature. Check the provider's current pricing.
Do I need to disclose AI use to users and stores?
You should describe what data is sent to AI providers in your privacy policy and store privacy disclosures. Verify the current Apple and Google requirements before submission.
Can LLMs run on the phone itself?
Smaller models can run on-device for some tasks, which helps with privacy and offline use, but with limits in quality and app size. Evaluate for your specific use case.