Skip to content

Chatbot System Design

Design production-ready conversational AI systems with multi-turn context, personas, and human handoff


Learning Objectives

By the end of this module, you will be able to:

  • Design end-to-end chatbot architectures for customer support, sales, and general assistance
  • Implement multi-turn conversation management with context windows and summarization
  • Create persona systems that maintain consistent chatbot personality and behavior
  • Build human handoff mechanisms for seamless escalation to live agents
  • Address safety concerns specific to conversational AI systems

Chatbot Architecture Overview

High-Level System Architecture

Core Components

ComponentPurposeKey Considerations
Channel GatewayUnified entry point for all channelsProtocol translation, rate limiting
Session ManagerTrack user sessions across interactionsTTL, persistence, multi-device
Context ManagerMaintain conversation stateWindow size, summarization
Persona EngineConsistent chatbot personalityTone, boundaries, brand voice
Dialog ManagerOrchestrate conversation flowState machine, error recovery
Action ExecutorExecute business actionsIdempotency, rollback

Multi-Turn Conversation Management

The Context Challenge

Context Management Strategies

StrategyDescriptionProsCons
Full HistorySend entire conversationMaximum contextToken cost, latency
Sliding WindowLast N messagesBounded costLoses early context
SummarizationSummarize older messagesEfficient, comprehensiveSummary quality varies
HybridRecent full + summarized olderBalanceImplementation complexity
Memory RetrievalRetrieve relevant past exchangesEfficient for long conversationsRetrieval quality

Hybrid Context Architecture

Context Window Sizing

Token Budget Allocation

For a 4K token context window, allocate:

  • System prompt: ~500 tokens (12.5%)
  • Summary: ~500 tokens (12.5%)
  • Recent history: ~2000 tokens (50%)
  • Current turn + response space: ~1000 tokens (25%)
Context SizeMessagesSummary StrategyUse Case
4K tokens~10-15Aggressive summarizationSimple support
8K tokens~25-30Periodic summarizationStandard chatbot
32K tokens~100-120Minimal summarizationComplex workflows
128K+ tokens~500+Full history possibleLong-running sessions

Conversation State Machine


Persona Design

Persona Architecture

Persona Template

yaml
persona:
  name: "Alex"
  role: "Customer Support Specialist"
  company: "TechCorp"

  voice:
    tone: "friendly, professional, helpful"
    style: "conversational but efficient"
    vocabulary: "avoid jargon, explain technical terms"

  boundaries:
    can_do:
      - Answer product questions
      - Process returns and exchanges
      - Check order status
      - Provide troubleshooting help
    cannot_do:
      - Discuss competitor products
      - Share internal policies
      - Make promises about future features
      - Discuss pricing negotiations

  behavior:
    greeting: "Hi there! I'm Alex from TechCorp support."
    farewell: "Thanks for chatting! Have a great day!"
    uncertainty: "I want to make sure I give you accurate info..."
    escalation: "Let me connect you with a specialist..."

Persona Consistency Techniques

TechniqueDescriptionImplementation
System PromptDefine persona in system instructionsAlways include persona details
Few-Shot ExamplesShow example conversations3-5 representative exchanges
Style GuidePost-process for consistencyRegex patterns, style classifiers
Tone AnalysisMonitor response toneSentiment analysis, custom models
A/B TestingTest persona variationsMeasure user satisfaction

Human Handoff System

Handoff Architecture

Handoff Triggers

Trigger TypeDetection MethodPriority
Explicit Request"Talk to human", "Agent please"Immediate
High FrustrationSentiment score, repeated questionsHigh
Complex IssueMulti-domain, policy questionsMedium
Safety ConcernThreat detection, urgent issuesImmediate
Bot FailureRepeated misunderstandingHigh
High-Value CustomerUser tier, purchase historyMedium

Handoff Scoring Model

python
def calculate_handoff_score(conversation):
    score = 0

    # Explicit request (immediate)
    if detect_explicit_handoff_request(conversation.last_message):
        return 100

    # Sentiment analysis (0-30 points)
    sentiment = analyze_sentiment(conversation.recent_messages)
    score += (1 - sentiment) * 30  # Lower sentiment = higher score

    # Frustration indicators (0-25 points)
    frustration = detect_frustration(conversation)
    score += frustration * 25

    # Bot confidence (0-20 points)
    confidence = conversation.last_bot_confidence
    score += (1 - confidence) * 20

    # Conversation length (0-15 points)
    turns = len(conversation.turns)
    score += min(turns / 20, 1) * 15  # Max at 20 turns

    # Repeated issues (0-10 points)
    repetitions = count_repeated_intents(conversation)
    score += min(repetitions / 3, 1) * 10

    return min(score, 100)

Warm Handoff Flow


Safety in Conversational AI

Conversational Safety Architecture

Chatbot-Specific Safety Concerns

ConcernRiskMitigation
ManipulationUser manipulating bot to give harmful infoStrict persona boundaries, detection
ImpersonationBot pretending to be humanClear bot disclosure
Over-PromiseMaking commitments bot can't keepAction boundary enforcement
Emotional ManipulationExploiting user emotionsSentiment monitoring, handoff
Data CollectionInadvertent data gatheringPII detection, minimal storage
Confidentiality BreachRevealing other users' dataStrict data isolation

Interview Q&A

Q1: How do you handle context in long conversations?

Answer: "I implement a hybrid context management strategy:

  1. Sliding window: Keep the last N turns (e.g., 10) in full detail
  2. Progressive summarization: Summarize older context periodically
  3. Key entity extraction: Maintain structured data (order numbers, user preferences)
  4. Memory retrieval: For very long conversations, retrieve relevant past exchanges

The architecture:

[System Prompt] + [Summary of turns 1-20] + [Full turns 21-30] + [Current turn]

I trigger summarization when context exceeds 70% of the budget, keeping buffer for response generation. For critical entities, I store them separately and always inject into context."

Q2: How do you ensure consistent chatbot persona?

Answer: "I implement persona consistency at multiple levels:

  1. System prompt: Detailed persona definition with name, role, voice, boundaries
  2. Few-shot examples: 3-5 representative conversations showing desired behavior
  3. Post-processing: Style classifiers to catch tone drift, regex for forbidden phrases
  4. Continuous monitoring: Track persona consistency metrics, A/B test variations

For boundary enforcement, I use a two-pass approach:

  • First pass: Generate response
  • Second pass: Validate against persona constraints (can/cannot do lists)

If validation fails, regenerate with stricter instructions or use a fallback response."

Q3: Design the human handoff system for a support chatbot.

Answer: "I design a multi-signal handoff system:

Detection: Score conversations on:

  • Explicit requests (immediate trigger)
  • Sentiment trajectory (frustration detection)
  • Bot confidence scores
  • Conversation length and repetition
  • Issue complexity classification

Routing: Once handoff triggers:

  1. Check agent availability and skills
  2. Generate conversation summary
  3. Queue with priority based on customer tier and issue urgency
  4. Notify user with expected wait time

Transition: Warm handoff process:

  1. Bot: 'Connecting you with a specialist...'
  2. Agent receives summary + full context
  3. Agent: 'Hi, I'm Sarah. I see you're having trouble with...'

Fallback: If agents unavailable:

  • Offer callback scheduling
  • Provide email option with case number
  • Estimate response time

Agent Tools: Give agents:

  • AI-suggested responses
  • Sentiment analysis
  • Quick action buttons
  • Knowledge base integration"

Q4: How would you handle a user trying to manipulate the chatbot?

Answer: "I implement defense in depth:

Detection:

  • Prompt injection patterns (ignoring instructions, roleplay attempts)
  • Jailbreak attempts (pretend games, hypothetical scenarios)
  • Social engineering (claiming to be admin, urgency tactics)

Prevention:

  • Strong system prompt with explicit boundaries
  • Input sanitization and classification
  • Output validation against allowed topics
  • Behavioral anomaly detection

Response Strategy:

  1. Soft redirect: 'I'm here to help with X, Y, Z. How can I assist?'
  2. Firm boundary: 'I can't help with that, but I can...'
  3. Handoff: For persistent attempts, offer human agent
  4. Session termination: For clearly malicious behavior

I also log manipulation attempts for analysis and model improvement, while being careful not to create a cat-and-mouse game that trains better attacks."


Trade-off Analysis

DecisionOption AOption BRecommendation
Context StrategyFull history (accurate)Summarized (efficient)Hybrid: recent full + summarized old
Persona EnforcementHard rules (safe)Soft guidance (natural)Hard for boundaries, soft for style
Handoff TimingEarly (safe)Late (cost-effective)Dynamic scoring with user preference
Channel UnificationSingle codebase (maintainable)Channel-specific (optimized)Unified core + channel adapters
State StorageClient-side (simple)Server-side (rich)Server-side with client session ID

System Design Checklist

Before finalizing your chatbot design, verify:

  • Multi-channel support architecture defined
  • Context management strategy with token budgets
  • Persona definition with boundaries and voice
  • Human handoff triggers and routing logic
  • Input/output safety guardrails
  • Session management and persistence
  • Integration with business systems (CRM, ticketing)
  • Monitoring and evaluation metrics
  • Fallback strategies for edge cases
  • Scalability plan for peak loads

PreviousNext
Design FrameworkEnterprise RAG

Sources