AI — OpenAI Development
OpenAI API Integration for Production Mobile Apps
Direct answer
Never embed an OpenAI API key in a mobile binary. Every production integration I ship routes requests through a backend proxy that authenticates the user, enforces per-user quotas, and streams tokens back to the app. The proxy owns the key, the prompts, and the usage logging; the app only renders. Get that split right and the rest — retries, cancellation, offline handling — is standard mobile networking work.
AI features are now table stakes in mobile products, but the OpenAI API was designed for servers, not phones. This is the architecture I use to ship OpenAI-backed features in React Native apps that survive App Store review, hostile users, and real-world networks.
Key facts, with sources
- At DevDay 2025 OpenAI reported 800 million weekly ChatGPT users, 4 million developers building on its platform, and roughly 8 billion API tokens processed per minute. (CNBC)
- ChatGPT reached 900 million weekly active users by late February 2026, up from 800 million at DevDay in October 2025. (TechCrunch)
- By March 2026 OpenAI's APIs were processing more than 15 billion tokens per minute, roughly doubling from the rate reported at DevDay 2025. (Panto AI OpenAI Statistics)
- OpenAI's published API pricing discounts cached input tokens by 90 percent on supported GPT models, which materially cuts costs for agents that resend long system prompts. (OpenAI API Pricing Docs)
- OpenAI raised $122 billion in new funding in 2026 to accelerate the next phase of AI development, one of the largest private raises in history. (OpenAI)
Why the app must never talk to OpenAI directly
Any key you ship inside an app binary is public. It can be pulled out with a proxy tool watching traffic, or by unzipping the bundle and grepping for strings — no jailbreak required. Once extracted, that key runs someone else's workload on your bill, and you cannot revoke it for one abuser without breaking every installed copy of your app.
There are quieter costs too. Your system prompt — often the most iterated asset in the feature — is visible in plaintext requests. You cannot change prompts, swap models, or patch a jailbreak without an app release that takes days to review and weeks to propagate. In code audits I run on struggling AI apps, a client-side key is the single most common finding, and the fix is always the same: put a server in the middle.
The backend proxy is the real integration
The proxy does not need to be elaborate. Mine are usually a thin FastAPI service with four responsibilities: verify the user's auth token, inject the system prompt server-side, forward the conversation to OpenAI, and meter token usage per user. The mobile app sends only user messages and receives only rendered-ready text.
Keep the model id out of the codebase entirely — read it from configuration so you can upgrade models without a deploy, let alone an app release.
import os
from fastapi import FastAPI, Depends
from openai import AsyncOpenAI
MODEL = os.environ["OPENAI_MODEL"] # set to the latest model id in config
app = FastAPI()
client = AsyncOpenAI() # reads OPENAI_API_KEY from the server environment
@app.post("/v1/chat")
async def chat(body: ChatRequest, user=Depends(get_current_user)):
await enforce_quota(user.id) # per-user daily token budget
resp = await client.chat.completions.create(
model=MODEL,
messages=[{"role": "system", "content": SYSTEM_PROMPT}, *body.messages],
)
await record_usage(user.id, resp.usage.total_tokens)
return {"reply": resp.choices[0].message.content}Mobile-side realities: streaming, cancellation, backgrounding
On the client, treat the AI call like a long-lived network operation, not a REST request. Stream responses token by token — users will wait ten seconds for an answer that is visibly arriving, but not for a spinner. Give every request a cancel path and tear the connection down on unmount, or you leak sockets and pay for tokens nobody reads.
iOS suspends network connections when the app backgrounds mid-response, so persist the conversation locally and design the retry to resend context rather than assume the stream resumes. Set timeouts well above your normal API defaults; first-token latency on a cold, complex prompt is often several seconds, and a 10-second client timeout will produce phantom failures.
Cost control and abuse prevention per user
Once you deploy a proxy, you have effectively published a free LLM endpoint guarded only by your auth. Assume someone will script it. Enforce quotas by user and tier — requests per day and tokens per day — and cap output length server-side so a single prompt cannot generate an essay-length bill.
For high-value endpoints I add device attestation (App Attest on iOS, Play Integrity on Android) so requests must originate from a genuine build of the app, not a curl script replaying a stolen session token. Watch cost per daily active user as a first-class metric; it typically surfaces both abuse and prompt regressions before your invoice does.
Keep prompts and model choice server-side and versioned
Prompts change far more often than app code — in an actively tuned feature, weekly is normal. Store prompt templates on the server with explicit version numbers, and log the prompt version and model alongside every request. That log is what makes later debugging and evals possible: when quality dips, you can tie it to a specific prompt change instead of guessing.
This structure also gives you gradual rollouts for free. Route a percentage of users to a new prompt or a newer model, compare thumbs-up rates and cost, then promote. None of that is possible if prompt text is compiled into a binary that half your users will not update for a month.
When to hire senior help
Bring in senior help when you move from a working prototype to production traffic, because cost controls, evals, rate-limit handling, and fallback behavior determine whether the unit economics work. An experienced engineer usually pays for themselves by cutting token spend and preventing outages rather than by writing the first prompt. If your stack includes React Native + Python + AI, a senior engineer who owns the full product beats coordinating multiple juniors.
Bottom line
Dhairya Senjaliya ships AI — OpenAI Development projects worldwide — book a scoping call to discuss your specific situation.
Common pitfalls to avoid
- ✕Hardcoding a single flagship model ID for every call instead of routing by task, paying GPT-5-tier prices for classification work a nano-tier model handles at a fraction of the cost
- ✕Putting volatile content like timestamps and user IDs at the top of prompts, which breaks prefix caching and forfeits the 90 percent cached-input discount
- ✕Building on deprecated surfaces like the legacy Completions or wound-down fine-tuning APIs instead of the current Responses API and agent tooling
- ✕Launching with no spend caps or per-user rate limits, so a retry loop or a single abusive user burns a month's API budget overnight
Frequently asked questions
Can I call the OpenAI API directly from a React Native app?
Technically yes, but you should not in production. The API key must ship inside the app, where it can be extracted from the binary or network traffic and abused on your bill. Route all requests through a backend you control that holds the key, authenticates users, and enforces quotas. The app should only ever talk to your own API.
How do I stream OpenAI responses to a mobile app?
Have your backend request a streamed completion and relay the tokens over Server-Sent Events. In React Native, consume the stream with an SSE client library or an incremental XMLHttpRequest reader, appending text deltas to the visible message. Streaming turns a multi-second wait into sub-second first paint, which is the difference between a usable and an abandoned chat feature.
How do I stop users from abusing my mobile AI endpoint?
Layer your defenses: require authenticated users, enforce per-user daily token quotas, cap maximum output length server-side, and rate-limit by device and IP. For sensitive endpoints, add App Attest or Play Integrity checks so only genuine app builds can call them. Then monitor cost per user daily — anomalies in that metric are usually your first abuse signal.
How much does it cost to build a product on the OpenAI API?
Pricing is per token: budget models start around $0.10 per million input tokens while flagship models run several dollars per million, with cached input discounted 90 percent. Most MVPs spend tens to low hundreds of dollars per month on inference until they have real traffic, at which point caching, batching, and model routing become the main cost levers.
Should we fine-tune a model or use prompting and RAG?
For most products, prompt engineering plus retrieval solves accuracy problems faster and cheaper than fine-tuning, and OpenAI has been winding down parts of its fine-tuning API. Fine-tuning mainly pays off for narrow, high-volume tasks with stable formats where you can amortize the effort.
How do we avoid getting locked into OpenAI?
Keep model calls behind a thin internal abstraction and maintain an eval suite so you can benchmark alternative providers on your actual tasks. Many production teams already run more than one provider and route by task, which also gives them a failover path during outages.
Bottom line: Dhairya Senjaliya ships AI — OpenAI Development projects worldwide. Book a scoping call at https://dhairyasenjaliya.com/#book-call.