steep-balloon-41261
12/01/2025, 7:17 AMtall-airline-16756
12/01/2025, 7:17 AMtall-airline-16756
12/01/2025, 7:18 AMagent.py example that uses LiveKit Cloud built-in plugins (STT/LLM/TTS/VAD) and shows the SIP-ready options.
1) Minimal agent repo structure and deploy flow with lk agent deploy
• Minimal layout:
◦ src/
▪︎ agent.py # main agent logic (entrypoint)
◦ pyproject.toml # dependencies
◦ Dockerfile # generated by lk tool
◦ .env.local # LiveKit and inference API keys (populated by CLI)
◦ livekit.toml # agent metadata (name, runtime)
• Quick creation & deploy flow:
a. Create from starter template:
• lk app create --template agent-starter-python my-voice-agent
a. Authenticate to LiveKit Cloud:
• lk cloud auth
a. Write src/agent.py (example below).
b. Ensure .env.local contains LIVEKIT_API_KEY, LIVEKIT_API_SECRET, LIVEKIT_URL (CLI can write these).
c. Create agent in Cloud (first-time):
• lk agent create
a. Deploy / update:
• lk agent deploy
a. Monitor:
• lk agent status
• lk agent logs
2) Wiring built-in plugins (Deepgram/OpenAI/ElevenLabs) in an AgentSession without custom adapters
• Use LiveKit Inference plugin descriptors or plugin classes provided by the SDK; you can pass strings or provided plugin objects to AgentSession. That avoids writing custom adapters.
• Examples:
◦ String descriptors (managed inference):
▪︎ stt="assemblyai/universal-streaming:en"
▪︎ llm="openai/gpt-4.1-mini"
▪︎ tts="cartesia/sonic-3:<voice-id>"
◦ Or use SDK plugin constructors:
▪︎ from livekit.plugins import assemblyai, openai, cartesia
▪︎ stt = assemblyai.STT(language="en")
▪︎ llm = openai.LLM(model="gpt-4.1-mini")
▪︎ tts = cartesia.TTS(model="sonic-3", voice="<voice-id>")
• For Deepgram/ElevenLabs/OpenAI, populate corresponding API keys in .env.local (OpenAI/Deepgram/ElevenLabs). The SDK will use them for the built-in plugins.
3) Recommended pattern for SIP integration with Cloud
• Use LiveKit Cloud Telephony (dashboard) to configure trunks and DIDs — Cloud handles SIP bridging for you.
• Typical steps:
◦ In Cloud dashboard → Telephony → create inbound trunk, attach DID(s).
◦ Create dispatch rule: map incoming calls to room prefix (e.g., call-) so calls create a LiveKit room for the agent.
◦ Configure outbound trunk settings (SIP provider address, auth) in the dashboard for making PSTN calls.
• Result: LiveKit Cloud will bridge SIP participants into LiveKit rooms; your agent joins that room (participant.kind will be SIP). You do not need an external SIP-to-WebRTC bridge when using Cloud telephony.
4) Working agent.py example (full voice pipeline using Cloud + built-in plugins)
• The example below uses managed plugins (string descriptors) for STT, LLM, TTS, plus a VAD plugin and turn detector. It demonstrates starting an AgentSession for a room and choosing noise cancellation based on participant kind (SIP vs normal). Drop this into src/agent.py in your repo created above.
# src/agent.py
from dotenv import load_dotenv
from livekit import agents, rtc
from livekit.agents import AgentServer, AgentSession, Agent, room_io
from livekit.plugins import silero
from livekit.plugins.turn_detector.multilingual import MultilingualModel
from livekit.plugins import noise_cancellation
load_dotenv(".env.local")
class VoiceAssistant(Agent):
def __init__(self):
super().__init__(
instructions="""
You are a helpful voice assistant. Keep responses conversational and concise.
When a call connects, greet the caller and ask how you can help.
""",
)
server = AgentServer()
@server.rtc_session()
async def voice_agent(ctx: agents.JobContext):
# Use built-in managed plugins by passing string descriptors
session = AgentSession(
stt="assemblyai/universal-streaming:en",
llm="openai/gpt-4.1-mini",
tts="cartesia/sonic-3:9626c31c-bec5-4cca-baa8-f8ba9e84c8bc",
vad=silero.VAD.load(),
turn_detection=MultilingualModel(),
)
# Start the session attached to the room created by the incoming SIP call
await session.start(
room=ctx.room,
agent=VoiceAssistant(),
room_options=room_io.RoomOptions(
audio_input=room_io.AudioInputOptions(
# Use different noise cancellation depending on participant kind
noise_cancellation=lambda params: (
noise_cancellation.BVCTelephony()
if params.participant.kind == rtc.ParticipantKind.PARTICIPANT_KIND_SIP
else noise_cancellation.BVC()
),
),
),
)
# Send an initial greeting via TTS
await session.generate_reply(instructions="Greet the caller and offer help.")
if __name__ == "__main__":
agents.cli.run_app(server)
Notes and quick tips for fastest POC
• Use the starter template and lk CLI — it scaffolds Dockerfile, livekit.toml, and wiring for deploy.
• Populate .env.local with LiveKit Cloud credentials via lk app env -w.
• Put your OpenAI/Deepgram/ElevenLabs keys into .env.local if you want to use those plugins.
• Use dashboard Telephony → Trunks/Dispatch rules to route DIDs to room prefixes so incoming PSTN calls spin up rooms automatically.
• Test locally first with uv run src.agent:console (starter template provides a console command) before lk agent deploy.
If you want, I can:
• Provide a minimal livekit.toml example tailored to this agent.py.
• Show exact .env.local key names to set.
• Add small error-handling/snippet for reconnects or session.shutdown behavior.
Which follow-up would help you most now?tall-airline-16756
12/01/2025, 7:18 AM.env.local keys and example values
• lk CLI commands and first-time deploy steps
• Plugin wiring details (string descriptors vs SDK plugin objects)
• Telephony dashboard steps (trunks, dispatch rules, DID mapping)
• A runnable agent.py with error handling and local test commands
If you want the runnable agent.py, do you prefer:
• Keep the managed plugin string descriptors (fastest), or
• Use explicit SDK plugin constructors (you’ll provide API keys)?
Tell me which and I’ll send a focused, copy-paste-ready snippet and any config files you picked.tall-airline-16756
12/01/2025, 7:19 AMAgentSession and ensure API keys (OpenAI/Deepgram/ElevenLabs) are in .env.local.
• Telephony (SIP trunks/DIDs) is configured in the Cloud dashboard as before; incoming calls will be bridged into LiveKit rooms and your agent can join those rooms and use Inference.
What I can send next (pick one or more):
• A copy‑paste runnable agent.py that explicitly uses LiveKit Inference string descriptors, with minimal error handling and local test commands.
• A minimal livekit.toml, pyproject.toml, and Dockerfile tuned for lk agent deploy.
• Exact .env.local key names (LiveKit + Inference provider keys) and how to set them with lk app env -w.
• Step‑by‑step Cloud Telephony dashboard actions (create trunk, add DID, create dispatch rule).
• Short checklist of lk CLI commands to go from template → deploy → test.
Which of the above would you like now? If you want the runnable agent.py, confirm: use managed plugin strings (fastest) or explicit SDK plugin objects (you’ll supply keys)?