agentleFS
rijansrijans/agent-skills/skills/openai-realtime-webrtc/SKILL.md

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

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
  1. Distinctive:

    Before implementing OpenAI Realtime WebRTC:

    line 32
  2. Very specific:

    Verify OPENAI_API_KEY is configured in backend environment variables.

    line 35
  3. Very specific:

    Ensure browser context supports navigator.mediaDevices.getUserMedia (requires HTTPS or localhost).

    line 40
  4. Very specific:

    Check microphone constraints (echoCancellation: true, noiseSuppression: true, autoGainControl: true).

    line 41
  5. Very specific:

    Never embed master OPENAI_API_KEY in frontend code, client bundles, or repository files.

    line 63
  6. Very specific:

    Always mint a short-lived ephemeral session token on the backend (POST https://api.openai.com/v1/realtime/sessions) and send only …

    line 64
  7. Very specific:

    When calling getUserMedia(), always specify { audio: { echoCancellation: true, noiseSuppression: true, autoGainControl: true } }.

    line 66
  8. Distinctive:

    Never play incoming model audio back into the microphone stream.

    line 67
  9. Very specific:

    When using server_vad, OpenAI automatically detects when the user begins speaking.

    line 69
  10. Very specific:

    When a function call finishes, send conversation.item.create with type function_call_output containing stringified JSON, immediately …

    line 72
  11. Very specific:

    Never Pass Tool Arguments Directly to Shell or eval: Treat dynamic arguments as untrusted user input.

    line 83
  12. Distinctive:

    Enforce Backend Authorization for Data Operations: If a voice agent triggers a database read or payment action, verify session …

    line 84
  13. Very specific:

    System Instructions Sandboxing: When injecting dynamic context (e.g.

    line 85
  14. Distinctive:

    When providing OpenAI Realtime WebRTC solutions:

    line 91

≈ “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.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

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:

  1. OpenAI Credentials & Model Target:
    • Verify OPENAI_API_KEY is configured in backend environment variables.
    • Target model: gpt-4o-realtime-preview (or latest GA realtime model).
  2. 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.
  3. Audio Capture Environment:
    • Ensure browser context supports navigator.mediaDevices.getUserMedia (requires HTTPS or localhost).
    • Check microphone constraints (echoCancellation: true, noiseSuppression: true, autoGainControl: true).

Workflow & Progressive Routing

Load only the reference file required for your active task. Do not preload all references at once.

Task / Focus AreaActionable TriggerReference to Load
Session Minting & Ephemeral Tokens/v1/realtime/sessions, client_secret, voice selection, instructions, securityreferences/session-and-ephemeral-tokens.md
WebRTC Handshake & Audio StreamsRTCPeerConnection, SDP offer/answer, getUserMedia, ontrack, echo cancellationreferences/webrtc-handshake-and-media.md
Data Channel & Barge-In Eventsoai-events, session.update, speech_started, conversation.item.truncatereferences/events-and-data-channel.md
Function Calling & Tool Executionresponse.function_call_arguments.done, function_call_output, response.createreferences/tools-and-function-calling.md
Waveforms & Audio StateWeb Audio API, AudioContext, AnalyserNode, canvas visualizer, listening/speaking statereferences/audio-visualization-and-state.md
Upstream Specs & SourcesOpenAI Realtime API endpoints, WebRTC RFCs, breaking changesreferences/sources.md

Core Guardrails & Best Practices

  1. Zero Master API Key Leakage:
    • Never embed master OPENAI_API_KEY in 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 only client_secret.value to the browser.
  2. 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.
  3. Turn Detection & Barge-In Truncation:
    • When using server_vad, OpenAI automatically detects when the user begins speaking.
    • Listen for input_audio_buffer.speech_started on the data channel to immediately mute/truncate local audio playback and synchronize conversation history via conversation.item.truncate.
  4. Tool Call Response Sequencing:
    • When a function call finishes, send conversation.item.create with type function_call_output containing stringified JSON, immediately followed by sending { type: "response.create" } to prompt the model to speak the result. Omitting response.create causes the model to remain silent.
  5. HTTPS Requirement:
    • Modern browsers strictly disallow getUserMedia over unencrypted HTTP (except localhost). Production deployments must serve over HTTPS.

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:

  1. Provide valid ESM code split cleanly between secure backend session minting and browser WebRTC handling.
  2. Include explicit echoCancellation constraints and audio stream binding (ontrack).
  3. Set up the oai-events WebRTC data channel with robust event dispatching.
  4. Distinguish client-side DOM actions from secure backend-delegated tools.

Download now (ZIP)

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
What did you use it for? e.g. Reviewing pull requests in a Rails app

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.