DEV Community

nRouter
nRouter

Posted on

Built for the main thread: notes on nRouter's Android SDK

An Android LLM client has constraints a server SDK never meets. The process can be killed mid-request. A rotation can recreate the screen under an in-flight call. The APK is a public download, so a key baked into it is a published key. And the main thread must never wait on the network. The nRouter Android guide is written around those four facts, and the SDK's shape follows from them.

Full guide: https://nrouter.ai/docs/sdks/android

Setup

The SDK ships as an AAR, not a plain JVM jar:

// repositories: mavenLocal() + mavenCentral()
dependencies {
    implementation("ai.nrouter:nrouter-sdk-android:3.1.2")
}
Enter fullscreen mode Exit fullscreen mode

Minimum SDK is 21. The client is created from a Context, and the guide's pattern is to hold it in an AndroidViewModel:

class ChatViewModel(app: Application) : AndroidViewModel(app) {
    private val client = NRouterAndroid.create(app.applicationContext, key)

    fun ask(prompt: String) {
        viewModelScope.launch {
            val result = client.chatCompletions(
                JSONObject()
                    .put("model", "nrouter/auto")
                    .put("messages", JSONArray().put(
                        JSONObject().put("role", "user").put("content", prompt)))
                    .put("nrouter_optimize", "balanced")
            )
            val text = result.body.getJSONArray("choices")
                .getJSONObject(0).getJSONObject("message").getString("content")
            val cost = result.meta.cost
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

The ViewModel placement is doing real work: it survives configuration changes, and viewModelScope cancels in-flight calls when the screen is done. Calls are main-thread-safe — the network work happens off the UI thread.

What comes back

chatCompletions returns body plus meta, extracted from the gateway's response headers. The field that matters most on mobile is result.meta.cost: per-call cost as a typed value, in the app, where trials, budget warnings and usage tallies actually live.

Routing rides in the request JSON: "model": "nrouter/auto", "nrouter_optimize" (quality, balanced, cost), and under cost, "nrouter_cost_band" (low, mid, high). A keystroke-level autocomplete and a long summary are different economic objects; the client can say so per call.

Streaming is a cold Flow

messagesStream returns a cold Flow — nothing is requested until something collects it, and collecting inside viewModelScope ties the stream's lifetime to the screen's. Each chunk exposes chunk.delta, the new text only. Stop collecting and the work stops; there is no callback registration to leak.

The key rule, in the guide's own words

In production, pass short-lived virtual keys minted by your backend — never a static key in the APK. An APK is mirrored, unpacked and read; a permanent key inside it should be treated as published. The backend-minted pattern costs one small endpoint and removes the whole category of leak. For tests and local CLI work the client falls back to the NROUTER_API_KEY environment variable.

Errors you can branch on

Refusals map to typed NRouterError exceptions: Request (400), GuardrailBlocked (400), Authentication (401), Credit (402), RateLimit (429), Service (503). On mobile those classes are UI decisions: 401 with short-lived keys usually means refresh silently, 402 is a state the interface should present, 429 asks for backoff.

That is the whole shape: an Android-native client that treats the lifecycle, key custody and cost visibility as the design, not as footnotes. The guide has the full walkthrough: https://nrouter.ai/docs/sdks/android

Top comments (0)