Voice Agent Code Samples

Production-ready code examples for building enterprise voice agents with OpenAI, ElevenLabs, and Twilio

Worth $500k+ β€’ Production-Ready β€’ Copy & Deploy in Minutes

Enterprise-Grade

Built for production with error handling, logging, and scalability

Multi-AI Voice

Integrates GPT-5, Whisper, and ElevenLabs for premium voice quality

Phone Integration

Complete Twilio integration for real-world phone calls

One-Click Copy

Copy any code sample with a single click and deploy immediately

Basic Voice Agent

Complete voice agent with STT β†’ LLM β†’ TTS pipeline

# 🎀 KOJIE AI Voice Agent - Production Ready
import os
from openai import OpenAI
import requests

class VoiceAgent:
    """Enterprise voice agent with <500ms latency"""
    
    def __init__(self, agent_config):
        self.name = agent_config.get('name', 'KOJIE Voice Agent')
        self.system_prompt = agent_config.get('system_prompt', 'You are a helpful AI assistant.')
        self.voice_id = agent_config.get('voice_id', '21m00Tcm4TlvDq8ikWAM')
        
        # API clients
        self.openai_client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
        self.elevenlabs_api_key = os.environ.get("ELEVENLABS_API_KEY")
        self.conversation_history = []
        
    def process_audio_input(self, audio_data: bytes) -> str:
        """Convert speech to text using OpenAI Whisper"""
        response = self.openai_client.audio.transcriptions.create(
            model="whisper-1",
            file=("audio.wav", audio_data),
            response_format="text"
        )
        return response.strip()
    
    def generate_ai_response(self, user_input: str) -> str:
        """Generate intelligent response using GPT-5"""
        self.conversation_history.append({"role": "user", "content": user_input})
        
        messages = [
            {"role": "system", "content": self.system_prompt}
        ] + self.conversation_history
        
        response = self.openai_client.chat.completions.create(
            model="gpt-5",
            messages=messages,
            max_tokens=200,
            temperature=0.7
        )
        
        ai_response = response.choices[0].message.content
        self.conversation_history.append({"role": "assistant", "content": ai_response})
        return ai_response
    
    def synthesize_speech(self, text: str) -> bytes:
        """Convert text to speech using ElevenLabs"""
        url = f"https://api.elevenlabs.io/v1/text-to-speech/{self.voice_id}/stream"
        
        headers = {
            "Accept": "audio/mpeg",
            "Content-Type": "application/json",
            "xi-api-key": self.elevenlabs_api_key
        }
        
        data = {
            "text": text,
            "model_id": "eleven_flash_v2_5",
            "voice_settings": {
                "stability": 0.5,
                "similarity_boost": 0.75,
                "style": 0.0,
                "use_speaker_boost": True
            },
            "optimize_streaming_latency": 4
        }
        
        response = requests.post(url, json=data, headers=headers, stream=True)
        
        if response.status_code == 200:
            return b''.join([chunk for chunk in response.iter_content(chunk_size=1024) if chunk])
        return b""

# Usage Example
config = {
    'name': 'Customer Support Agent',
    'system_prompt': 'You are a professional customer support agent who helps customers efficiently.',
    'voice_id': '21m00Tcm4TlvDq8ikWAM'  # Professional female voice
}

agent = VoiceAgent(config)
# Process audio: audio_bytes β†’ transcription β†’ AI response β†’ speech audio
OpenAI GPT-5 Whisper STT ElevenLabs TTS < 500ms Latency

Phone Agent with Twilio

Complete phone system with call handling and routing

# πŸ“ž KOJIE AI Phone Agent System - Twilio Integration
import os
import uuid
from datetime import datetime
from enum import Enum
from dataclasses import dataclass
from typing import Dict, List, Optional

class AgentType(Enum):
    CUSTOMER_SUPPORT = "customer_support"
    SALES_AGENT = "sales_agent"
    APPOINTMENT_SCHEDULER = "appointment_scheduler"
    TECHNICAL_SUPPORT = "technical_support"

class VoicePersonality(Enum):
    PROFESSIONAL_FEMALE = "professional_female"
    PROFESSIONAL_MALE = "professional_male"
    FRIENDLY_FEMALE = "friendly_female"
    FRIENDLY_MALE = "friendly_male"

@dataclass
class PhoneAgent:
    agent_id: str
    name: str
    agent_type: AgentType
    voice_personality: VoicePersonality
    phone_number: str
    business_name: str
    business_hours: str
    system_prompt: str
    greeting_message: str
    transfer_number: Optional[str] = None
    enabled: bool = True

class PhoneAgentSystem:
    """Enterprise Phone Agent Integration"""
    
    def __init__(self):
        self.agents: Dict[str, PhoneAgent] = {}
        self._initialize_twilio()
        
        # Voice ID mapping for ElevenLabs
        self.voice_ids = {
            VoicePersonality.PROFESSIONAL_FEMALE: '21m00Tcm4TlvDq8ikWAM',
            VoicePersonality.PROFESSIONAL_MALE: 'TxGEqnHWrfWFTfGW9XjX',
            VoicePersonality.FRIENDLY_FEMALE: 'EXAVITQu4vr4xnSDxMaL',
            VoicePersonality.FRIENDLY_MALE: 'pNInz6obpgDQGcFmaJgB'
        }
    
    def _initialize_twilio(self):
        """Initialize Twilio client"""
        try:
            account_sid = os.environ.get("TWILIO_ACCOUNT_SID")
            auth_token = os.environ.get("TWILIO_AUTH_TOKEN")
            
            if account_sid and auth_token:
                from twilio.rest import Client
                self.twilio_client = Client(account_sid, auth_token)
                print("βœ… Twilio client initialized")
            else:
                print("⚠️ Twilio credentials not found")
        except Exception as e:
            print(f"❌ Twilio initialization failed: {e}")
    
    def create_agent(self, agent_config: Dict) -> PhoneAgent:
        """Create a new phone agent"""
        agent_id = f"agent_{uuid.uuid4().hex[:8]}"
        
        agent = PhoneAgent(
            agent_id=agent_id,
            name=agent_config.get('name', 'AI Assistant'),
            agent_type=AgentType(agent_config.get('agent_type', 'customer_support')),
            voice_personality=VoicePersonality(agent_config.get('voice_personality', 'professional_female')),
            phone_number=agent_config.get('phone_number', ''),
            business_name=agent_config.get('business_name', 'Your Business'),
            business_hours=agent_config.get('business_hours', '9 AM - 5 PM'),
            system_prompt=self._generate_system_prompt(agent_config),
            greeting_message=self._generate_greeting(agent_config),
            transfer_number=agent_config.get('transfer_number')
        )
        
        self.agents[agent_id] = agent
        print(f"βœ… Created phone agent: {agent.name} ({agent_id})")
        return agent
    
    def _generate_system_prompt(self, config: Dict) -> str:
        """Generate context-aware system prompt"""
        return f"""You are {config.get('name', 'an AI assistant')} for {config.get('business_name', 'the company')}.

Business Hours: {config.get('business_hours', 'Standard business hours')}
Your Role: {config.get('agent_type', 'customer support')}

Be professional, helpful, and concise. If you cannot help, offer to transfer the call."""
    
    def _generate_greeting(self, config: Dict) -> str:
        """Generate professional greeting"""
        return f"Thank you for calling {config.get('business_name', 'us')}. How may I help you today?"

# Usage Example
phone_system = PhoneAgentSystem()

agent_config = {
    'name': 'Sarah',
    'business_name': 'ABC Medical Office',
    'agent_type': 'customer_support',
    'voice_personality': 'professional_female',
    'phone_number': '+1234567890',
    'business_hours': 'Monday-Friday 9 AM - 5 PM',
    'transfer_number': '+1987654321'
}

agent = phone_system.create_agent(agent_config)
print(f"Agent ready: {agent.name} at {agent.phone_number}")
Twilio Multi-Voice Call Routing Professional

CRM Voice Agent Trainer

Super simple CRM integration with business knowledge training

# 🎯 KOJIE AI - Simple CRM Voice Agent Trainer
from dataclasses import dataclass
from typing import List, Dict, Any
from enum import Enum

class BusinessType(Enum):
    RESTAURANT = "restaurant"
    MEDICAL_OFFICE = "medical_office"
    AUTO_REPAIR = "auto_repair"
    REAL_ESTATE = "real_estate"
    SALON_SPA = "salon_spa"
    RETAIL_STORE = "retail_store"

@dataclass
class VoicePersonality:
    personality_id: str
    name: str
    description: str
    good_for: List[str]
    sample_greeting: str

class CRMVoiceAgentTrainer:
    """The simplest CRM voice agent trainer"""
    
    def __init__(self):
        self.voice_options = self._create_voice_options()
    
    def _create_voice_options(self) -> List[VoicePersonality]:
        """Create easy-to-understand voice options"""
        return [
            VoicePersonality(
                personality_id="professional_sarah",
                name="Professional Sarah",
                description="Sounds like a professional receptionist - clear, trustworthy",
                good_for=["Medical offices", "Legal firms", "Financial services"],
                sample_greeting="Good morning, thank you for calling ABC Company. This is Sarah, how may I help you today?"
            ),
            VoicePersonality(
                personality_id="friendly_mike",
                name="Friendly Mike",
                description="Sounds like a helpful guy who genuinely cares",
                good_for=["Auto repair", "Construction", "Restaurants"],
                sample_greeting="Hey there! Thanks for calling Mike's Auto Repair. I'm Mike, what can I do for you?"
            ),
            VoicePersonality(
                personality_id="caring_emma",
                name="Caring Emma",
                description="Warm and empathetic - perfect for healthcare",
                good_for=["Healthcare", "Veterinary", "Counseling"],
                sample_greeting="Hello, thank you for calling. This is Emma, and I'm here to help you today."
            ),
            VoicePersonality(
                personality_id="energetic_alex",
                name="Energetic Alex",
                description="Upbeat and enthusiastic - great for sales",
                good_for=["Retail", "Gyms", "Entertainment", "Sales"],
                sample_greeting="Hi there! You've reached the best team in town! I'm Alex - what can I help you find today?"
            )
        ]
    
    def setup_business_agent(self, business_info: Dict[str, Any]) -> Dict[str, Any]:
        """Quick setup for business voice agent"""
        
        # Auto-select best voice based on business type
        business_type = business_info.get('business_type')
        recommended_voice = self._recommend_voice(business_type)
        
        return {
            'agent_id': f"agent_{business_info['business_name'].lower().replace(' ', '_')}",
            'voice_personality': recommended_voice,
            'system_prompt': self._generate_business_prompt(business_info),
            'greeting': self._generate_business_greeting(business_info),
            'ready_to_deploy': True
        }
    
    def _recommend_voice(self, business_type: str) -> str:
        """Recommend voice based on business type"""
        recommendations = {
            'medical_office': 'professional_sarah',
            'auto_repair': 'friendly_mike',
            'restaurant': 'energetic_alex',
            'salon_spa': 'caring_emma',
            'real_estate': 'professional_sarah'
        }
        return recommendations.get(business_type, 'professional_sarah')
    
    def _generate_business_prompt(self, info: Dict) -> str:
        """Generate custom prompt for business"""
        return f"""You are {info['business_name']}'s AI receptionist.

Business Type: {info.get('business_type', 'Service Business')}
Hours: {info.get('business_hours', 'Standard business hours')}
Services: {', '.join(info.get('services', ['General services']))}

Be friendly, professional, and help customers with:
- Scheduling appointments
- Answering common questions
- Taking messages when needed

Transfer complex issues to: {info.get('transfer_number', 'the manager')}"""
    
    def _generate_business_greeting(self, info: Dict) -> str:
        """Generate personalized greeting"""
        return f"Thank you for calling {info['business_name']}. How can I help you today?"

# Usage Example
trainer = CRMVoiceAgentTrainer()

# Simple business setup
business_info = {
    'business_name': 'Downtown Auto Repair',
    'business_type': 'auto_repair',
    'business_hours': 'Monday-Friday 8 AM - 6 PM',
    'services': ['Oil changes', 'Brake repair', 'Engine diagnostics'],
    'transfer_number': '+1234567890'
}

agent_config = trainer.setup_business_agent(business_info)
print(f"βœ… Agent ready for {business_info['business_name']}")
print(f"Voice: {agent_config['voice_personality']}")
print(f"Greeting: {agent_config['greeting']}")
CRM Integration Auto-Configuration Business-Ready 5-Min Setup

Quick Start Guide

  1. Copy the code you need using the buttons above
  2. Set up your API keys:
    • OPENAI_API_KEY - Get from OpenAI
    • ELEVENLABS_API_KEY - Get from ElevenLabs
    • TWILIO_ACCOUNT_SID & TWILIO_AUTH_TOKEN - Get from Twilio
  3. Install dependencies: pip install openai requests twilio
  4. Run your agent and start handling voice calls!