When starting an outbound call the agent speaks im...
# ask-ai
b
When starting an outbound call the agent speaks immediately, how can I add a small delay before it speaks?
t
Thanks for reaching out, Anuar. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
If you'd like to add a little pause before the agent speaks during an outbound call, here are a few easy ways to do it: • Set `min_endpointing_delay`: This controls how long the agent waits after the user stops talking before jumping in. For example, you can set it to 1 second like this using the
AgentSession
constructor:
Copy code
session = AgentSession(
    min_endpointing_delay=1.0  # Wait 1 second after user stops speaking
)
• Add a Custom Delay in `on_user_turn_completed`: You can also program a delay using
asyncio.sleep()
inside the
on_user_turn_completed
callback. Here's how:
Copy code
async def on_user_turn_completed(self, turn_ctx: ChatContext, new_message: ChatMessage):
    await asyncio.sleep(1.0)  # Add a 1-second delay
    self.session.generate_reply(instructions="Respond to the user")
• Manage Initial Speech: For outbound calls, you can have the agent stay quiet at first and only reply after the user starts talking. Just make sure to call
generate_reply()
only when it’s the right time. If you want, I’m here to help you set any of these up—just let me know! Sources: VoicePipelineAgent | LiveKit Docs | livekit-agents/livekit/agents/voice/agent_session.py | Agents telephony integration | LiveKit Docs
b
give me an example on how to reply after the user starts talking
t
Here’s a simple way you can set up the agent to reply only after the user starts talking:
Copy code
class MyAgent(Agent):
    async def on_user_turn_completed(self, turn_ctx: ChatContext, new_message: ChatMessage):
        # Wait for the user to finish their turn
        if new_message.text:
            # Then respond naturally to what they said
            self.session.generate_reply(instructions="Respond naturally to the user.")
So, in this example, the
on_user_turn_completed
method makes sure the agent waits until the user has finished their turn (we check this with
new_message.text
) before jumping in with a reply. Feel free to tweak the response logic to fit what you need. Just give me a shout if you want me to walk you through more details or help customize it further!
b
is there an event or a way to know when the user answered the call?
t
Hey there! To figure out when a user picks up a call in LiveKit, you’ve got a couple of handy options: • Event-based detection: Keep an eye on the
sip.callStatus
participant attribute. Once the user answers, this changes to
active
. You can listen for these participant events in the LiveKit room to catch that update. • Synchronous detection: When you create a SIP participant with the
create_sip_participant
API, just set
wait_until_answered=True
. This will pause the call until the user answers or it fails, so you get a clear confirmation right away. These tips are great for SIP-based outbound calls. If you want me to walk you through more details or give you some examples, just let me know—I’m here to help! Sources: Making calls using SIP | LiveKit Docs | Making outbound calls | LiveKit Docs | agent.py
b
how to get the sip.callStatus?
t
To check out the
sip.callStatus
in LiveKit, you can find it in the SIP participant's
attributes
field. This little attribute tells you what's going on with the SIP call right now, and you can keep an eye on it as things happen. For example, you might do something like this:
Copy code
call_status = participant.attributes.get("sip.callStatus")
Here’s a quick rundown of what the
sip.callStatus
values mean: • active: The call is live and the SIP participant is active in the room. • automation: The call is connected, and DTMF tones are being sent. • dialing: The outbound call is on its way, waiting for someone to pick up. • hangup: The call has finished. • ringing: An incoming call is ringing right now. Also, you can listen for changes with events like
on_participant_attributes_changed
to catch updates to
sip.callStatus
as they happen. If you want me to walk you through anything else, just let me know—I’m happy to help! Sources: SIP participant | LiveKit Docs | Making calls using SIP | LiveKit Docs | telephony/sip_lifecycle.py | livekit/livekit_sip.pb.go
b
what are all the participant attributes?
t
Participant attributes in LiveKit are pretty flexible — you can set your own key-value pairs, but there are also some predefined ones for specific participant types like SIP and Ingress participants. Here’s a quick rundown: • SIP Participant Attributes: ◦ sip.callID: This is LiveKit’s SIP call ID ◦ sip.callStatus: Shows the current status of the SIP call (like
active
,
dialing
,
ringing
, or
hangup
) ◦ sip.phoneNumber: The caller or dialed phone number (unless
HidePhoneNumber
is turned on) ◦ sip.trunkID: The SIP Trunk ID used for the call ◦ sip.ruleID: The SIP Dispatch Rule ID for inbound calls ◦ sip.trunkPhoneNumber: Phone number linked to the SIP trunk ◦ sip.h.:* These are automatically mapped SIP headers with the
sip.h.
prefix • Ingress Participant Attributes: ◦ ingress.ingressID: The Ingress ID tied to the participant ◦ ingress.resourceID: The resource ID for the ingress session • Custom Attributes: Feel free to define your own custom attributes for participants by including them in the access token or updating them on the fly via SDKs. These live in the
attributes
field. • Metadata: Each participant also has a
metadata
field to store any app-specific data you want, all packed into a single string. If you want me to walk you through any of these or need some examples, just let me know — I’m happy to help! Sources: Participant attributes and metadata | LiveKit Docs | rpc/io.pb.go | SIP participant | LiveKit Docs
b
can you help me with this runtime error: RuntimeError: cannot use mark_generation_done: no active generation is running.
t
Hey there! That error
RuntimeError: cannot use mark_generation_done: no active generation is running
pops up when
_mark_generation_done()
gets called but there isn’t an active generation going on. No worries though—here’s how you can fix it: • Authorize Generation: Make sure you start a generation with
_authorize_generation()
before you try to mark it as done:
Copy code
speech_handle._authorize_generation()
• Wait for Authorization: If you’re working asynchronously, use
_wait_for_authorization()
to keep things in the right order:
Copy code
await speech_handle._wait_for_authorization()
• Safe Cleanup: Before marking a generation done, double-check that one is actually active:
Copy code
if speech_handle._generations:  # Just making sure a generation exists
    speech_handle._mark_generation_done()
• Use High-Level APIs: It’s often easier to let methods like
AgentSession.generate_reply()
or
SpeechHandle.wait_for_playout()
handle the lifecycle for you. • Handle Interruptions: If a generation gets interrupted, confirm its state before cleaning up:
Copy code
if speech_handle.interrupted and speech_handle._generations:
    speech_handle._mark_generation_done()
If you want me to walk you through any of this or share some examples, just let me know—I’m happy to help! Sources: livekit-agents/livekit/agents/voice/agent_activity.py
b
how to listen to on_participant_attributes_changed event?
t
Hey there! If you want to listen to the
on_participant_attributes_changed
event in LiveKit, you can easily set up an event handler on the
Room
object. This event pops up whenever a participant's attributes get updated. Here’s a quick example to get you started: Python Example:
Copy code
@room.on("participant_attributes_changed")
def on_participant_attributes_changed(changed_attributes: dict[str, str], participant: rtc.Participant):
    print(f"Attributes changed: {changed_attributes}")
    print(f"All attributes: {participant.attributes}")
Just a few things to keep in mind: • Participants need the
canUpdateOwnMetadata
permission in their access token to update their own attributes. • If you want to delete an attribute, just set its value to an empty string (
''
). • This event fires for all participants (both local and remote) and gives you the full set of updated attributes. This approach works smoothly across all LiveKit SDKs, like Python, JavaScript, Go, and more. If you want me to walk you through examples for a specific platform, just let me know—I’m happy to help! Sources: livekit-rtc/livekit/rtc/room.py | Job lifecycle | LiveKit Docs | Inside a session | LiveKit Docs | Participant attributes and metadata | LiveKit Docs | src/room/events.ts