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
- Client: Kotlin Multiplatform with Compose: one code base for Android, iOS, the web (WebAssembly) and the desktop (JVM). The screens, view models and gateways to the service (with Ktor) are shared; only small pieces (audio, speech, files) are per platform.
- Shared types: the requests and responses are Kotlin classes in a module (core) that the client and the service both use, so both sides always use the same JSON serialization.
- Service: Kotlin as a Service! I deploy it to Cloudflare Workers (Kotlin compiled to JavaScript). Another cloud will work the same way (I tried!), with the Kotlin built for the JVM: Cloud Run, Azure Functions or AWS Lambda.
- Database: SQLite, as Cloudflare D1: users, content, images and the cost of every AI call. Other clouds have similar SQL databases.
- AI: the service calls the models: for answers and corrections, for generating pictures and for judging them. Anthropic, OpenAI and Gemini are interchangeable per purpose, by configuration.
- Hosting: Cloudflare serves both the service and the web app; the Android app can be downloaded from the same site.
- Authentication: Firebase (or any OAuth provider), with Resend for the e-mails (confirming an address, resetting a password).
- Map tiles (optional): MapTiler or TomTom, through the service, which caches them; the key stays on the server.
- Speech (optional): Azure's neural voices read texts aloud; the device's own voice is the fallback.
Components
A request, step by step
Logging in once, then a typical request of BabelMagic: a chat message, which the service corrects, answers and illustrates.
Not tied to one provider
| Part | Used now | Similar elsewhere |
|---|---|---|
| Service | Cloudflare Workers (Kotlin/JS) | Cloud Run, Azure Functions, AWS Lambda (Kotlin/JVM) |
| Database | Cloudflare D1 (SQLite) | Turso, Cloud SQL, Azure SQL, Amazon RDS |
| Web app and download | Cloudflare static assets | Firebase Hosting, Azure Static Web Apps, S3 + CloudFront |
| AI | Anthropic, OpenAI, Gemini | any provider with an HTTP API, chosen per purpose |
| Authentication and mail | Firebase, Resend | any OAuth provider, any mail service |
| Map tiles | MapTiler, TomTom | any tile provider; cached by the service |
| Speech | Azure neural voices | Google 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
- The apps hold no keys: every external call goes through the service.
- The authentication follows the state-of-the-art practice (OAuth).
- Guests can try the app without an account but with authentication.
- Each user has a budget; the service checks it before each AI call and stores the cost of each call.
- AI uses a cheap model where it is good enough, a stronger one for heavier tasks.
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.