Skip to main content

View Source Code

Browse the complete example on GitHub
This example demonstrates how to integrate the Koog framework with LeapSDK on Android to build intelligent AI agents. Koog is an open-source framework for creating AI agents that can understand natural language, invoke tools, manage context, and integrate with MCP (Model Context Protocol) servers. LeapKoogAgent shows how to bring autonomous agent capabilities to mobile devices, enabling apps that can reason, plan, and execute complex tasks on behalf of users—all running locally on Android.

What’s inside?

LeapKoogAgent showcases advanced AI agent capabilities:
  • Koog Framework Integration - Build structured AI agents with reasoning capabilities
  • Natural Language Understanding - Process user intents and commands
  • Tool Invocation - Enable agents to call functions and use external tools
  • Context Management - Maintain conversation state and agent memory
  • Extensible Architecture - Easily add new capabilities and tools
  • MCP Server Integration - Connect to Model Context Protocol servers for tool retrieval and execution
  • Event Handling - Respond to agent events and state changes
  • On-device Agent Runtime - Complete agent execution without cloud dependency
This example demonstrates building production-ready AI agents that run entirely on Android devices.

What is the Koog Framework?

Koog is an open-source framework for building AI agents. It provides a structured approach to creating agents that can:
  • Reason and plan - Break down complex tasks into steps
  • Use tools - Invoke functions to accomplish specific tasks (calculations, API calls, database queries)
  • Maintain context - Remember conversation history and user preferences
  • Handle events - React to user input, system events, or external triggers
  • Execute workflows - Chain multiple actions together to complete objectives
  • Integrate with MCP - Access tools and resources from Model Context Protocol servers
Key concepts:
  • Agents - Autonomous entities that process input, reason, and take actions
  • Tools - Functions that agents can invoke (e.g., search, calculate, retrieve data)
  • Context - The agent’s knowledge state including conversation history
  • MCP (Model Context Protocol) - A standard for exposing tools and resources to agents
  • Events - Notifications about agent state changes or actions
Common agent use cases:
  • Personal assistants that manage tasks and calendars
  • Customer service bots with access to knowledge bases
  • Data analysis agents that query databases and generate reports
  • Workflow automation agents that orchestrate multiple services
  • Educational tutors that adapt to learner needs
  • Smart home controllers that understand natural language commands

Learn More About Koog

Explore the Koog framework documentation and examples

Environment setup

Before running this example, ensure you have the following:
Download and install Android Studio (latest stable version recommended).Make sure you have:
  • Android SDK installed
  • An Android device or emulator configured
  • USB debugging enabled (for physical devices)
This example requires:
  • Minimum SDK: API 31 (Android 12)
  • Target SDK: API 36
  • Kotlin: 2.3.0 or higher
Hardware recommendations:
  • At least 4GB RAM (agents require more memory for reasoning)
  • Sufficient storage for model bundles
LeapKoogAgent uses an LFM2-1.2B GGUF checkpoint suited for tool use. You can either let LeapModelDownloader.loadModel(...) pull it from the LEAP Model Library on first launch, or push it manually via ADB:
Note: Apps cannot read /tmp/ on Android — use /data/local/tmp/<your-namespace>/ for ADB-pushed assets (matches the other Android examples). If you deploy to a different location, update the modelPath in your app code accordingly. The example snippets below use loadSimpleModel(model: ModelSource(...)) to load the sideloaded file; switch to loadModel(modelName:, quantizationType:) if you’d rather have the SDK download the model automatically.
Add the required dependencies to your app-level build.gradle.kts:

How to run it

Follow these steps to build and run AI agents on Android:
  1. Clone the repository
  2. Deploy the model
    • Follow the ADB commands in the setup section above
    • Ensure the GGUF is at /data/local/tmp/liquid/lfm2-1.2b-q5_k_m.gguf
  3. Open in Android Studio
    • Launch Android Studio
    • Select “Open an existing project”
    • Navigate to the LeapKoogAgent folder
  4. Build the project
    • Wait for Gradle sync to complete
    • Resolve any dependency issues
  5. Run on device or emulator
    • Connect your Android device or start an emulator
    • Click “Run” or press Shift + F10
  6. Interact with the agent
    • On launch, the agent will initialize (may take 10-20 seconds)
    • Enter a command or question in the input field
    • Watch the agent reason, plan, and execute tasks
    • The agent can invoke tools to accomplish complex objectives
    • Try commands like:
      • “Calculate the sum of 25 and 37”
      • “What’s the weather like today?” (if weather tool is configured)
      • “Set a reminder for 3pm”
      • “Search for information about quantum computing”

Understanding the architecture

Koog Agent Initialization

Load the LEAP model first, then bridge it to a Koog agent. The Koog APIs below are illustrative — see the linked GitHub example for the canonical adapter wiring, since ai.koog:koog-agents evolves independently from LeapSDK.

Defining Agent Tools

Create tools that the agent can invoke:

Processing User Input

Handle user messages and agent responses:

MCP Server Integration

Connect to MCP servers to expand agent capabilities:

Event Handling

Listen to agent events for monitoring and debugging:

Context Management

Maintain conversation context and agent memory:

Resource Cleanup

Important: Always clean up agent resources properly to prevent memory leaks and ANRs:
Why this matters:
  • Avoid runBlocking in onCleared() - it blocks the main thread and can cause ANRs (Application Not Responding)
  • Use CoroutineScope(Dispatchers.IO).launch instead - model unloading happens asynchronously
  • Always clean up agent resources - prevents memory leaks and ensures proper shutdown
  • Catch exceptions - cleanup should never crash the app

Results

LeapKoogAgent demonstrates powerful autonomous agent capabilities on Android: Example interaction:
Multi-step task:
All agent reasoning and tool execution happens entirely on your Android device.

Further improvements

Here are some ways to extend this example:
  • Custom tool library - Build domain-specific tools for your app
  • Multi-agent systems - Create multiple specialized agents that collaborate
  • Persistent memory - Store long-term memories in a database
  • Voice interaction - Add speech-to-text and text-to-speech
  • Proactive agents - Trigger agents based on time, location, or events
  • Knowledge base integration - Connect to local or remote knowledge sources
  • Task scheduling - Let agents schedule and execute delayed tasks
  • Learning capabilities - Implement feedback loops to improve agent behavior
  • Agent marketplace - Allow users to install pre-built agent configurations
  • Multi-modal inputs - Process images, audio, and text together
  • Security and permissions - Implement fine-grained tool access control
  • Agent telemetry - Track agent performance and decision quality
  • Offline-first design - Ensure agents work without internet connectivity
  • Cross-device sync - Sync agent context across multiple devices

Need help?

Join our Discord

Connect with the community and ask questions about this example.