Is RunContext passed into all function tool calls ...
# ask-ai
w
Is RunContext passed into all function tool calls by default?
t
Thanks for reaching out, Arthur. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! Just to clarify,
RunContext
isn’t automatically passed into all function tool calls. It only gets injected when your function explicitly includes a parameter typed as
RunContext
. The system checks the function’s parameters to decide if it should add the context. So, if you want your function tool to receive
RunContext
, make sure to add it to your function signature like this:
Copy code
@function_tool()
async def my_tool(
    self,
    context: RunContext,  # You need to declare this explicitly
    other_param: str,
) -> str:
    # Here you can access session, function_call, speech_handle, userdata via context
    return "Tool executed"
Just a heads-up:
RunContext
gives you access to the current
session
,
function_call
,
speech_handle
, and
userdata
, but you have to opt-in by adding that type annotation. Hope that helps! Sources: livekit-agents/livekit/agents/llm/utils.py | Tool definition and use | LiveKit Docs
w
Ok, so if I want my agent to hangup the SIP call based on some input from the SIP partiicpant. Should i close the room or end the session?
t
Hey there! If you want to hang up a SIP call based on input from the SIP participant, the best way is to delete the room instead of just ending the session. Deleting the room makes sure the call ends properly for everyone, so the SIP participant won’t be left hearing awkward silence after the agent disconnects. Here’s a simple example to show you how:
Copy code
@function_tool
async def end_call(self, ctx: RunContext):
    """Called when the user wants to end the call"""
    # Let the agent finish speaking
    current_speech = ctx.session.current_speech
    if current_speech:
        await current_speech.wait_for_playout()

    # Delete the room to end the call for all participants
    job_ctx = get_job_context()
    await job_ctx.api.room.delete_room(
        api.DeleteRoomRequest(
            room=job_ctx.room.name,
        )
    )
Also, you can keep an eye on the SIP participant’s call status (like
sip.callStatus
) to catch when they want to hang up. When the status changes to
hangup
, that’s your cue to delete the room and wrap things up.
w
Elaborate on the current_speech attribute
t
The
current_speech
attribute in LiveKit shows you the speech that's currently being generated or played by the agent. It gives you a
SpeechHandle
object, or
None
if there's no speech happening at the moment. Here’s how it can help you: • Waiting for Speech to Finish: You can use
current_speech
to pause and wait until the agent finishes speaking before moving on to the next step:
Copy code
# Wait for the agent to finish speaking
current_speech = ctx.session.current_speech
if current_speech:
    await current_speech.wait_for_playout()
• Handling Interruptions: The system uses
current_speech
to manage interruptions smoothly. For example, it checks if the current speech can be interrupted when a user starts talking. • Manually Interrupting Speech: If you want to stop the current speech yourself, you can call the
interrupt()
method like this:
Copy code
current_speech = self._current_speech
if current_speech is not None:
    current_speech = current_speech.interrupt()
Some quick details: • Type: Returns
SpeechHandle | None
• Access: You can get it through the session context (
ctx.session.current_speech
) • State Management: LiveKit takes care of managing this automatically • Thread Safety: It’s safely handled within the agent’s internal locking to avoid any concurrency issues In short,
current_speech
is your go-to for keeping speech operations in sync, making sure things happen at the right time, and managing interruptions smoothly in voice-based apps. Sources: livekit-agents/livekit/agents/voice/agent_activity.py | Agent speech and audio | LiveKit Docs
w
Should the wait for playout function be called before shutting down the room the agent is in?
t
Absolutely! You’ll want to call the
wait_for_playout
function before shutting down the room the agent is in. This way, any speech the agent is currently delivering gets to finish smoothly, so the user doesn’t experience any sudden cutoffs. Here’s a handy pattern to follow:
Copy code
@function_tool
async def end_call(self, ctx: RunContext):
    """Called when the user wants to end the call"""
    # Let the agent finish speaking
    current_speech = ctx.session.current_speech
    if current_speech:
        await current_speech.wait_for_playout()

    # Proceed to shut down the room
    await hangup_call()
By calling
wait_for_playout
, you make sure the agent’s last message is fully heard, keeping the call ending feeling smooth and professional. Sources: livekit-agents/livekit/agents/job.py | Agents telephony integration | LiveKit Docs | Agent speech and audio | LiveKit Docs | agent.py