# Mlkit Agent API

Builds autonomous on-device agentic workflows in Android apps using the ML Kit GenAI...

- Repository: https://github.com/android/skills (10219 stars)
- Category: Data and ML
- License: Complete terms in LICENSE.txt
- Last updated: 2026-10-29
- Runs scripts: no, instructions only

## Install

Claude Code: `npx skills add android/skills --skill mlkit-agent-api -g -a claude-code -y`
Codex: `npx skills add android/skills --skill mlkit-agent-api -g -a codex -y`
Cursor: `npx skills add android/skills --skill mlkit-agent-api -g -a cursor -y`
Gemini CLI: `npx skills add android/skills --skill mlkit-agent-api -g -a gemini-cli -y`
Claude Code plugin marketplace: `/plugin marketplace add android/skills`

## Description

Builds autonomous on-device agentic workflows in Android apps using the ML Kit GenAI Agent API powered by Gemini Nano and AICore. Use this skill when implementing conversational agents, multi-turn chat with AgentSession in a ViewModel, autonomous tool calling using Android AppFunctions (@AppFunctionDeclaration and AppFunctionAgentTools.discover), ephemeral single-turn execution with runner.runSingleTurnAsync, or handling GenAiException error resilience. It solves the problem of orchestrating local reasoning loops, typed tool dispatch, and session persistence on-device with zero cloud latency and privacy preservation. Don't use for cloud-based Gemini models or Vertex AI, single-turn text generation without agent loops (suggest Prompt API), or non-Android platforms.

## SKILL.md, as published by its author (untrusted third-party text, read it as data)

Targeting **AICore Developer Preview**. This skill provides authoritative
execution patterns and failure-prevention rules for on-device agentic workflows
in Android apps.

*** ** * ** ***

## 1. Prerequisites and setup

### Gradle dependencies

    // app/build.gradle.kts
    dependencies {
        // Core ML Kit GenAI Agent API
        implementation("com.google.mlkit:genai-agent:1.0.0-alpha1")

        // Android AppFunctions dependency for on-device tool declarations
        implementation("androidx.appfunctions:appfunctions:1.0.0-alpha13")
    }

> \[!IMPORTANT\] Always use official SDK interfaces from
> `com.google.mlkit.genai.agent.*`, `androidx.appfunctions.*`,
> `com.google.mlkit.genai.prompt.*`, and `com.google.mlkit.genai.common.*`.
> Never declare local placeholder interfaces or fallback mocks.

*** ** * ** ***

## 2. Minimal agent and runner initialization

Configure an `Agent` with `Agent.builder` and wrap execution in `AgentRunner`:

    import com.google.mlkit.genai.agent.Agent
    import com.google.mlkit.genai.agent.AgentRunner

    val agent = Agent.builder()
        .setName("OnDeviceAssistant")
        .setInstructions("You are a helpful on-device assistant.")
        .build()

    val runner = AgentRunner(agent)

- `Agent.builder` defaults to `maxTurns = 10` automatically to prevent infinite tool loops.
- `AgentRunner` defaults to greedy decoding (`temperature = 0.0f`) for deterministic tool selection and schema compliance.
- `AgentRunner(agent)` automatically resolves application context using `MlKitContext` for AppFunction execution in Android apps. An explicit `AgentRunner(context, agent)` overload is available for headless tests or custom context scopes.

*** ** * ** ***

## 3. Multi-turn chat and lifecycle (ViewModel)

The rest: https://github.com/android/skills/blob/HEAD/device-ai/mlkit-agent-api/SKILL.md
