Claude API Integration in Mobile Applications
Claude API from Anthropic is an alternative to OpenAI with several technical differences important for mobile development. Large context window (200k tokens for claude-3-5-sonnet), native vision support in the same Messages API, excellent Russian language support. Mobile integration follows the same security principles as any LLM API.
Secure Key Management
Anthropic API key — sk-ant-.... One rule: backend only. Mobile client communicates with your proxy server, which adds x-api-key header and forwards to api.anthropic.com.
Proxy architecture: any backend — Laravel, FastAPI, Cloudflare Worker. Minimal Cloudflare Worker implementation takes ~30 lines and handles both regular and streaming requests. Workers cold start is 5–10 ms, adding no noticeable latency.
On mobile client side: JWT authentication on your proxy. Proxy verifies token, applies rate limiting (e.g., 20 requests/minute per user) and logs input_tokens/output_tokens for cost analytics.
Messages API: Key Differences from OpenAI
Anthropic Messages API differs from OpenAI Chat Completions in several details:
- System prompt — separate
systemfield, not an array element inmessages. Important: keep system context insystem, not inmessages[0]withrole: "system". - Roles: only
userandassistant(nosystemin messages). - No old-style
function_calling— insteadtoolswithinput_schemain JSON Schema format.
{
"model": "claude-haiku-4-5",
"max_tokens": 1024,
"system": "You are an assistant in a mobile application ...",
"messages": [
{"role": "user", "content": "Explain this document"},
{"role": "assistant", "content": "Of course, ..."},
{"role": "user", "content": "What does section 3 mean?"}
]
}
Mobile Streaming
Claude API supports SSE-streaming with "stream": true. Format differs slightly from OpenAI:
content_block_delta event carries delta.text — the token. message_stop event marks end of stream. On iOS, parse via URLSessionDataDelegate, similar to ChatGPT. On Android — OkHttp EventSource.
Delta events arrive frequently (every 10–50 ms during active generation). Buffer before UI update: update @Published var streamText not on every event, but through Throttle publisher (iOS) or distinctUntilChanged + debounce (Android Flow).
Vision: Image Analysis
Claude 3+ natively supports images in messages. Format:
{
"role": "user",
"content": [
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/jpeg",
"data": "<base64>"
}
},
{"type": "text", "text": "What is shown in the photo?"}
]
}
On mobile: compress image before sending. JPEG quality 70, max size 1568×1568 (API limit). Camera RAW — 10–15 MB — not practical: more expensive in tokens, slower. Resize + compress via UIGraphicsImageRenderer (iOS) or Bitmap.createScaledBitmap + compress(Bitmap.CompressFormat.JPEG, 70, out) (Android).
Conversation Management
Claude holds long context well — 200k tokens. But for mobile chat this is overkill and expensive. In practice, sliding window of last 20 messages is sufficient for most tasks.
For specialized apps (legal assistant, medical reference) — RAG (Retrieval Augmented Generation): store documents in vector DB on backend, supplement system prompt with relevant fragments on each request. This doesn't increase history size but gives access to large knowledge base.
Handling Anthropic API Errors
529 Overloaded — servers overloaded, exponential backoff. 400 with error.type = "invalid_request_error" — usually max_tokens exceeded or invalid content format. 401 — invalid key on proxy. Log all errors with request ID (x-request-id response header) — needed for Anthropic support.
Case study: legal assistant for B2B app. claude-3-5-sonnet-20241022, contract analysis. User photographs contract page, assistant highlights key terms and risks. Image resize to 1200px on long side, JPEG 80. Average request: 2400 input tokens (image ~1800 + text 600) + 800 output. Streaming — first words appear in 350 ms. Users don't notice latency with streaming vs "blank screen 4 seconds" without it.
Timeline
Basic integration with streaming, conversation context, and backend-proxy — 3–5 working days. With image support and RAG — 1–2 weeks. Cost is calculated individually.







