perkun.eu Services Portfolio Blog About Contact PL
← Blog

1/22/2024

Claude API in practice — building a support ticket classifier

TL;DR: Claude API over HTTP — messages endpoint, claude-3-haiku model (cheapest), classifying support tickets in < 1s. Python + requests, zero frameworks.

If you’ve ever managed a support team, you know what mornings look like: an inbox full of tickets, each requiring reading and routing to the right queue. Bug, feature request, documentation question, billing issue — everything looks similar in a subject line. Claude can classify them faster than an employee opens the first ticket.

The use case

A company receives 200 support tickets per day via email. Manual classification into four categories — bug, feature, question, billing — takes an experienced employee about 30 minutes per day. That seems like a small number, but add it up over a year: over 180 hours of human labor on a task that requires no creativity or situational judgment.

After deploying the Claude API, classification accuracy was 94% (measured over a week with manual verification of a 10% sample). Errors occurred mainly on ambiguous tickets that a human would also classify differently. Response time: under one second per ticket.

Code: classifying a single ticket

You don’t need any framework or SDK. Plain HTTP:

import requests
import json

ANTHROPIC_API_KEY = "sk-ant-..."

def classify_ticket(ticket_text: str) -> str:
    response = requests.post(
        "https://api.anthropic.com/v1/messages",
        headers={
            "x-api-key": ANTHROPIC_API_KEY,
            "anthropic-version": "2023-06-01",
            "content-type": "application/json",
        },
        json={
            "model": "claude-3-haiku-20240307",
            "max_tokens": 10,
            "system": (
                "You are a support ticket classifier. "
                "Respond with exactly one word: bug, feature, question, or billing. "
                "Nothing else."
            ),
            "messages": [
                {"role": "user", "content": ticket_text}
            ],
        },
        timeout=5,
    )
    response.raise_for_status()
    return response.json()["content"][0]["text"].strip().lower()

# Example
ticket = "Hey, I was charged twice for my subscription this month"
category = classify_ticket(ticket)
print(category)  # billing

Key details: the claude-3-haiku-20240307 model is the cheapest in the Claude 3 family, max_tokens: 10 limits the response to one word (and cuts costs), and the system prompt with the explicit instruction “respond with exactly one word” keeps the model from over-explaining.

Email integration

Python’s imaplib library lets you fetch unread emails, extract the subject and body, pass them to Claude, and save the result:

import imaplib
import email

def fetch_and_classify(imap_host, username, password):
    mail = imaplib.IMAP4_SSL(imap_host)
    mail.login(username, password)
    mail.select("INBOX")

    _, message_ids = mail.search(None, "UNSEEN")
    for msg_id in message_ids[0].split():
        _, msg_data = mail.fetch(msg_id, "(RFC822)")
        msg = email.message_from_bytes(msg_data[0][1])

        subject = msg.get("Subject", "")
        body = ""
        if msg.is_multipart():
            for part in msg.walk():
                if part.get_content_type() == "text/plain":
                    body = part.get_payload(decode=True).decode()
                    break
        else:
            body = msg.get_payload(decode=True).decode()

        ticket_text = f"Subject: {subject}\n\n{body[:500]}"
        category = classify_ticket(ticket_text)

        # Save to DB: INSERT INTO tickets (msg_id, category) VALUES (...)
        save_to_db(msg_id, subject, category)

In practice, it’s worth running this script as a cron job every 5 minutes or as a long-running process with IMAP IDLE.

Costs

claude-3-haiku costs $0.25 per million input tokens. One ticket is the system prompt (~50 tokens) plus the ticket body (~150 tokens) = ~200 tokens. 200 tickets/day × 200 tokens = 40,000 tokens = $0.01/day. Annually: $3.65.

Compare that to 30 minutes of human work per day at even a modest hourly rate — the ROI is immediate from day one.

What’s next

A natural next step is connecting this system with a Filament PHP panel so a support manager can see classified tickets in a clean table with category filtering. Another direction is using n8n as the orchestrator — instead of a cron script, a visual workflow: IMAP trigger → Claude node → save to DB → Slack notification. We cover n8n in the next article.

Summary

Claude API with claude-3-haiku is one of the cheapest ways to automate text classification. No frameworks, no SDKs — just HTTP POST and JSON. Near-zero operating costs, 94% accuracy, sub-second response time. For simple classification tasks, you don’t need anything more.