页面简介
Lessons from LiveKit solution architects and forward deployed engineers on taking a voice agent from POC to production: natural conversation, observability, reliability, and cost.
正文摘录
Blog/GuideGiven the number of solutions today (including LiveKit), and how easy it's gotten to code with AI, pretty much anyone can get a demo voice agent running in a matter of minutes. In fact, tens of thousands of developers (and some non-developers) get agents up and running on LiveKit each month. However, this guide isn't just about helping you get a voice agent up and running. It's meant to share the years of experience our solution architects and forward deployed engineers have gained helping clients turn their POCs into live production agents that serve millions of minutes of AI voice agent conversations. AI voice agent production readiness comes down to 3 questionsLink to this section In the next sections, we'll walk you through some recommendations for tackling each bucket of requirements and some tips our engineers have learned implementing AI voice agents. Is the agent smart and natural-sounding enough to converse with our users? Will we be able to figure out how it's doing in production? Did we build it in a way that we can reliably and cost-efficiently scale it? 7 ways to build a smart and natural-sounding voice agentLink to this section 1. Optimize the agent's first response as much as possibleLink to this section When I took tennis lessons in college, my coach said that if you want to be great, you should spend most of your practice time on one stroke: the serve. It's the only shot you control from start to finish, and it sets up the rest of the point. Your voice agent's first response works the same way. Getting the first few seconds of a call right is the number one concern one of our forward deployed engineers hears from teams going to production. And it's well-supported by data we've seen in the field. After measuring, one company found that around 3 out of 4 calls ended at their agent's greeting step. Our recommendation comes down to a couple of things: Measure the first turn on its own. Track the time from pickup to the agent's first word as its own metric, separate from your p50 and p95 turn latency. When we dug into one team's slow openings, the models weren't the problem. Before the agent could speak, it was waiting on an HTTP call to choose its opener that alone took almost an entire second. Moving it out of the way was some of the cheapest latency we've ever found. Load only what the agent needs to say hello, and fetch the rest while it talks. Split your context into two buckets. Bucket one holds the minimum needed for a good greeting, often just the caller's name. Pass it in with the call, through job metadata, room metadata, or a SIP header, so the agent never waits on a lookup. Meanwhile, bucket two holds everything else, and it starts loading while the agent greets the caller. PythonTypeScript1# This code shows how to greet the caller by name while their full record loads in the background2 3import asyncio4import json5 6from livekit.agents import Agent, AgentServer, AgentSession, JobContext, function_tool7 8 9class SupportAgent(Agent):10 def __init__(self, customer_record: asyncio.Task[dict]) -> None:11 super().__init__(instructions="You are a friendly support agent. Keep replies short.")12 self._customer_record = customer_record13 14 @function_tool()15 async def get_account_details(self) -> dict:16 """Look up the caller's account, plan, and recent orders."""17 return await self._customer_record # usually loaded long before the caller asks18 19 20server = AgentServer()21 22 23@server.rtc_session(agent_name="support-agent")24async def entrypoint(ctx: JobContext):25 # Bucket one: just enough to greet, passed in with the dispatch26 metadata = json.loads(ctx.job.metadata)27 28 # Bucket two: start the slow lookup now, but don't wait for it29 customer_record = asyncio.create_task(load_customer_record(metadata["customer_id"]))30 31 session = AgentSession() # stt, llm, tts32 await session.start(room=ctx.room, agent=SupportAgent(customer_record))33 session.generate_reply(34 instructions=f"Greet {metadata['first_name']} by name in one short sentence."35 ) TipKeep the first sentence short and the important part first. 2. Handle turn-taking and interruptions like a human doesLink to this section It's immensely frustrating to chat with an AI voice agent (or a human, for that matter) if they keep interrupting you at the wrong moments or continue rambling on about something that isn't relevant to you. To counter this poor experience, most teams bringing an agent to production use a turn-taking model like LiveKit's turn detector, paired with adaptive interruption handling so a quick "uh-huh" doesn't cut the agent off. It's one line: turn_detection=inference.TurnDetector(). It's also the default, so AgentSession turns it on even if you leave the line out, and it adds adaptive interruption handling whenever your speech-to-text model supports it. Unfortunately, a set-and-forget turn-taking model isn't always perfect for every use case. Our solution architects and forward deployed engineers recommend testing long mid-sentence pauses, short answers like "yes" or "no," and interruptions in every language you support before you launch. If you spot any gaps, override only the setting that fixes what you heard and leave everything else at its default: PythonTypeScript1# This code shows how to turn on LiveKit's turn detector and tune it only if testing finds gaps2 3from livekit.agents import AgentSession, TurnHandlingOptions, inference4 5session = AgentSession(6 # ... stt, llm, tts7 turn_handling=TurnHandlingOptions(8 # LiveKit's turn detector (on by default)9 turn_detection=inference.TurnDetector(),10 # Optional: uncomment only to fix a gap your testing finds11 # endpointing={"min_delay": 0.6}, # wait longer when callers pause mid-thought12 # interruption={"min_words": 2}, # don't stop for a cough or a lone "yeah"13 ),14) For the full list of options and which symptom each one fixes, see the turn-taking tuning guide or Configuring turn detection and interruptions in LiveKit Agents. TipIf you don't have a native speaker on hand to test pauses, LiveKit Agent Simulations with voice are a helpful place to start. 3. Avoid unnatural dead airLink to this section It's pretty normal for a human customer service agent to be on the phone with you and take a second to think about an answer, look something up, or take action. But when they do pause, you're generally confident they're still on the line, and they're usually clear about what they're doing. AI agents also need some time to think, look something up, or take action (especially if they're working with legacy systems), but how you tune the agent to handle and minimize dead air can make a big difference. The three most common ways to do that are: Use fast AI models that get the job done. Throwing in the LLMs you're familiar with (like the latest ones from OpenAI or Anthropic) isn't always the right choice. In fact, in many cases, our solution architects recommend against using them. For example, Gemma 4 31B on LiveKit Inference can often get to the right answers and tool calls with far less thinking time and latency than more general-purpose models. A couple of our clients shifted from GPT-4.1 to Gemma and saw a 300–500ms savings at p50 while still having the agent pass all evals. You can compare models yourself on the LiveKit Inference latency benchmarks. Time to first token on identical requests, from the Gemma 4 launch benchmarks (July 2026). Lower is faster. Gemma 4 31B · LiveKit192 msGPT-5.5966 msGPT-4.11,006 msGemini 3.0 Flash1,095 ms Call long-running tools asynchronously. Our forward deployed engineers work with companies of all shapes and sizes, and many of them run into a system that takes seconds to respond. When you know a lookup is going to take a while, you can make the tool asynchronous so the agent keeps the conversation going while the work runs. Done well, it can multitask better than a human. Here's how a call plays out with an async lookup: Sequence diagram of an agent handling an async claim status lookup: it starts the lookup in the background, keeps talking with the caller, and reports back once the lookup finishes See sequence digram1sequenceDiagram2 participant C as Caller3 participant A as Agent4 participant S as Claims system5 C->>A: "What's the status of claim 4417?"6 A->>S: Starts the lookup in the background7 A->>C: "Looking up claim 4417 now."8 C->>A: "While you're at it, can I update my address?"9 A->>C: Helps with the address10 Note over C,A: 5 seconds of silence11 A->>C: "Still pulling that up, one moment."12 S-->>A: Claim status13 A->>C: "Claim 4417 was approved yesterday." PythonTypeScript1# This code shows how to keep talking during a slow lookup instead of leaving dead air2 3from livekit.agents import RunContext, function_tool4 5 6@function_tool()7async def check_claim_status(ctx: RunContext, claim_id: str) -> str:8 """Look up a claim in the claims system. This can take 10 seconds or more."""9 # The first update makes the tool non-blocking: the agent acknowledges the request,10 # and the conversation keeps going while the lookup runs in the background11 await ctx.update(f"Looking up claim {claim_id} now.")12 13 # If the line goes quiet for 5 seconds, the agent says this instead of leaving dead air14 async with ctx.with_filler("Still pulling that up, one moment.", delay=5):15 claim = await claims_api.get_claim(claim_id)16 17 # Voiced as a follow-up reply once the lookup finishes18 return f"Claim {claim_id} is {claim.status}." Be careful about prompt size and caching. One of the cheapest fixes for slow responses that our solution architects and forward deployed engineers help with is optimizing the size of the prompt and caching it properly. For context, the difference between a small prompt (~2k tokens) and a large prompt (>15k tokens) can be up to ~500ms at p50 and a full second at p95. Caching can be as simple as reordering a prompt: one of our internal tests found a 38% latency improvement just by moving the static con