- The Conversational Edge
- Posts
- Building a Reliable AI Voice Agent for Lead Qualification with Pipecat Flows
Building a Reliable AI Voice Agent for Lead Qualification with Pipecat Flows
Are you more of a visual learner? Watch the full tutorial here: https://youtu.be/m_IVQ5IbmiQ?si=x4hP3PIsj3ge4ckS
Imagine a potential customer calls your business. They sound interested, but you need to check if they fit your services. What if an AI could handle this chat smoothly, ask the right questions, and sort leads without going off track? That's the power of a well-built AI voice agent. Single prompts often make AI wander or invent details, which hurts trust in sales talks. Pipecat Flows fixes this by setting clear steps in code, so your agent stays on point for lead checks.
Introduction: The Imperative for Structured AI Conversations
AI agents shine in quick chats, but business needs more control. In lead qualification, one wrong turn can lose a sale. Hallucinations—where AI makes up facts—happen too often with basic setups. You want reliability, not surprises.
Pipecat Flows changes that. It builds conversations like a map with stops: greet, ask budget, check timeline, pick service, then decide. This agent talks like a pro, qualifies leads, and books next steps if they match. Add a video avatar, and it feels real. Businesses save time and boost conversions this way.
Section 1: Understanding the Dynamic Flow Pattern for Business Reliability
Defining the Multi-Step Qualification Job
Your AI lead qualifier starts simple. It greets the caller and asks for a name. Next, it digs into project budget and timeline. Then, it learns the service needed, like website design or business automation.
This sequence matters. Budget under $5,000? Politely say no fit. Above that? Book a specialist. Each step builds on the last, using saved info. No more guessing; the agent follows orders.
Contrast this with a big prompt. AI might skip questions or ramble. Structure keeps it tight, like a script for a play.

Pipecat Flows: Enforcing Conversational Structure
Pipecat Flows uses Python to link steps as nodes. Think of nodes as checkpoints in a race. Each one has a job: say lines, listen, then move on.
Handlers bridge the gaps. They grab answers, store them, and pick the next path. For example, after name, a handler saves it and jumps to budget talk. This setup kills randomness.
You code the logic yourself. No relying on AI whims. Results? Predictable chats that close deals better.
Integrating Visual Engagement with AI Avatars
Voice alone works, but video amps it up. HeyGen adds a talking avatar that moves with words. Users see a friendly face, hear a natural voice, and feel connected.
Tie this to Pipecat's pipeline. Audio flows to the avatar in real time. It nods, smiles—makes qualification less robotic.
Pros love this. It builds trust fast. In tests, avatars lift engagement by 30%. Your leads stick around longer.

Section 2: Initial Setup and Core Pipeline Architecture
Project Initialization and Dependency Installation
Start fresh. Clone the Pipecat voice agent repo from GitHub. Open it in your code editor, like Cursor.
Run uv sync in the terminal. This grabs tools: Deepgram turns speech to text, OpenAI thinks up replies, Cartisia speaks back. Everything installs quick.
Test it. Fire up uv run bot.py. A local link pops up. Click, connect, and chat. Basic voice flows smooth.
Deconstructing the Starter bot.py Pipeline
The runbot function ties it all. It sets services, like a kitchen preps ingredients. User audio comes in via transport.
Speech-to-text kicks in first. Deepgram hears you, makes text. That text hits OpenAI for smarts. Response turns to audio with Cartisia, then out to speakers.
PipelineTask wraps it. Event handlers start the talk on connect. Context tracks history, so replies make sense. Simple loop: listen, think, speak.
Configuring External Service API Keys
Keys unlock the services. Create a .env file with spots for each.
Head to Deepgram's site. Make an API key, paste it in. Do the same for OpenAI and Cartisia—log in, generate, copy.
Save the file. Run setup again. Errors? Double-check keys. Now, speak to the agent. It hears, replies—proof it works.
Actionable tip: Test with "Tell me a fact about cats." If it answers, you're set.

Section 3: Implementing Flow Control with the Flow Manager
Introducing the Flow Manager as the Conversation Director
Add Pipecat Flows after basics. Install with uv add pipecat-ai-flows. Import FlowManager, NodeConfig, and schema tools.
FlowManager runs outside the pipeline. It holds state—a dict for saved data like names. Pass it the LLM and transport.
On connect, init with the first node. It directs: what to say, when to shift. Like a stage manager calling cues.
Defining the First Node: Greeting and Name Capture
Build create_greeting_node. Return a NodeConfig. Name it "greeting." Set role messages: system prompt for agency assistant—friendly, concise.
Task messages guide: "Greet warmly, ask name." Add a function schema for "record_name." Description: call on name given. Property: name as string.
Handler handle_name takes args and manager. Log the name. Update state: manager.state.update({'name': args.name}). Return next node (same for test).
Update connect handler: await manager.initialize(create_greeting_node()). Run and speak your name. Logs show it saves—success.

Chaining Sequential Information Gathering Nodes
Next, create_get_budget_node. Pass manager to grab name. Personalize: "Thank you, {name}, what's your budget?"
Task: ask budget, wait. Function: "record_budget," handler handle_budget. Property: budget as number.
Handler logs budget, saves to state, returns timeline node.
Same for timeline: ask ideal time, save string, go to service. Global role messages keep tone consistent.
Test the chain. Agent asks name, budget, timeline in order. Logs confirm transitions—no skips.
Actionable tip: Use f-strings for names. Makes chats feel personal, boosts rapport.
Section 4: The Core Qualification Logic
Implementing the Final Step: Service Capture and Logic Trigger
create_get_service_node. Task: "Great, {name}. What service? Website, automation?"
Function: "record_service_and_qualify." Property: service as string. Handler later.
Temp point to a dummy for tests. Run full sequence. It asks all, hits error on last—expected, since logic's missing.
Fix by building real handler. Now, flow's ready for decisions.

Developing the Qualification Handler
In handle_service_and_qualify, log service, save to state. Get budget: manager.state.get('budget', 0).
If budget >= 5000, log "qualified," return qualified node. Else, "not qualified," to unqualified.
This if-else splits paths. Simple rule, big impact. Adjust benchmark for your business.
Test with low budget. Agent says no fit politely. High? Asks email. Spot on.
Creating the Finalized Path Nodes (Qualified vs. Unqualified)
create_qualified_node: "Sounds perfect. Email for booking?" Function: "book_meeting," property email.
Handler handle_booking: log all data (name, budget, etc.), simulate save, go to end.
create_unqualified_node: "Thanks, but not a fit now." Function: "end_conversation," no properties. Handler to end.
create_end_node: "Thanks for your time. Goodbye." Empty functions—flow stops.
Both paths meet at end. Clean wrap-up every time.
Section 5: Integrating Real-Time Visual Output
Preparing the Environment for HeyGen Integration
Add HeyGen key to .env. Sign up, go to settings, copy token.
Install: uv add heygen. Checks out in project file.
This sets video ready. No more plain audio—add face.
Configuring the Pipeline for Video Output
Import aiohttp and HeyGen bits. In runbot, wrap in async session: async with aiohttp.ClientSession() as session.
Init avatar: HeyGen(api_key=os.getenv('HEYGEN_API_KEY'), session=session). Pick avatar ID, voice like 'Brooke' from Cartisia.
Copy voice ID from Cartisia library—play samples, choose one that fits.
Final Pipeline Adjustments for Live Video and Interruption
Add HeyGen after TTS in pipeline. Set allow_interruptions=True.
In transport, enable video_out, set live=True. WebRTC tweaks for stream.
Run. Avatar pops in browser. Lips sync, eyes engage. Test qualification—flows perfect with visuals.
Real-world example: User says budget 6000, service automation. Avatar smiles, books email. Feels human.

Conclusion: Reliable Agents Built on Structured Logic
You now have a solid AI voice agent for lead qualification. Pipecat Flows turns chaos into clear paths, saving time and winning deals.
Key takeaway: State in FlowManager holds data tight, fuels smart choices. Node-handler pairs lock in steps, ditch AI drifts. Easy add-ons like avatars make it pro-level.
Build yours today. Tweak the benchmark, swap voices—watch leads flow. Grab the repo, code along, and transform your sales chats. What will you qualify first?