ag2-live
Skill12 starsChanged 9 days agoApache-2.0View on GitHub
Build voice agents with AG2's `ag2.live` module. Three patterns: (1) turn-by-turn STT → `Agent` → TTS — `OpenAITranscriber` / `ElevenLabsTranscriber` / `MistralTranscriber` `.pipe(agent)` (`VoicePipeline`, `VoiceInput`) plus `TTSObserver` with `OpenAITTSConfig`, `ElevenLabsStreamingTTSConfig`, `MistralTTSConfig` and friends; (2) realtime speech-to-speech `LiveAgent` with `OpenAIRealTimeConfig` or `GeminiRealTimeConfig` (`AudioOutput` / `TextOutput` / `InputConfig`, voices, VAD, tools); (3) `CascadeConfig` — any STT + any LLM + any TTS behind `LiveAgent`, with `SilenceTurnDetector` and barge-in. Audio I/O via `SoundDeviceRecorder` / `SoundDevicePlayer`; headless testing by pushing `RecordedAudioEvent`s. Use for talking agents, voice assistants, live captions, or speaking a text agent's reply. For sending one audio file into a text agent see `ag2-multimodal-input`.
- Reads credentials
- Installs packages
In this repository27 other files it gives its agents.
- AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- ag2-a2aSkill
- ag2-acpSkill
- ag2-add-custom-toolSkill
- ag2-ag-uiSkill
- ag2-eval-comparisonSkill
- ag2-evaluationSkill
- ag2-hitlSkill
- ag2-knowledge-and-memorySkill
- ag2-mcpSkill
- ag2-middlewareSkill
- ag2-multimodal-inputSkill
- ag2-network-discussionSkill
- ag2-network-governanceSkill
- ag2-network-quickstartSkill
- ag2-network-tools-and-viewsSkill
- ag2-network-workflowSkill
- ag2-observers-and-alertsSkill
- ag2-overviewSkill
- ag2-quickstartSkill
- ag2-shell-toolSkill
- ag2-structured-outputSkill
- ag2-subagent-delegationSkill
- ag2-telemetrySkill
- ag2-testingSkill
- ag2-use-builtin-toolsSkill
The file
We split this skill into the rules it gives an agent
1 rule. About 0 are common, 1 is distinctive, and 0 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈1
Distinctive · estimate
This author's own choices. Worth a look.
0
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 106
Run it exactly like pattern 2.
≈ “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
Voice and realtime agents (ag2.live)
When to use
- A talking agent: mic in, voice out, low latency (OpenAI Realtime, Gemini Live), optionally with live transcription.
- A voice pipeline over an existing text
Agent: transcribe → run the agent → speak the reply, or just add TTS to a text agent.
A recorded audio file as one input to a text agent → ag2-multimodal-input (AudioInput). Real sessions need a provider key plus a mic/speaker; everything here constructs without hardware, and scripts/headless_voice_check.py exercises a session with recorded audio.
Pick a pattern
| Pattern | Build with | Latency | Use when |
| Turn-by-turn | Agent + <Transcriber>.pipe(agent) + TTSObserver | 1–3 s/turn | Add voice to an existing text agent; need structured output or other text-agent features |
| Realtime S2S | LiveAgent + OpenAIRealTimeConfig / GeminiRealTimeConfig | <500 ms | Barge-in, server VAD, phone-call UX |
| Cascade | LiveAgent + CascadeConfig(stt=, model=, tts=) | between | Continuous session but a specific voice/LLM/provider mix |
Patterns 2 and 3 differ only in the config= line. Recorded file into a text agent → ag2-multimodal-input.
Install and keys
pip install "ag2[openai]" "sounddevice[numpy]" # OpenAI STT/TTS/Realtime + local mic/speaker
pip install "ag2[gemini]" # Gemini Live (GEMINI_API_KEY / GOOGLE_API_KEY)
pip install "ag2[elevenlabs]" # ElevenLabs (ELEVENLABS_API_KEY)
pip install "ag2[mistral]" "mistralai[realtime]" # Mistral Voxtral (MISTRAL_API_KEY); [realtime] for the Realtime* classesNo ag2[openai-realtime] / ag2[gemini-realtime] extras exist. A missing SDK leaves a placeholder raising ImportError on first use. OpenAI classes take api_key= or client= (not both).
1. Turn-by-turn (STT → Agent → TTS)
from ag2 import Agent, MemoryStream
from ag2.config import OpenAIResponsesConfig
from ag2.context import ConversationContext
from ag2.live import OpenAITTSConfig, OpenAITranscriber, SoundDevicePlayer, SoundDeviceRecorder, TTSObserver
agent = Agent(
"assistant", "You are a helpful voice assistant. Be brief.",
config=OpenAIResponsesConfig(model="gpt-4.1-mini", streaming=True), # streaming -> sentence-level TTS
observers=[TTSObserver(OpenAITTSConfig("gpt-4o-mini-tts", voice="nova"))],
)
pipeline = OpenAITranscriber("gpt-4o-mini-transcribe").pipe(agent)
recorder = SoundDeviceRecorder()
stream = MemoryStream() # one stream for the conversation, one player per turn
reply = None
for _ in range(3):
voice = recorder.record(duration=5) # blocks -> VoiceInput
async with SoundDevicePlayer(context=ConversationContext(stream)): # exit waits for playback to end
reply = await (pipeline.ask(voice, stream=stream) if reply is None else reply.ask(voice))
print(reply.body)VoiceInput(pcm16_bytes, frame_rate, channels) works from any PCM source. Swap providers freely: ElevenLabsTranscriber("scribe_v2"), MistralTranscriber("voxtral-mini-latest"), ElevenLabsStreamingTTSConfig("eleven_flash_v2_5") (streams → playback at time-to-first-byte), MistralTTSConfig(voice_id=...) (needs voice_id or ref_audio).
2. Realtime speech-to-speech
import asyncio
from ag2.live import LiveAgent, SoundDevicePlayer, SoundDeviceRecorder, openai
agent = LiveAgent(
"assistant", prompt="You are a helpful voice assistant.",
config=openai.RealTimeConfig(
"gpt-realtime-mini",
output=openai.AudioOutput(voice="marin"), # or openai.TextOutput()
input=openai.InputConfig(transcription={"model": "gpt-4o-mini-transcribe"}),
),
) # tools: decorate functions with @agent.tool, same as Agent
async def main() -> None:
async with (
agent.run() as context, # yields the shared ConversationContext
SoundDevicePlayer(context=context), # SynthesizedAudioEvent -> speaker
SoundDeviceRecorder(context=context), # mic -> RecordedAudioEvent (24 kHz default)
):
await asyncio.Future() # keep the session openGemini: from ag2.live import gemini → gemini.RealTimeConfig("gemini-3.8-live", output=gemini.AudioOutput(voice="Puck"), input=gemini.InputConfig(transcribe=True)) and SoundDeviceRecorder(context=context, sample_rate=16000) (Gemini input is fixed at 16 kHz).
3. Cascade
from ag2.config import OpenAIResponsesConfig
from ag2.live import CascadeConfig, ElevenLabsStreamingTTSConfig, LiveAgent, OpenAITranscriber, SilenceTurnDetector
agent = LiveAgent("assistant", prompt="Answer in one or two short sentences.", config=CascadeConfig(
stt=OpenAITranscriber("gpt-4o-mini-transcribe"),
model=OpenAIResponsesConfig("gpt-4.1-mini", streaming=True), # any ModelConfig
tts=ElevenLabsStreamingTTSConfig("eleven_flash_v2_5"), # any TTS config
turn_detector=lambda: SilenceTurnDetector(threshold=500.0, silence=0.7), # a factory, not an instance
barge_in=False, # True only with headphones
))Run it exactly like pattern 2. Tools work (≤10 tool round-trips per turn).
Testing without a mic
Everything is events on context.stream: push RecordedAudioEvent(pcm_chunk) into a running session (synthesize the question with OpenAITTSConfig(...).synthesize(text) → 24 kHz PCM) and subscribe to SynthesizedAudioEvent / ModelResponse instead of opening devices. scripts/headless_voice_check.py does this for a cascade end to end.
Pitfalls
- Recorder, player and
run()must share one context (passcontext=context), or audio never reaches the session/speaker. - Never attach
TTSObserverto something that already speaks (AudioOutputS2S orCascadeConfig) → two voices. It belongs on a textAgentor aLiveAgentwithTextOutput(). player.join()does not wait for playback — use oneasync with SoundDevicePlayer()per speaking turn, or the mic records the bot's own voice.- Realtime turn ends with
ModelResponse(text viaModelMessageChunk), notModelMessage. Gemini sends onlyTranscriptionChunkEvents for user speech. - Sample rates: player/recorder/OpenAI default 24 kHz mono int16; Gemini in 16 kHz; ElevenLabs
output_formatmust stay raw PCM (pcm_24000default). - Option types are provider-module only:
openai.AudioOutputvsgemini.AudioOutput— not exported fromag2.live.gemini-3.8-liverejectsTextOutput. - Only function tools over realtime; server-side tool types raise
NotImplementedError. No structured output onLiveAgent— hand off to a text agent via a tool wrappingAgent.as_tool(). OpenAITranslationTranscriberuses OpenAI's SDK-deprecated translations endpoint; in testingwhisper-1returned French untranslated. Prefer a transcriber + an English-answering prompt.
Reference
references/live_reference.md — every constructor and knob, voices/models, events, CascadeConfig/SilenceTurnDetector parameters, streaming TTS protocols, LiveAgent.run() overrides and usage_report. Upstream: website/docs/user-guide/live/{live,stt_tts,live_agent}.mdx, examples/live_playground/.
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.

