Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Realtime API

AI gateways front realtime sessions too: one WebSocket, JSON events both ways, text and audio. The mock LLM speaks the OpenAI-shaped realtime protocol at

GET /ai/openai/v1/realtime?model=<model>      (WebSocket upgrade)

with the same deterministic answers as the other AI paths: the modes, the scripted rules and the tool calls come from the same engine.

A session

A session starts with session.created. Send events as JSON text frames:

# One event a line; the sleep keeps the socket open for the answer.
(
  echo '{"type": "session.update", "session": {"instructions": "Be terse."}}'
  echo '{"type": "conversation.item.create", "item": {"type": "message", "role": "user", "content": [{"type": "input_text", "text": "What did you receive?"}]}}'
  echo '{"type": "response.create"}'
  sleep 1
) | websocat --header='X-Request-Id: docs-realtime-1' \
  "$WS/ai/openai/v1/realtime?model=rustybin-echo&tokens_per_second=0"
Client eventDoesAnswered with
session.updateSets instructions, tools, tool_choice, modalities (["text"] or ["audio", "text"]), voice, input_audio_format, output_audio_format, max_response_output_tokenssession.updated
conversation.item.createAdds a message (content input_text or input_audio) or a function_call_outputconversation.item.created
input_audio_buffer.appendAdds base64 audio to the buffernothing
input_audio_buffer.commitMakes the buffer a user messageinput_audio_buffer.committed, conversation.item.created
input_audio_buffer.clearEmpties the bufferinput_audio_buffer.cleared
response.createAsks for a response to the conversationThe response events below
response.cancelStops the response being sentresponse.done with status: cancelled

A response is response.created, response.output_item.added, conversation.item.created, response.content_part.added, then the deltas: response.text.delta for a text session, response.audio_transcript.delta and response.audio.delta for an audio one. Then the matching .done events, response.output_item.done, response.done with the usage, and rate_limits.updated. Every server event has an event_id.

An event the session does not know, or a frame that is not a JSON object, is answered with an error event (invalid_request_error) and the session goes on.

Tools

Tools given in session.update are called as on the chat paths: when the user's text is about the tool, the response is a function_call item whose arguments are valid for the tool's schema.

(
  echo '{"type": "session.update", "session": {"tools": [{"type": "function", "name": "get_weather", "description": "Get the current weather for a location", "parameters": {"type": "object", "properties": {"location": {"type": "string"}}, "required": ["location"]}}]}}'
  echo '{"type": "conversation.item.create", "item": {"type": "message", "role": "user", "content": [{"type": "input_text", "text": "What is the weather in Paris?"}]}}'
  echo '{"type": "response.create"}'
  sleep 1
) | websocat "$WS/ai/openai/v1/realtime?tokens_per_second=0"

Send the result back as a function_call_output item with the call's call_id, then response.create again for the text answer.

Audio

Audio is never decoded or kept. Input audio is counted: its bytes give a duration in the session's input_audio_format (pcm16 at 24 kHz, g711_ulaw or g711_alaw at 8 kHz), and the duration gives tokens (ten a second), reported as usage.input_token_details.audio_tokens.

Output audio is valid frames in the session's output_audio_format: 100 ms of silence a delta, or a 440 Hz tone with ?audio=tone. The transcript deltas carry the text the engine answered. A gateway that meters, records or transforms audio has real frames to work on, and nothing that sounds like speech.

Pacing and faults

?ttft_ms=, ?tokens_per_second=The pace of a response's deltas. X-Rustybin-TTFT-Ms and X-Rustybin-Tokens-Per-Second on the upgrade do the same, and so does session.update with "rustybin": {"ttft_ms": 200, "tokens_per_second": 20}
?fail=<kind> (or X-Rustybin-Fail)Refuses the upgrade with the provider's error, as on every AI path: 429, 503, auth
?fail=drop_after:NDrops the connection after the Nth server event, without a close frame
?fail=error_after:NSends an error event (server_error) after the Nth server event; the session goes on
curl -s -o /dev/null -w '%{http_code}' --http1.1 "$BASE/ai/openai/v1/realtime?fail=429" \
  -H 'Connection: Upgrade' -H 'Upgrade: websocket' \
  -H 'Sec-WebSocket-Version: 13' -H 'Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ=='
# The connection is dropped after the third server event.
(
  echo '{"type": "conversation.item.create", "item": {"type": "message", "role": "user", "content": [{"type": "input_text", "text": "hello"}]}}'
  echo '{"type": "response.create"}'
  sleep 1
) | websocat "$WS/ai/openai/v1/realtime?fail=drop_after:3&tokens_per_second=0" || true

Credentials are checked on the upgrade as on the other OpenAI paths (Authorization: Bearer, enforced with X-Rustybin-Require-Auth or RUSTYBIN_AI_REQUIRE_AUTH), and the answer carries X-Rustybin-Credential.

What the mock saw

A session is one record of the request log, kind: realtime, under the upgrade's X-Request-Id: the totals of its responses as usage, and detail.events, each event's direction, type, size and text (the text of a delta, of a message, of an error), at most 200. Never audio.

curl -s "$BASE/ai/requests/docs-realtime-1"

Limits

Sessions open at once64 (the ai.realtime.max_sessions setting, 1 to 256); one more is refused with the provider's 429
A session30 minutes (ai.realtime.session_minutes), and closed after 5 minutes without a client event
A client event512 KiB
A conversation200 items; the oldest go first

On every surface

SurfaceWhere
APIGET /ai/openai/v1/realtime (WebSocket); the session in GET /ai/requests and /ai/requests/{id}
ConsoleAI playground: Realtime session (connect, say something, the reply and the events as a timeline) and the Request log, where a realtime session shows its events
CLI and management MCPNothing to configure: this is the data plane. The caps are settings (rustybin settings set ai.realtime.max_sessions 16, update_settings)
rustybin.yamlsettings: with ai.realtime.max_sessions and ai.realtime.session_minutes