← All case studies

// engineering

Designing an AI Layer That Knows When to Stop Talking

OpenAI APIInstagram APIGoHighLevelPydantic

Overview

A fitness coach generating leads through Instagram, comments, story replies, and DMs, needed a way to turn those conversations into usable CRM data automatically. On busier posting days, that meant 200 to 300 new conversations arriving in a single day. The challenge wasn't connecting Instagram to a CRM, that part is straightforward. The challenge was that at that volume, with people describing what they want in completely inconsistent ways, neither of the two obvious approaches, a rigid rule-based bot or a fully open AI model, actually held up.

The Problem

At 200 to 300 new conversations a day, manual triage wasn't realistic, and a simple keyword or rule-based bot was too rigid to handle how people naturally write on Instagram, missing intent whenever someone phrased a request slightly differently than expected. A rule tuned for "I'm interested in the workshop" simply didn't fire for something like "This is fire! 🔥 I gotta know more about your workshops, what's the deal?", even though both messages mean the same thing. But removing the rules entirely and letting an AI model handle the conversation freely created a different problem: it would make assumptions, ask questions that didn't need asking, and extract inconsistent information from one conversation to the next. Neither extreme produced CRM data reliable enough to act on, at a volume too high to fall back on manually checking every conversation.

Investigation

Testing the fully open approach first showed the failure mode clearly: without constraints, the model behaved less like a focused intake process and more like an open-ended chat. It would sometimes infer details that were never actually stated, ask a lead a clarifying question a human wouldn't have needed to ask, or capture the same kind of information in a different shape each time, which made downstream CRM automation unreliable.

The rule-based approach failed in the opposite direction. It handled expected phrasings fine, "I'm interested in the workshop" matched cleanly, but broke the moment someone described their interest in a less common way, which, on a platform like Instagram, is most of the time. A message like "This is fire! 🔥 I gotta know more about your workshops, what's the deal?" carries exactly the same intent, but no reasonable set of keyword rules was going to catch it, and at 200 to 300 messages a day, that gap wasn't a rare edge case, it was routine.

This pointed to the real design problem: the AI needed enough freedom to understand natural language, but tight enough constraints that its output was as predictable as a rule-based system's, specifically around what it was allowed to extract, when it was allowed to ask a follow-up, and when it needed to stop entirely and hand off to a person.

intake_flow.txt
Instagram Comment / Story Reply / DM
        |
        v
Step 1: Extract Intent
  (identify what the lead wants, nothing more)
        |
        v
Step 2: Validate & Structure
  (check extracted fields before writing anything)
        |
   +----+----+
   |         |
confident   low confidence
   |         |
   v         v
CRM Record   Force Handoff
   |         |
   v         v
Step 3: Summarise for VA
        |
        v
VA takes over -> automation stops responding

The Solution

The prompt was scoped narrowly to a single job: identify the lead's intent, extract only the specific fields that mattered, validate what it could before passing anything to the CRM, and recognise the point at which a conversation needed a human rather than continuing to respond itself.

A second, smaller AI step ran alongside this: generating a short summary of the conversation so far, so that once a lead was handed to a VA, they had immediate context rather than needing to read the full comment or DM thread from the start.

Once handoff happened, the automation stopped responding entirely, specifically to prevent the AI and the VA both replying to the same lead at the same time.

The extraction step returns a fixed shape rather than free text, so anything malformed or incomplete is caught before it ever reaches the CRM.

lead_intake.pyfrom pydantic import BaseModel, ValidationError
from typing import Optional

class LeadIntake(BaseModel):
    intent: str
    contact_method: Optional[str] = None
    interest_area: Optional[str] = None
    ready_for_handoff: bool
    confidence: str  # "high" | "low"

def process_ai_response(raw_response: dict) -> Optional[LeadIntake]:
    """Validate structured extraction before it reaches the CRM."""
    try:
        lead = LeadIntake(**raw_response)
    except ValidationError:
        # Malformed extraction, route to a human rather than guess
        return None

    if lead.confidence == "low":
        lead.ready_for_handoff = True  # force handoff on uncertainty

    return lead

Engineering Trade-offs

The core decision was treating "extract and validate" and "decide when to hand off" as two separate, tightly scoped responsibilities rather than one open-ended conversational agent. Giving a single AI step full ownership of the whole conversation would have been simpler to build, but it's exactly what caused the inconsistent extraction and unnecessary questions in the first place. Splitting intent extraction, validation, summarisation, and handoff into distinct, narrow steps meant each one could be constrained and checked independently, rather than trusting one large, loosely bounded model call to get everything right at once.

Challenges

A few specific failure modes needed deliberate handling rather than being left to chance:

Outcome

Reflection

The instinct when AI first enters a workflow like this is often to give it as much responsibility as possible, since it can technically handle the whole conversation. The more reliable result came from doing the opposite: giving it a narrow, well-defined job, extract this, validate that, know when to stop, rather than one broad one. The interesting engineering problem here wasn't really about AI capability, it was about scoping AI's role tightly enough that its output became as predictable and trustworthy as a traditional rule-based system, while still keeping the flexibility that made it worth using in the first place.