Skip to main content
Version: Next

Realtime Voice Internals

Open Agent Kernel (OAK) uses a native live agent session for each call. The OpenAI Agents SDK or Google ADK decides which agent speaks, runs that agent's tools, and handles handoffs. OAK carries audio and text between that session and the LiveKit room.

Who owns what​

ComponentResponsibility
LiveKit edgeRoom participants, caller audio and chat, playback, and the active speaker's participant
OAK realtime poolOne connection per OAK session, input conversion, output pacing, queue delivery, interruptions, and cleanup
OpenAI RealtimeSession or ADK Runner.run_live()Model connection, agent turns, native tools, tool results, handoffs, and native session state

The edge never sends an agent participant's own audio or chat back to the model. The SDK conversation stays in one OAK session even when the speaking agent changes. For example, a coordinator can invite billing to answer, billing can call its invoice tool, and billing can hand the conversation back. The pool only receives the speaker changes and output.

Runner contract​

RealtimeRunner is the shared input and output contract. An adapter accepts caller audio with append_audio() and text with send_text(). Its callback reports audio_delta, transcript_delta, input_transcript, agent_changed, interrupt, speech_started, done, or error. The pool paces these events in order, so the next speaker is announced before their first output. The pool announces the root agent as the first speaker (initial_speaker); an adapter whose root does not speak, such as an ADK Workflow, returns None and reports the first speaking node itself in its agent_changed event, with the roster.

The caller's words​

input_transcript carries one finished caller utterance as text (never partial text, never an empty one). The pool emits it as an InputTranscript stream event when the framework reports it: it rides the same paced queue, so it may reach the edge after agent audio the caller has already heard (a provider can finish transcribing after the response that follows the utterance), and a barge-in keeps it (it is the caller's words, not output the caller will no longer hear). It adds no setting of its own; it appears when the SDK's own input transcription is on:

  • Google ADK: on by default (RunConfig.input_audio_transcription). The adapter reports the finished aggregate of an event authored by user, and ignores partial pieces and history replayed into a new agent at a transfer.
  • OpenAI: set input_audio_transcription in the session's model settings through Module.run_options(..., model_config={"initial_model_settings": {...}}). Without it no InputTranscript is produced and nothing fails.

An edge that does not show a transcript ignores the event. A bring-your-own edge receives a new event type in deliver_chunk: handle only the types you know, or ignore this one.

The pool does not execute tools. A custom runner that emits the old tool_call callback fails the connection with a clear error. Native tools are declared on the native agent and executed by its SDK.

The pool's output queue can hold audio before LiveKit plays it. Sending audio to that queue does not prove the caller heard it. OpenAI's native playback estimate is used until the LiveKit edge can report actual playout. Interruption clears queued output that the caller has not heard.

Native adapters​

  • OpenAI: the entry agent is an agents.realtime.RealtimeAgent. One SDK RealtimeRunner and RealtimeSession serve the call. RealtimeAgent.handoffs select the next agent. Module.run_options(agent, model_config=...) sets the session model and voice.
  • Google ADK: one Runner.run_live() and one LiveRequestQueue serve the call. Event.author identifies the speaker. ADK handles transfers and any Gemini connection changes. Speaking agents in a live workflow must set mode='chat' or mode='task'; ADK raises nothing for a node without one, and the caller hears nothing from it; ADK does not run ParallelAgent or LoopAgent in live mode; they fail inside ADK. Use Gemini(speech_config=...) for a speaking agent's voice, or RunConfig.speech_config for a session voice.

The SDK runs the tools; the pool does not. Before the native session starts, the pool sets ToolContext, Session.current() and Agent.current() once for the call, and the SDK's own tasks inherit them, so a tool sees them as it does on the unary path. The session lock is not held, since a call is one long-lived session. OAK system tools (multimodal, sandbox, schedule, knowledge base) and their prompt guidance are attached as in every other execution mode; an ADK Workflow root holds no tools, so they attach to its speaking agents, looked up by their own names for the agents allow-lists.

A realtime session does not carry the OAK session's framework_context. A finished turn (done, after its MessageEnd passed the post-hooks) stores a copy of the OAK session: it is pickled on the pool loop, where every change to the session happens, then unpickled and written on a worker thread, in turn order. Each write waits for the one before it, so every turn is written in order, also behind a slow store. RealtimeConnection.close() stores the live session once more, after the adapter has disconnected and before the call's identity is unpublished and its volatile cache cleared: the order Runtime.stream uses. Writes run off the loop because it is shared by every call and a store is network I/O. A failed call (_fail, a failed connect) stores nothing more, so a halted or half-finished turn is never stored and the next call starts from the last finished turn.

OAK hooks run on a call as they do in a delta mode: typed text through PreHook.on_run, each caller audio chunk through the optional PreHook.on_stream_input, and every output event through PostHook.on_stream_event, followed by a MessageEnd before each done. See Realtime calls. The input guardrail checks typed text only, and the output guardrail does not run on a streamed reply; use the SDK's own live guardrails where supported. A tool needing human approval or confirmation cannot continue through this LiveKit edge because the room has no decision path; both adapters fail the connection when one is requested (an OpenAI tool with needs_approval, an ADK FunctionTool with require_confirmation) rather than approving it.

A realtime call vs a text run​

A call runs no Runtime.run() or stream(): the native SDK owns the loop. The pool still takes the same Runtime steps those methods take, so a step added there reaches a call too.

Steprun() / stream()Realtime call
Session.current(), Agent.current()async with session and the agent's own scopingRealtimeConnection._hook_context, around connect and each hook call, without the lock
Publish identityRuntime._publishRuntime._publish, when the caller changes (#603 §8.4)
Pre- and post-hooksRuntime hook loopsThe same loops (run_stream_input_hooks, run_stream_event_hooks)
Store the sessionsessions().store() on a normal endThe same call on a worker thread, after each finished turn and once more at close
Unpublish, clear the volatile cachesession._unpublish_run() and the volatile-cache clear, in finallyThe same two calls, at close
Trace spanThe traced runner (run() only)The traced realtime runner, one span per call

What still differs, and why:

  • No session lock. A call lasts minutes; holding the lock would block the session. None is needed: one call owns its session id while it lasts, so only that call uses the session. A new call for the same id waits for the previous call's close to finish first.
  • No human-in-the-loop. A live call cannot pause for an approval. Deferred.
  • ToolContext has an empty request list. Audio has no request list; the pool sets the context around connect.
  • Identity is published per input, the session stored per finished turn and at the end. One call has many turns.
  • stream() has no OAK span. A gap in stream(), tracked separately.

Connection lifetime​

Each OAK session has its own native session and queue. Closing one call closes its resources without closing another call.

An edge that stops, or loses its room, or sees its last caller leave, sends an end-of-session control on the input queue for its session id: an ordinary run request with no requests, carrying the realtime_end message attribute. The pool closes that session's native connection at once, instead of after the idle timeout (300 s). The LiveKit edge stops staging input before it sends the control, so no late frame of its own reopens the session. Because it travels through the queue, it works when the edge and the pool run in different processes, and it arrives after the session's earlier input. If the control cannot be sent, the connection ends by idle eviction as before.

If a call ends while its native connection is still opening, the pool stops the opening and closes the connection once, so no connection is left open.

A native session that fails right after it opens (a bad setting, a quota error) is not reopened by every audio frame. The pool backs off per session: after a failure it drops that session's input for 1 s, then 2 s, 4 s and so on up to 30 s for each failure in a row, and opens no connection before that time. A connection that produced output (audio, a transcript or a turn end) before failing restarts the count, and ending the session clears it. A connect that fails sends the edge one terminal error chunk naming the failure: the back-off drops the redelivered input, so that input never reaches its permanent-failure path, and without the chunk the room would hear nothing.

A call that cannot have its agent is turned away the same way, before any native session is opened. When the runner does not host the agent a call names, or the caller is not allowed to use it, the pool sends the edge one terminal error chunk (Realtime session could not start: Agent '<name>' not found) and backs the session off as above. The input is not handed back to the queue to be retried, and the caller's next frames are dropped until the retry time, when the pool checks again. Ending the session clears this record too.

EdgeLifecycleRESTRequestHandler hosts an EdgeLifecycleAdapter such as LiveKitLifecycleAdapter: it starts one edge per LiveKit room from LiveKit's signed room_started webhook (and from participant_joined when the room has no edge, so a caller who rejoins after it ended gets a new one), on a thread of the IOHandler process, and stops it on room_finished by stopping its runner. The edge also ends its own session when the last caller leaves, since its own agent participants keep the room open and LiveKit would not finish it while the edge is still in it. An edge stopped before it starts never joins. At process shutdown the handler starts no new edge, and one watcher thread, blocked on the shutdown flag, stops every live one; the process does not wait for them, so a session whose end control was not sent closes by idle eviction. The edge registry removes only its own adapter on unregister, so a stopped edge cannot remove a newer one for the same session id. A failed native session produces a terminal error and is removed; a later input starts a new native session. OAK does not replay a transcript or restore a call after a process restart.

Ship your first agent today

Free and open source under Apache 2.0. No licensing costs and no lock-in.