> ## 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.

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

try session.start()

for try await event in session.events {
    switch event {
    case .userTranscript(let text):
        transcript.append(("user", text))
    case .assistantText(let text):
        transcript.append(("assistant", text))
    default:
        break
    }
}
```

## Signature

```swift theme={"theme":"css-variables"}
func createSession(
    stt: ModelRef,
    llm: ModelRef,
    tts: ModelRef,
    vad: VadOptions? = nil,
    turnHandling: TurnHandlingOptions? = nil,
    generation: LlmOptions? = nil,
    downloadIfNeeded: Bool = true
) async throws -> 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

```swift theme={"theme":"css-variables"}
var events: AsyncThrowingStream<VoiceEvent, Error>
func start() throws
func say(_ text: String) async throws -> SpeechHandle
func interrupt() async
func close() async
```

`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

```swift theme={"theme":"css-variables"}
let session = try await 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 will cut the model off.

## Steering the replies

```swift 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 session

```swift theme={"theme":"css-variables"}
@MainActor
@Observable
final class VoiceAgent {
    var transcript: [(String, String)] = []
    private var session: VoiceSession?

    func start() async throws {
        let session = try await RunAnywhere.voice.createSession(
            stt: ModelRef(id: "whisper-base"),
            llm: ModelRef(id: "qwen3-0.6b"),
            tts: ModelRef(id: "piper-en-amy"),
            generation: LlmOptions(
                systemPrompt: "Answer in one or two sentences.",
                maxOutputTokens: 100
            )
        )
        self.session = session

        try session.start()
        _ = try await session.say("Hello. What can I help with?")

        for try await event in session.events {
            switch event {
            case .userTranscript(let text):
                transcript.append(("user", text))
            case .assistantText(let text):
                transcript.append(("assistant", text))
            default:
                break
            }
        }
    }

    func stop() async {
        await session?.close()
        session = nil
    }
}
```

## Microphone permission

Add `NSMicrophoneUsageDescription` to your Info.plist. The session cannot start without it.

## Diarization

To answer "who spoke when" over a recording rather than run a live conversation, use the
diarization namespace:

```swift theme={"theme":"css-variables"}
let result = try await RunAnywhere.diarization.diarize(AudioInput.file(path))
```

It also streams:

```swift theme={"theme":"css-variables"}
let events = try await RunAnywhere.diarization.diarizeStream(audioStream)
```


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