◉CLAUDE LABJP
●CC 2.1.330 — Claude Code 2.1.330 fixes the multi-minute freeze right after compacting a long conversation●SONNET 4.5 — Sonnet 4.5 retires from the Claude API on 11/30, 8 days left. The suggested replacement is claude-sonnet-5-5●MCP 401 — Users report MCP servers that return 401 time out silently instead of showing an error●NEW — New: Desktop creates a worktree for every session? Three places to check●OPUS 4.5 — The 11/24 date for Opus 4.5 is a not-sooner-than floor, not a retirement notice●SCHEMA — Same schema, different shape: compare structured output from the Agent SDK and the Messages API●CC 2.1.330 — Claude Code 2.1.330 fixes the multi-minute freeze right after compacting a long conversation●SONNET 4.5 — Sonnet 4.5 retires from the Claude API on 11/30, 8 days left. The suggested replacement is claude-sonnet-5-5●MCP 401 — Users report MCP servers that return 401 time out silently instead of showing an error●NEW — New: Desktop creates a worktree for every session? Three places to check●OPUS 4.5 — The 11/24 date for Opus 4.5 is a not-sooner-than floor, not a retirement notice●SCHEMA — Same schema, different shape: compare structured output from the Agent SDK and the Messages API
Articles/API & SDK
⬡ API & SDK/2026-04-14Advanced

Claude API × Kotlin Multiplatform — Building Production AI Features for iOS and Android

Integrating Claude API with Kotlin Multiplatform (KMP) to ship production-quality AI assistant features on iOS and Android. Stream termination, mid-stream error events, retry strategies, expect/actual alignment, and testing — written from an indie developer's production experience.

kotlin-multiplatformkmpclaude-api84ios14android9mobile4aisdk5

✦ Premium Article

The hardest part of Kotlin Multiplatform isn't writing shared code — it's accepting that the gap between "runs in commonMain" and "works properly on both platforms" is often wider than you'd expect. You can unify your networking with Ktor, but getting Claude API's streaming responses to flow correctly to your UI thread, with proper error handling and retry behaviour, takes careful design.

I build iOS and Android apps on my own, and I have been adding Claude API as an in-app assistant to several of them. I reached for KMP for an unglamorous reason: maintaining two separate codebases had stopped being realistic for one person.

What I did not expect was how often iOS-only or Android-only code behaves differently once it moves into a KMP project — and how many of those differences compile cleanly, which is exactly what delays discovery.

This article is the record of that work. It covers what I actually did to take Claude API from "runs in KMP" to "runs reliably for 24 hours in production alongside AdMob mediation," with the design decisions, pitfalls, and operational numbers I encountered as an indie developer. My goal is straightforward: to save you from getting stuck in the places where I got stuck.

Project Structure and Design Philosophy

Here's the recommended folder structure for Claude API integration in KMP:

shared/
├── src/
│   ├── commonMain/
│   │   └── kotlin/
│   │       └── com/example/ai/
│   │           ├── ClaudeClient.kt       # API client
│   │           ├── ClaudeModels.kt       # Request/response models
│   │           ├── StreamingHandler.kt   # Streaming logic
│   │           └── RetryPolicy.kt        # Retry and error handling
│   ├── androidMain/
│   │   └── kotlin/
│   │       └── com/example/ai/
│   │           └── PlatformClient.android.kt  # Android-specific
│   └── iosMain/
│       └── kotlin/
│           └── com/example/ai/
│               └── PlatformClient.ios.kt      # iOS-specific
androidApp/
iosApp/

Why this structure: Communication logic — request building, JSON parsing, and retry handling — has no platform differences, so it belongs entirely in commonMain. Only genuinely platform-specific concerns (SSL certificate pinning, Keychain/EncryptedSharedPreferences storage) go into androidMain and iosMain. This separation means bug fixes and features land in one place, not two.

Step 1: Gradle Configuration and Ktor Client

// shared/build.gradle.kts
plugins {
    kotlin("multiplatform")
    kotlin("plugin.serialization")
    id("com.android.library")
}
 
kotlin {
    androidTarget()
 
    listOf(
        iosX64(),
        iosArm64(),
        iosSimulatorArm64()
    ).forEach {
        it.binaries.framework {
            baseName = "shared"
        }
    }
 
    sourceSets {
        commonMain.dependencies {
            // Ktor — the KMP-native HTTP client
            implementation("io.ktor:ktor-client-core:3.1.2")
            implementation("io.ktor:ktor-client-content-negotiation:3.1.2")
            implementation("io.ktor:ktor-serialization-kotlinx-json:3.1.2")
            implementation("io.ktor:ktor-client-logging:3.1.2")
 
            // kotlinx.serialization — JSON parsing
            implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.8.0")
 
            // kotlinx.coroutines — async/flow
            implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.10.1")
        }
 
        androidMain.dependencies {
            // Android: OkHttp engine (connection pooling, HTTP/2)
            implementation("io.ktor:ktor-client-okhttp:3.1.2")
        }
 
        iosMain.dependencies {
            // iOS: Darwin engine wraps NSURLSession
            implementation("io.ktor:ktor-client-darwin:3.1.2")
        }
    }
}

Why OkHttp for Android and Darwin for iOS: OkHttp isn't available on iOS. The Darwin engine wraps NSURLSession, which integrates naturally with iOS-specific SSL and proxy settings. On Android, OkHttp's connection pool and HTTP/2 support are significant practical advantages for real devices on mobile networks.

✦

Thank you for reading this far.

Continue Reading

What follows includes implementation code, benchmarks, and practical content we hope you'll find useful. This site runs without ads — server and development costs are supported entirely by members like you. If it's been helpful, we'd be truly grateful for your support.

WHAT YOU'LL LEARN
✦Why the obvious SSE implementation never fires `StreamEvent.Done`, how a Claude API stream actually terminates, and the measured difference after the fix
✦Operational knowledge not in the official docs: expect/actual signature alignment, Kotlin/Native GC tuning, SKIE / Swift Concurrency interop
✦A 14-item pre-release checklist covering API key protection, Crashlytics wiring, and behaviour on poor mobile networks
Secure payment via Stripe · Cancel anytime
✦

Unlock This Article

Get full access to the rest of this article. Buy once, read anytime. This site is ad-free — your support goes directly toward keeping it running.

or
Unlock all articles with Membership →
Share

Thank You for Reading

Claude Lab is ad-free, supported entirely by members like you. We publish practical guides daily with implementation code, benchmarks, and production-ready patterns. If you've found it useful, we'd love to have you on board.

  • ✦Copy-paste ready implementation code
  • ✦New advanced guides published daily
  • ✦$5/mo or $10 for lifetime access
View Membership →

Related Articles

⬡ API & SDK2026-05-13
Design Decisions Every Indie Developer Faces When Integrating Claude API into Mobile Apps
The design decisions indie mobile developers hit when integrating Claude API — model selection, async UX, context management, offline resilience, and cost control — with working code and a per-user cost table recalculated from official October 2026 pricing.
⬡ API & SDK2026-11-20
When the Claude API Only Says “Connection error.”, Follow the Exception Three Levels Down and Sort It Into Four Causes
The Python SDK raises the same APIConnectionError message for DNS, proxy, certificate, and timeout failures. Here is a script that walks __cause__, the real output from reproducing all four, and the order I fix them in.
⬡ API & SDK2026-09-22
Character counts won't predict PDF input tokens — bracket the cost per page before you send
How to estimate the input tokens of a PDF before sending it to the Claude API, by bracketing from page count rather than character count. Includes a measured 6.6x gap on a nine-page A4 document, the two separate increases that come with current models, and the count_tokens limitation that bites when you use file_id.