Hey <@U08CKJH7C12>, how can I make the agent wait ...
# agents
b
Hey @refined-appointment-81829, how can I make the agent wait till the user speaks in case if the user asks the agent to hold on for some time. Use case - if the user asks the agent to hold on, it waits for some time and then hallucinates a response from itself. I want it to hold for the seconds specified in user_away_timeout parameter. If the user didn't speak for 20 sec then only ask something to confirm if user is there or not. I have defined a wait tool in my agent code -
Copy code
@function_tool()
async def wait_tool(self):
        """
        Called when you detect hold music, or you have been put on hold or the user asks you wait.
        """
        # logger.info("[Waiting]")
        log.info("[Waiting]")
        self.session._chat_ctx.insert(ChatMessage(type="message",role="assistant",content=["[Waiting]"]))
Following are the parameters for the agent session - for turn detection, I am using EnglishModel().
Copy code
turn_detection=EnglishModel(),
user_away_timeout=20.0,
vad= silero.vad.load(),
min_endpointing_delay=0.75,
max_endpointing_delay=3.0,
For one of the calls, this is the transcript -
Copy code
{
	'id': 'item_b79e5d6aa9d7',
	'type': 'message',
	'role': 'user',
	'content': ["Okay. Hold on. I'm checking."],
	'interrupted': False,
	'transcript_confidence': 0.9838867
}, {
	'id': 'item_908762549fce',
	'type': 'message',
	'role': 'assistant',
	'content': ['Okay, sure.\n'],
	'interrupted': False
}, {
	'id': 'item_11e8fb357f4c',
	'type': 'message',
	'role': 'assistant',
	'content': ['[Waiting]'],
	'interrupted': False
}, {
	'id': 'item_fc6438515241',
	'type': 'message',
	'role': 'assistant',
	'content': ['Okay, great. Could you tell me if the vehicle is released for pickup?\n'],
	'interrupted': False
}, {
	'id': 'item_f11b78b3224e',
	'type': 'message',
	'role': 'user',
	'content': ["I'm still checking. Hold on a minute."],
	'interrupted': False,
	'transcript_confidence': 0.9975586
}
Over here, the agent used waiting tool, but again asked a question after some time. It is not waiting till user_away_timeout parameter and not asking the question "are you there" that I have defined for user state changed to away.
c
If you just wanna wait until user_away_timeout, You can direct agent with system prompt as: 'If user say wait a second, you can say: Okay, sure'. Then you can capture user away event as below
Copy code
@session.on("user_state_changed")
    def _on_user_state_changed(ev: UserStateChangedEvent):
        if ev.new_state == "away":
            asyncio.create_task(inactivity_handler(session))
In inactivity function you can ask is user still in there
Copy code
async def inactivity_handler(session: AgentSession):
    """Handle user inactivity by playing a message and eventually hanging up

    This method is called when the user_away event is triggered after
    user_away_timeout seconds of silence.
    """
    print("User inactivity detected - asking if they're still there")

    try:
        # First warning - ask if they're still there
        await session.say("Are you still in there.", allow_interruptions=True)
In your method probably the problem is after tool call llm try to generate response by using tool response. LLM do not have sense of time.
b
Hmm, so what you are saying is to remove the tool call completely. I can try this. I tried removing user_away_timeout parameter, I set it to None. Then the agent don't interrupt while waiting for the user. I tried one call and it waited for 7 mins till the point I spoke. My theory is that the tool call is fine, and the actual issue is with user_away_timeout parameter. After timeout, it forces the model to generate some output, which is right, but it is not asking the right question.