<!-- generated: do not edit. source: content/docs/turns/index.md -->

# Turn detection

Turn detection lets you find out whether a caller has finished their turn. You
send the caller's audio, and the response carries the probability that the
caller is done. The model reads the audio itself, so there is no transcription
step to run first.

| Transport | Send | Returns |
| --- | --- | --- |
| `POST /v1/turns` | One audio clip | One decision for the moment the clip ends |
| WebSocket `/v1/turns` | The caller's audio as it arrives | An event at each turn boundary |

Use turn detection when your agent has to choose the moment to reply: soon
after a finished sentence, and not during a pause in the middle of one.

## Get an API key
Create a Sprag account and open [API keys](/app/api-keys) in the app to generate
one.

Copy the key immediately. The full value is shown once and is not recoverable
after you leave the page.

Export it as an environment variable, replacing the placeholder with your key:

```bash
export SPRAG_API_KEY=<your_sprag_api_key>
```

## Make a request
Send a clip of the caller's speech as `file`. The clip ends at the moment you
want judged.

```bash
curl https://api.sprag.ai/v1/turns \
  -H "Authorization: Bearer $SPRAG_API_KEY" \
  -F file=@caller.wav \
  -F model=sprag-eot
```

```python
import os
import requests

with open("caller.wav", "rb") as clip:
    response = requests.post(
        "https://api.sprag.ai/v1/turns",
        headers={"Authorization": f"Bearer {os.environ['SPRAG_API_KEY']}"},
        files={"file": clip},
        data={"model": "sprag-eot"},
    )
response.raise_for_status()

print(response.json())
```

```typescript
import { openAsBlob } from "node:fs";

const form = new FormData();
form.append("file", await openAsBlob("caller.wav"), "caller.wav");
form.append("model", "sprag-eot");

const response = await fetch("https://api.sprag.ai/v1/turns", {
  method: "POST",
  headers: { Authorization: `Bearer ${process.env.SPRAG_API_KEY}` },
  body: form,
});

console.log(await response.json());
```

## Read the decision
`probability` is the probability that the caller has finished the turn, from 0
to 1. `ended` is `true` when `probability` reaches `eot_threshold`.

```json
{
  "object": "turn.decision",
  "model": "sprag-eot",
  "probability": 0.87,
  "ended": true,
  "eot_threshold": 0.6,
  "usage": { "invocations": 1 }
}
```

To act on the decision, read `ended`. To apply your own threshold, read
`probability` instead.

## Where to go next

**[API](/docs/turns/api).** Look up the request fields, the response, and the
errors for `POST /v1/turns`.

**[WebSocket](/docs/turns/websocket).** Stream a caller's audio and look up
the session's settings, events, and errors.

**[Models](/docs/turns/models).** See which models detect turns.

To detect turns in a Pipecat pipeline, see
[Pipecat: turn detection](/docs/integrations/pipecat/turns).
