View Source Code
Browse the complete Android project
Environment setup
Use Android Studio with JDK 17, Android SDK 36, NDK29.0.13113456, and CMake 3.31.6. The projects pin Kotlin 2.3.20 and llama.cpp v0.9.0.
Clone the complete repository so the app can include examples/android-common and its pinned llama.cpp submodule:
android-common directories together.
How to run it
- Open
cookbook/examples/AndroidAgentin Android Studio and run it on Android 12+. - Select the calculator or weather agent.
- Submit a calculation such as
1234 + 4321, or a location such asLondon tomorrow. - On first use, the app downloads and verifies the model (about 850 MB).
- Inspect the tool results and final answer. If a request fails, correct it or retry.
How the agent works
- Send the conversation and tool definitions to the model.
- llama.cpp applies the checkpoint’s chat template and parses generated tool calls into structured
tool_calls. - Kotlin decodes each tool’s arguments and executes it.
- Append the assistant’s tool call and the corresponding
toolresult to the conversation. - Repeat until the model answers or the iteration limit is reached.
Code walkthrough
Agent.kt implements the loop; Tool.kt defines typed arguments, JSON schemas, execution, and result formatting. LlamaChat.kt adapts the shared NativeAgentModel to Kotlin JSON objects:
LocalModel shares one loaded model across agents. The runtime serializes every native operation, so calculator and weather requests cannot mutate its context at the same time. Each native call receives the full conversation for that request. Older GGUF templates can ignore structured tool calls when rendering history, so the runtime also preserves the generated call text in assistant content when the template needs it. Keep the full assistant reply in the conversation, including both content and parsed calls.
Calculator tools
The calculator exposes arithmetic tools with numerica and b arguments. Kotlin computes the result and returns it to the model for the final response.