openai-realtime-webrtc
Skill2 starsChanged 34 days agoMITView on GitHub
Build low-latency voice AI agents with OpenAI Realtime API over WebRTC, ephemeral session tokens, RTCPeerConnection audio, and data channel tools. Use for sub-300ms speech-to-speech loops, gpt-4o-realtime-preview, server_vad turn detection, push-to-talk, oai-events data channels, and client/server function calling.
- Reads credentials
In this repository11 other files it gives its agents.
- AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- cloudflare-email-workersSkill
- discord-activitiesSkill
- duckdb-wasmSkill
- humanize-textSkill
- mtcuteSkill
- openauthSkill
- restate-sdkSkill
- telegram-serverless-botSkill
- use-telegram-serverlessSkill
The file
Show its rules and text
We split this skill into the rules it gives an agent
14 rules. About 0 are common, 4 are distinctive, and 10 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈4
Distinctive · estimate
This author's own choices. Worth a look.
10
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 32
Before implementing OpenAI Realtime WebRTC:
- Very specific: line 35
Verify OPENAI_API_KEY is configured in backend environment variables.
- Very specific: line 40
Ensure browser context supports navigator.mediaDevices.getUserMedia (requires HTTPS or localhost).
- Very specific: line 41
Check microphone constraints (echoCancellation: true, noiseSuppression: true, autoGainControl: true).
- Very specific: line 63
Never embed master OPENAI_API_KEY in frontend code, client bundles, or repository files.
- Very specific: line 64
Always mint a short-lived ephemeral session token on the backend (POST https://api.openai.com/v1/realtime/sessions) and send only …
- Very specific: line 66
When calling getUserMedia(), always specify { audio: { echoCancellation: true, noiseSuppression: true, autoGainControl: true } }.
- Distinctive: line 67
Never play incoming model audio back into the microphone stream.
- Very specific: line 69
When using server_vad, OpenAI automatically detects when the user begins speaking.
- Very specific: line 72
When a function call finishes, send conversation.item.create with type function_call_output containing stringified JSON, immediately …
- Very specific: line 83
Never Pass Tool Arguments Directly to Shell or
eval: Treat dynamic arguments as untrusted user input. - Distinctive: line 84
Enforce Backend Authorization for Data Operations: If a voice agent triggers a database read or payment action, verify session …
- Very specific: line 85
System Instructions Sandboxing: When injecting dynamic context (e.g.
- Distinctive: line 91
When providing OpenAI Realtime WebRTC solutions:
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
OpenAI Realtime WebRTC (Voice AI Agents)
Build ultra-low-latency, conversational voice AI applications and speech-to-speech agents using the OpenAI Realtime API over WebRTC. Connect browser clients directly to OpenAI's edge infrastructure via RTCPeerConnection, stream bidirectional Opus audio with hardware echo cancellation, manage sessions securely with backend-minted ephemeral tokens, and execute real-time function calls over the oai-events data channel.
When to Use
- Building conversational voice AI assistants, call center agents, or audio chatbots requiring sub-300ms speech-to-speech latency.
- Orchestrating direct browser-to-OpenAI WebRTC connections using ephemeral client secrets (
/v1/realtime/sessions) to eliminate media server bandwidth costs. - Managing Voice Activity Detection (VAD) and barge-in interruptions, automatically halting model speech when the user speaks.
- Implementing Push-to-Talk (PTT) walkie-talkie controls or microphone mute toggles alongside continuous conversational mode.
- Executing bidirectional function calling (client-side UI state changes or backend API delegation) while maintaining a natural voice dialogue loop.
- Visualizing live user/agent audio waveforms using the browser's Web Audio API (
AnalyserNode).
When Not to Use
- Standard Text LLM Completions: For traditional text streaming or chat interfaces, use standard Chat Completions or the Vercel AI SDK.
- Asynchronous Batch Transcription: For transcribing pre-recorded audio files, use the standard OpenAI Whisper API (
/v1/audio/transcriptions). - Durable Distributed Sagas: For fault-tolerant microservice sagas or multi-day workflows, use
restate-sdk. - In-Browser Analytical SQL: For client-side Parquet and database analytics, use
duckdb-wasm.
Inputs to Inspect First
Before implementing OpenAI Realtime WebRTC:
- OpenAI Credentials & Model Target:
- Verify
OPENAI_API_KEYis configured in backend environment variables. - Target model:
gpt-4o-realtime-preview(or latest GA realtime model).
- Verify
- Backend Runtime & Architecture:
- Identify backend framework (Node.js, Bun, Hono, Express, Cloudflare Workers, Next.js API route) for minting ephemeral tokens via
POST https://api.openai.com/v1/realtime/sessions.
- Identify backend framework (Node.js, Bun, Hono, Express, Cloudflare Workers, Next.js API route) for minting ephemeral tokens via
- Audio Capture Environment:
- Ensure browser context supports
navigator.mediaDevices.getUserMedia(requires HTTPS orlocalhost). - Check microphone constraints (
echoCancellation: true,noiseSuppression: true,autoGainControl: true).
- Ensure browser context supports
Workflow & Progressive Routing
Load only the reference file required for your active task. Do not preload all references at once.
| Task / Focus Area | Actionable Trigger | Reference to Load |
| Session Minting & Ephemeral Tokens | /v1/realtime/sessions, client_secret, voice selection, instructions, security | references/session-and-ephemeral-tokens.md |
| WebRTC Handshake & Audio Streams | RTCPeerConnection, SDP offer/answer, getUserMedia, ontrack, echo cancellation | references/webrtc-handshake-and-media.md |
| Data Channel & Barge-In Events | oai-events, session.update, speech_started, conversation.item.truncate | references/events-and-data-channel.md |
| Function Calling & Tool Execution | response.function_call_arguments.done, function_call_output, response.create | references/tools-and-function-calling.md |
| Waveforms & Audio State | Web Audio API, AudioContext, AnalyserNode, canvas visualizer, listening/speaking state | references/audio-visualization-and-state.md |
| Upstream Specs & Sources | OpenAI Realtime API endpoints, WebRTC RFCs, breaking changes | references/sources.md |
Core Guardrails & Best Practices
- Zero Master API Key Leakage:
- Never embed master
OPENAI_API_KEYin frontend code, client bundles, or repository files. - Always mint a short-lived ephemeral session token on the backend (
POST https://api.openai.com/v1/realtime/sessions) and send onlyclient_secret.valueto the browser.
- Never embed master
- Mandatory Acoustic Echo Cancellation:
- When calling
getUserMedia(), always specify{ audio: { echoCancellation: true, noiseSuppression: true, autoGainControl: true } }. - Never play incoming model audio back into the microphone stream; unconstrained audio creates catastrophic feedback loops.
- When calling
- Turn Detection & Barge-In Truncation:
- When using
server_vad, OpenAI automatically detects when the user begins speaking. - Listen for
input_audio_buffer.speech_startedon the data channel to immediately mute/truncate local audio playback and synchronize conversation history viaconversation.item.truncate.
- When using
- Tool Call Response Sequencing:
- When a function call finishes, send
conversation.item.createwith typefunction_call_outputcontaining stringified JSON, immediately followed by sending{ type: "response.create" }to prompt the model to speak the result. Omittingresponse.createcauses the model to remain silent.
- When a function call finishes, send
- HTTPS Requirement:
- Modern browsers strictly disallow
getUserMediaover unencrypted HTTP (exceptlocalhost). Production deployments must serve over HTTPS.
- Modern browsers strictly disallow
Handling External Content & Indirect Prompt Injection Defense
Voice agents process live conversational speech and dynamic tool outputs. Treat all external payloads as untrusted data:
- Isolate Function Call Arguments: Validate all arguments emitted by the model against a strict schema (Valibot or Zod) before executing client or server functions.
- Never Pass Tool Arguments Directly to Shell or
eval: Treat dynamic arguments as untrusted user input; sanitize SQL parameters and escape DOM inputs. - Enforce Backend Authorization for Data Operations: If a voice agent triggers a database read or payment action, verify session authorization on your backend rather than trusting client-side claims.
- System Instructions Sandboxing: When injecting dynamic context (e.g. user profiles or search results) into session instructions, wrap them in clear boundaries (
<user_context>...</user_context>) to resist voice prompt injection.
Deliverable Contract
When providing OpenAI Realtime WebRTC solutions:
- Provide valid ESM code split cleanly between secure backend session minting and browser WebRTC handling.
- Include explicit
echoCancellationconstraints and audio stream binding (ontrack). - Set up the
oai-eventsWebRTC data channel with robust event dispatching. - Distinguish client-side DOM actions from secure backend-delegated tools.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

