thousands-needle-14336
11/26/2025, 1:36 PMtall-airline-16756
11/26/2025, 1:37 PMtall-airline-16756
11/26/2025, 1:37 PMclass MyAgent(Agent):
async def tts_node(self, text_stream, model_settings):
async def patched():
async for chunk in text_stream:
yield chunk.replace("Phase II", "Phase two")
return await Agent.default.tts_node(self, patched(), model_settings)
• GitHub examples:
◦ Pronunciation/postprocess pattern: https://github.com/livekit/agents/blob/main/examples/voice_agents/structured_output.py
◦ Related example with text transforms: https://github.com/livekit/agents/blob/main/examples/voice_agents/restaurant_agent.py
2) Calling APIs with auth during tool calls
• Yes. Tools can perform authenticated API calls using headers/tokens. Secure secrets are injected from environment variables or a secrets store and used in the tool implementation.
• Sample (conceptual Python):
@function_tool()
async def fetch_profile(ctx):
import aiohttp, os
headers = {"Authorization": f"Bearer {os.getenv('API_BEARER_TOKEN')}"}
async with aiohttp.ClientSession() as s:
async with s.get("https://api.example.com/user", headers=headers) as r:
return await r.json()
• GitHub examples:
◦ Forwarding/tool-call examples: https://github.com/livekit/agents/blob/main/examples/voice_agents/forward_tool_call_frontend.py
◦ Secrets usage reference: builder/secrets docs example (see docs link in repo)
3) Sync and async tool calls
• Yes — you can do both:
◦ Synchronous (await): Agent waits for the tool result before continuing.
◦ Asynchronous (fire-and-forget): Spawn background tasks (e.g., asyncio.create_task(...)) and return immediately so the agent flow continues.
• Samples:
◦ Sync:
result = await slow_api_call()
• Async:
asyncio.create_task(background_job(ctx))
return "Background started"
• GitHub:
◦ Long-running / async examples: https://github.com/livekit/agents/blob/main/examples/voice_agents/long_running_function.py
4) Fallback TTS / STT / LLM models
• Yes. The framework supports fallback adapters so you can define primary and fallback providers for STT, LLM, and TTS. On failure the adapter will attempt the next provider and mark unhealthy providers accordingly.
• Sample (conceptual):
session = AgentSession(
stt=stt.FallbackAdapter([assemblyai.STT(), deepgram.STT()]),
llm=llm.FallbackAdapter([openai.LLM(...), azure.LLM(...)]),
tts=tts.FallbackAdapter([elevenlabs.TTS(), cartesia.TTS()])
)
• GitHub:
◦ Fallback example: https://github.com/livekit/agents/blob/main/examples/voice_agents/error_callback.py
5) Idle message (detect user not responding and prompt)
• Yes. You can configure an inactivity timeout and handle user state change events to send an idle prompt (e.g., “Are you still there?”) after X seconds of silence.
• Sample (conceptual):
session = AgentSession(user_away_timeout=15.0)
@session.on("user_state_changed")
async def on_user_state_change(ev):
if ev.new_state == "away":
await session.generate_reply("Are you still there?")
• GitHub:
◦ Inactive-user handling: https://github.com/livekit/agents/blob/main/examples/voice_agents/inactive_user.py
6) End-of-call (can the agent end the call?)
• Yes. The agent can end the call by deleting the room (or using the room management API) after finishing playback. Use wait_for_playout() to ensure final speech finishes before triggering hangup.
• Sample (conceptual):
await session.generate_reply("Thanks, goodbye!")
await session.wait_for_playout()
await ctx.api.room.delete_room(api.DeleteRoomRequest(room=ctx.room.name))
• GitHub:
◦ Hangup example (telephony agent): https://github.com/livekit-examples/python-agents-examples/blob/main/telephony/agent.py
7) Parallel tool calls
• Yes. Tools can be invoked in parallel/concurrently. The agent/LLM flow can await multiple tool results or treat some as background tasks. When the LLM requests multiple tools, calls typically run concurrently and results are returned as they resolve.
• Sample pattern:
t1 = asyncio.create_task(call_tool_a())
t2 = asyncio.create_task(call_tool_b())
res_a, res_b = await asyncio.gather(t1, t2)
• GitHub:
◦ Tool call ordering / parallel examples: https://github.com/livekit/agents/blob/main/examples/voice_agents/tool_call_ordering.py
If you’d like, I can:
• Produce a single runnable example agent that demonstrates text replacement, an authenticated tool call, sync/async tool patterns, fallback providers, idle prompts, hangup behavior, and parallel tool calls.
• Or provide the TypeScript equivalents if you prefer Node.js for your AWS hosts.
Which would you prefer — a Python runnable example or a TypeScript one for your AWS environment?