# Streaming > Show your agent's answer as it is written with the QwryAI Android SDK ## Two ways to get an answer The SDK gives you two calls for a new message. Both run your client-side tools for you, up to 10 rounds by default. * **`send()`** streams the answer and calls your handlers as each piece arrives, then returns the complete `ChatResponse`. Use it for chat screens. * **`sendNonStreaming()`** waits and returns the whole answer in one response. Use it when nobody is watching the text appear, such as a background task. ## send `class QwryClient`, **Package:** `com.qwryai.sdk` ```kotlin suspend fun send( text: String, conversationId: String? = null, handlers: SendHandlers.() -> Unit = {} ): ChatResponse ``` Sends a message, streams the answer, runs any client-side tools the agent calls, and returns the finished `ChatResponse`. **text** `String` *required* The message to send. **conversationId** `String?` The conversation to continue. Leave it out to continue `currentConversationId`, or to start a new conversation when there's none. **handlers** `SendHandlers.() -> Unit` Handlers for what happens while the answer is written. ```kotlin val response = client.send("Where is my order?") { onTextDelta = { delta -> // Each new piece of text, in order print(delta) } onToolCall = { call -> println("Agent is calling: ${call.name}") } onToolResult = { result -> println("Tool result: ${result.output}") } } println("\nDone! Message ID: ${response.message.id}") ``` ### SendHandlers `class SendHandlers`, **Package:** `com.qwryai.sdk` > [!WARNING] > > Handlers run on a background thread. Switch to the main thread before you touch views, for example with `withContext(Dispatchers.Main)` or by writing to a `StateFlow` that your UI collects. `ConversationState` does this for you. ```kotlin var onTextDelta: ((String) -> Unit)? ``` Called with each new piece of the agent's text. ```kotlin var onToolCall: ((MessagePart.ToolCall) -> Unit)? ``` Called when the agent calls a tool, before it runs. This covers tools your app registered and actions QwryAI runs on the server. ```kotlin var onToolResult: ((MessagePart.ToolResult) -> Unit)? ``` Called when a tool has finished, with what it returned. There's no start, finish or error handler: `send` returns the `ChatResponse` when the answer is complete and throws a `QwryException` when it fails. See [Error Handling](/docs/android-sdk/error-handling). ### ChatResponse `data class ChatResponse`, **Package:** `com.qwryai.sdk` What `send`, `sendNonStreaming` and `retry` return once the answer is complete. **message** `Message` *required* The agent's answer: its `id`, `role` (`ASSISTANT`), `parts` and `text`. **conversationId** `String` *required* The conversation the answer belongs to. Pass it to continue that conversation. **userMessageId** `String?` The ID the server gave the message you sent. **userId** `String?` The identified user, or `null` while the SDK is anonymous. **finishReason** `ChatResponse.FinishReason` *required* Why the answer ended: `STOP`, `ERROR` or `TOOL_CALLS`. **credits** `Double` *required* Message credits this answer used. See [ChatResponse](/docs/android-sdk/chat-response) for every field. ## sendNonStreaming ```kotlin suspend fun sendNonStreaming( text: String, conversationId: String? = null ): ChatResponse ``` Like `send`, but the server returns the whole answer at once and no handlers run. Client-side tools still run. **text** `String` *required* The message to send. **conversationId** `String?` The conversation to continue. Leave it out to continue `currentConversationId` or start a new one. ```kotlin val response = client.sendNonStreaming("Summarise my last order in one line.") println(response.message.text) ``` > [!NOTE] > > An answer can pause while the agent works, for example while an action runs. The SDK waits up to 10 minutes for a turn to finish, which is also when the server ends one. ## Continuing a conversation The client remembers the conversation it last used. After a reply, the next call without a `conversationId` continues it: ```kotlin // First message: starts a new conversation client.send("My name is Alice.") println(client.currentConversationId) // the new conversation's ID // Later messages continue it client.send("What is my name?") // The agent remembers: "Alice" ``` To start a fresh conversation: ```kotlin client.newConversation() client.send("Fresh start!") // Creates a new conversation ``` > [!TIP] > > See [Conversations & History](/docs/android-sdk/conversations) to list conversations and load earlier messages. ## retry ```kotlin suspend fun retry( conversationId: String, messageId: String, handlers: SendHandlers.() -> Unit = {} ): ChatResponse ``` Asks the agent to answer again in place of one of its messages. That answer and everything after it are replaced. Streams and runs tools exactly like `send`. **conversationId** `String` *required* The conversation that holds the answer. **messageId** `String` *required* The ID of the agent's message to answer again. **handlers** `SendHandlers.() -> Unit` The same handlers as `send`. ```kotlin val response = client.send("Hello") // ... later: val retried = client.retry(response.conversationId, response.message.id) { onTextDelta = { delta -> print(delta) } } ``` ## Streaming into Compose `ConversationState` keeps one conversation's messages in a `StateFlow` and grows the agent's answer in place as it streams, so you don't have to write handlers yourself: ```kotlin @Composable fun Chat(client: QwryClient) { val chat = remember { ConversationState(client) } val messages by chat.messages.collectAsState() val sending by chat.isSending.collectAsState() val scope = rememberCoroutineScope() LazyColumn { items(messages, key = { it.id }) { Text(it.text) } } Button(enabled = !sending, onClick = { scope.launch { chat.send("Hello") } }) { Text("Send") } } ``` `chat.retry(messageId)` asks for a new answer in place of one shown, and `chat.error` holds the last failure. See [Conversations & History](/docs/android-sdk/conversations). ## Related > [card] Client-Side Tools | /docs/android-sdk/client-side-tools | Register functions your agent can call on the device. > [card] Error Handling | /docs/android-sdk/error-handling | QwryException, its codes and how to handle them.