Once my realtime agent completes a conversation, i...
# ask-ai
i
Once my realtime agent completes a conversation, it automatically loops to the beginning without user input. How do I prevent this?
t
Thanks for reaching out, Matthew. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To help you prevent your realtime agent from looping back to the beginning of a conversation after it’s completed, here are some friendly steps you can try: • Disable Listening After Completion: At the end of the conversation, make sure to disable audio input by using
setAudioEnabled(false)
. This helps the agent avoid responding to any unintended input. • Use Proper Turn Detection: It’s important to set up turn detection modes carefully. In noisy environments, try not to rely on VAD alone and adjust endpointing delays to stop premature responses. • Implement onExit Handling: Consider using the
onExit
lifecycle node to send a final message and clearly signal the session’s end, so there are no open-ended prompts left hanging. • Manage Session Context: Keep an eye on your session context—make sure it’s either preserved or reset on purpose to avoid any accidental restarts. If you’d like, feel free to share more about the looping behavior you’re seeing—like if it’s repeating a greeting, restarting audio, or starting a new session. I’d be happy to provide more tailored advice to help you out! Sources: Workflows | LiveKit Docs | OpenAI Realtime API integration guide | LiveKit Docs | plugins/openai/src/realtime/realtime_model.ts | livekit-agents/livekit/agents/voice/agent_activity.py | Azure OpenAI Realtime API and LiveKit | LiveKit Docs
i
This is happening without the user speaking at all. So audio input is not the issue, nor is turn detection.
t
It’s great that audio input and turn detection aren’t causing the issue! To help you stop the agent from looping, I suggest using manual turn control. This way, you can turn off automatic triggers and have full control over when the agent processes input. Here’s a simple way to set it up: • Set
turn_detection="manual"
in the
AgentSession
constructor to turn off automatic turn detection. • Take charge of the agent’s actions by using methods like
session.commit_user_turn()
to start responses when you want, and
session.clear_user_turn()
to clear any input you don’t want processed. • Double-check that there aren’t any conflicting settings, like server-side turn detection, enabled in your LLM configuration. This approach makes sure the agent only responds when you say so, preventing any unwanted restarts or loops. If you’d like, I’m here to walk you through the setup or answer any other questions! Sources: Turn detection and interruptions | LiveKit Docs | livekit-agents/livekit/agents/voice/agent_activity.py
i
Is it not the default for an agent that initiates the conversation to not restart the conversation afterwards? I am using an rpc call tool that signals completion to the front-end that fires off midway through the agent's final speech so I cannot cut the session with that tool either.
t
I understand how important it is to have your agent behave just the way you expect, so let me walk you through this gently. By default, an agent that starts a conversation won’t automatically restart once it’s done. But if you’re using an RPC call tool that signals completion while the agent is still speaking, it might cause some unexpected behavior. Here are a few friendly steps to help you manage this: • Explicitly End the Session: Try using a function tool to close the session right after the agent finishes speaking. For instance,
ctx.wait_for_playout()
can help make sure the agent completes its speech before the session ends. • Suppress Auto-Generated Replies: If your tool triggers a reply you don’t want, you can return
None
from the tool or use
ev.cancel_tool_reply()
within the
function_tools_executed
event to stop any unintended responses. • Manual Turn Control: Consider disabling audio input by default and instead use RPC methods like
start_turn
and
end_turn
to clearly control when the agent listens and responds. Following these steps should help keep the agent from restarting or continuing after it’s done with its task. If anything feels unclear or you’d like a hand setting this up, please don’t hesitate to reach out—I’m here to help! Sources: livekit-plugins/livekit-plugins-aws/livekit/plugins/aws/experimental/realti... | Agent speech and audio | LiveKit Docs | Agent speech and audio | LiveKit Docs | packages/basics/src/uninterruptable.ts | Turn detection and interruptions | LiveKit Docs
i
1. Explicit ends cut the speech. Not an option 2. Where would I invoke this? I just need it to not loop after the final message which triggers my rpc tool. 3. Absolutely not
t
I completely understand how important it is to prevent the agent from looping after delivering its final message, while still allowing it to finish speaking naturally. Here are a few friendly approaches you might find helpful: • Coordinate Final Speech: You can use
ctx.wait_for_playout()
or the
SpeechHandle
returned by
say()
. This way, the agent completes its final message smoothly before any other actions come into play. • Use State Management: Try keeping track of a
conversation_ended
flag within your agent’s logic. After the final message is delivered, you can gently stop further replies by raising
StopResponse()
in the
on_user_turn_completed
lifecycle hook. • Leverage on_exit(): Another great option is to implement the
on_exit()
method. This lets you send a one-time farewell message right when the session ends, so there’s no risk of looping. These strategies should help your agent wrap up conversations smoothly and avoid any unintended restarts. If you'd like, I’m here to guide you through implementing any of these solutions—just let me know! Sources: Agents telephony integration | LiveKit Docs | Pipeline nodes and hooks | LiveKit Docs | Agent speech and audio | LiveKit Docs | examples/voice_agents/session_close_callback.py | pipeline-llm/interrupt_user.py