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

# Audio encoding

> Set the input and output audio encoding and sample rate for a voice agent.

The encoding determines the sample rate and bit depth of the audio going in (microphone) and coming out (agent speech). Set it on the agent under `input.format` and `output.format` when you [create](/voice-agents/voice-agent-api/create-agent) or [update](/voice-agents/voice-agent-api/manage-agents) it, or inline via [`session.update`](/voice-agents/voice-agent-api/session-configuration).

Most agents can leave this alone — the defaults are the highest quality. Change it mainly for **telephony**. For how to actually stream and play the audio bytes, see [Stream audio](/voice-agents/voice-agent-api/audio-format).

Input and output encodings are independent and can differ. Both default to `audio/pcm` (24 kHz) if omitted.

| Encoding     | Sample rate | Best for                                                 |
| ------------ | ----------- | -------------------------------------------------------- |
| `audio/pcm`  | 24,000 Hz   | Default. Highest quality, ideal for browser and app use. |
| `audio/pcmu` | 8,000 Hz    | Telephony (G.711 μ-law).                                 |
| `audio/pcma` | 8,000 Hz    | Telephony (G.711 A-law).                                 |

For telephony, use `audio/pcmu` or `audio/pcma` (8 kHz) to match the phone network and avoid resampling. See [Connect to Twilio](/voice-agents/voice-agent-api/connect-to-twilio) for a full phone integration.

Set `format.encoding` under `input` and `output`. You can also pass an explicit `sample_rate` inside `format`:

<CodeGroup>
  ```bash cURL theme={null}
  curl -X POST https://agents.assemblyai.com/v1/agents \
    -H "Authorization: $ASSEMBLYAI_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "name": "Support Assistant",
      "system_prompt": "You are a friendly support agent. Keep replies under two sentences.",
      "voice": { "voice_id": "ivy" },
      "input":  { "format": { "encoding": "audio/pcmu", "sample_rate": 8000 } },
      "output": { "format": { "encoding": "audio/pcmu", "sample_rate": 8000 } }
    }'
  ```

  ```python Python theme={null}
  # pip install requests
  import os
  import requests

  resp = requests.post(
      "https://agents.assemblyai.com/v1/agents",
      headers={"Authorization": os.environ["ASSEMBLYAI_API_KEY"]},
      json={
          "name": "Support Assistant",
          "system_prompt": "You are a friendly support agent. Keep replies under two sentences.",
          "voice": {"voice_id": "ivy"},
          "input": {"format": {"encoding": "audio/pcmu", "sample_rate": 8000}},
          "output": {"format": {"encoding": "audio/pcmu", "sample_rate": 8000}},
      },
  )
  resp.raise_for_status()
  print(resp.json())
  ```

  ```javascript Node.js theme={null}
  // Node 18+ has fetch built in
  const res = await fetch("https://agents.assemblyai.com/v1/agents", {
    method: "POST",
    headers: {
      Authorization: process.env.ASSEMBLYAI_API_KEY,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      name: "Support Assistant",
      system_prompt: "You are a friendly support agent. Keep replies under two sentences.",
      voice: { voice_id: "ivy" },
      input: { format: { encoding: "audio/pcmu", sample_rate: 8000 } },
      output: { format: { encoding: "audio/pcmu", sample_rate: 8000 } },
    }),
  });
  const data = await res.json();
  console.log(data);
  ```
</CodeGroup>

| Field                    | Type    | Required | Notes                                                            |
| ------------------------ | ------- | -------- | ---------------------------------------------------------------- |
| `input.format.encoding`  | string  | No       | `audio/pcm`, `audio/pcmu`, or `audio/pcma`. Default `audio/pcm`. |
| `output.format.encoding` | string  | No       | Same values as input. Default `audio/pcm`.                       |
| `format.sample_rate`     | integer | No       | Sample rate in Hz. Determined by the encoding if omitted.        |

<Note>
  Set the volume separately — see [Volume](/voice-agents/voice-agent-api/volume). When configured inline via [`session.update`](/voice-agents/voice-agent-api/session-configuration#mutability-after-sessionready), `output.format` is **immutable** after `session.ready` and must be set on your first update.
</Note>
