Systems Library / Customer Service / How to Build an AI Ticket Classification System
Customer Service ticket management

How to Build an AI Ticket Classification System

Classify and route support tickets to the right team automatically using AI.

Jay Banlasan

Jay Banlasan

The AI Systems Guy

An ai support ticket classification and routing system eliminates the manual triage step that slows down every support queue. I build these for teams processing 50+ tickets daily where misrouted tickets add hours of delay. The AI reads the ticket, classifies it by type, and routes it to the right team instantly.

The accuracy gets better over time as you feed corrections back into the system.

What You Need Before Starting

Step 1: Define Your Classification Categories

Map every ticket type to a team:

CATEGORIES = {
    "billing": {"team": "finance", "priority": "medium", "sla_hours": 24},
    "technical": {"team": "engineering", "priority": "high", "sla_hours": 4},
    "shipping": {"team": "logistics", "priority": "medium", "sla_hours": 12},
    "returns": {"team": "support", "priority": "medium", "sla_hours": 24},
    "account_access": {"team": "security", "priority": "high", "sla_hours": 2},
    "feature_request": {"team": "product", "priority": "low", "sla_hours": 72},
    "general": {"team": "support", "priority": "low", "sla_hours": 48},
}

Step 2: Build the Classifier

import anthropic
import json

client = anthropic.Anthropic()

def classify_ticket(subject, body):
    category_list = ", ".join(CATEGORIES.keys())

    response = client.messages.create(
        model="claude-sonnet-4-20250514",
        max_tokens=100,
        messages=[{
            "role": "user",
            "content": f"""Classify this support ticket into ONE of these categories: {category_list}

Subject: {subject}
Body: {body}

Respond with ONLY a JSON object: {{"category": "category_name", "confidence": 0.95}}"""
        }]
    )

    result = json.loads(response.content[0].text)
    return result

Step 3: Route Based on Classification

def route_ticket(ticket_id, subject, body):
    classification = classify_ticket(subject, body)
    category = classification["category"]
    confidence = classification["confidence"]

    routing = CATEGORIES.get(category, CATEGORIES["general"])

    if confidence < 0.7:
        routing["team"] = "triage"
        routing["note"] = f"Low confidence ({confidence}). Manual review needed."

    assign_ticket(ticket_id, routing["team"], routing["priority"], routing["sla_hours"])
    log_classification(ticket_id, category, confidence)

    return routing

Step 4: Process Incoming Tickets via Webhook

from flask import Flask, request

app = Flask(__name__)

@app.route("/ticket-webhook", methods=["POST"])
def new_ticket():
    data = request.json
    ticket_id = data["ticket_id"]
    subject = data["subject"]
    body = data["body"]

    routing = route_ticket(ticket_id, subject, body)

    return {"ticket_id": ticket_id, "routed_to": routing["team"], "category": routing}

Step 5: Build a Feedback Loop

When agents reclassify tickets, log the correction to improve accuracy:

def log_correction(ticket_id, ai_category, correct_category):
    conn = sqlite3.connect("classifications.db")
    conn.execute("""
        INSERT INTO corrections (ticket_id, predicted, actual, corrected_at)
        VALUES (?, ?, ?, datetime('now'))
    """, (ticket_id, ai_category, correct_category))
    conn.commit()

def get_accuracy_report():
    conn = sqlite3.connect("classifications.db")
    total = conn.execute("SELECT COUNT(*) FROM classifications").fetchone()[0]
    correct = conn.execute(
        "SELECT COUNT(*) FROM classifications c LEFT JOIN corrections co ON c.ticket_id = co.ticket_id WHERE co.ticket_id IS NULL"
    ).fetchone()[0]
    return {"total": total, "correct": correct, "accuracy": correct / total if total else 0}

What to Build Next

Add sub-classification. Once a ticket hits the billing team, further classify it into "charge dispute," "refund request," or "invoice question" so agents can prioritize within their queue.

Related Reading

Want this system built for your business?

Get a free assessment. We will map every system your business needs and show you the ROI.

Get Your Free Assessment

Related Systems