๐Ÿง  Android AI Projects and Production

Monitoring AI Latency, Errors, and Inference Costs

Once an AI feature ships, you need to know: is it fast, is it working, how much does it cost, and do users find it useful? AI adds new metrics, such as time to first token, tokens per request and cost per user, on top of normal app monitoring. Here's what to measure and how.

โœ๏ธ Yunok Tech ๐Ÿ“… Updated โฑ 3 min read ๐ŸŽฏ Advanced
On this page
  1. What to measure
  2. Instrumentation overview
  3. Client instrumentation
  4. Token usage and cost
  5. Dashboards, alerts and budgets
  6. Quality signals
  7. Acting on the data
  8. Frequently asked questions
  9. Conclusion

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

CategoryMetricWhy
LatencyTime to first token (TTFT), total time, p50/p95TTFT drives perceived speed
ReliabilityError rate by type (429, timeout, blocked, parse)Detect outages and limits
UsageRequests per feature/user, input/output tokensCapacity planning
CostCost per request, per active user, per featureUnit economics
TierOn-device vs cloud vs fallback shareDevice availability reality
QualityThumbs up/down, edits, retries, stopsUsefulness signals

Instrumentation overview

AI monitoring pipeline The app measures time to first token, total latency, outcome and tier, and sends events without content. The backend or provider records token usage and cost. Both feed dashboards and alerts on latency, error rate and spend, and quality signals from user feedback. AppTTFT ยท latency ยท outcome Backend / providertokens ยท cost Events storeno prompt content Dashboards+ alerts, budgets
Client metrics show user experience; server metrics show cost and provider health.

Client instrumentation

Measuring a streamed callKotlin
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:

Cost estimate (prices are inputs, not constants)Kotlin
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.

See all our apps โ†’

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.

Share: LinkedIn X / Twitter WhatsApp