cuddly-noon-8767
09/01/2025, 9:30 PMclass SoundoffExecutorAgent(Agent):
def __init__(self) -> None:
super().__init__(
instructions=(
"You are the Soundoff Executor.\n"
"- Say exactly the next countdown number in the format: [Executor] N.\n"
"- Immediately after speaking, call the tool `route_after_speak`.\n"
"- Do NOT print JSON or tool arguments."
)
)
async def on_enter(self) -> None:
# Mark active agent
if hasattr(self.session, "userdata") and self.session.userdata is not None:
self.session.userdata.active_agent = "SoundoffExecutor"
_log_state("agent_enter:Executor", self.session)
# Compute the value and instruct LLM to speak it, then call the routing tool
next_value = None
if hasattr(self.session, "userdata") and self.session.userdata is not None:
next_value = self.session.userdata.next_value
if next_value is None:
_log_state("handoff:Executor->GeneralChat:no_state", self.session)
await self.session.generate_reply(
instructions=(
"Speak exactly: [Executor] No countdown set. Handoff -> GeneralChat.\n"
"Then call the tool `finalize_turn_and_route`."
),
tool_choice= NamedToolChoice(
type="function",
function=Function(name= "finalize_turn_and_route"))
)
return
await self.session.generate_reply(
instructions=(
f"Speak exactly: [Executor] {next_value}. Then call the tool `finalize_turn_and_route`."
),
tool_choice= NamedToolChoice(
type="function",
function=Function(name= "finalize_turn_and_route"))
)
@function_tool
async def finalize_turn_and_route(self, context: RunContext):
"""Update countdown state and hand off to the next agent. Does not speak."""
_log_state("tool:finalize_turn_and_route", self.session)
# Use the current next_value (already spoken) and decrement
next_value = None
if hasattr(self.session, "userdata") and self.session.userdata is not None:
next_value = self.session.userdata.next_value
if next_value is None:
_log_state("handoff:Executor->GeneralChat:no_state", self.session)
return GeneralChatAgent()
new_value = next_value - 1
if hasattr(self.session, "userdata") and self.session.userdata is not None:
self.session.userdata.next_value = new_value
if new_value < 0:
_log_state("handoff:Executor->GeneralChat:complete", self.session)
return GeneralChatAgent()
_log_state("handoff:Executor->Coordinator", self.session)
return SoundoffCoordinatorAgent()tall-airline-16756
09/01/2025, 9:30 PMtall-airline-16756
09/01/2025, 9:30 PMsession.generate_reply() method. It’s a great way to keep the conversation smooth and natural before jumping into actions like tool calls.
For your situation, you can tweak the on_enter method to have the agent say something first, then call the tool. Here’s a quick example:
async def on_enter(self):
# Generate a response to the user
await self.session.generate_reply(
instructions=(
f"Speak exactly: [Executor] {next_value}. Then call the tool `finalize_turn_and_route`."
)
)
# Proceed with the tool call
await self.session.generate_reply(
tool_choice=NamedToolChoice(
type="function",
function=Function(name="finalize_turn_and_route")
)
)
This way, the agent talks to the user first, making the interaction feel more natural before running the tool. If you want me to walk you through it or have any questions, just let me know—I’m here to help!
Sources: agents/src/voice/agent_activity.ts | Agent speech and audio | LiveKit Docs | examples/voice_agents/basic_agent.pycuddly-noon-8767
09/01/2025, 9:33 PMtall-airline-16756
09/01/2025, 9:33 PMgenerate_reply multiple times in a single turn can sometimes cause issues, especially when handing off between agents. But don’t worry—LiveKit has a handy built-in feature to handle this smoothly. If generate_reply is triggered while another generation is still underway, it automatically cancels the previous request and focuses on the latest one. This helps avoid any race conditions or overlapping responses.
To keep things neat and avoid any unexpected behavior during handoffs, it’s a good idea to bundle the user-facing message and the tool call into one single generate_reply call. Here’s an example:
await self.session.generate_reply(\n instructions=(\n f\"Speak exactly: [Executor] {next_value}. Then call the tool `finalize_turn_and_route`.\"\n ),\n tool_choice=NamedToolChoice(\n type=\"function\",\n function=Function(name=\"finalize_turn_and_route\")\n )\n)
This way, the LLM crafts the entire response—including the tool call—in one go, which really helps reduce the chance of any race conditions. If you want me to walk you through it more or have any other questions, just let me know—I’m here to help!
Sources: livekit-plugins/livekit-plugins-openai/livekit/plugins/openai/realtime/real... | agents/src/voice/agent_session.ts | Voice activity detection (VAD) parameters | LiveKit Docscuddly-noon-8767
09/01/2025, 9:35 PMtall-airline-16756
09/01/2025, 9:35 PMgenerate_reply call, make sure your instructions clearly include both the user-facing response and the tool call. For example:
await self.session.generate_reply(\n instructions=(\n f\"Speak exactly: [Executor] {next_value}. Then call the tool `finalize_turn_and_route`.\"\n ),\n tool_choice=NamedToolChoice(\n type=\"function\",\n function=Function(name=\"finalize_turn_and_route\")\n )\n)
This way, the LLM can handle both the text response and the tool call at once, thanks to the framework’s ability to manage them on separate channels. If it still doesn’t work quite right, just let me know and we’ll figure it out together.
Sources: livekit-plugins/livekit-plugins-aws/livekit/plugins/aws/experimental/realti...cuddly-noon-8767
09/01/2025, 9:39 PMtall-airline-16756
09/01/2025, 9:40 PM