What event is to get Agent response in livekit whe...
# ask-ai
b
What event is to get Agent response in livekit when i use Realtime model
t
Thanks for reaching out, Samuel. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks — I’m responding to your question about which event signals an agent response when using a Realtime model in LiveKit. • The event to listen for is
speech_created
on the
AgentSession
. ◦
speech_created
is emitted whenever the agent generates speech (for example via
session.say()
or
session.generate_reply()
). • To proactively trigger a response from the agent in a Realtime session, use
generate_reply()
. ◦
generate_reply()
returns a
SpeechHandle
you can await or attach callbacks to (for example,
await handle.wait_for_playout()
to know when playout finishes). If you’d like, tell me: • Which SDK/language you’re using (JavaScript, Python, etc.), and • Whether you want an example that awaits playout or attaches a callback, and I’ll provide a short code snippet you can drop into your app. Sources: Agent speech and audio | LiveKit Documentation | Streamline troubleshooting with Agent Observability
b
Python SDK
t
Thanks — I’ll show how to detect the agent’s response in the Python SDK for a Realtime model. Key points: • Listen for the
speech_created
event on the
AgentSession
— this is emitted whenever the agent generates speech (for example from
session.say()
or
session.generate_reply()
). • Use
generate_reply()
to ask the agent to produce a reply; it returns a
SpeechHandle
you can await or attach callbacks to (e.g.,
wait_for_playout()
). Example (Python-style pseudocode showing the pattern):
Copy code
# assume you have an AgentSession instance named `session`
# and an async event loop

# 1. Attach a handler to speech_created
def on_speech_created(speech):
    # `speech` is the Speech object created by the agent
    print("Agent generated speech:", speech.id)
    print("Text:", getattr(speech, "text", "<no text available>"))
    # You can access additional metadata on speech here if provided

session.on("speech_created", on_speech_created)

# 2. Trigger the agent to generate a reply
async def ask_agent():
    # generate_reply returns a SpeechHandle
    handle = await session.generate_reply(instructions="Greet the user and introduce yourself")
    
    # wait for the speech to finish playing out
    await handle.wait_for_playout()
    print("Agent playout finished")

# run your ask_agent coroutine in your event loop
Notes and tips: • The
speech_created
event fires as soon as the speech object is produced. Use
SpeechHandle
methods (like
wait_for_playout()
) to know when audio playout completes. • If you prefer callbacks instead of awaiting, attach them to the
SpeechHandle
(for example, a completion callback) if the SDK exposes such methods. • If you’d like, I can provide a concrete runnable snippet that matches the exact Python SDK classes/method names you’re using — tell me which LiveKit Python SDK version you have or paste the small part of your current session setup.