I’m facing an issue where the agent sometimes make...
# agents
n
I’m facing an issue where the agent sometimes makes a tool call (MCP tool, with a 20-second timeout set), but it doesn’t wait for the response — not even for a second — and proceeds to speak in parallel. After the tool finishes execution (which takes about 2 seconds), the agent responds again. Someone else has also encountered a similar issue — here’s the reference link: https://livekit-users.slack.com/archives/C088ZNU7QQ5/p1761729896570059
hey @refined-appointment-81829 can you pls help here
l
Is this a regression? And is there code to reproduce?
n
My use case is that the agent should respond only when triggered by specific wake words; otherwise, it should remain silent. To achieve this, I implemented the handling logic inside the
on_user_turn_completed
function (code snippet provided below). However, when a user asks the agent a question, it needs to create a record in my system, which requires making a tool call. The issue is that during this process, the agent makes the tool call but starts speaking in parallel instead of waiting for the tool’s response. Then, once the tool execution completes, the agent responds again with the same message.
Copy code
async def on_user_turn_completed(
            self, turn_ctx: llm.ChatContext, new_message: llm.ChatMessage
    ) -> None:

        if self.is_observer and new_message.role == "user":
            transcript = new_message.text_content.strip()
            transcript_lower = transcript.lower()
            logging.info(f"case_id {self.case_id} agent_id {self.agent_id} user said: {transcript}")
            wake_detected = any(wake in transcript_lower for wake in self.wake_words_lower)
            if wake_detected:
                logging.info(
                    f"case_id {self.case_id} agent_id {self.agent_id} Wake word detected! Unmuting agent and responding...")
                self.session.output.set_audio_enabled(True)
                asyncio.create_task(
                    create_item_and_send_to_webhook(new_message.text_content, "user", new_message.created_at,
                                                    self.room.name, self.webhook_url))
                turn_ctx.add_message(role="user", content=new_message.content)
                await self.update_chat_ctx(turn_ctx)
                clean_query = transcript_lower
                for wake in self.wake_words_lower:
                    clean_query = clean_query.replace(wake, "", 1).strip()
                if clean_query:
                    logging.info(f"clean_query {clean_query}")
                    await self.session.generate_reply(
                        instructions=f"Answer this question briefly and directly: {clean_query}",
                        allow_interruptions=True
                    )
                else:
                    await self.session.generate_reply(
                        instructions="The user successfully called you. Respond warmly and ask how you can help.",
                        allow_interruptions=True
                    )

                logging.info(
                    f"case_id {self.case_id} agent_id {self.agent_id} Response complete. Clearing chat context and Muting agent again...")
                self.session.output.set_audio_enabled(False)
                raise StopResponse()
also MCP setting is given below that I am using
Copy code
mcp.MCPServerHTTP(
            url=url,
            timeout=20,
            client_session_timeout_seconds=20,
        )
This issue occurs intermittently
l
I see - have you seen this example, https://github.com/livekit-examples/python-agents-examples/blob/main/home_assistant/homeautomation.py#L77? It implements a wake word by hooking into the STT node, and invokes StopResponse() if the wake word is not detected.