how to check the who is last speaked agent | calle...
# ask-ai
l
how to check the who is last speaked agent | caller before session.generate reply(). python sudo code if last_speech=="agent": pass else: self.session.generate_reply()
t
Thanks for reaching out, Rajadurai. 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 how to check who spoke last (agent or caller) before calling
session.generate_reply()
in Python. Short answer: listen for the session events that indicate when the agent or caller starts speaking, update a
last_speaker
variable, and check that variable before calling
generate_reply()
. Below is a clear Python pseudocode example you can adapt. It uses event handlers for
agent_state_changed
and
user_state_changed
to maintain who spoke most recently, and only calls
session.generate_reply()
when the caller was last:
Copy code
class MyAgent:
    def __init__(self, session):
        self.session = session
        self.last_speaker = "none"   # "agent", "caller", or "none"
        self._register_listeners()

    def _register_listeners(self):
        # When agent starts speaking, mark last_speaker = "agent"
        @self.session.on("agent_state_changed")
        def on_agent_state_change(event):
            if event.new_state == "speaking":
                self.last_speaker = "agent"

        # When user/caller starts speaking, mark last_speaker = "caller"
        @self.session.on("user_state_changed")
        def on_user_state_change(event):
            if event.new_state == "speaking":
                self.last_speaker = "caller"

    def should_generate_reply(self):
        return self.last_speaker == "caller"

    async def maybe_generate_reply(self, **kwargs):
        # Call this when you would normally generate a reply
        if self.should_generate_reply():
            await self.session.generate_reply(**kwargs)
        else:
            # Optionally log or defer until caller speaks
            pass
Notes and tips: • Use the
agent_state_changed
event to detect the agent starting to speak and
user_state_changed
for the caller starting to speak. • If you need to handle edge cases (overlapping speech, short interruptions), consider: ◦ Adding a small debounce/delay to ensure you’re not reacting to transient state flips. ◦ Tracking timestamps with each event (e.g.,
last_speaker_time
) and only generating replies if the caller spoke within an expected window. • If you want the agent to interrupt the caller in certain situations, add a separate override flag instead of relying only on
last_speaker
. If you’d like, share a bit of your current event payload shape (what fields
event
contains) or the exact conditions when you want to generate replies (e.g., only after silence of N ms), and I’ll adapt the pseudocode to match. Sources: Agent session | LiveKit Documentation | Turns overview | LiveKit Documentation | Agent speech and audio | LiveKit Documentation