> ## Documentation Index
> Fetch the complete documentation index at: https://docs.runanywhere.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Voice Agent

> A full spoken conversation

`RunAnywhere.voice` assembles speech-to-text, a language model, and text-to-speech into one
session that listens, thinks, and answers out loud. It owns the microphone while it runs.

```kotlin theme={"theme":"css-variables"}
val session = RunAnywhere.voice.createSession(
    stt = ModelRef("whisper-base"),
    llm = ModelRef("qwen3-0.6b"),
    tts = ModelRef("piper-en-amy")
)

session.start()

session.events.collect { event ->
    when (event) {
        is VoiceEvent.UserTranscript -> transcript += "user: ${event.text}"
        is VoiceEvent.AssistantText -> transcript += "assistant: ${event.text}"
        else -> {}
    }
}
```

## Signature

```kotlin theme={"theme":"css-variables"}
suspend fun createSession(
    stt: ModelRef,
    llm: ModelRef,
    tts: ModelRef,
    vad: VadOptions? = null,
    turnHandling: TurnHandlingOptions? = null,
    generation: LlmOptions? = null,
    downloadIfNeeded: Boolean = true
): VoiceSession
```

Leaving `vad` unset ensures the catalogued default Silero VAD. `downloadIfNeeded` defaults to
`true`, so any of the three models that is missing is fetched first.

## VoiceSession

```kotlin theme={"theme":"css-variables"}
val events: Flow<VoiceEvent>
fun start()
fun say(text: String): SpeechHandle
suspend fun interrupt()
fun close()
```

`say` speaks a line without waiting for the user, which is how you greet someone.
`interrupt` cuts off whatever is being spoken.

Always `close()` the session. It holds the microphone.

## Turn handling

```kotlin theme={"theme":"css-variables"}
val session = RunAnywhere.voice.createSession(
    stt = sttModel,
    llm = llmModel,
    tts = ttsModel,
    turnHandling = TurnHandlingOptions(
        endpointing = Endpointing(minDelayMs = 500, maxDelayMs = 3000),
        interruption = Interruption(enabled = true, minDurationMs = 500)
    )
)
```

| Setting | Default | Meaning |
| - | - | - |
| `endpointing.minDelayMs` | 500 | Shortest silence that ends a turn |
| `endpointing.maxDelayMs` | 3000 | Longest wait before ending it anyway |
| `interruption.enabled` | true | A person talking over the model stops it |
| `interruption.minDurationMs` | 500 | Speech this long counts as an interruption |

Raise `interruption.minDurationMs` in a noisy room, or a cough cuts the model off.

## Steering the replies

```kotlin theme={"theme":"css-variables"}
generation = LlmOptions(
    systemPrompt = "You are a voice assistant. Answer in one or two sentences.",
    maxOutputTokens = 100
)
```

Spoken replies want to be short. A model that writes four paragraphs is unbearable to listen
to, so cap `maxOutputTokens` lower than you would for a chat interface.

## A complete ViewModel

```kotlin theme={"theme":"css-variables"}
class VoiceAgentViewModel(app: Application) : AndroidViewModel(app) {
    private val _transcript = MutableStateFlow<List<Pair<String, String>>>(emptyList())
    val transcript = _transcript.asStateFlow()

    private var session: VoiceSession? = null

    fun start() {
        viewModelScope.launch {
            val s = RunAnywhere.voice.createSession(
                stt = ModelRef("whisper-base"),
                llm = ModelRef("qwen3-0.6b"),
                tts = ModelRef("piper-en-amy"),
                generation = LlmOptions(
                    systemPrompt = "Answer in one or two sentences.",
                    maxOutputTokens = 100
                )
            )
            session = s

            s.start()
            s.say("Hello. What can I help with?")

            s.events.collect { event ->
                when (event) {
                    is VoiceEvent.UserTranscript ->
                        _transcript.value += "user" to event.text
                    is VoiceEvent.AssistantText ->
                        _transcript.value += "assistant" to event.text
                    else -> {}
                }
            }
        }
    }

    override fun onCleared() {
        session?.close()
        session = null
        super.onCleared()
    }
}
```

Closing in `onCleared` is what stops the microphone when the screen goes away.

## Permission

`RECORD_AUDIO` has to be granted at runtime before `start()`:

```kotlin theme={"theme":"css-variables"}
if (ContextCompat.checkSelfPermission(context, Manifest.permission.RECORD_AUDIO)
    != PackageManager.PERMISSION_GRANTED) {
    launcher.launch(Manifest.permission.RECORD_AUDIO)
}
```

## Diarization

To answer "who spoke when" over a recording rather than run a live conversation:

```kotlin theme={"theme":"css-variables"}
val result = RunAnywhere.diarization.diarize(AudioInput.file(path))
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.