Architecture

One Kotlin code base for four platforms, a small service in the cloud, and AI providers and other services behind it. This is my current ideal architecture and how I build my apps. babel-magic is the example here; wine-atlas, canopy-history-atlas, hexcavation and my unpublished tutorial project follow exactly the same pattern.

The parts

Components

Component diagram: the clients, Cloudflare with the web app, the service and the database, and the external services Clients Kotlin Multiplatform Shared app Compose UI · view models gateways to the service core request and response types, also used by the service Android APK iOS native Web WebAssembly Desktop JVM one code base, four apps Cloudflare or another cloud Web app (static files) page · Wasm app · Android download Service (Worker) Kotlin, compiled to JavaScript login · budget per user prompts · costs of each AI call Database (D1, SQLite) users · content · images · costs also used by the service: Images resizing Cache map tiles Turnstile human check Rate limit guests AI providers Anthropic · OpenAI · Gemini answers · corrections pictures · judging pictures Authentication Firebase, or any OAuth provider Mail Resend Map tiles (optional) MapTiler or TomTom Speech (optional) Azure neural voices loads HTTPS, JSON Ktor
The clients talk only to the service; the service holds every key and talks to the database and the external services. Dashed: optional. Dotted: possible, not used yet (mails are sent by Firebase now).

A request, step by step

Logging in once, then a typical request of BabelMagic: a chat message, which the service corrects, answers and illustrates.

Sequence diagram: logging in, then sending a chat message Log in, once A chat message App Service Database Firebase AI provider POST /login: e-mail, password check the password ID token access and refresh token POST /chat: text + token user, budget left? correct the learner's text answer as the chat partner draw the scene store messages, picture, costs correction, answer, picture
Each request includes the token; the service checks it and the user's budget before it calls a model, and records what each call cost. Dashed arrows: replies.

Not tied to one provider

PartUsed nowSimilar elsewhere
ServiceCloudflare Workers (Kotlin/JS)Cloud Run, Azure Functions, AWS Lambda (Kotlin/JVM)
DatabaseCloudflare D1 (SQLite)Turso, Cloud SQL, Azure SQL, Amazon RDS
Web app and downloadCloudflare static assetsFirebase Hosting, Azure Static Web Apps, S3 + CloudFront
AIAnthropic, OpenAI, Geminiany provider with an HTTP API, chosen per purpose
Authentication and mailFirebase, Resendany OAuth provider, any mail service
Map tilesMapTiler, TomTomany tile provider; cached by the service
SpeechAzure neural voicesGoogle Cloud or Amazon Polly text-to-speech; the device's voice

Costs

I chose Cloudflare for try-outs, new projects and my tutorial: for low traffic and modest demands it is completely free, the service, the database and the hosting included. For higher demands a $5,= rate per month will suffice to provide many users.

Earlier I also used Azure, which works well too: for low traffic it costs around $0.35 per month. It is paid per use, so in the rare case of a bug (code that keeps calling itself, for example) a month can cost more than planned; a budget alert in Azure warns early.

The AI calls are paid per use, whichever cloud runs the service; the budget per user keeps them within limits.

Principles

Summary

A state-of-the-art architecture with Kotlin everywhere: the apps on four platforms, the service and the types they share are all written in Kotlin. The cloud and the providers behind the service can be exchanged without changing the apps.