# Client-Side Tools > Run your agent's client-side actions as Kotlin functions on the device ## What are client-side tools? A client-side tool is a function in your app that your agent can call in the middle of an answer. Register a handler under the action's name; when the agent calls it, the SDK runs your handler, sends what it returns back to the agent, and the agent carries on answering with it. > [!NOTE] > > Each tool matches a **Custom Action** on your agent whose type is **Run client-side code**. The name you register must be the action's name exactly (3 to 64 letters, numbers, underscores or hyphens). Turn the action on for this channel in **Channels › Android SDK › Actions**. ## tool `class QwryClient`, **Package:** `com.qwryai.sdk` ```kotlin fun tool(name: String, handler: suspend (input: JSONObject) -> Any?) ``` Registers a handler for a client-side action. Registering the same name again replaces the handler. **name** `String` *required* The action's name, as set on your agent. **handler** `suspend (JSONObject) -> Any?` *required* A suspend function that receives the agent's input as an `org.json.JSONObject` and returns a JSON value: a `JSONObject`, `JSONArray`, string, number, boolean or `null`. ```kotlin client.tool("get_order_status") { input -> val orderNumber = input.getString("order_number") // Read from your app's own data, call your API, use a sensor... val order = orders.find(orderNumber) JSONObject() .put("order_number", orderNumber) .put("status", order.status) .put("eta", order.eta) } ``` The agent can now call `get_order_status` while it answers. Each `send`, `sendNonStreaming` or `retry` allows up to 10 rounds of tool calls by default (set `maxToolLoopSteps` in `Qwry.create` to change it). If the agent keeps calling tools beyond that, the call throws a `QwryException` with the code `TOOL_LOOP_LIMIT_EXCEEDED`. If your handler throws, the agent receives `{"error": ""}` and decides what to say. If the agent calls a tool your app hasn't registered, it receives an error saying so. Either way, the answer continues. > [!WARNING] > > A tool result may be at most **20 KB** as JSON. A larger one fails with `400` `VALIDATION_INVALID_BODY`. Return only the fields the agent needs. ## removeTool ```kotlin fun removeTool(name: String) ``` Unregisters a tool handler. If the agent calls that tool later, it's told no tool by that name is registered. **name** `String` *required* The name you registered. ## Tracking execution Use the `onToolCall` and `onToolResult` handlers of `send` to show what the agent is doing: ```kotlin client.send("Where is order 1042?") { onToolCall = { call -> println("Agent is calling: ${call.name}") println("Input: ${call.input}") } onToolResult = { result -> println("Tool result: ${result.output}") } onTextDelta = { delta -> print(delta) // The agent's answer, written after the tool result } } ``` ### MessagePart.ToolCall `data class MessagePart.ToolCall`, **Package:** `com.qwryai.sdk` Passed to `onToolCall` before the tool runs. ```kotlin data class ToolCall( val id: String, val name: String, val input: Any? ) : MessagePart ``` `input` is a JSON value as read by `org.json`, usually a `JSONObject`. ### MessagePart.ToolResult `data class MessagePart.ToolResult`, **Package:** `com.qwryai.sdk` Passed to `onToolResult` after the tool has run. ```kotlin data class ToolResult( val id: String, val name: String, val output: Any? ) : MessagePart ``` `id` matches the `ToolCall` it answers. `output` is what your handler returned, or the `{"error": …}` object the agent received. > [!NOTE] > > Both handlers run on a background thread. Switch to the main thread before you update views. ## Interactive tools Handlers are `suspend` functions, so they can wait for the user. For example, ask the user to choose a delivery slot and return their choice: ```kotlin val slotRequests = MutableStateFlow?>(null) client.tool("choose_delivery_slot") { _ -> val choice = CompletableDeferred() // Your UI collects slotRequests and shows a picker slotRequests.value = choice // Suspend until the user picks a slot val slot = choice.await() slotRequests.value = null JSONObject().put("slot", slot) } ``` ## Related > [card] Streaming | /docs/android-sdk/streaming | send, its handlers and retry. > [card] Error Handling | /docs/android-sdk/error-handling | Handle failures, including the tool loop limit.