DocsAPI Reference
Endpoints

Realtime

Speech-to-speech voice sessions over WebSocket and WebRTC with the OpenAI Realtime API, metered per turn.


Tokamak serves the OpenAI Realtime API (GA): the same paths, events and auth, with your Tokamak key in place of an OpenAI one. Clients built for OpenAI Realtime (the openai SDKs, the OpenAI Agents SDK, LiveKit Agents, Pipecat) work by pointing their base URL at Tokamak.

EndpointUse
GET /v1/realtime?model=A voice session over WebSocket (servers, or browsers with a client secret)
GET /v1/realtime?intent=transcriptionA transcription-only session over WebSocket
POST /v1/realtime/client_secretsA one-time token for a browser
POST /v1/realtime/callsA WebRTC call: send an SDP offer, get the answer
POST /v1/realtime/calls/{call_id}/hangupEnd a call

The Realtime models are openai/gpt-realtime-2.1 and openai/gpt-realtime-2.1-mini, priced on audio_input and audio_output. They are not in the default Models list, which coding agents read; add X-PROVIDER-DATA: true to see them there.

WebSocket, from a server

import asyncio
from openai import AsyncOpenAI

client = AsyncOpenAI(base_url="https://api.tokamak.sh/v1", api_key=TOKAMAK_API_KEY)

async def main():
    async with client.realtime.connect(model="openai/gpt-realtime-2.1-mini") as conn:
        await conn.session.update(session={"type": "realtime", "output_modalities": ["text"]})
        await conn.conversation.item.create(item={"type": "message", "role": "user",
            "content": [{"type": "input_text", "text": "What colour is the sky?"}]})
        await conn.response.create()
        async for event in conn:
            if event.type == "response.done":
                print(event.response.usage)
                break

asyncio.run(main())

Any WebSocket client works: connect to wss://api.tokamak.sh/v1/realtime?model=openai/gpt-realtime-2.1 with Authorization: Bearer <key> and exchange OpenAI's GA events. Send input_audio_buffer.append with 24 kHz 16-bit PCM (or G.711) to talk; with server VAD on (the default), the model answers when you stop speaking.

From a browser: client secrets

Never put an API key in a browser. Your server mints a one-time token and hands it to the page:

curl -sS -X POST https://api.tokamak.sh/v1/realtime/client_secrets \
  -H "Authorization: Bearer $TOKAMAK_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"session": {"type": "realtime", "model": "openai/gpt-realtime-2.1-mini", "instructions": "Be brief."}}'
{ "value": "tkrt_eyJhbGciOi…", "expires_at": 1793634572, "session": { "type": "realtime", "model": "openai/gpt-realtime-2.1-mini", "instructions": "Be brief." } }

The token (tkrt_…) opens one session or call, for the model it was minted for, and expires within 60 seconds. The session settings you minted it with are applied when it is used. It cannot call any other endpoint, and it cannot mint another token.

In the browser, pass it as the WebSocket subprotocol, as OpenAI's browser clients do:

const ws = new WebSocket(
  "wss://api.tokamak.sh/v1/realtime?model=openai/gpt-realtime-2.1-mini",
  ["realtime", "openai-insecure-api-key." + token]
);

Only a tkrt_ token is accepted in that subprotocol; an API key there is refused.

WebRTC calls

Audio flows directly between the browser and the model provider; Tokamak sets up the call and keeps a server-side control channel on it, where every turn is metered.

const pc = new RTCPeerConnection();
pc.ontrack = (e) => (audio.srcObject = e.streams[0]);
pc.addTrack((await navigator.mediaDevices.getUserMedia({ audio: true })).getTracks()[0]);
const events = pc.createDataChannel("oai-events");
events.onmessage = (m) => console.log(JSON.parse(m.data).type);

await pc.setLocalDescription(await pc.createOffer());
const r = await fetch("https://api.tokamak.sh/v1/realtime/calls?model=openai/gpt-realtime-2.1-mini", {
  method: "POST",
  body: pc.localDescription.sdp,
  headers: { Authorization: "Bearer " + token, "Content-Type": "application/sdp" },
});
const callId = r.headers.get("Location").split("/").pop();
await pc.setRemoteDescription({ type: "answer", sdp: await r.text() });

POST /v1/realtime/calls also takes OpenAI's multipart form (sdp and session). Hang up from your server with POST /v1/realtime/calls/{call_id}/hangup using an API key of the user who created the call (a client secret is used up by the call itself).

What Tokamak controls in a session

Events pass through as OpenAI sends them, with four controls applied:

  • The model stays the one you connected with. A model in session.update is ignored, as OpenAI ignores it.
  • Every response is capped at 4,096 output tokens by default. A larger max_output_tokens (or "inf") is lowered to the cap; a smaller one is kept. On a WebRTC call, a response your browser starts over its data channel goes straight to the model, so its own max_output_tokens holds; credit is held for that figure (up to the model's limit).
  • Tokamak starts each automatic response. With server or semantic VAD, Tokamak creates the response when your speech is committed, after admitting it for billing. Your client sees the same events it would see from OpenAI, and the session shows your own create_response setting.
  • A transcription model must be a priced Tokamak model (for example openai/gpt-transcribe). An unknown one is answered with an error event and the update is not applied.

Billing

  • Each turn is billed on its own (one response.create and its response.done), at the turn's own text, audio and image token counts. A transcribed input item is billed separately on the transcription model, per second. Nothing is billed again at the end of the session.
  • Each turn is admitted before it starts. Tokamak holds credit for the most a turn can cost: the model's context window at its dearest input rate, plus the output cap at its dearest output rate (about $4.40 for gpt-realtime-2.1, $1.40 for gpt-realtime-2.1-mini). The hold is released when the turn settles; you pay only for what the turn used.
  • When credit runs out, the next turn is refused: you get an error event with code insufficient_quota and the session closes with code 1008. A session or call that cannot afford its first turn is refused with 402 before it opens.
  • Every usage row of a session carries its session id: the WebSocket session's id or the WebRTC call id.

Limits

  • Sessions and calls close after 15 minutes, and a WebSocket session after 5 minutes with no events. Reconnect for a longer conversation.
  • Each organization may hold up to 20 WebRTC calls at once, and up to 20 WebSocket sessions on each Tokamak server (a deployment with several servers allows that many per server).
  • Not served: realtime translation sessions (/v1/realtime/translations), SIP calls (accept, reject, refer), and the retired beta API (/v1/realtime/sessions, OpenAI-Beta: realtime=v1).
  • Audio cannot be screened before it is sent, so a key a guardrail policy applies to gets 403 guardrail_policy_unsupported, unless an organization admin turned on Allow unscreened voice for that policy.

On this page