How to Build an AI SMS Sales Assistant with Twilio and OpenAI
Build an AI-powered SMS system that responds to leads in under 60 seconds and qualifies them through natural conversation.
How to build an AI SMS sales assistant that responds to leads in under 60 seconds, qualifies them through natural conversation, and logs everything to your CRM — using Twilio and OpenAI.
Introduction
Five minutes is too slow. Research from Lead Connect shows that responding within 60 seconds increases conversions by 391% compared to waiting even five minutes. InsideSales.com (now XANT) found that leads contacted within one minute are 391% more likely to convert, and that after five minutes the odds of qualifying a lead drop by 80%. Most service businesses respond in hours. Some respond the next day. A few never respond at all.
An AI SMS sales assistant fixes this by doing one thing: responding to every inbound lead within seconds, carrying on a natural text conversation, qualifying the lead, and either booking a call or handing off to a human. No missed leads. No forgotten follow-ups. No "sorry I just saw this" messages sent three days later.
This guide walks through how to build one from scratch using Twilio for SMS delivery, OpenAI or Claude for the AI brain, and a CRM to log everything. We build systems like this at Luminous Digital Visions, and the architecture below is the same pattern we use in production. If you want to skip the build and have us set it up, get in touch.
Why response speed decides who gets the deal
Why response speed decides who gets the deal
A lead fills out your form at 2:14 PM. They're sitting at their desk, phone in hand, actively thinking about the problem they need solved. If you text them at 2:15 PM, you catch them in that window. If you text them at 6:30 PM, they've moved on. They filled out three other forms. They're making dinner. Your message is just another notification they swipe away.
The data backs this up consistently:
- MIT and InsideSales.com research found that the odds of contacting a lead drop by 10x if you wait longer than five minutes after the inquiry.
- Vendasta reports that 78% of customers buy from the first responder.
- A Harvard Business Review study found that companies who contacted leads within an hour were seven times more likely to qualify them than companies who waited two hours.
This is the single biggest reason service businesses lose leads. Not pricing, not competition, not product-market fit. Slow response time. And it's the easiest thing to fix with automation.
We've written about the broader problem of lead leakage in service businesses before. Slow SMS response is the number one leak.
System architecture overview
System architecture overview
Here's the full flow from lead to booked call:
Website Form / Landing Page
↓
Webhook (POST)
↓
Your Backend Server
↓
AI Layer (OpenAI / Claude API)
↓
Twilio SMS API → Lead's Phone
↓
Lead Replies → Twilio Webhook → Your Server → AI → Reply
↓
Qualification Complete → Book Call / Human Handoff
↓
CRM Updated (deal stage, tags, conversation log)
The pieces:
Twilio handles sending and receiving SMS messages. You buy a phone number, configure webhooks, and Twilio routes everything through their API. Cost is about $0.0079 per outbound SMS segment in the US.
Your backend server (Node.js, Python, Go, or whatever you prefer) receives webhooks from both your website forms and Twilio's inbound message handler. It orchestrates the flow.
The AI layer takes the conversation history, the lead's context, and a system prompt you've written, then generates the next message. You send this through OpenAI's chat completions API or Anthropic's messages API.
Your CRM (GoHighLevel, HubSpot, Pipedrive, etc.) stores the lead record, conversation log, deal stage, and any tags. We typically use GoHighLevel for service businesses because its pipeline and automation features pair well with this kind of system.
This architecture works whether you're handling 10 leads a day or 500. The AI layer and Twilio both scale horizontally. Your database is the only bottleneck, and for conversation storage even SQLite handles thousands of concurrent threads without issue.
Setting up Twilio for SMS
Setting up Twilio for SMS
Start by creating a Twilio account and buying a phone number. You want a local number (not a toll-free or short code) because local numbers have better deliverability for conversational SMS and feel more personal to the recipient.
Step 1: Buy a number. In the Twilio console, go to Phone Numbers > Buy a Number. Pick one with SMS capability in your area code. Cost is $1.15/month.
Step 2: Register for A2P 10DLC. If you're sending SMS in the US, you need to register your brand and campaign with The Campaign Registry through Twilio's console. This is mandatory since 2023. Without it, carriers will filter your messages. Registration takes 1-3 business days and costs a one-time $15 brand fee plus $0.10/month per campaign.
Step 3: Configure the inbound webhook. Under your phone number's settings, set the "A message comes in" webhook to your server's endpoint. Something like https://yourserver.com/api/twilio/inbound. Set the method to POST. Twilio will send the sender's phone number, message body, and metadata to this endpoint every time someone texts your number.
Step 4: Set up your outbound function. Sending an SMS through Twilio is a single API call:
const twilio = require('twilio');
const client = twilio(process.env.TWILIO_ACCOUNT_SID, process.env.TWILIO_AUTH_TOKEN);
async function sendSMS(to, body) {
const message = await client.messages.create({
body: body,
from: process.env.TWILIO_PHONE_NUMBER,
to: to
});
return message.sid;
}
That's the Twilio side. The hard part is what happens between receiving a webhook and deciding what to send back.
Building the AI conversation layer
Building the AI conversation layer
The AI layer is where your SMS assistant goes from "dumb autoresponder" to "sales rep who never sleeps." You're building three things: a system prompt, a conversation memory store, and a response generator.
The system prompt
Your system prompt defines who the AI is, how it behaves, what it knows about your business, and what its goal is. Here's a real example we'd use for a home services company:
You are a friendly SMS sales assistant for ABC Plumbing, a residential plumbing
company in Austin, TX. Your name is Sarah.
GOAL: Qualify inbound leads and book them for a phone consultation with the
sales team. You are NOT trying to close deals over text. You're trying to
get them on a call.
WHAT YOU KNOW:
- ABC Plumbing handles residential plumbing: repairs, remodels, water heaters,
drain clearing, leak detection
- Service area: Austin, Round Rock, Cedar Park, Pflugerville, Georgetown
- Business hours: Mon-Fri 7AM-6PM, emergency service 24/7
- Free estimates for jobs over $500
- Licensed and insured, 15 years in business
QUALIFICATION QUESTIONS (ask naturally, not as a list):
1. What plumbing issue are they dealing with?
2. Is it urgent/emergency or can it wait?
3. Residential or commercial property?
4. What's their zip code? (confirm service area)
5. When would they like someone to come out?
RULES:
- Keep messages under 300 characters when possible. This is SMS, not email.
- Sound human. Use casual language. No corporate speak.
- Never give pricing over text. Say "pricing depends on the specific situation,
which is why we do a quick call first."
- If they ask something you don't know, say "Let me check with the team and
get back to you" then flag for human handoff.
- If they say they're not interested or ask to stop, immediately acknowledge
and stop messaging.
- If they seem ready to book, offer 2-3 specific time slots for a call.
- After qualifying, hand off to a human with a summary.
TONE: Friendly, direct, helpful. Like texting a knowledgeable friend
who happens to work at a plumbing company.
This prompt does most of the heavy lifting. A good system prompt eliminates 80% of the edge cases you'd otherwise need to handle in code.
Conversation memory
SMS conversations happen over hours or days. The AI needs context from previous messages to avoid repeating questions or losing the thread. Store every message (both inbound and outbound) in a database table:
CREATE TABLE conversations (
id SERIAL PRIMARY KEY,
lead_phone VARCHAR(20) NOT NULL,
direction VARCHAR(10) NOT NULL, -- 'inbound' or 'outbound'
message_text TEXT NOT NULL,
created_at TIMESTAMP DEFAULT NOW(),
metadata JSONB
);
CREATE INDEX idx_conversations_phone ON conversations(lead_phone);
When a new inbound message arrives, pull the full conversation history for that phone number and pass it to the AI as the message array. This gives the model full context on what's already been discussed.
For conversations that stretch past 30-40 messages (rare for SMS but it happens), trim older messages or summarize them. OpenAI's GPT-4o handles about 128k tokens, and Claude handles 200k, so you have plenty of room for typical SMS threads.
The response generator
Here's the function that ties it together:
async function generateAIResponse(leadPhone, inboundMessage) {
// 1. Store the inbound message
await db.query(
'INSERT INTO conversations (lead_phone, direction, message_text) VALUES ($1, $2, $3)',
[leadPhone, 'inbound', inboundMessage]
);
// 2. Fetch conversation history
const history = await db.query(
'SELECT direction, message_text FROM conversations WHERE lead_phone = $1 ORDER BY created_at ASC',
[leadPhone]
);
// 3. Build the messages array
const messages = [
{ role: 'system', content: SYSTEM_PROMPT },
...history.rows.map(msg => ({
role: msg.direction === 'inbound' ? 'user' : 'assistant',
content: msg.message_text
}))
];
// 4. Call the AI
const response = await openai.chat.completions.create({
model: 'gpt-4o',
messages: messages,
max_tokens: 200,
temperature: 0.7
});
const aiMessage = response.choices[0].message.content;
// 5. Store the outbound message
await db.query(
'INSERT INTO conversations (lead_phone, direction, message_text) VALUES ($1, $2, $3)',
[leadPhone, 'outbound', aiMessage]
);
// 6. Send via Twilio
await sendSMS(leadPhone, aiMessage);
return aiMessage;
}
This is the core loop. Every inbound message triggers a database read, an AI call, a database write, and an SMS send. Total latency is typically 2-4 seconds.
If you want to build a similar system but using voice instead of SMS, we've written about voice AI assistants and how they pair with text-based systems for full coverage.
The conversation flow from first contact to booked call
The conversation flow from first contact to booked call
Here's what a real conversation looks like with this system. The lead filled out a form on ABC Plumbing's website asking about a leaking water heater.
[AI - 0:47 after form submit]
Hey Mike, this is Sarah from ABC Plumbing! I saw your request
about a water heater issue. Can you tell me a bit more about
what's going on?
[Lead - 3 min later]
Yeah the water heater in my garage is leaking from the bottom.
It's pooling on the floor
[AI - 4 seconds later]
That's definitely something we can help with. Is the leak
steady or does it come and go? And do you still have hot water
or has it gone out completely?
[Lead - 1 min later]
It's steady and getting worse. Still have hot water but I'm
worried it's going to go out
[AI - 3 seconds later]
Got it. Sounds like it could be the drain valve or the tank
itself. How old is the unit, roughly? And what's your zip code
so I can confirm we service your area?
[Lead - 2 min later]
Maybe 10 years? I'm in 78745
[AI - 4 seconds later]
78745 is right in our service area. A 10-year-old unit with a
steady leak usually means it's time to talk replacement options.
We do free estimates for water heater work. Want to hop on a
quick 10-min call with our team? I have openings tomorrow at
10am or 2pm.
[Lead - 5 min later]
2pm works
[AI - 3 seconds later]
Perfect, you're booked for 2pm tomorrow. You'll get a call
from our office number. Talk soon Mike!
That entire exchange happened without a human touching it. The AI asked qualification questions naturally, confirmed the service area, identified urgency, and booked the call. Total elapsed time: about 12 minutes. Compare that to most businesses where the lead would still be sitting in an unread inbox.
This kind of AI-driven lead qualification is where the real value sits. The AI handles the 80% of conversations that follow a predictable pattern, and humans handle the 20% that need judgment.
Handling multi-turn conversations and edge cases
Handling multi-turn conversations and edge cases
Real conversations are messy. Leads go silent for hours, change topics mid-thread, send one-word answers, or ask questions the AI can't answer. Your system needs to handle all of these.
Threading by phone number. Every conversation is keyed to the lead's phone number. When a message comes in, your server looks up the existing thread, loads the history, and continues the conversation. If there's no existing thread, it's a new lead and you start fresh.
Stale conversation handling. If a lead texts back after 48 hours of silence, the AI should acknowledge the gap. Add logic that checks the timestamp of the last message. If it's been more than 24 hours, prepend a note to the system prompt: "The lead has been inactive for [X] hours. Acknowledge the gap naturally and re-engage." This prevents the AI from picking up mid-sentence as if no time has passed.
Human handoff triggers. Some messages should immediately route to a human. Build a classifier (or just use the AI itself) to detect: complaints, legal questions, pricing pressure beyond what the system can handle, or explicit requests to talk to a person. When triggered, the system sends a notification to your team and pauses automated responses.
Opt-out detection. If a lead texts "stop," "unsubscribe," "don't text me," or anything similar, your system must immediately stop all messaging. This isn't optional. More on compliance later.
Short/unclear responses. Leads often reply with "ok" or "sure" or "yeah." Your system prompt should instruct the AI to ask a clarifying follow-up rather than making assumptions about what they agreed to.
These patterns show up in every AI SMS system. We cover more of the conversational design side in our complete guide to conversational AI chatbots, which applies to SMS just as much as web chat.
Automated follow-up sequences
Automated follow-up sequences
The first response is the most important, but the follow-up sequence is what actually converts leads who don't book on the first exchange. Most leads need 2-5 touchpoints before they commit.
Build a follow-up scheduler that triggers when a conversation goes cold. Here's the cadence that works:
4 hours after last message (if no response to a question): A gentle nudge. "Hey Mike, just circling back on the water heater. Want me to set up that call, or do you need more time?"
24 hours after last message: A value-add message. "Quick heads up Mike, water heater leaks that start at the base tend to get worse pretty fast. Happy to get someone out this week if you want to get ahead of it."
72 hours after last message: Final attempt with a different angle. "Hey Mike, just wanted to check in one more time about the water heater. If the timing isn't right, no worries at all. We're here whenever you need us."
After three follow-ups with no response, the system stops and tags the lead as "cold" in your CRM. No lead should receive more than 3-4 follow-up messages total. More than that and you're spamming.
The timing and content of these follow-ups can be AI-generated too. Pass the conversation history to the AI along with instructions like "Generate a follow-up message for a lead who hasn't responded in 24 hours. Be helpful, not pushy."
We've written a full breakdown of AI follow-up workflows for small service teams that covers the 14-day cadence across SMS, email, and call tasks.
CRM integration and deal tracking
CRM integration and deal tracking
An AI SMS assistant that doesn't log to your CRM is just generating conversations in a void. Every message, qualification answer, and status change needs to flow into your CRM so your sales team has full context.
Here's what to sync:
Contact creation. When a new phone number texts in (or a form submission triggers the first outbound), create a contact in your CRM with the phone number, name (from the form), and source.
Conversation log. Append every SMS (both directions) to the contact's activity timeline. Most CRMs have a notes or activity API. GoHighLevel's API makes this straightforward with their conversations endpoint.
Deal stage updates. Map conversation milestones to pipeline stages. "New Lead" when the first message sends. "Engaged" when the lead replies. "Qualified" when the AI has collected the key info. "Call Booked" when they pick a time. "No Response" after follow-ups exhaust.
Tagging. Auto-tag leads based on what the AI learns. Service type, urgency level, location, budget range. These tags let your sales team filter and prioritize.
Human handoff alerts. When the AI determines a lead needs a human, push a notification (Slack, email, CRM task) with a summary: "Mike at 512-555-1234 has a leaking 10-year-old water heater in 78745. Wants a call tomorrow at 2pm. Conversation attached."
If you're running GoHighLevel, the CRM integration is native and you can trigger workflows based on tags or pipeline stage changes. For other CRMs, you'll build webhook integrations or use their REST APIs. We've written about connecting AI systems to your CRM in detail.
Our full AI systems automation service covers this end-to-end if you'd rather not build the integrations yourself.
TCPA compliance and opt-out handling
TCPA compliance and opt-out handling
Sending automated text messages in the US falls under the Telephone Consumer Protection Act (TCPA). Getting this wrong means lawsuits. Individual TCPA violations carry $500-$1,500 per message in statutory damages. Class actions have resulted in settlements north of $100 million.
Here's what you need:
Prior express written consent. Before your AI texts anyone, the lead must have given written consent to receive text messages from your business. A website form with a clear disclosure and checkbox works. The disclosure should say something like: "By submitting this form, you agree to receive text messages from [Business Name]. Message and data rates may apply. Reply STOP to opt out."
Opt-out handling. Your system must immediately honor "STOP," "UNSUBSCRIBE," "CANCEL," "END," and "QUIT." Twilio handles some of this at the carrier level, but you should also build detection into your application. When an opt-out is detected, stop all automated messages, update the CRM, and send a confirmation: "You've been unsubscribed. You won't receive any more texts from us."
Time-of-day restrictions. Don't send texts before 8 AM or after 9 PM in the recipient's local time zone. Build this into your follow-up scheduler.
Message frequency limits. There's no specific legal cap, but sending more than a few messages per week to someone who hasn't engaged will get your number flagged by carriers and could trigger TCPA complaints. Keep follow-ups to the 3-4 message maximum described above.
Record keeping. Log consent timestamps, opt-out timestamps, and every message sent. If you're ever audited or sued, you need to prove the lead opted in and you respected their opt-out.
If you're running missed call text-back automation, the same compliance rules apply. The triggering event is different (missed call vs. form fill), but TCPA doesn't care.
Cost breakdown per conversation
Cost breakdown per conversation
One of the best things about this system is how cheap it is to operate. Here's the math:
Twilio costs:
- Phone number: $1.15/month
- Outbound SMS: $0.0079 per segment (160 characters)
- Inbound SMS: $0.0075 per segment
- A2P 10DLC registration: $15 one-time + $0.10/month
A typical qualification conversation runs 8-12 messages total (both directions). At roughly $0.008 per message, that's about $0.06-$0.10 in Twilio costs per conversation.
AI API costs:
- GPT-4o: ~$2.50 per million input tokens, $10 per million output tokens
- Claude Sonnet: ~$3 per million input tokens, $15 per million output tokens
A typical SMS conversation uses about 1,000-2,000 input tokens (system prompt + history) and 50-100 output tokens per response. With 6 AI-generated responses per conversation, you're looking at about $0.02-$0.05 per conversation in API costs.
Total cost per qualified lead conversation: roughly $0.08-$0.15.
Compare that to what you'd pay a human sales rep to handle those same conversations. Even at minimum wage, a rep handling 50 conversations a day costs far more than the $4-$8 the AI would cost for the same volume. And the AI responds in 3 seconds at 2 AM on a Saturday.
Server costs are negligible. A basic VPS or serverless setup handles this workload easily. If you're on AWS Lambda or Vercel functions, you're probably within free tier for moderate volume.
The cost efficiency is why AI revenue systems make sense even for small businesses doing 20-30 leads per month. The system pays for itself if it converts even one additional lead.
How to get started
How to get started
You have two paths. Build it yourself using the architecture and code patterns above, or have someone build it for you.
If you're building it yourself, start small. Get Twilio set up, write a basic system prompt, and run the AI layer against your first 10 leads manually before automating the full flow. Watch the conversations, adjust the prompt, and add edge case handling as you discover real patterns.
If you want it built and managed, our AI agent development service covers this exact system. We handle the Twilio setup, AI prompt engineering, CRM integration, compliance, and ongoing optimization. You can see how our process works or book a call to discuss your setup.
Either way, the gap between businesses that respond in seconds and businesses that respond in hours is only going to widen. The tech is available now, the cost is minimal, and every day without it is leads you're losing to whoever texts them back first.
Frequently asked questions
Frequently asked questions
How long does it take to build an AI SMS sales assistant? If you have development experience and existing infrastructure, a basic version takes 2-4 days. A production-ready system with CRM integration, compliance handling, follow-up sequences, and edge case management takes 2-4 weeks. Having it built by a team that's done it before typically takes 1-2 weeks.
Will leads know they're texting with an AI? Most leads don't notice, especially if your system prompt is well-written and the messages are short and conversational. Some businesses choose to disclose it upfront. Whether you disclose or not is a business decision, but the AI should never lie if asked directly. Instruct it to say something like "I'm an AI assistant for [Business Name]. Want me to connect you with someone on the team?"
What happens if the AI says something wrong? It can happen, which is why human handoff triggers are important. Keep the AI focused on qualification and booking rather than giving technical advice, pricing, or guarantees. The narrower the scope, the fewer mistakes. Review conversation logs weekly during the first month and adjust the system prompt based on what you find.
Can this work for businesses outside the US? Yes. Twilio operates in over 100 countries. The AI layer is location-agnostic. Compliance rules differ by country though. Canada has CASL, the UK has PECR, and the EU has GDPR. Research your local regulations before sending automated messages.
Should I use GPT-4o or Claude for the AI layer? Both work well for SMS conversations. GPT-4o is slightly cheaper and faster for short-form responses. Claude tends to follow complex system prompts more reliably and is less likely to break character. Test both with your specific prompt and pick the one that produces better conversations for your use case.
How do I prevent the AI from getting into weird conversations? Constrain it in the system prompt. Tell it explicitly what topics to avoid, what to do when asked off-topic questions, and when to hand off to a human. Add a catch-all instruction like: "If the conversation goes off-topic, redirect politely back to their service needs. If they persist, offer to connect them with a team member."
What's the maximum number of leads this can handle simultaneously? The system scales linearly. Each conversation is independent. Twilio can handle thousands of concurrent messages, and AI APIs process requests in parallel. The bottleneck is usually your database, and even a modest PostgreSQL instance handles thousands of concurrent conversation threads without breaking a sweat.
Do I need a separate phone number for each service or location? Not necessarily, but it helps with tracking and local presence. If you serve multiple cities, having a local number for each area code improves answer rates. For multiple services, one number with smart routing based on the lead's inquiry works fine.
Related Articles
Missed-Call Text Back Automation That Books More Leads
Missed-call text back detects unanswered calls and instantly sends an SMS with a booking link. Here is how it works for service businesses, what the messages should say, and what it costs.
AI WhatsApp Automation for Business: Lead Capture, Replies, and Booking
How to set up AI-powered WhatsApp automation for your business — covering the WhatsApp Business API, AI-driven lead capture and qualification, appointment booking, and compliance.
How to Build an AI Phone Answering System for Your Business
How to build an AI phone answering system that answers calls, captures caller details, and sends structured data to your CRM — comparing Twilio, Vapi, and Retell AI.
Need Help Implementing This?
Our team at Luminous Digital Visions specializes in SEO, web development, and digital marketing. Let us help you achieve your business goals.
Get Free Consultation