← Back to DevBytes

How AI Agents Handle 90% of Customer Support Tickets

What Are AI Agents in Customer Support?

AI agents are autonomous software programs that understand, triage, and resolve customer support tickets without human intervention. Unlike simple chatbots that follow rigid scripts, modern AI agents leverage large language models (LLMs), retrieval-augmented generation (RAG), and tool-calling capabilities to handle the vast majority of routine inquiries—often up to 90% of incoming tickets.

An AI agent for support typically performs the following steps:

When implemented well, these agents can resolve common issues instantly, 24/7, in any language, while seamlessly handing off complex or sensitive cases to human agents. This tutorial will teach you how to build such an agent step by step.

Why Automating 90% of Support Tickets Matters

From a business perspective, automating the majority of support interactions delivers clear value:

For developers, hitting the 90% mark means building a system that is reliable enough to handle the long tail of everyday questions. It also means designing a graceful failure mode: the remaining 10% of tickets are often ambiguous, emotionally charged, or require human judgment. A well-architected agent knows when to step back.

Technically, automating 90% of tickets requires a combination of accurate intent detection, robust knowledge retrieval, and a well-tuned confidence threshold. It’s not about replacing humans—it’s about letting them focus on the hardest 10%.

How AI Agents Process a Support Ticket

Let’s walk through the typical pipeline. We’ll use pseudocode and a real Python snippet to illustrate intent classification, one of the most critical steps.

1. Ticket Intake and Preprocessing

The ticket arrives via email, chat, or API. The agent cleans the text: normalizes whitespace, removes signatures, and optionally translates non-English messages.

2. Intent Classification

This step assigns a predefined label like "refund_request", "technical_issue", or "account_help". Modern agents use an LLM with a well-crafted prompt. Here’s a Python example using the OpenAI API:

import openai

def classify_intent(ticket_text: str) -> dict:
    prompt = f"""
    You are a support triage assistant. Classify the customer message into exactly one intent.
    Available intents: refund_request, technical_issue, account_help, general_inquiry.

    Return a JSON object with keys: "intent" and "confidence" (0-1).

    Customer message: {ticket_text}
    """

    response = openai.ChatCompletion.create(
        model="gpt-4",
        messages=[{"role": "user", "content": prompt}],
        temperature=0.0,
        max_tokens=150
    )

    raw = response.choices[0].message.content
    # Parse the JSON string (omitting error handling for brevity)
    import json
    result = json.loads(raw)
    return result

# Example usage
ticket = "I never received my order #12345 and I want my money back."
classification = classify_intent(ticket)
# Output: {"intent": "refund_request", "confidence": 0.96}

3. Entity Extraction

Extract structured data like order IDs, product names, email addresses, and dates. This can be done in the same LLM call or a separate one. The goal is to fill in the blanks needed to actually solve the problem.

4. Knowledge Retrieval (RAG)

The agent searches your internal documentation, knowledge base articles, or past resolved tickets. A vector database (like Pinecone or pgvector) stores embeddings of articles. The query is constructed from the intent and entities.

5. Response Generation and Actions

Based on intent and retrieved knowledge, the agent either generates a direct answer or takes an action (e.g., creates a refund record via an API). The response is returned to the customer.

6. Confidence Scoring and Escalation

If confidence is below a threshold (say 0.85), the agent escalates to a human. The threshold is tunable; too low and you annoy customers with wrong answers, too high and you miss automation opportunities.

Building Your First AI Support Agent: A Step-by-Step Guide

Let’s build a complete, runnable agent that handles a support ticket from intake to resolution. We’ll create a simple Flask API that accepts a ticket, processes it, and returns either a resolved response or an escalation message.

Setup and Dependencies

Install the required packages:

pip install openai flask

Set your OpenAI API key as an environment variable OPENAI_API_KEY.

Full Agent Code (app.py)

The agent will:

import os
import json
import openai
from flask import Flask, request, jsonify

openai.api_key = os.getenv("OPENAI_API_KEY")

app = Flask(__name__)

# -------------------------------------------------------------------
# 1. Intent classification + entity extraction (single call for speed)
# -------------------------------------------------------------------
def analyze_ticket(ticket_text: str) -> dict:
    prompt = f"""
You are a support triage agent. Analyze the customer message and return a JSON object with the following fields:
- "intent": one of ["refund_request", "technical_issue", "account_help", "general_inquiry"]
- "confidence": a number between 0 and 1 indicating how certain you are
- "entities": an object containing any relevant details (order_id, product, error_code, email, etc.)

Customer message: {ticket_text}
"""
    resp = openai.ChatCompletion.create(
        model="gpt-4",
        messages=[{"role": "user", "content": prompt}],
        temperature=0.0,
        max_tokens=200,
    )
    raw = resp.choices[0].message.content
    # Clean up possible markdown fences
    if raw.startswith(""):
        raw = raw.split("\n", 1)[1]
        raw = raw.rsplit("", 1)[0]
    return json.loads(raw)

# -------------------------------------------------------------------
# 2. Mock knowledge base – replace with actual vector search
# -------------------------------------------------------------------
KNOWLEDGE_BASE = {
    "refund_request": "Our refund policy allows returns within 30 days. Please visit your orders page to initiate a return. If the item was never delivered, we will process a full refund immediately.",
    "technical_issue": "For login problems, try resetting your password via the 'Forgot Password' link. If error persists, clear your browser cache or contact our IT team.",
    "account_help": "You can update your account details under 'My Profile'. For email changes, a verification link will be sent to the new address.",
    "general_inquiry": "Thank you for contacting us! How can I assist you further today?"
}

# -------------------------------------------------------------------
# 3. Generate final response based on intent and entities
# -------------------------------------------------------------------
def generate_response(intent: str, entities: dict, knowledge: str) -> str:
    entity_text = json.dumps(entities) if entities else "none"
    prompt = f"""
You are a helpful support assistant. Using the knowledge below and the provided entities, craft a concise, friendly reply that directly addresses the customer's issue.
Knowledge: {knowledge}
Extracted entities: {entity_text}

Write only the final response message.
"""
    resp = openai.ChatCompletion.create(
        model="gpt-4",
        messages=[{"role": "user", "content": prompt}],
        temperature=0.2,
        max_tokens=250,
    )
    return resp.choices[0].message.content.strip()

# -------------------------------------------------------------------
# 4. Main ticket handler
# -------------------------------------------------------------------
def handle_ticket(ticket_text: str, confidence_threshold=0.85):
    analysis = analyze_ticket(ticket_text)
    intent = analysis["intent"]
    confidence = analysis["confidence"]
    entities = analysis.get("entities", {})

    if confidence < confidence_threshold:
        return {
            "resolved": False,
            "escalation_reason": "Low confidence, routing to human agent.",
            "intent": intent,
            "confidence": confidence
        }

    # Retrieve knowledge for the intent (fallback to general)
    knowledge = KNOWLEDGE_BASE.get(intent, KNOWLEDGE_BASE["general_inquiry"])
    reply = generate_response(intent, entities, knowledge)

    return {
        "resolved": True,
        "reply": reply,
        "intent": intent,
        "confidence": confidence
    }

# -------------------------------------------------------------------
# 5. Flask endpoint
# -------------------------------------------------------------------
@app.route("/ticket", methods=["POST"])
def ticket_endpoint():
    data = request.get_json()
    ticket_text = data.get("text", "")
    if not ticket_text:
        return jsonify({"error": "No ticket text provided"}), 400

    result = handle_ticket(ticket_text)
    return jsonify(result)

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5000, debug=True)

Testing the Agent

Start the server and send a sample ticket:

curl -X POST http://localhost:5000/ticket \
  -H "Content-Type: application/json" \
  -d '{"text": "I ordered item #ORD-9876 but it arrived broken. I want a replacement."}'

Expected response (resolved):

{
  "resolved": true,
  "reply": "I'm sorry to hear that your item arrived damaged! Since it's within our return window, you can initiate a return from your orders page. If you prefer, we can also arrange a replacement—just let us know!",
  "intent": "refund_request",
  "confidence": 0.92
}

If the ticket is ambiguous or emotional, confidence may drop below the threshold, and the agent will escalate.

Best Practices for High-Performing AI Agents

Hitting 90% automation requires more than just a working prototype. Here are the key practices observed in production systems:

Conclusion

AI agents capable of resolving 90% of customer support tickets are not science fiction—they are a practical engineering reality. By combining intent classification, entity extraction, knowledge retrieval, and a well-tuned confidence gate, you can build a system that handles routine issues instantly and escalates only the truly difficult ones.

The tutorial above gave you a complete, runnable blueprint. From that foundation, you can integrate real knowledge bases, connect to order management APIs, and add conversation memory. The key is to treat the agent not as a monolithic black box, but as a pipeline where each stage can be monitored, tuned, and improved independently.

Start building, measure your automation rate, and keep refining—your customers (and your support team) will thank you.

— Ad —

Google AdSense will appear here after approval

← Back to all articles