Wreckless Auto Claims is a voice agent for auto insurance claims. Policyholders call to hear where their claim stands, and Wreckless staff ask questions that search every claim at once. It's built with Deepgram Flux speech-to-text and text-to-speech, Pipecat, Amazon Bedrock, and AWS Strands, and a Next.js claims UI shows the claim on screen while the agent talks about it.
This project started from pipecat-ai/aws-deepgram-workshop, the starter code for the AWS Workshop Building intelligent voice AI agents with Amazon Nova Sonic, Amazon Bedrock and Pipecat. If you want to learn how a Pipecat voice agent comes together step by step, start with the workshop. It also sets up the AWS environment this code needs, including the Bedrock knowledge base, so follow its setup instructions before you run this agent.
To learn more about the architecture of a Pipecat botfile, you can read through the Pipecat Overview. The rest of this guide will use some basic Pipecat terminology that's explained in those docs.
Let's build the agent.py file that contains your agent.
You'll need these imports at the top of agent.py. You can add them now, or refer back to this list as you add other code to the file.
import asyncio
import dataclasses
import os
import re
import threading
import time
import unicodedata
from collections.abc import Awaitable, Callable
from datetime import datetime
from urllib.parse import unquote, urlparse
import boto3
from botocore.exceptions import ClientError
from dotenv import load_dotenv
from loguru import logger
from pipecat.adapters.llm.schemas.function_schema import FunctionSchema
from pipecat.adapters.llm.schemas.tools_schema import ToolsSchema
from pipecat.frames.frames import (
EndFrame,
Frame,
InputAudioRawFrame,
LLMMessagesAppendFrame,
LLMRunFrame,
LLMUpdateSettingsFrame,
STTUpdateSettingsFrame,
TTSSpeakFrame,
TTSUpdateSettingsFrame,
UserIdleTimeoutUpdateFrame,
)
from pipecat.pipeline.pipeline import Pipeline
from pipecat.pipeline.worker import PipelineParams, PipelineWorker
from pipecat.processors.aggregators.llm_context import LLMContext
from pipecat.processors.aggregators.llm_response_universal import (
LLMContextAggregatorPair,
LLMUserAggregatorParams,
)
from pipecat.processors.frame_processor import FrameDirection, FrameProcessor
from pipecat.processors.frameworks.rtvi.frames import RTVIServerMessageFrame
from pipecat.processors.frameworks.rtvi.observer import (
RTVIFunctionCallReportLevel,
RTVIObserverParams,
)
from pipecat.runner.types import RunnerArguments
from pipecat.runner.utils import create_transport
from pipecat.services.aws.llm import AWSBedrockLLMService
from pipecat.services.deepgram.flux.stt import DeepgramFluxSTTService
from pipecat.services.deepgram.flux.tts import DeepgramFluxTTSService
from pipecat.services.llm_service import FunctionCallParams
from pipecat.transports.base_transport import BaseTransport, TransportParams
from pipecat.transports.daily.transport import DailyParams
from pipecat.utils.text.transforms import expand_currency
from pipecat.workers.runner import WorkerRunner
from strands import Agent, tool
from strands.models import BedrockModel
import claims
import voices
from voices import VoiceYour bot uses a few different third-party services. The standard security practice is to make the API keys for those services available as environment variables. Here, we'll use a Python library to load a .env file with our keys:
load_dotenv(override=True)
# Bedrock Knowledge Base Configuration
KNOWLEDGE_BASE_ID = os.getenv("KB_ID")
# How long a claim lookup runs before the agent says a hold line. A lookup
# that answers sooner goes straight to the answer: a hold line before an
# instant answer is a delay, not a courtesy.
HOLD_LINE_DELAY_SECS = 1.0
# How much of the caller's first audio Flux hears as silence. Opening a
# microphone can click, and Flux can hear the click as a word ("two"). It's
# over in a few tens of milliseconds, and a caller can't start talking in less
# than 200, so nothing they say is lost.
MIC_OPEN_GATE_MS = 200
# How long the caller can stay quiet after the agent finishes speaking before
# it asks if they're still there. Another stretch that long and it says goodbye
# and hangs up, so an abandoned call doesn't hold a session open. 0 turns it
# off, and an empty value in .env means the default.
USER_IDLE_TIMEOUT_SECS = float(os.getenv("USER_IDLE_TIMEOUT_SECS") or "15")Rename example.env to .env and add your keys, as well as your KB_ID.
USER_IDLE_TIMEOUT_SECS is optional. It's how many seconds of silence the agent waits, after it finishes speaking, before it checks in, and then again before it hangs up. It defaults to 15. Set it to 5 to try the hang-up without a long wait, or to 0 to turn it off. See Hanging up when the caller goes quiet.
The knowledge base holds the sample auto claims in kb-data/claims/, one file per claim, named by claim ID (CLM-2026-847291.txt). If your knowledge base still has older sample claims, follow Reset the knowledge base to erase them and load these. Uploading the new files on top isn't enough, because the old records stay in the vector store.
If you're using git, make sure to ignore that file in your .gitignore file:
# .gitignore
(...)
.env
Back in your agent.py file, we'll create a function called run_bot. This function contains the core logic of your bot. We'll use some other functions later in the file to run this function.
run_bot accepts three parameters:
- The
transportparameter describes how your bot should connect to the user. By passing in different transport objects and configurations, you can use this samerun_botlogic through a direct WebRTC connection using aSmallWebRTCTransport, or through Daily's hosted WebRTC service usingDailyTransport, or through a websocket to Twilio using aWebsocketTransport. - The
runner_argsparameter includes information unique to this bot session. For example, you can userunner_argsto greet each user by name, or even pass the bot's system prompt in throughrunner_args. - The
personaparameter says who's on the call: a policyholder or a Wreckless Auto employee.
Two kinds of people use this agent, and they need different tools, prompts, and screens:
| Persona | Who | What the agent does |
|---|---|---|
policyholder |
A customer calling about their own claim | Verifies the caller with a claim number and last name, reads that one claim record, and answers from it directly |
employee |
Wreckless staff | Searches every claim on file. The pipeline LLM hands each question to a Strands research agent that runs as many knowledge base searches as it needs |
The persona travels with the call. The claims UI has a Policyholder / Wreckless staff toggle on its landing screen, and it sends the choice with the WebRTC offer. Pipecat's runner hands it to the bot as runner_args.body, and the bot() entry point at the bottom of the file reads it with persona_from_body before calling run_bot. Anything other than "employee", including no body at all, gets the policyholder persona, so a client that knows nothing about personas still works. The body also carries the voice picked in the UI's Settings panel, which voice_from_body reads the same way; see Picking a voice in Settings. Add the persona names just above where run_bot will go:
# Who is on the call. The claims UI picks it before the call starts.
POLICYHOLDER = "policyholder"
EMPLOYEE = "employee"
# The tools that look up claims, which can take long enough to need a hold line
CLAIM_TOOLS = {"search_knowledge_base", "search_claims"}
def persona_from_body(body) -> str:
"""The persona the client asked for: employee, or policyholder by default."""
if isinstance(body, dict) and body.get("persona") == EMPLOYEE:
return EMPLOYEE
return POLICYHOLDER
def voice_from_body(body) -> Voice:
"""The voice the call starts in: the one picked in Settings, if it's one we have.
The browser remembers the pick, so a stale or hand-edited name can arrive
here. Anything that isn't in the catalog starts the call in the default.
"""
if isinstance(body, dict) and isinstance(body.get("voice"), str):
return voices.find_voice(body["voice"]) or voices.DEFAULT_VOICE
return voices.DEFAULT_VOICECLAIM_TOOLS names the tools that look up claims, one per persona. The hold line you'll add later plays for either of them.
async def run_bot(
transport: BaseTransport,
runner_args: RunnerArguments,
persona: str = POLICYHOLDER,
voice: Voice = voices.DEFAULT_VOICE,
):
logger.info(f"Starting Wreckless claims voice agent ({persona})")The heart of your botfile is the Pipeline. This is where you define the series of processors that will receive input from the user, process it, and generate output. Let's define our pipeline now, inside the run_bot function:
# Build the pipeline
pipeline = Pipeline(
[
transport.input(),
mic_gate,
stt,
user_aggregator,
llm,
tts,
transport.output(),
assistant_aggregator,
]
)Let's look quickly at each processor in the pipeline.
The transport.input() is receiving media from the user and sending it through the pipeline as audio (and possibly video) frames. It's also creating other frames for things like connect/disconnect events, or when the user starts and stops speaking.
The stt processor is a speech-to-text service that receives the user's audio frames, sends them to a transcription service, and outputs the received transcription as TextFrames. We're using Deepgram Flux, a conversational speech recognition model with built-in turn detection: along with the transcript, it tells us when the user starts speaking and when they've finished their turn.
The user_aggregator processor is collecting text frames from the STT processor and listening for Flux's start-of-turn and end-of-turn events to know when the user has completed a 'turn'. Once Flux reports the end of the turn, this processor emits an LLM context frame to prompt a completion from the LLM processor.
The llm sends LLM context frames to an LLM service and returns the responses as a stream of text frames.
The tts service collects text frames from the LLM and sends them to a text-to-speech service, and then outputs audio frames with the generated audio.
The transport.output() receives the audio frames from the TTS service and plays them for the user.
Finally, the assistant_aggregator sees the audio and text frames after the transport has 'played' them, and aggregates them to store the bot's responses. The user and assistant aggregators are created as a pair that shares one LLMContext, which is how the user and bot turns are stored together.
If you try to run this botfile right now, you'd get an error, because none of those processors are actually defined yet. Let's add code just above here to define a few of these processors.
kb = BedrockKnowledgeBaseClient(KNOWLEDGE_BASE_ID)
# Claim records read on this call, by claim ID, so each is read once
records: dict[str, dict] = {}
# Employees research across claims with a Strands agent; policyholders
# look up one record and don't need one
research_agent = ClaimsResearchAgent(kb, records) if persona == EMPLOYEE else None
# The voice is the caller's only language setting: it picks the listen
# model and the reply language too. The call starts in the voice picked
# in Settings, English by default.
current_voice = voice
# FluxSTT is DeepgramFluxSTTService with one guard for language switches;
# see "Applying a switch" below
stt = FluxSTT(
api_key=os.getenv("DEEPGRAM_API_KEY"),
settings=DeepgramFluxSTTService.Settings(**voices.listen_settings(current_voice)),
)
tts = DeepgramFluxTTSService(
api_key=os.getenv("DEEPGRAM_API_KEY"),
sample_rate=24000,
settings=DeepgramFluxTTSService.Settings(voice=current_voice.model),
# The prompt asks for no markdown; this guarantees it is never spoken
text_transforms=[("*", strip_markup_symbols)],
)
# The model has no calendar, so tell it what day it is
now = datetime.now()
today = f"{now:%A, %B} {now.day}, {now.year}"
llm = AWSBedrockLLMService(
aws_region="us-east-1",
settings=AWSBedrockLLMService.Settings(
model="us.anthropic.claude-haiku-5-5",
system_instruction=build_system_instruction(persona, current_voice, today),
),
)Every call gets its own BedrockKnowledgeBaseClient and its own records cache, keyed by claim ID, so a claim is read from the knowledge base once per call however many questions the caller asks about it. Only the employee persona gets a ClaimsResearchAgent. A policyholder's question is about one verified record, so there's nothing for a second agent to research.
Each service takes its configuration through a Settings object. The LLM's system_instruction is the system prompt that tells the bot how to behave, and build_system_instruction writes it for the persona on the call. Add it, with the greetings, above run_bot:
GREETINGS = {
POLICYHOLDER: (
"Thanks for calling Wreckless Auto claims. I can look up the "
"status, repair estimate, payments, or adjuster notes on an existing auto "
"claim. What's your claim number?"
),
EMPLOYEE: "Wreckless Auto claims desk. What would you like to look up?",
}
async def on_screen_view(record: dict) -> dict:
"""The claim a staff member has open, as the prompt gets it.
The masked record, with dates and dollar amounts ready to speak, the same
view the policyholder lookup returns. It's built from the record the
server read, never from anything the browser sent.
"""
return {
"claim_id": record["claim_id"],
"record": await expand_currency(claims.spoken_view(record), "*"),
}
def on_screen_section(on_screen: dict) -> str:
"""The ON SCREEN block: the claim a staff member has open, and how to use it.
It lives in the system prompt, not the conversation, so it always matches
the screen: opening another claim replaces it, and closing one removes it.
"""
return (
"ON SCREEN:\n"
f"- The staff member has claim {on_screen['claim_id']} open and can see "
"it. Its record is below.\n"
"- 'This claim', 'this one', 'what I'm looking at', or 'on screen' means "
"this record.\n"
"- Answer questions about it from this record, without calling "
"search_claims. Call search_claims only when they ask about other "
"claims, or about something this record doesn't have.\n"
"- They're reading along, so show them the answer instead of telling "
"them where it is: before you answer, call show_on_screen with the "
"sections the answer is in, and their screen highlights them. Never "
"name a card or say where something is on the screen.\n"
"- Answer only what was asked; they can read the rest.\n"
f"RECORD: {on_screen['record']}"
)
def greeting_instruction(persona: str, voice: Voice) -> str:
"""The first turn's instruction: the persona's greeting, in the voice's language.
The greeting is written in English. In English it's said word for word;
in any other language the model translates it, since a Spanish voice
reading English text sounds like an accent, not a greeting.
"""
greeting = GREETINGS[persona]
if voice.lang == "en":
return f"Start by saying exactly this: '{greeting}'"
return f"Start by saying this, in {voices.language_of(voice).name}: '{greeting}'"
def build_system_instruction(
persona: str, voice: Voice, today: str, on_screen: dict | None = None
) -> str:
"""The system prompt, written for the ear: the agent is heard, not read.
It's rebuilt whenever the voice changes, since it names the voice and the
language to reply in, and whenever a staff member opens or closes a claim.
HOW TO SPEAK, PRIVACY, and VOICE AND LANGUAGE are the same for both
personas. on_screen, from on_screen_view, is the claim a staff member has
open; policyholders see only their own verified claim, so it's ignored
for them.
"""
if persona == EMPLOYEE:
role = (
"You are the internal claims assistant for Wreckless Auto staff. "
"You answer questions across every auto claim on file: which claims "
"match, their status, amounts, adjusters, and notes."
)
else:
role = (
"You are the auto claims line voice agent for Wreckless Auto. You "
"help callers look up existing auto claims: status, repair estimates, "
"payments, dates, the vehicle, and adjuster notes."
)
how_to_speak = (
"HOW TO SPEAK:\n"
"- You are being heard, not read. Never use markdown, bullet points, "
"numbered lists, headings, or emoji.\n"
"- Keep replies to one or two sentences, and ask one question at a time.\n"
"- Say dollar amounts in words and dates with their weekday, e.g. "
"'Monday, September 14th'.\n"
"- When you repeat a claim ID back, say the letters, then the digits one "
"at a time, e.g. 'C L M, two zero two six, eight four seven two nine one'.\n"
"- If you do not know something, say so plainly. Never guess.\n"
)
if persona == EMPLOYEE:
work = (
"SEARCHING CLAIMS:\n"
"- For any question about claims, call search_claims. Never answer "
"claim details from memory.\n"
"- Pass the question as a standalone question: turn 'what about the "
"adjuster on that one?' into 'who is the adjuster on Daniel Mitchell's "
"claim?'.\n"
"- Say how many claims matched, then the few that matter most. Offer "
"to go through the rest instead of listing them all.\n"
"- You may answer general auto insurance questions briefly, in general "
"terms, without searching.\n"
"- Staff can open one claim from the results. If they ask about 'this "
"claim' or what's on screen and there's no ON SCREEN section below, "
"say no claim is open and ask which one they mean.\n"
"- Never invent a claim, amount, date, or status.\n"
)
else:
work = (
"CLAIM LOOKUPS:\n"
"- For any question about a specific claim, call search_knowledge_base. "
"Never answer claim details from memory.\n"
"- The lookup returns the whole claim record. Answer only what was "
"asked, in one or two sentences; leave out the rest of the record.\n"
"- Call search_knowledge_base for every question about the claim, "
"follow-ups included, even when its record is already in the "
"conversation. The call is what shows the caller the answer on their "
"screen, so pass the sections the question is about.\n"
"- Claim IDs look like CLM-2026-847291: the letters C L M, a four-digit "
"year, and a six-digit number. Callers may skip the letters or say the "
"year as 'twenty twenty-six'; pass the ID as CLM-2026-847291.\n"
"- If you are missing the year or any of the six digits, ask for them. "
"Never fill in or change a digit.\n"
"- You may answer general auto insurance questions, like what a "
"deductible is or how collision and comprehensive differ, briefly and "
"in general terms.\n"
"- Never invent a claim, amount, date, or status.\n"
)
privacy = (
"PRIVACY:\n"
"- Never read out a policyholder's address, phone number, or email, or "
"a full VIN or license plate. You may say the last four characters. "
"Changes to contact details go through the policyholder's Wreckless Auto agent.\n"
"- You may say the other driver's name and insurer, never their policy "
"number, VIN, or plate.\n"
"- Never read map coordinates or photo IDs. You may say how many photos "
"are on file.\n"
)
if persona == EMPLOYEE:
rules = (
"ESCALATION:\n"
"- This desk looks up claims; it can't change them. If asked to update "
"a claim, say the assigned adjuster makes changes.\n"
)
else:
rules = (
"VERIFICATION:\n"
"- Before you share anything about a claim, verify the caller. Once you "
"have the claim number, ask for the policyholder's last name.\n"
"- Call search_knowledge_base only when you have both, and pass both.\n"
"- If the lookup says the details don't match, tell the caller the claim "
"number and last name don't match our records, and ask them to repeat the "
"number and spell the last name.\n"
"- Never say, confirm, or hint at the name on a claim, and never share "
"details of a claim the caller has not been verified for.\n"
"\n"
"ESCALATION:\n"
"- If the caller wants to file a new claim, dispute a decision, or asks for "
"a person, say this line can only look up existing claims and their "
"assigned adjuster can help.\n"
"- If the caller needs a tow or roadside help, say this line can't send "
"it and they should call the roadside assistance number on their "
"insurance card.\n"
"- If the caller describes an emergency or says someone is hurt, tell them "
"to hang up and call 911.\n"
)
voice_rules = (
"VOICE AND LANGUAGE:\n"
"- If the caller asks for another language, or to speak with a different "
"voice, call set_voice. Every language has its own voices.\n"
"- Call set_voice before saying anything about the switch; you confirm "
"after it.\n"
"- Claim records are in English. Translate what you read from them into "
"the language you reply in."
)
blocks = [
role,
voices.voice_intro(voice),
f"Today is {today}.",
"",
how_to_speak,
work,
privacy,
rules,
voice_rules,
]
if persona == EMPLOYEE and on_screen:
blocks += ["", on_screen_section(on_screen)]
return "\n".join(blocks)The prompt is built from blocks. HOW TO SPEAK, PRIVACY, and VOICE AND LANGUAGE are the same for both personas, because both hear the agent the same way and both get the same masked claim data. The role line, the block that describes the claim tool, and the rules at the end differ. A policyholder gets CLAIM LOOKUPS, VERIFICATION, and the full ESCALATION list. An employee gets SEARCHING CLAIMS, which tells the model to rewrite a follow-up like "and the adjuster?" into a standalone question before it searches, and a one-line ESCALATION: the desk looks up claims but can't change them. When a staff member has a claim open, an ON SCREEN block at the end holds its masked record, so "what am I looking at?" is answered without a search; What staff have open covers how it gets there. It's written for a listener who hears every word, and the Prompting for the ear section below explains each rule in it.
The STT model, the TTS voice, and one line of the prompt all come from current_voice, because the caller can change language mid-call by asking for a different voice. Switching languages by voice covers how that works.
Our main LLM, the one in the pipeline, is the one that talks to the caller. It reaches claim data through one tool call per persona. Claim records are personal, so the policyholder's tool verifies the caller before it returns anything: the LLM must pass the policyholder's last name with the claim ID. Define the search_knowledge_base handler, along with the helpers it uses to message the screen and load a claim:
# Failed verifications per claim ID, so a caller can't keep guessing names
max_verification_attempts = 3
failed_verifications: dict[str, int] = {}
# The claim on the caller's screen, so it's sent once per verified claim
shown_claim_id: str | None = None
# Staff only: the claim open on their screen, from on_screen_view, for
# the prompt's ON SCREEN section. The browser only ever sends its ID.
on_screen: dict | None = None
# Bumped each time a claim is opened or closed, so a slow load can't put
# back a claim the staff member has already closed or moved on from
screen_changes = 0
async def refresh_prompt():
"""Rebuild the system prompt for the current voice and the claim on screen.
It replaces the old prompt rather than adding to the conversation, so
the model never answers from a claim that's no longer open.
"""
await llm.queue_frame(
LLMUpdateSettingsFrame(
delta=AWSBedrockLLMService.Settings(
system_instruction=build_system_instruction(
persona, current_voice, today, on_screen
)
)
)
)
async def send_to_client(data: dict):
"""Send a message to the claims UI. task is created below, before any call."""
await task.queue_frame(RTVIServerMessageFrame(data=data))
async def send_voice():
await send_to_client(
{
"type": "voice",
"voice": current_voice.name,
"language": current_voice.lang,
"language_name": voices.language_of(current_voice).name,
"stt_model": voices.listen_settings(current_voice)["model"],
}
)
async def load_claim(claim_id: str) -> dict | None:
"""The claim's record, read once per call: one search and one file read."""
if claim_id not in records:
# boto3 blocks, so it runs on a thread: on the event loop it would
# freeze the whole pipeline, hold line included, until it returned
record = await asyncio.to_thread(kb.load_claim, claim_id)
if record is None:
return None
records[claim_id] = record
return records[claim_id]
async def search_knowledge_base(params: FunctionCallParams):
nonlocal shown_claim_id
query = params.arguments.get("query", "")
claim_id = normalize_claim_id(str(params.arguments.get("claim_id", "")))
last_name = params.arguments.get("last_name", "")
if not (query and claim_id and last_name):
await params.result_callback(
{
"error": "Missing full claim ID or last name",
"response": "Ask the caller for their full claim number, the letters C L M followed by the four-digit year and the six-digit number, and the policyholder's last name before looking up a claim.",
}
)
return
if failed_verifications.get(claim_id, 0) >= max_verification_attempts:
await send_to_client({"type": "verification", "status": "locked"})
await params.result_callback(
{
"verified": False,
"response": "Too many failed verification attempts for this claim. Tell the caller you can't share details on this claim during this call, and that they can contact their Wreckless Auto agent for help.",
}
)
return
if not claims.latin_letters(last_name):
# A name in another script can't match the English records. Ask
# for a spelling instead of counting it as a failed attempt.
await params.result_callback(
{
"verified": False,
"response": "Ask the caller to spell the policyholder's last name letter by letter, as it is written on the policy.",
}
)
return
# Verify in code, against the record itself, before any of it reaches
# the LLM, so no prompt can talk the agent out of it
if claim_id != shown_claim_id:
await send_to_client({"type": "verification", "status": "checking"})
started = time.perf_counter()
try:
record = await load_claim(claim_id)
except Exception as e:
logger.error(f"Error loading claim {claim_id}: {e}")
await send_to_client({"type": "verification", "status": "error"})
await params.result_callback(
{
"verified": False,
"response": "The claim lookup failed. Apologize and ask the caller to try again in a moment.",
}
)
return
if record is None or not claims.policyholder_matches(record, last_name):
failed_verifications[claim_id] = failed_verifications.get(claim_id, 0) + 1
locked = failed_verifications[claim_id] >= max_verification_attempts
# The UI learns only that it failed, never why
await send_to_client(
{"type": "verification", "status": "locked" if locked else "failed"}
)
logger.info(
f"Verification failed for claim {claim_id} "
f"({failed_verifications[claim_id]}/{max_verification_attempts})"
)
# The same answer whether the claim doesn't exist or the name is
# wrong, so a caller can't probe for valid claims or names
await params.result_callback(
{
"verified": False,
"response": "The claim number and last name don't match our records. Share no claim details. Ask the caller to repeat the claim number and spell the policyholder's last name.",
}
)
return
logger.info(
f"Verified caller for claim {claim_id} in "
f"{(time.perf_counter() - started) * 1000:.0f} ms; answering: {query}"
)
await send_to_client({"type": "verification", "status": "verified"})
sections = [
s for s in params.arguments.get("sections") or [] if s in CLAIM_SECTIONS
]
# Sent before the answer, so the screen points at it as it's spoken
await send_to_client(
{"type": "claim-focus", "claim_id": claim_id, "sections": sections or ["overview"]}
)
# The masked record, the same one the screen shows: the LLM answers
# from it directly, with amounts and dates ready to speak
await params.result_callback(
{
"query": query,
"record": await expand_currency(claims.spoken_view(record), "*"),
}
)
# After the answer is on its way. The record is already loaded, so
# showing it costs nothing.
if claim_id != shown_claim_id:
await send_to_client({"type": "claim-loaded", "claim": claims.to_client_view(record)})
shown_claim_id = claim_idThe verification lives in code, not just in the prompt. A prompt is a request: a determined caller can talk an LLM into skipping a step. Here, load_claim reads the claim's whole record, and claims.policyholder_matches compares the caller's last name with the policyholder's name on it before any of the record reaches the LLM. If the check fails, the LLM gets nothing from the record, so there's nothing for it to leak. The failure response is identical whether the claim doesn't exist or the name is wrong, so a caller can't use the agent to discover valid claim numbers or the names on them. After three failed attempts on a claim, the agent stops checking it for the rest of the call. The VERIFICATION section of the system prompt tells the LLM to ask for the last name and how to respond to a mismatch.
One read serves the whole lookup. load_claim costs one knowledge base search and one S3 read the first time a claim comes up, and nothing after that, because the record stays in records for the rest of the call. The same record verifies the caller, answers the question, and fills the screen: claim-loaded goes out straight from it, with no second fetch.
The LLM answers from the record directly. The tool result is claims.spoken_view(record), the same masked view the screen shows, with dates and amounts already shaped for speech. Because the masking happens in code before the model sees anything, the policyholder's address, phone, and email, the full VIN and plate, and the map coordinates never reach the LLM at all. The prompt's PRIVACY rules become a second line of defense instead of the only one, and the CLAIM LOOKUPS rule "answer only what was asked" keeps a whole record from turning into a monologue.
Earlier versions of this agent passed the verified question to a Strands sub-agent, which searched for the record again and summarized it for the main LLM to rephrase. Once the record is fixed by verification, that sub-agent has nothing to decide, and it cost two extra model calls on every question. Handing the record to the main LLM removes them.
The employee persona registers a different tool. Its handler hands the question to the research agent and sends the claims the answer names to the screen:
async def search_claims(params: FunctionCallParams):
question = str(params.arguments.get("question", "")).strip()
if not question:
await params.result_callback(
{"response": "Ask the staff member what they want to find."}
)
return
started = time.perf_counter()
try:
answer, matched = await research_agent.research_async(question)
except Exception as e:
logger.error(f"Error researching '{question}': {e}")
await params.result_callback(
{
"question": question,
"response": "The search failed. Apologize and ask them to try again in a moment.",
}
)
return
logger.info(
f"Researched '{question}' in {(time.perf_counter() - started) * 1000:.0f} ms; "
f"answer names {matched}"
)
await params.result_callback({"question": question, "answer": answer})
# The claims the answer names, masked like the voice, for the screen
await send_to_client(
{
"type": "search-results",
"query": question,
"claims": [claims.to_client_view(records[c]) for c in matched if c in records],
}
)You'll build the ClaimsResearchAgent in The employee persona: research with a Strands agent. Its research_async returns the answer and the claim IDs it names, best match first, and the handler turns those IDs into masked views for the screen from the same records cache.
Both claim tools can take long enough that the caller deserves a hold line, so the next block adds one, registers the claim tool for the persona on the call, and adds the voice tools:
# Claim lookups still working, and the hold line waiting to be said
lookups_running = 0
hold_line_timer: asyncio.Task | None = None
last_hold_line: str | None = None
async def say_hold_line_after_delay():
nonlocal last_hold_line
await asyncio.sleep(HOLD_LINE_DELAY_SECS)
if not lookups_running:
return
last_hold_line = voices.pick_filler(current_voice, last=last_hold_line)
await tts.queue_frame(TTSSpeakFrame(last_hold_line, append_to_context=False))
def cancel_hold_line():
nonlocal hold_line_timer
if hold_line_timer is not None:
hold_line_timer.cancel()
hold_line_timer = None
def holds_the_line(handler):
"""Count a lookup as running until its result is handed back.
The hold line is cancelled before the result reaches the LLM, so a
quick answer is never preceded by "one moment".
"""
async def counted(params: FunctionCallParams):
nonlocal lookups_running
lookups_running += 1
running = True
def finish():
nonlocal lookups_running, running
if running:
running = False
lookups_running -= 1
if not lookups_running:
cancel_hold_line()
async def result_callback(result, **kwargs):
finish()
await params.result_callback(result, **kwargs)
try:
await handler(dataclasses.replace(params, result_callback=result_callback))
finally:
finish()
return counted
def reports_timing(handler):
"""Tell the claims UI how long a function took, for its function call row.
The bot times it, since only the bot knows when the function really
started and finished. The timing goes out just before the result.
"""
async def timed(params: FunctionCallParams):
started = time.perf_counter()
async def result_callback(result, **kwargs):
await send_to_client(
{
"type": "function-timing",
"tool_call_id": params.tool_call_id,
"ms": round((time.perf_counter() - started) * 1000),
}
)
await params.result_callback(result, **kwargs)
await handler(dataclasses.replace(params, result_callback=result_callback))
return timed
if persona == EMPLOYEE:
llm.register_function("search_claims", holds_the_line(reports_timing(search_claims)))
else:
llm.register_function(
"search_knowledge_base", holds_the_line(reports_timing(search_knowledge_base))
)
async def switch_voice(voice: Voice) -> bool:
"""Switch voice, and with it the language, in place. True if the language changed.
Both the set_voice tool and the Settings panel switch through here,
so the prompt, the voice, the listen settings, and the UI stay in step.
"""
nonlocal current_voice
old_voice, current_voice = current_voice, voice
language_changed = voice.lang != old_voice.lang
logger.info(f"Switching voice to {voice.model} (language changed: {language_changed})")
# The new voice and language, keeping any claim a staff member has open
await refresh_prompt()
# A voice change reconnects Flux TTS, which would cut off speech still
# streaming in the old voice, so let that finish first
waited = 0.0
while tts.has_active_audio_context() and waited < 3.0:
await asyncio.sleep(0.1)
waited += 0.1
if waited:
logger.info(f"Waited {waited:.1f}s for speech in progress before switching voice")
await tts.queue_frame(
TTSUpdateSettingsFrame(delta=DeepgramFluxTTSService.Settings(voice=voice.model))
)
# A model change reconnects Flux STT; a hints change is a Configure on
# the open connection. listen_steps orders them so a Configure only
# ever reaches a flux-general-multi connection, and FluxSTT holds the
# hints back while a reconnect to it waits for the caller to finish.
for step in voices.listen_steps(old_voice, voice):
await stt.queue_frame(
STTUpdateSettingsFrame(delta=DeepgramFluxSTTService.Settings(**step))
)
await send_voice()
return language_changed
async def apply_voice(voice: Voice, params: FunctionCallParams):
"""Switch voice for the set_voice and reset_voice tools.
The prompt, voice, and listen updates are queued before the function
result, so the confirmation is the first turn in the new voice.
"""
language_changed = await switch_voice(voice)
await params.result_callback(
{"response": voices.confirmation(voice, language_changed)}
)
async def set_voice(params: FunctionCallParams):
voice = voices.find_voice(str(params.arguments.get("voice", "")))
if voice is None:
await params.result_callback(
{"response": "No voice by that name. Call list_voices for the names."}
)
return
await apply_voice(voice, params)
async def list_voices(params: FunctionCallParams):
await params.result_callback(
{"response": voices.list_voices_text(str(params.arguments.get("language", "")))}
)
async def reset_voice(params: FunctionCallParams):
await apply_voice(voices.DEFAULT_VOICE, params)
llm.register_function("set_voice", reports_timing(set_voice))
llm.register_function("list_voices", reports_timing(list_voices))
llm.register_function("reset_voice", reports_timing(reset_voice))
# If a claim lookup runs long, say a hold line so the caller isn't left
# in silence. A lookup that answers within HOLD_LINE_DELAY_SECS skips it.
# The line isn't added to the LLM context, it's in the current voice's
# language, and it's never the same line twice in a row. When a voice
# switch comes in the same batch, the switch's confirmation takes its place.
@llm.event_handler("on_function_calls_started")
async def on_function_calls_started(service, function_calls):
nonlocal hold_line_timer
names = {fc.function_name for fc in function_calls}
if not names & CLAIM_TOOLS or names & {"set_voice", "reset_voice"}:
return
cancel_hold_line()
hold_line_timer = asyncio.create_task(say_hold_line_after_delay())
# The caller interrupted, so the lookup is cancelled: nothing to hold for
@llm.event_handler("on_function_calls_cancelled")
async def on_function_calls_cancelled(service, function_calls):
cancel_hold_line()Everything a lookup does that blocks, the boto3 calls and the Strands agent, runs on a worker thread with asyncio.to_thread. That matters more than it looks. Pipecat runs the whole call on one event loop: listening, speaking, and sending audio to the browser. A blocking call in a function handler freezes all of it, so the caller hears silence for the length of the lookup, then everything that queued up behind it at once.
A slow lookup still needs a hold line, because dead air on a phone call feels broken. A fast one doesn't: "One moment, please" followed instantly by the answer just adds a second of delay. So the hold line waits. The on_function_calls_started event fires as soon as the LLM decides to call a tool, and starts a timer when the batch includes one of the CLAIM_TOOLS. If the lookup is still running after HOLD_LINE_DELAY_SECS, the timer queues a TTSSpeakFrame straight to the TTS service. holds_the_line wraps the handler and cancels the timer the moment a result is ready, before the result reaches the LLM. A caller who interrupts cancels the lookup, and on_function_calls_cancelled cancels the timer too.
In practice the two personas behave differently. A policyholder's first lookup reads the record and usually earns a hold line; every follow-up comes from the records cache and answers too fast to need one. An employee's research runs several model calls and searches, so it almost always plays the line.
The line itself comes from a short list for the current voice's language, never the same one twice in a row, so the agent doesn't sound like a recording. Setting append_to_context=False keeps the hold line out of the conversation history, so it doesn't land between the tool call and its result. It only plays for claim lookups, since the voice tools answer instantly.
The set_voice, list_voices, and reset_voice functions let the caller change the agent's voice and language. They're explained in Switching languages by voice.
Next, let's create the context and its aggregators. The LLMContext stores the user and bot turns, plus the tools the LLM can call: tools_for(persona) gives each persona its own claim tool alongside the voice tools. The greeting comes from GREETINGS, so a policyholder hears about claim lookups and staff hear the claims desk. greeting_instruction asks for it word for word in English, and translated when the call starts in another language. We don't need to give the user aggregator a VAD analyzer: Deepgram Flux sends the start-of-turn and end-of-turn signals, and the aggregator uses them automatically. For more on how this works, see Pipecat's documentation on context management.
context = LLMContext(
messages=[
{"role": "user", "content": greeting_instruction(persona, current_voice)},
],
tools=tools_for(persona),
)
# The caller can interrupt the greeting like any other reply. The click of
# their microphone opening, which Flux can hear as a word, never reaches
# Flux: MicGate, at the front of the pipeline, sends silence in its place.
#
# The idle timer starts each time the agent stops speaking, and the
# caller speaking, the agent speaking, or a lookup in progress stops it.
# So it never runs before the greeting ends or while a hold line covers a
# slow lookup.
user_aggregator, assistant_aggregator = LLMContextAggregatorPair(
context,
user_params=LLMUserAggregatorParams(
user_idle_timeout=USER_IDLE_TIMEOUT_SECS,
),
)The caller can interrupt the greeting, like any other reply. That needs one guard. When the browser opens the microphone, the first audio can hold a click that Flux hears as a short word, often "two". The RTVI observer sends Flux's transcript to the browser as soon as Flux produces it, so the caller sees a "two" they never said as their first message, and the agent stops its greeting to answer it.
MicGate sits at the front of the pipeline, between the transport and the STT, and replaces the first MIC_OPEN_GATE_MS (200 ms) of the caller's audio with silence of the same length. The click is over in a few tens of milliseconds, and nobody starts talking that fast, so nothing the caller says is lost. It counts audio rather than clock time, so the 200 ms start with the first audio the caller sends, however long the call took to connect:
class MicGate(FrameProcessor):
"""Sends speech-to-text silence for the first moment the microphone is open."""
def __init__(self, gate_ms: int = MIC_OPEN_GATE_MS):
super().__init__()
self._gate_ms = gate_ms
# Counted in samples, which are whole numbers: summing frame lengths
# in seconds drifts, and ten 20 ms frames would fall short of 200 ms
self._heard_samples = 0
async def process_frame(self, frame: Frame, direction: FrameDirection):
await super().process_frame(frame, direction)
if isinstance(frame, InputAudioRawFrame):
gate_samples = frame.sample_rate * self._gate_ms // 1000
if self._heard_samples < gate_samples:
self._heard_samples += frame.num_frames
frame.audio = bytes(len(frame.audio))
await self.push_frame(frame, direction)Create it in run_bot with mic_gate = MicGate(), and put it first in the pipeline below.
user_idle_timeout turns on Pipecat's idle timer for the caller. You'll use it after the pipeline worker is set up, to hang up on a caller who has gone quiet.
Your agent is heard, not read, and that changes how you write its prompts. LLMs default to writing for a screen. Leave that unchecked and every answer comes back in markdown, which Flux TTS faithfully pronounces: **Status:** comes out as "star star status star star." The prompt build_system_instruction writes follows the five rules for voice prompts from the Deepgram Builder Essentials course:
| Rule | Why it matters out loud |
|---|---|
| No markup. "Never use markdown, bullet points, numbered lists, headings, or emoji." | Otherwise the caller hears every asterisk. Telling the model it's being heard is the instruction that changes its output. "Be conversational" doesn't. |
| Short turns. One or two sentences, one question at a time. | A caller can't skim. Ask two questions in one breath and you get one answer, and you won't know which question it answers. |
| Data for the ear. Amounts in words, dates with their weekday, claim IDs digit by digit. | A misheard "claim fifty" could be claim 15 or claim 50. Reading the digits back, "five zero," gives the caller a chance to catch it, and a weekday does the same for a date. |
| Confident phrasing. Say what you know, and say plainly when you don't. | Hedging sounds evasive in speech. "Never guess" is the line that stops the agent inventing a claim status. |
| Named escalation triggers. Spell out the situations the agent hands off. | "Use your judgement" isn't a trigger. "If the caller wants to file a new claim" is. |
The prompt stays short because Pipecat re-sends it on every turn, and a long prompt slows the first reply. The hard constraints sit at the end, where they're least likely to fade after a few turns. The prompt also tells the model today's date, since the model has no calendar of its own.
The greetings follow the same thinking. A greeting is the only thing the agent says before it knows anything about the caller, so it names what the agent can do and asks for the one thing it needs next. The policyholder greeting asks for the claim number. The staff greeting is shorter, because staff already know what the desk is for.
A prompt is a request, not a guarantee. To make sure markdown never reaches the caller, the TTS service runs its text through strip_markup_symbols before synthesis. Add it to agent.py just below KNOWLEDGE_BASE_ID:
_MARKUP_SYMBOLS = re.compile(r"[*#`]")
async def strip_markup_symbols(text: str, text_type: str) -> str:
"""Drop markdown symbols before speech synthesis.
Flux TTS receives the LLM's output token by token, so this has to work on a
lone "**" as well as on whole sentences.
"""
return _MARKUP_SYMBOLS.sub("", text)Flux TTS streams the LLM's output to Deepgram token by token, so a ** often arrives on its own. Pipecat's built-in strip_markdown transform only removes paired markers like **bold**, which is why this transform drops the symbols one character at a time instead. Text transforms change only what's sent to TTS; the conversation context keeps the LLM's original text. Hyphens stay, because the claim data uses them in claim IDs like CLM-2026-847291 and policy numbers like WRK-AUTO-482977.
A caller can ask the agent to speak Spanish, or to use a different voice, and the agent switches mid-call. The design has one rule: the voice implies the language. Each Flux TTS voice belongs to exactly one language, so choosing a voice also chooses the listen model and the language the agent replies in. There's no separate language setting that could disagree with the voice.
All of that lives in voices.py, next to agent.py. Copy it from this repository: most of it is a catalog of about 145 voices. It's built from two tables:
@dataclass(frozen=True)
class Language:
code: str # ISO code: the model suffix and the Flux language hint
name: str # used in the prompt and tool text
# Hold lines, one spoken when a claim lookup runs long. Written in the
# language itself, since the voice would otherwise read the English text
# with a foreign accent. Each is neutral about the speaker's gender, since
# any voice in the language may say it.
fillers: tuple[str, ...]
@dataclass(frozen=True)
class Voice:
name: str # the short name the model chooses by
model: str # the Flux TTS model id sent to Deepgram
blurb: str # e.g. "Spanish woman"
lang: str # a code from LANGUAGES
featured: bool = False # named in the set_voice descriptionThe catalog offers only the languages the agent can hear. A voice in a language the STT model can't understand is a voice nobody can talk back to. flux-general-multi hears English, Spanish, French, German, Hindi, Russian, Portuguese, Italian, and Dutch, so those nine are offered. (It also hears Japanese, but poorly enough that a Japanese voice would need Nova-3 for listening, a second STT service this agent doesn't run.)
voices.listen_settings() picks the STT settings for a voice. English listens with flux-general-en. Every other language listens with flux-general-multi and the language hints [code, "en"]. Keeping English in the hints matters: it's how "English, please" still gets heard after a switch to Spanish.
The LLM gets three functions:
set_voice(voice)changes the voice, and with it the language. Its description names one featured voice per language and a handful of English voices. It has noenumof all 145 names, which would bloat every request. An unknown name is answered with "No voice by that name. Call list_voices for the names."list_voices(language)returns one language's voices with their accent and gender. A voice's name says nothing about either, so the description tells the model to call this first when a caller asks for "a man's voice."reset_voice()goes back to the default voice, and so to English.
voices.voice_intro() adds one sentence to the system prompt, right after the role, in every language including English:
Your voice is Carmen, a Spanish woman. Reply only in Spanish; for any other language, call set_voice first.
The wording comes from measurements on another Deepgram project, a voice device that switches languages the same way. Without a language line, English sessions answered "Can you speak French?" in French, in the English voice, without switching, in 5 of 16 tries. With the line, 0 of 16. A softer "Reply in Spanish unless asked otherwise" missed 1 of 12 language changes. "Reply only in… call set_voice first" missed none.
switch_voice makes the switch in place, without dropping the call, and apply_voice wraps it for the set_voice and reset_voice tools. It queues three settings updates, one for each service, and then returns the function result:
| Update | What the service does |
|---|---|
LLMUpdateSettingsFrame with a new system_instruction |
Replaces the system prompt, so the old language line is gone, not just contradicted. refresh_prompt builds it from the current voice and keeps a staff member's open claim in it |
TTSUpdateSettingsFrame with the new voice |
Flux TTS reconnects with the new voice model |
One STTUpdateSettingsFrame per step from listen_steps(), only when the language changes |
A change of model (English to or from flux-general-multi) reconnects Flux STT once the caller stops speaking. A change of hints alone (Spanish to French) is sent as a Configure message on the open connection |
Because the updates are queued before the result, the confirmation the LLM speaks next ("Sí, ahora hablo español") is the first turn in the new voice. The result asks for it in the new language: Done: you are now Carmen, a Spanish voice. Say so briefly, in Spanish.
The order of the listen steps matters. Only flux-general-multi accepts language hints, so listen_steps() sets hints after reconnecting to it and clears them before leaving it. That way a Configure never reaches a flux-general-en connection.
Ordering alone isn't quite enough. Pipecat holds a model change's reconnect until the caller stops speaking, but it sends a hints Configure right away, checked against the model in settings, which has already changed. So a switch out of English while the caller is still talking would send hints to the flux-general-en connection that's still open. FluxSTT, defined next to MicGate in agent.py, checks the model the open connection was made with instead, and leaves the hints to the reconnect, whose URL carries them:
class FluxSTT(DeepgramFluxSTTService):
"""Flux STT that sends language hints only to a connection that accepts them."""
_connected_model: str | None = None
def _build_query_string(self) -> str:
# Built for each new connection, so it records the open connection's model
self._connected_model = self._settings.model
return super()._build_query_string()
async def _send_configure(self, fields: set[str]):
if self._connected_model != self._MULTILINGUAL_MODEL:
fields = fields - {"language_hints"}
if not fields:
return
await super()._send_configure(fields)It overrides two of Pipecat's private methods, so tests/test_flux_stt.py checks it against the pinned Pipecat version: rerun it when you upgrade.
The TTS update waits, for up to three seconds, for any speech still streaming in the old voice, because reconnecting Flux TTS would cut it off mid-word. The prompt also tells the LLM to call set_voice before saying anything about the switch.
The claims UI's Settings panel has a Voice section: a language, listed by its own name ("Español", "Deutsch", "हिन्दी"), then a voice in that language, suggested voices first. Picking a language picks its first suggested voice, so one choice is enough. The browser remembers the pick in localStorage, not the agent: callers are anonymous until they're verified, so there's no one on the server to remember it for.
- Between calls, the pick is the voice the next call starts in. The UI sends it with the persona as
request_data, andvoice_from_bodyturns it into the starting voice, falling back to the default for a name that isn't in the catalog. STT, TTS, and the prompt are all built from it, andgreeting_instructionasks for the greeting in that language. - During a call, the UI also sends a
set-voiceclient message, and the agent switches on the spot. See Finishing the Botfile for the handler.
The picker reads the catalog from client/src/lib/voices.json, which scripts/export_voices.py writes from voices.py. Rerun it after changing the catalog; tests/test_voices.py fails until you do:
.venv/bin/python scripts/export_voices.py- Claim records are in English. The prompt tells the LLM to translate what it reads from them into the reply language. On screen, record values stay as recorded; see The screen speaks the caller's language.
policyholder_matchescompares letters A to Z, with accents removed. A last name the caller says in another script, such as Cyrillic, has to be spelled out in Latin letters.- A caller who interrupts during the moment Flux STT reconnects may not be heard.
tests/test_voices.py checks the catalog: unique names, the flux-<name>-<lang> pattern, one featured voice per language, and that every voice's language can be heard. It also checks that client/src/lib/voices.json matches voices.py, that voice_from_body falls back to the default, and the greeting instruction for each language. tests/test_flux_stt.py holds a reconnect, as a caller who's still talking does, and checks that hints wait for it. scripts/probe_flux_languages.py checks the live API: for each language it synthesises a sentence with Flux TTS and streams it back through flux-general-multi. scripts/switch_test.py drives the whole agent through a series of switches with synthesised speech and reports the voice and pitch of every reply.
.venv/bin/python -m unittest discover tests
.venv/bin/python scripts/probe_flux_languages.py
.venv/bin/python scripts/switch_test.pyWith the main LLM speaking for the ear, the research agent behind the employee persona needs the same treatment.
A policyholder asks about one record they've already proved is theirs. Staff ask questions like "which claims are waiting on a title?" or "which claims are being adjusted by Luis Ortega, and where do they stand?" No single search answers those reliably. The right claims might only match on a second wording, or the answer might combine what two searches found. That's a loop: search, read, decide whether to search again, then answer. It's exactly the job Strands Agents is built for, so the employee persona hands each question to one. Just above the persona names, create a ClaimsResearchAgent class:
class ClaimsResearchAgent:
"""A Strands agent that answers a staff member's question across all claims.
A question like "which claims are waiting on a title?" can take several
searches with different wording, and an answer that combines what they
found. That's an agent loop, so it runs here, behind one tool call, and
the voice LLM only hears the answer.
"""
def __init__(self, kb: BedrockKnowledgeBaseClient, records: dict[str, dict]):
self.kb = kb
# Shared with the call, so a claim is read from S3 once per call
self.records = records
# The claims this question's searches found, best first
self.matched: list[str] = []
# One Agent runs one request at a time, so questions take turns
self._lock = threading.Lock()
session = boto3.Session(region_name=os.getenv("AWS_REGION", "us-east-1"))
self.agent = Agent(
tools=[self.search_claims],
model=BedrockModel(model_id="us.amazon.nova-2-lite-v1:0", boto_session=session),
# The answer goes back to the voice LLM; don't print it to the console
callback_handler=None,
system_prompt=(
"You research Wreckless Auto claims for a staff "
"member. Another assistant speaks your answer, so write plain "
"sentences: no markdown, bullet points, headings, or emoji. "
"Call search_claims to find claims. Search as many times as you "
"need, with different wording, to answer fully, then answer only "
"from what the searches returned. Name each claim you mention by "
"its policyholder and claim ID. If nothing matches, say so. "
"Never invent a claim, amount, date, or status. "
"Records are already masked: never try to reconstruct contact "
"details, a full VIN, or a plate."
),
)
@tool
def search_claims(self, query: str) -> str:
"""Search every claim record for a question, e.g. 'stolen vehicle not recovered' or 'waiting on title'. Returns the best-matching claims as JSON."""
logger.info(f"Research search: {query}")
found = []
for hit in self.kb.search_claims(query):
record = self.records.get(hit.claim_id)
if record is None:
record = self.kb.read_claim_file(hit)
if record is None:
continue
self.records[hit.claim_id] = record
if hit.claim_id not in self.matched:
self.matched.append(hit.claim_id)
found.append(claims.spoken_view(record))
if not found:
return "No claims matched that search."
return "[" + ",\n".join(found) + "]"
def research(self, question: str) -> tuple[str, list[str]]:
"""The agent's answer, and the claims it names, best match first.
A search returns its best matches whether they fit or not, so the
screen lists only the claims the answer names, not every one found.
Each question starts with an empty history: the voice LLM rewrites
follow-ups as standalone questions, so the answer always comes from
fresh searches.
"""
with self._lock:
self.matched = []
self.agent.messages.clear()
answer = str(self.agent(question)).strip()
return answer, [c for c in self.matched if c in answer]
async def research_async(self, question: str) -> tuple[str, list[str]]:
"""research on a worker thread.
The Strands agent blocks until it's done, so on the event loop it
would stall the voice pipeline. A caller who interrupts cancels the
wait, not the thread, so the lock lives in research: a question still
finishing on its thread keeps the next one waiting.
"""
return await asyncio.to_thread(self.research, question)The agent has one tool, search_claims, defined with the @tool decorator. Each call runs a knowledge base search, loads the records it found through the same records cache the call uses, and returns them as masked, speakable JSON with claims.spoken_view. The agent can call it as many times as it likes with different wording, then answers from what came back. Its model is Nova 2 Lite: the research is a reading task, and the pipeline LLM still decides what the caller hears.
research clears the agent's history before every question, so an answer always comes from fresh searches. That's safe because the pipeline LLM rewrites follow-ups into standalone questions before it calls the tool, as the SEARCHING CLAIMS prompt asks. It also returns only the claims the answer names. A search returns its best matches whether they fit or not, and the screen should list the claims the answer is about, not every claim the agent happened to read. Like the knowledge base calls, the agent blocks until it's done, so research_async runs it on a worker thread, and the lock lives in research so a question still finishing after an interruption keeps the next one waiting.
The research agent never talks to the caller directly, but its answer becomes the tool result the main LLM reads aloud. Any markdown it writes ends up in the caller's ear, so its system_prompt asks for plain sentences too.
Next, define the tools the pipeline LLM can call, just below the build_system_instruction function:
# The parts of a claim the UI can point at, for search_knowledge_base's sections
CLAIM_SECTIONS = [
"overview",
"progress",
"coverage",
"policyholder",
"vehicle",
"incident",
"location",
"other_party",
"damage",
"photos",
"estimate",
"payments",
"adjuster",
"notes",
]
search_function = FunctionSchema(
name="search_knowledge_base",
description=(
"Look up an existing Wreckless Auto claim by its claim ID. Use "
"this whenever the caller asks about a specific claim: its status, the "
"vehicle, the accident, the other driver, the damage, the repair estimate, "
"the deductible, rental, payments, the police report, the adjuster, or "
"adjuster notes. Call it only once the caller has given both the full "
"claim ID and the policyholder's last name; the lookup verifies the name "
"before it returns any details."
),
properties={
"claim_id": {
"type": "string",
"description": (
"The full claim ID as CLM-YYYY-NNNNNN, e.g. 'CLM-2026-847291': "
"the four-digit year and the six-digit number."
),
},
"last_name": {
"type": "string",
"description": (
"The policyholder's last name in Latin letters, as written on the "
"policy. If the caller said or spelled it in another script, "
"transliterate it."
),
},
"query": {
"type": "string",
"description": "The claim ID and what the caller wants to know, e.g. 'claim ID CLM-2026-847291 repair estimate'.",
},
"sections": {
"type": "array",
"items": {"type": "string", "enum": CLAIM_SECTIONS},
"description": (
"The parts of the claim the question is about, e.g. ['damage', "
"'photos'] for 'where's the damage?'. The caller's screen "
"highlights them. Use 'overview' when the caller first asks for "
"the claim."
),
},
},
required=["claim_id", "last_name", "query"],
)
set_voice_function = FunctionSchema(
name="set_voice",
description=voices.set_voice_description(),
# No enum: listing ~150 voices would bloat every request
properties={
"voice": {
"type": "string",
"description": "The voice's short name in Latin letters, as listed, e.g. 'carmen'.",
}
},
required=["voice"],
)
list_voices_function = FunctionSchema(
name="list_voices",
description=(
"List the voices for one language, with each voice's accent and gender. "
"Use it before set_voice when the caller describes a voice instead of "
"naming one."
),
properties={
"language": {
"type": "string",
"enum": list(voices.LANGUAGES),
"description": "ISO language code, e.g. 'es' for Spanish.",
}
},
required=["language"],
)
reset_voice_function = FunctionSchema(
name="reset_voice",
description="Go back to the default voice, and so to English.",
properties={},
required=[],
)
search_claims_function = FunctionSchema(
name="search_claims",
description=(
"Search every Wreckless Auto claim to answer a staff member's question, "
"e.g. 'which claims are waiting on a title?' or 'who is adjusting Daniel "
"Mitchell's claim?'. A research agent runs as many searches as it needs "
"and answers from the claim records."
),
properties={
"question": {
"type": "string",
"description": (
"The staff member's question as a standalone question. Rewrite a "
"follow-up like 'and the adjuster?' with the claim or policyholder "
"it refers to."
),
},
},
required=["question"],
)
# Staff answer from the open claim without a lookup, so nothing would point
# their screen at the answer. This does, as the policyholder lookup's
# sections do.
show_on_screen_function = FunctionSchema(
name="show_on_screen",
description=(
"Highlight parts of the claim the staff member has open, so their screen "
"shows the answer while you say it. Call it before answering any "
"question about the open claim."
),
properties={
"sections": {
"type": "array",
"items": {"type": "string", "enum": CLAIM_SECTIONS},
"description": (
"The parts of the claim the answer is in, e.g. ['estimate'] for "
"'how much is the repair?' or ['adjuster', 'notes'] for 'who's "
"handling it?'."
),
},
},
required=["sections"],
)
VOICE_TOOLS = [set_voice_function, list_voices_function, reset_voice_function]
def tools_for(persona: str) -> ToolsSchema:
"""The pipeline LLM's tools: the persona's claim tools, plus the voice tools."""
claim_tools = (
[search_claims_function, show_on_screen_function]
if persona == EMPLOYEE
else [search_function]
)
return ToolsSchema(standard_tools=[*claim_tools, *VOICE_TOOLS])The tool descriptions are prompts as well. A description is the only thing an LLM reads when it decides whether to call a tool, so each one says when to use the tool, not only what it does. "Search the knowledge base" tells the model what a tool is. The search_function description tells the model when to reach for it, and the search_claims_function description tells it to pass a standalone question. The @tool docstring plays the same role for the research agent. tools_for gives each persona its own claim tools and the same voice tools, so a policyholder's LLM never sees a tool that searches every claim. Staff also get show_on_screen, which highlights parts of the claim they have open (see What staff have open).
Both personas read claims through a BedrockKnowledgeBaseClient. Define it, with the claim ID helpers it needs, right above the ClaimsResearchAgent class:
def claim_id_pattern(claim_id: str) -> re.Pattern:
"""Match a claim record's "Claim ID" line for exactly this claim.
The \b word boundary keeps "Claim ID CLM-2026-84729" from matching
"Claim ID CLM-2026-847291".
"""
return re.compile(rf"\bClaim ID {re.escape(claim_id)}\b", re.IGNORECASE)
# The claim a knowledge base passage belongs to: every passage of a claim file
# repeats its claim ID, in the header or in a section title
_ANY_CLAIM_ID = re.compile(r"\bClaim ID (CLM-\d{4}-\d{6})\b", re.IGNORECASE)
# Claim files are named for their claim: claims/CLM-2026-847291.txt
_CLAIM_FILE = re.compile(r"\b(CLM-\d{4}-\d{6})\.txt$", re.IGNORECASE)
# s3.amazonaws.com, s3.us-east-1.amazonaws.com, s3-us-west-2.amazonaws.com
_S3_HOST = r"s3(?:[.-][a-z0-9-]+)*\.amazonaws\.com"
_S3_PATH_STYLE = re.compile(rf"^{_S3_HOST}$")
_S3_VIRTUAL_HOSTED = re.compile(rf"^(?P<bucket>[a-z0-9.-]+)\.{_S3_HOST}$")
def s3_object(uri: str) -> tuple[str, str] | None:
"""The bucket and key of an S3 location, or None if it isn't one.
Knowledge base results name their source file as s3://bucket/key or as an
https URL, with the bucket either in the host name or first in the path.
"""
url = urlparse(uri)
host = url.hostname or ""
path = unquote(url.path).lstrip("/")
if url.scheme == "s3":
bucket, key = url.netloc, path
elif url.scheme == "https" and _S3_PATH_STYLE.match(host):
bucket, _, key = path.partition("/")
elif url.scheme == "https" and (match := _S3_VIRTUAL_HOSTED.match(host)):
bucket, key = match.group("bucket"), path
else:
return None
return (bucket, key) if bucket and key else None
def ascii_digits(text: str) -> str:
"""Only the digits, as 0 to 9: Devanagari "१२३४" becomes "1234"."""
return "".join(str(unicodedata.digit(c)) for c in text if unicodedata.digit(c, None) is not None)
def normalize_claim_id(text: str) -> str:
"""Turn a claim number as heard into "CLM-2026-847291", or "" if incomplete.
Only the digits count, so "CLM-2026-847291", "c l m 2026 847291", and
digits transcribed in another script all give the same ID. Anything but a
four-digit year plus a six-digit number comes back empty, so a partial
number is never guessed into a full one.
"""
digits = ascii_digits(text)
return f"CLM-{digits[:4]}-{digits[4:]}" if len(digits) == 10 else ""
@dataclasses.dataclass(frozen=True)
class ClaimHit:
"""A claim a search found, and the file that holds it."""
claim_id: str
bucket: str
key: str
class BedrockKnowledgeBaseClient:
"""Client for interacting with Amazon Bedrock Knowledge Base"""
def __init__(self, knowledge_base_id: str):
self.knowledge_base_id = knowledge_base_id
# Credentials come from boto3's default chain: AWS_ACCESS_KEY_ID,
# AWS_SECRET_ACCESS_KEY and AWS_SESSION_TOKEN in .env, or AWS_PROFILE.
self.bedrock_agent_runtime = boto3.client(
"bedrock-agent-runtime",
region_name=os.getenv("AWS_REGION", "us-east-1"),
)
# Reads a claim's source file once search has found it
self.s3 = boto3.client("s3", region_name=os.getenv("AWS_REGION", "us-east-1"))
logger.info(
f"Initialized Bedrock Knowledge Base client for KB: {knowledge_base_id}"
)
def _retrieve(self, text: str, search_type: str, max_results: int) -> dict:
try:
return self.bedrock_agent_runtime.retrieve(
knowledgeBaseId=self.knowledge_base_id,
retrievalQuery={"text": text},
retrievalConfiguration={
"vectorSearchConfiguration": {
"numberOfResults": max_results,
"overrideSearchType": search_type,
}
},
)
except ClientError as e:
# Managed knowledge bases reject vectorSearchConfiguration and
# take managedSearchConfiguration instead. It picks the search
# type itself, but still honors the number of results.
if "vectorSearchConfiguration is not supported" not in str(e):
raise
return self.bedrock_agent_runtime.retrieve(
knowledgeBaseId=self.knowledge_base_id,
retrievalQuery={"text": text},
retrievalConfiguration={
"managedSearchConfiguration": {"numberOfResults": max_results}
},
)
def _hits(self, response: dict) -> list[ClaimHit]:
"""Each claim the results name, best match first, with its file.
A passage names its claim in its text, or, for a passage cut from the
middle of a long section, in its file name. A claim counts once,
however many of its passages matched. read_claim_file checks the
claim against the file itself, so a misnamed file can't stand in.
"""
hits: dict[str, ClaimHit] = {}
for result in response.get("retrievalResults", []):
location = s3_object(
result.get("location", {}).get("s3Location", {}).get("uri", "")
)
if location is None:
continue
text = result.get("content", {}).get("text", "")
match = _ANY_CLAIM_ID.search(text) or _CLAIM_FILE.search(location[1])
if match is None:
continue
claim_id = match.group(1).upper()
if claim_id not in hits:
hits[claim_id] = ClaimHit(claim_id, *location)
return list(hits.values())
def read_claim_file(self, hit: ClaimHit) -> dict | None:
"""A claim's record as fields, read whole from its source file.
Search returns passages, not records, so the file is read in full:
the record is exact, whichever of its passages search happened to
rank. None unless the file is that claim's record, not one that only
mentions it.
"""
body = self.s3.get_object(Bucket=hit.bucket, Key=hit.key)["Body"].read()
record = claims.parse_claim(body.decode("utf-8"))
return record if record["claim_id"] == hit.claim_id else None
def load_claim(self, claim_id: str) -> dict | None:
"""One claim's record as fields, found by its exact claim ID.
One search and one file read. The policyholder lookup verifies the
caller against this record, then answers from it and shows it, so a
claim is read once per call.
"""
response = self._retrieve(f"Claim ID {claim_id}", "HYBRID", 10)
for hit in self._hits(response):
if hit.claim_id == claim_id:
return self.read_claim_file(hit)
return None
def search_claims(self, query: str, max_results: int = 10) -> list[ClaimHit]:
"""The claims whose records best match a question, best match first."""
return self._hits(self._retrieve(query, "HYBRID", max_results))_retrieve runs a knowledge base search. A managed knowledge base, like the one the workshop creates, rejects the vectorSearchConfiguration setting, so _retrieve falls back to managedSearchConfiguration, which picks the search type itself.
A search returns passages, not records. _hits turns them into claims: one ClaimHit per claim, best match first, with the S3 file it came from. Every result names its source file in location.s3Location.uri, and s3_object turns that URI into a bucket and key. A passage usually names its claim in a Claim ID line, but the knowledge base splits each record into passages, and one cut from the middle of a long section may not repeat the ID. For those, _hits reads the claim ID from the file name instead, since every claim file is named for its claim (CLM-2026-847291.txt).
read_claim_file then reads the whole file and parses it with claims.parse_claim. Reading the file, instead of piecing the record together from search results, gives the agent the exact record every time, whichever passages the search happened to rank. The parsed record must carry the same claim ID as the hit, so a misnamed file can never stand in for another policyholder's claim.
load_claim is the policyholder's lookup. It searches for the exact claim ID and reads the file of the matching hit. The \b word boundary in the claim ID patterns keeps a mistyped ID like CLM-2026-84729 from matching CLM-2026-847291. search_claims is the employee's: it searches with the question itself and returns every claim it found, for the research agent to read.
The two helpers next to them handle callers who aren't speaking English. ascii_digits turns digits from any script into 0 to 9, so a claim number transcribed in Devanagari still finds the record. normalize_claim_id builds on it. Callers say claim IDs in many ways: "C L M twenty twenty-six, eight four seven two nine one," just the digits, or a mix. Only the digits matter, so it keeps those and rebuilds the ID as CLM-2026-847291. Anything other than a four-digit year and a six-digit number comes back empty, and search_knowledge_base asks the caller for the full number instead of guessing the missing year.
The rest of the claim handling lives in claims.py, next to agent.py. Copy it from this repository, like voices.py: most of it is parse_claim, which reads a claim file's sections into fields, and to_client_view, which applies the privacy rules. Three parts of it matter to the agent. The first shapes a record for the ear:
_ORDINAL = {1: "st", 2: "nd", 3: "rd", 21: "st", 22: "nd", 23: "rd", 31: "st"}
_ISO_DATE = re.compile(r"\b\d{4}-\d{2}-\d{2}\b")
def spoken_date(iso: str) -> str:
"""Format an ISO date the way a person says it, e.g. 'Monday, September 14th'.
The weekday gives the caller a free second chance to catch a misheard date.
"""
day = date.fromisoformat(iso)
return f"{day:%A, %B} {day.day}{_ORDINAL.get(day.day, 'th')}"
def speak_dates(text: str) -> str:
"""Rewrite every ISO date in the text as a spoken date."""
def replace(match: re.Match) -> str:
try:
return spoken_date(match.group())
except ValueError:
return match.group()
return _ISO_DATE.sub(replace, text)
def spoken_view(claim: dict) -> str:
"""The claim as an LLM gets it: the client view, with dates ready to speak.
It's the masked view, never the raw record, so the privacy rules hold in
code. The prompt only has to keep the answer short.
"""
return speak_dates(json.dumps(to_client_view(claim), ensure_ascii=False))The knowledge base stores dates as 2026-09-14 and amounts as $4,850. Instead of asking the LLM to reformat them, the claim tools hand back text that's already shaped for speech. speak_dates turns 2026-09-14 into "Monday, September 14th," and the handler runs Pipecat's expand_currency transform over the result, which turns $4,850 into "four thousand, eight hundred and fifty dollars." Pre-formatted values also keep the LLM's paraphrase close to the source. spoken_view applies both to to_client_view, never to the raw record, so the masking holds for every model that reads a claim. The result also leaves out search relevance scores and file names. Whatever the LLM never receives, it can't read aloud.
The second checks the caller's name:
def latin_letters(text: str) -> str:
"""Only the letters a to z, accents removed: "W-A-L-S-H" and "walsh" match.
A name in another script, such as Cyrillic or Devanagari, comes back empty.
"""
decomposed = unicodedata.normalize("NFKD", text)
folded = "".join(c for c in decomposed if not unicodedata.combining(c)).lower()
return re.sub(r"[^a-z]", "", folded)
def policyholder_matches(claim: dict, last_name: str) -> bool:
"""Whether the caller's last name is the policyholder's on this claim.
Accepts the last word of the name, or everything after the first name for
multi-word surnames like "De La Cruz". Only letters count, so a spelled
out "W-A-L-S-H" matches "Walsh".
"""
heard = latin_letters(last_name)
names = claim["policyholder"]["name"].split()
if not heard or len(names) < 2:
return False
return heard in {latin_letters(names[-1]), latin_letters("".join(names[1:]))}latin_letters compares names on the letters a to z only, with accents removed, so "Núñez" matches "Nunez." Comparing letters only, without case, spaces, or punctuation, means a name the caller spells out, which Flux transcribes as "W-A-L-S-H," still matches "Walsh." A name transcribed in another script, such as Cyrillic, has no Latin letters at all, so search_knowledge_base asks the caller to spell it instead of counting a failed attempt. policyholder_matches accepts the last word of the name, or everything after the first name, so multi-word surnames like "De La Cruz" work either way.
Reading claim files takes one more AWS permission. The credentials in .env already need bedrock:Retrieve on the knowledge base. Add s3:GetObject on the bucket that holds the claim files, the one step 2 of Reset the knowledge base saves as BUCKET:
{
"Effect": "Allow",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::<claims-bucket>/*"
}Without it, every claim lookup and every search fails with AccessDenied, and the agent apologizes and asks the caller to try again. You can refer to the Amazon Knowledge Base documentation to learn more about this code.
That takes care of everything except for the transport object in the pipeline. We'll come back to that later.
Next, we need to define a pipeline worker. This is what runs the pipeline. Do this right after you've defined your pipeline:
# Configure the pipeline worker
task = PipelineWorker(
pipeline,
params=PipelineParams(
enable_metrics=True,
enable_usage_metrics=True,
),
# The claims UI shows which tool ran and with what arguments. Results
# stay hidden: they hold the claim passages the LLM reads.
rtvi_observer_params=RTVIObserverParams(
function_call_report_level={
"*": RTVIFunctionCallReportLevel.NAME,
"search_knowledge_base": RTVIFunctionCallReportLevel.ARGUMENTS,
"search_claims": RTVIFunctionCallReportLevel.ARGUMENTS,
"set_voice": RTVIFunctionCallReportLevel.ARGUMENTS,
"show_on_screen": RTVIFunctionCallReportLevel.ARGUMENTS,
}
),
)The PipelineParams enable metrics reporting, so you can see latency and token usage in the logs. The rtvi_observer_params decide what the claims UI learns about tool calls: the name of every call, and the arguments of the claim tools, set_voice, and show_on_screen, but never a result, since results hold the claim data the LLM reads.
The pipeline worker also runs Pipecat's RTVI processor, which carries messages between the bot and the claims UI. The agent sends the UI messages with RTVIServerMessageFrame. The UI sends messages back with sendClientMessage(type, data), and they arrive as the processor's on_client_message event. One handler looks each one up by type, so each feature adds an entry to client_handlers instead of another branch:
# Messages the claims UI sends during a call, by type
client_handlers: dict[str, Callable[[dict], Awaitable[None]]] = {}
@task.rtvi.event_handler("on_client_message")
async def on_client_message(rtvi, msg):
handler = client_handlers.get(msg.type)
if handler is None:
logger.warning(f"Ignoring unknown client message: {msg.type}")
return
await handler(msg.data or {})
async def on_set_voice(data: dict):
"""A voice picked in the Settings panel during the call.
It switches like set_voice, but nothing is said: the microphone is off
while Settings is open. A note in the context tells the model the
change was the caller's, so its next reply is in the new language.
"""
name = data.get("voice") if isinstance(data, dict) else None
voice = voices.find_voice(name) if isinstance(name, str) else None
if voice is None:
logger.warning(f"Ignoring set-voice for unknown voice: {name!r}")
await send_voice() # puts the picker back on the voice in use
return
if voice == current_voice:
return
await switch_voice(voice)
await task.queue_frame(
LLMMessagesAppendFrame(
messages=[{"role": "user", "content": voices.settings_switch_note(voice)}],
run_llm=False,
)
)
client_handlers["set-voice"] = on_set_voiceon_set_voice switches through the same switch_voice as the set_voice tool. It speaks no confirmation: the microphone is off while Settings is open, and nobody asked anything. Instead, an LLMMessagesAppendFrame with run_llm=False adds a note to the context, so the model knows the switch was the caller's choice and answers the next question in the new language. A name that isn't in the catalog is ignored, and send_voice() puts the picker back on the voice in use.
A caller who walks away mid-call leaves the session open, with the agent waiting in silence. So when the caller goes quiet, the agent checks in once, and if there's still no answer, it says goodbye and hangs up.
# Silences in a row with no answer from the caller, and whether the agent
# has given up and is saying goodbye
idle_strikes = 0
hanging_up = False
# The caller went quiet after the agent spoke. The first time, check they're
# still there. The second time, say goodbye and end the call, so a caller
# who walked away doesn't keep a session open. Both lines are in the
# current voice's language.
@user_aggregator.event_handler("on_user_turn_idle")
async def on_user_turn_idle(aggregator):
nonlocal idle_strikes, hanging_up
if hanging_up:
return
idle_strikes += 1
if idle_strikes == 1:
logger.info(f"Caller quiet for {USER_IDLE_TIMEOUT_SECS:g}s; checking in")
# The timer starts again when this line finishes. Unlike the hold
# line, it goes in the LLM context, so a "yes, I'm here" reaches the
# model as the answer to it.
await task.queue_frame(TTSSpeakFrame(voices.still_there(current_voice)))
return
logger.info("Caller still quiet after the check-in; hanging up")
hanging_up = True
# Before the goodbye, so the screen can say why the call ended
await send_to_client({"type": "call-ended", "reason": "idle"})
cancel_hold_line()
# The goodbye stays out of the context: nothing answers it
goodbye = TTSSpeakFrame(voices.goodbye(current_voice), append_to_context=False)
# The call ends either way, so a word from the caller mustn't cut the
# goodbye short
goodbye.interruptible = False
# EndFrame, not CancelFrame: it queues behind the goodbye, the TTS
# service holds it until the goodbye's audio is done, and the output
# transport plays everything queued before it. Only then does the bot
# leave the call.
await task.queue_frames([goodbye, EndFrame(reason="idle")])
# The caller spoke, so any check-in was answered
@user_aggregator.event_handler("on_user_turn_started")
async def on_user_turn_started(aggregator, strategy):
nonlocal idle_strikes
idle_strikes = 0
async def on_mic(data: dict):
"""Pause the idle timer while the caller's microphone is off.
The caller mutes, or opens Settings, which turns the microphone off.
Either way the agent can't hear them, so their silence isn't a sign
they've left. The update reaches the user aggregator's idle timer, which
stops at 0 and, on the way back, starts a full timeout over.
"""
nonlocal idle_strikes
if hanging_up:
return
enabled = bool(data.get("enabled"))
if enabled:
idle_strikes = 0
await task.queue_frame(
UserIdleTimeoutUpdateFrame(timeout=USER_IDLE_TIMEOUT_SECS if enabled else 0)
)
client_handlers["mic"] = on_micPipecat does the timing. The user_idle_timeout you gave the user aggregator starts a timer each time the agent stops speaking. The caller starting to speak, the agent starting to speak, or a function call starting cancels it, so the greeting, the hold line, and a slow lookup never count as silence. When the timer runs out, the aggregator fires on_user_turn_idle.
The first time, the agent asks "Are you still there?" in the current voice's language. That's a TTSSpeakFrame, kept out of the LLM context like the hold line. When it finishes, the agent has stopped speaking again, so the timer starts over by itself. If the caller says anything, on_user_turn_started clears the count.
The second time, the agent tells the UI why the call is ending, then queues the goodbye and an EndFrame together. An EndFrame is the graceful way to stop a pipeline: it travels behind the goodbye, the TTS service holds it until the goodbye's audio is done, and the output transport plays everything queued ahead of it before the bot leaves the call. A CancelFrame would stop everything at once and cut the goodbye off. The goodbye is also marked uninterruptible, because the call ends either way, and a cough shouldn't swallow the last thing the caller hears.
A caller with the microphone muted is quiet too, but they haven't left. The same goes for a caller reading Settings, which turns the microphone off while it's open. So the UI sends a mic message whenever the microphone goes on or off, and the mic handler queues a UserIdleTimeoutUpdateFrame. A timeout of 0 stops the timer while the microphone is off. When it comes back on, the full timeout starts over.
The check-in and goodbye lines live in voices.py, next to the hold lines, one pair per language.
Next, we need to add some event handlers so the pipeline knows how to handle the user connecting and disconnecting. First, when the user connects, we'll tell the screen which voice is speaking and push a frame that tells the LLM to run. The LLM responds to the greeting instruction we put in the context.
# Handle client connection event
@transport.event_handler("on_client_connected")
async def on_client_connected(transport, client):
logger.info(f"Client connected to the Wreckless claims voice agent ({persona})")
await send_voice()
# Kick off the conversation
await task.queue_frames([LLMRunFrame()])And when the user disconnects, we'll cancel any waiting hold line and tell the bot to stop.
# Handle client disconnection event
@transport.event_handler("on_client_disconnected")
async def on_client_disconnected(transport, client):
logger.info("Client disconnected from the Wreckless claims voice agent")
cancel_hold_line()
await task.cancel()Finally, we can create a WorkerRunner, add our pipeline worker to it, and actually run the pipeline.
# Run the pipeline
runner = WorkerRunner(handle_sigint=runner_args.handle_sigint)
await runner.add_workers(task)
await runner.run()That's all the code we need for the run_bot function. But in order to run that function, we need to define an entrypoint function called bot that configures the transport, reads the persona, and calls run_bot. Add this function at the bottom of your botfile, making sure it's at the top level of indentation so it's not inside the run_bot function.
async def bot(runner_args: RunnerArguments):
"""Main bot entry point for the bot starter."""
transport_params = {
"daily": lambda: DailyParams(
audio_in_enabled=True,
audio_out_enabled=True,
),
"webrtc": lambda: TransportParams(
audio_in_enabled=True,
audio_out_enabled=True,
),
}
transport = await create_transport(runner_args, transport_params)
# The claims UI sends the persona and the starting voice with its WebRTC
# offer, as request_data
body = getattr(runner_args, "body", None)
await run_bot(transport, runner_args, persona_from_body(body), voice_from_body(body))The transport_params object describes how we want to configure the various transports that this bot supports. In this case, we'll support the DailyTransport (daily) and the SmallWebRTCTransport (webrtc). The bot starter will request a transport type in the runner_args, and the create_transport function will create a transport object that gets passed to the run_bot function. runner_args.body carries the request_data the client sent with its offer, and persona_from_body and voice_from_body turn it into a persona and a starting voice. Reading it with getattr keeps bot() working when a runner passes arguments without a body.
When you're developing locally, your 'bot runner' is built in to pipecat. We need to add one more thing to the bottom of our botfile for local development.
if __name__ == "__main__":
from pipecat.runner.run import main
main()With this code at the bottom of the botfile, we can run python agent.py from the command line. The Pipecat built-in bot runner main() function will call the bot() function with some pre-configured arguments in runner_args, which will in turn call the run_bot function to actually run the bot.
A voice agent answers one question at a time, and a caller only hears what they asked about. The claims UI in client/ adds a second channel. For a policyholder, once the caller is verified, the full claim appears on screen, and as the agent answers, the screen highlights the part of the claim it's talking about. For staff, the screen lists the claims each answer names, and any of them opens in the same claim view. The UI is a Next.js app built on Pipecat's Voice UI Kit and client SDK, and it connects to the same agent.py over the SmallWebRTCTransport.
The landing screen has a Policyholder / Wreckless staff toggle above the claim examples. The choice changes the landing copy and example questions, and it rides along with the call: client/src/app/page.tsx keeps one set of connect parameters per persona, each with requestData: { persona } on its webrtcRequestParams. The small WebRTC transport sends requestData in the body of its offer to /api/offer. There's a catch: the runner's /api/offer endpoint only reads the snake_case request_data field and silently drops requestData, so every call would start as a policyholder. client/src/app/api/offer/route.ts sits in front of the agent's endpoint, renames the field, and passes the offer and its ICE candidates through. The agent then reads the persona as runner_args.body. The persona is fixed once the call starts. Changing the connect parameters makes the Voice UI Kit build a new client, which is fine between calls and would end one in progress, so the toggle is disabled while connecting. The starting voice rides along too, as voice next to persona, but it's written into the persona's existing parameters at Start conversation rather than making new ones. New parameters would rebuild the client, which mid-call drops the call, and between calls remounts the screens and closes Settings while a voice is being picked. The copy for each persona, including the Try asking prompts, lives in client/src/lib/persona.tsx.
Pipecat already sends the browser the conversation itself: transcripts, the agent's words as they're spoken, and tool calls. The claims UI needs a few things only the agent knows, so run_bot sends them as RTVI server messages with RTVIServerMessageFrame:
type |
Persona | When | What it carries |
|---|---|---|---|
voice |
Both | When the caller connects, and after each voice switch | The voice, its language, and the Flux listen model |
verification |
Policyholder | Each time the agent checks a claim number and last name | checking, verified, failed, locked, or error when the lookup itself fails, and never why a check failed |
claim-focus |
Policyholder | Before each answer about a verified claim | The parts of the claim the answer covers, such as damage and photos |
claim-loaded |
Both | The first time a policyholder's claim is verified, and each time staff open a claim card | The claim, masked for the screen |
search-results |
Employee | After each research answer | The question, and the claims the answer names, masked for the screen |
function-timing |
Both | Just before each function call's result | The call's tool_call_id and how long it took, in milliseconds, for its row in the chat log |
call-ended |
Both | When the agent hangs up on a quiet caller, before the goodbye | reason: "idle", so the landing screen can say why the call ended |
The UI talks back with one message, mic, carrying { enabled }, each time the microphone goes on or off: from the mic button, or from Settings opening and closing. Both go through useSetMic in client/src/lib/session.tsx, so the agent's idle timer always knows when the caller can't be heard.
When the agent hangs up, the bot leaves the call, and the browser hears about it as BotDisconnected. Leaving the call isn't the same as ending it for the browser: over Daily, it would stay in the room alone, still on the call screen. So client/src/app/page.tsx disconnects too when the bot leaves. The landing screen comes back, and after an idle hang-up it says the call ended because the agent didn't hear anything.
Each function call shows in the chat log as a one-line row, collapsed by default: the function, its main argument, and how long it took. Tap it to see the full request. The duration comes from the bot, since the browser only learns when a call started, and Pipecat backdates that so the row sorts before the reply it belongs to. The row shows arguments only, never results: the results hold the claim record.
The parts of the claim come from the LLM itself. search_knowledge_base takes an optional sections argument, and the model fills it in on the same call it already makes to look up the answer, so pointing at the screen costs no extra round trip.
The screen reads the same record the agent does. Verification loads the record into the call's records cache, and claim-loaded goes out from it right after the answer, so the screen costs no extra knowledge base search or S3 read. search-results works the same way: the research agent loads each record it reads into the cache, and the handler builds the screen's claim list from it.
On the employee's screen, the desk shows the question and one card per claim in the latest search-results: claim ID, policyholder, status, vehicle, and adjuster. Selecting a card opens that claim in the same review screen policyholders see, with a Results button to go back. The open claim stays open when a new search comes in, so staff can keep reading while they ask the next question.
Opening a card is something the staff member does, not the agent, so the agent has to be told. Otherwise "what am I looking at?" has no answer, and "who's the adjuster on this one?" sends the research agent off to search for a claim that's already on screen. Here the browser talks to the agent, instead of the other way round. EmployeeSession in client/src/components/employee/EmployeeDesk.tsx sends an RTVI client message each time a card opens or the staff member goes back to the results:
if (!client?.connected) return;
if (claim) client.sendClientMessage("claim-opened", { claim_id: claim.claim_id });
else client.sendClientMessage("claim-closed");Only the claim ID goes. The agent never takes claim data from the browser, so a page can't put words in the prompt. In run_bot, the handlers look the record up in the call's records cache, where the search that showed the card put it, or load it from the knowledge base. A claim ID that doesn't match a record is logged and ignored. Add them below search_claims:
async def claim_opened(data: dict):
"""A staff member opened a claim card: put its record in the prompt.
Only the claim ID is taken from the browser. The record comes from
this call's cache, where the search that showed the card put it, or
from the knowledge base, so the browser can't put words in the prompt.
"""
nonlocal on_screen, screen_changes
claim_id = normalize_claim_id(
str(data.get("claim_id", "")) if isinstance(data, dict) else ""
)
if not claim_id:
logger.warning(f"Ignoring claim-opened without a valid claim ID: {data}")
return
screen_changes += 1
change = screen_changes
try:
record = await load_claim(claim_id)
except Exception as e:
logger.error(f"Error loading on-screen claim {claim_id}: {e}")
return
if record is None:
logger.warning(f"Ignoring claim-opened for unknown claim {claim_id}")
return
view = await on_screen_view(record)
if change != screen_changes:
return
on_screen = view
logger.info(f"Staff opened claim {claim_id}")
await refresh_prompt()
# The session's claim, which the review screen's focus and highlights
# use. The staff desk routes on its own open claim, not on this.
await send_to_client({"type": "claim-loaded", "claim": claims.to_client_view(record)})
async def claim_closed(data: dict):
"""A staff member went back to the results: take the claim out of the prompt."""
nonlocal on_screen, screen_changes
screen_changes += 1
if on_screen is None:
return
logger.info(f"Staff closed claim {on_screen['claim_id']}")
on_screen = None
await refresh_prompt()
async def show_on_screen(params: FunctionCallParams):
"""Highlight the cards the answer is in, on the claim a staff member has open."""
if on_screen is None:
await params.result_callback(
{"response": "No claim is open. Say so, and ask which claim they mean."}
)
return
sections = [
s for s in params.arguments.get("sections") or [] if s in CLAIM_SECTIONS
]
await send_to_client(
{
"type": "claim-focus",
"claim_id": on_screen["claim_id"],
"sections": sections or ["overview"],
}
)
await params.result_callback(
{
"response": "Highlighted on their screen. Answer from the record "
"now, briefly, without saying where it is on the screen."
}
)Pipecat's RTVI processor raises on_client_message for every client message, with its type and data. One handler dispatches them by type, so each feature only adds an entry. Add it right after the pipeline worker, since it hangs off task.rtvi:
# Messages the claims UI sends during a call, by type
client_handlers: dict[str, Callable[[dict], Awaitable[None]]] = {}
@task.rtvi.event_handler("on_client_message")
async def on_client_message(rtvi, msg):
handler = client_handlers.get(msg.type)
if handler is None:
logger.warning(f"Ignoring unknown client message: {msg.type}")
return
await handler(msg.data or {})
# Staff tell the agent which claim they have open. Policyholders only see
# the claim they were verified for, so their calls don't accept these.
if persona == EMPLOYEE:
client_handlers["claim-opened"] = claim_opened
client_handlers["claim-closed"] = claim_closedThe open claim lives in the system prompt, not in the conversation. refresh_prompt rebuilds the prompt with an ON SCREEN block holding the claim's masked record, the same spoken_view the policyholder lookup returns, with amounts ready to speak. Opening another claim replaces the block, and going back removes it, so the model never answers from a claim that's no longer open. With no claim open, the staff prompt tells the model to say so if asked about "this claim". A voice switch goes through refresh_prompt too, so it keeps the open claim. The model doesn't speak when a card opens: it answers the next question with the claim in mind.
claim-loaded also goes back to the browser, which sets the session's claim for the review screen's focus and highlights. The staff desk doesn't route on it: which claim is open stays the desk's own state, so a late claim-loaded can't reopen a claim. While a claim is open, the Try asking prompts change to questions about it, such as "Who's the adjuster?".
Staff answer from the open claim without a lookup, so no lookup's sections would point their screen at the answer. Left alone, the model fills the gap by saying where to look ("it's in the Repair estimate card"), which is reading the screen aloud to someone who's looking at it. show_on_screen takes the same sections as the policyholder lookup and sends the same claim-focus message, and the ON SCREEN rules tell the model to call it before answering and never to name a card. Register it with the staff tools:
llm.register_function("show_on_screen", reports_timing(show_on_screen))With no claim open, it tells the model to say so instead of highlighting anything. It costs one more LLM round trip per answer, which is short next to a search_claims research run.
The UI's language follows the voice: pick a Spanish voice in Settings, or ask the agent to speak German, and the screen switches with it. During a call, every voice message from the agent becomes the remembered voice, so the screen follows the agent's switches, and the next call starts where the last one left off.
The text lives in client/src/i18n/, one typed dictionary per language: English, Spanish, French, German, Hindi, Russian, Portuguese, Italian, and Dutch. en.ts defines the shape, and every other language is typed against it, so npm run typecheck fails on a missing or extra key. Text with numbers or names in it is a function, such as claims: (n) => ..., so each language handles its own plurals. Components read the text with useT(), and dates go through Intl.DateTimeFormat for the current language. No i18n library is involved.
Claim records stay in English on screen. Names, notes, statuses, damage areas, and other values show as recorded; the agent translates them when it speaks. The screen does translate the labels the agent sends with a record, such as Deductible, Collision, or Paid to date, when the dictionary knows them (fieldLabels in each language), and shows any other label as sent.
The server renders the page in English, since it can't see the browser's storage. The browser switches to the remembered language right after it loads, and keeps <html lang> in step.
claims.to_client_view applies the agent's privacy rules before anything leaves the server. The screen shows the last four characters of the VIN and plate, leaves out the policyholder's address, phone, and email, shows the other driver's name, insurer, and vehicle but not their policy number, VIN, or plate, and drops map coordinates and photo IDs. When a claim happened at home, the record names the home address as the location, so to_client_view replaces the address wherever it appears. It's also the only view of a claim the models get, through spoken_view, so the screen and the voice can't disagree about what's private. Staff get the same view: anyone can pick the staff persona in this demo, so it opens up every claim but none of the contact details. The tests in tests/test_claim_view.py check every sample claim for each of these.
You need Node.js 20 or later. Start the agent in one terminal:
python agent.pyThen start the UI in another:
cd client
npm install
npm run devOpen http://localhost:3000 and select Start conversation. The UI sends its WebRTC offer to /api/offer, and client/src/app/api/offer/route.ts passes it to the agent on port 7860, with the persona renamed to the field the runner reads. client/next.config.ts passes the agent's other endpoints through. Set BOT_URL to point the UI at an agent somewhere else. Pipecat's prebuilt test client still works at http://localhost:7860/client.
Say a claim number, such as "C L M, two zero two six, eight four seven two nine one," and the last name "Mitchell" when the agent asks for it. The Try asking prompts also send their question as text, which helps in a noisy room. For the staff persona, select Wreckless staff before you start, and ask a question across the claims, such as "Which claims are waiting on a title?" or "Which claims are being adjusted by Luis Ortega?" Then open one of the claims and ask "What am I looking at?" or "Who's the adjuster on this one?" The agent answers from the open claim without searching again.
To work on the screens without a call, open http://localhost:3000/?fixture=CLM-2026-847291&screen=review. The screen can be landing, listening, found, or review, and any sample claim ID works. For the staff desk, open http://localhost:3000/?persona=employee&fixture=search, with screen=listening for the desk before its first search or screen=landing for the staff landing screen. The fixtures in client/src/fixtures/claims.json are the same masked view the agent sends; regenerate them after changing a claim record or to_client_view:
.venv/bin/python scripts/export_claim_fixtures.pyFixtures load only in development, so no claim data ships in a production build. Connecting the UI to an agent on Pipecat Cloud isn't covered yet.
You'll see one warning when the page loads, in the browser console and in the npm run dev terminal. It starts with [DEPRECATED] atomFamily is deprecated and will be removed in v3. It's safe to ignore. It doesn't come from this app's code: Pipecat's React SDK, @pipecat-ai/client-react, imports atomFamily from jotai/utils, and jotai 2.16 and later warn when it's used. Nothing in client/ imports jotai directly. The warning goes away once a React SDK release stops using atomFamily and the exact @pipecat-ai/client-react version in client/package.json is bumped to it.
You can deploy this bot to Pipecat Cloud and get production-ready infrastructure in about 5 minutes. Rename your file from agent.py to bot.py, then follow the instructions from the Pipecat Cloud Quickstart!