View Source Code
Browse the complete example on GitHub
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
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
- 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
- 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:Android Studio Installation
Android Studio Installation
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)
Minimum SDK Requirements
Minimum SDK Requirements
This example requires:
- Minimum SDK: API 24 (Android 7.0)
- Target SDK: API 34 or higher
- Kotlin: 1.9.0 or higher
- At least 4GB RAM (agents require more memory for reasoning)
- Sufficient storage for model bundles
Model Bundle Deployment
Model Bundle Deployment
LeapKoogAgent requires the LFM2-1.2B-Tool model bundle optimized for tool use.Deploy via ADB:Note: The path
/tmp/models is used in this example. If you deploy to a different location, update the model path in your app code accordingly.Dependencies Setup
Dependencies Setup
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:-
Clone the repository
-
Deploy the model bundle
- Follow the ADB commands in the setup section above
- Ensure the bundle is at
/tmp/models/lfm2-1.2b-tool.bundle
-
Open in Android Studio
- Launch Android Studio
- Select “Open an existing project”
- Navigate to the
LeapKoogAgentfolder
-
Build the project
- Wait for Gradle sync to complete
- Resolve any dependency issues
-
Run on device or emulator
- Connect your Android device or start an emulator
- Click “Run” or press
Shift + F10
-
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
Set up a Koog agent with LeapSDK backend: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:- Avoid
runBlockinginonCleared()- it blocks the main thread and can cause ANRs (Application Not Responding) - Use
CoroutineScope(Dispatchers.IO).launchinstead - 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: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