Email categorization is one of the safer, more accurate applications of an LLM in a business workflow, precisely because it's a narrow decision (pick one label from a fixed set) rather than open-ended generation. The reliability comes from constraining the model's output — giving it a specific, closed list of categories rather than letting it invent labels freely.
Building the categorization pipeline
1. Fetch new emails via the mailbox provider's API. Gmail and Outlook both expose APIs to read new messages and apply labels/folders programmatically — this is the integration point, not a scraping or forwarding hack.
2. Classify with a structured-output call constrained to your exact categories. Pass the email subject/body and ask the model to return one value from an explicit enum (e.g., "billing", "technical_support", "sales_inquiry", "spam", "needs_review") — constraining the schema prevents invented categories.
3. Apply the resulting label back through the same API, and route low-confidence cases to review. Include a "needs_review" fallback category so ambiguous emails get a human look instead of a forced, possibly-wrong classification.
# pip install anthropic
import anthropic
client = anthropic.Anthropic()
CATEGORIES = ["billing", "technical_support", "sales_inquiry", "spam", "needs_review"]
def categorize_email(subject: str, body: str) -> str:
response = client.messages.create(
model="claude-sonnet-5",
max_tokens=20,
system=f"Classify the email into exactly one of: {', '.join(CATEGORIES)}. "
f"Reply with only the category name.",
messages=[{"role": "user", "content": f"Subject: {subject}\n\n{body}"}]
)
category = response.content[0].text.strip()
return category if category in CATEGORIES else "needs_review"
| Approach | Reliability |
|---|---|
| Free-text classification, no constraint on output | Lower — model may invent labels that don't match your system |
| Structured output constrained to a fixed category list | Higher — output is guaranteed to be one of your actual categories, or falls back to review |
The reason this use case works well while others (like autonomous email replies) need more caution is the nature of the task: classification into a known set of options is exactly the kind of narrow, bounded decision LLMs handle reliably, whereas open-ended content generation carries more risk of subtle errors going unnoticed.
Practical Challenge
Build the categorize_email function above with your own category list, run it against 20 real (or sample) emails, and manually check the accuracy — note how many landed in "needs_review."
Concept Check
Sources & Further Reading
- Gmail API Documentation — reference for reading messages and applying labels programmatically.
- Anthropic: Prompt Engineering Overview — guidance on constraining model outputs to a fixed set of options.
AI