On this page
Tokens, Latency and Cost explains the concepts. This article is about operating AI in production: instrumenting calls, building dashboards, setting alerts and budgets, and connecting metrics to quality.
What to measure
| Category | Metric | Why |
|---|---|---|
| Latency | Time to first token (TTFT), total time, p50/p95 | TTFT drives perceived speed |
| Reliability | Error rate by type (429, timeout, blocked, parse) | Detect outages and limits |
| Usage | Requests per feature/user, input/output tokens | Capacity planning |
| Cost | Cost per request, per active user, per feature | Unit economics |
| Tier | On-device vs cloud vs fallback share | Device availability reality |
| Quality | Thumbs up/down, edits, retries, stops | Usefulness signals |
Instrumentation overview
Client instrumentation
fun <T> Flow<T>.instrumented(feature: String, model: String, source: String, telemetry: AiTelemetry): Flow<T> {
var start = 0L
var firstTokenAt = 0L
return this
.onStart { start = SystemClock.elapsedRealtime() }
.onEach { if (firstTokenAt == 0L) firstTokenAt = SystemClock.elapsedRealtime() }
.onCompletion { cause ->
val end = SystemClock.elapsedRealtime()
telemetry.record(
AiCallEvent(
feature = feature, model = model, source = source,
ttftMs = if (firstTokenAt > 0) firstTokenAt - start else null,
totalMs = end - start,
outcome = when (cause) {
null -> "ok"
is CancellationException -> "stopped"
else -> "error:${cause::class.simpleName}"
},
)
)
}
}
// Usage: model.generateContentStream(prompt).mapNotNull { it.text }.instrumented("summarize", "gemini-3.8-flash", "cloud", telemetry)Send these as analytics events or custom Performance Monitoring traces. Never include prompts or responses. See AI Privacy.
Token usage and cost
Responses usually include usage metadata (input and output token counts). Record it where you can trust it, ideally on your backend, and multiply by your model's current price:
data class ModelPrice(val inputPerMillion: Double, val outputPerMillion: Double) // from your config
fun estimateCost(inputTokens: Int, outputTokens: Int, price: ModelPrice): Double =
inputTokens / 1_000_000.0 * price.inputPerMillion +
outputTokens / 1_000_000.0 * price.outputPerMillion
// Load prices from remote config/backend; check the provider's pricing page for current rates.Firebase AI Logic also offers AI monitoring in the Firebase console, covering requests, latency, errors and tokens. Use it alongside your own feature-level metrics.
Dashboards, alerts and budgets
- Dashboards: p95 TTFT and error rate per feature and model; daily cost per feature; tier share.
- Alerts: error-rate spikes (for example 429s), latency regressions after model changes, cost above a daily threshold.
- Budgets: cloud billing budget alerts, plus per-user quotas on your backend to prevent abuse.
Quality signals
- Ratings, report rate, regenerate/retry rate, stop rate, and edit distance between suggestion and final text.
- Compare quality metrics when switching models or prompts (A/B tests with flags).
- Combine with offline evaluation sets. See Evaluating AI.
Acting on the data
High TTFT
Stream, shorten prompts, use a faster model, or move simple tasks on device.
High cost
Cap output tokens, trim context, cache results, route simple requests to cheaper models.
High errors
Check quotas, add backoff, and improve fallbacks. See Fallbacks.
Frequently asked questions
Can I trust client-reported token counts?
For dashboards, yes. For billing users or enforcing quotas, record usage on your backend, where clients can't tamper with it.
How do I monitor on-device AI?
Track availability, latency, errors (including busy and quota errors) and tier share. There's no per-token cost, but there may be battery impact.
Conclusion
Operate AI like any production dependency, plus a few new metrics: TTFT and latency percentiles, error types, tokens and cost per feature and user, tier share and quality signals, with dashboards, alerts and budgets, and without logging user content.
Explore more in the Android Developer Blog hub.
Official references
๐ฑ Apps built by Yunok Tech
Real Android apps on Google Play, free to download.
Walzi4K wallpapers for lock and home screenGet it โธ
NotiVaultNotification history: recover dismissed notificationsGet it โธ
Ready to build real Android applications?
Every AI feature still runs inside an Android app that has to be fast, reliable and well structured. Strengthen the Kotlin, Jetpack Compose and architecture skills that AI integrations depend on, then apply these AI concepts with confidence.
Move beyond theory and strengthen your Android development skills with Yunoktech's Android course. Learn core concepts, practical implementation, modern Android development techniques, and the reasoning behind building maintainable applications.
Explore the Android course and take your next step as an Android developer.