Sub topic 1
What classification and routing mean, and when an LLM fits
Prerequisites
This chapter assumes you have the following:
- Basic Python or similar scripting experience.
- Familiarity with calling a chat or text-completion API from code.
- A basic understanding of JSON.
- Awareness of classification metrics such as accuracy, precision, and recall.
No code runs in this chapter. Later chapters build the classifier step by step.
Classification and routing
Classification assigns a piece of text to one label from a fixed set. A support message might become billing, technical, account, or other. The label set is chosen by you, not by the model.
Routing uses that label to decide what happens next. A billing message goes to the billing team's queue. A message the system is unsure about goes to a person.
These are two separate steps:
- Classify: text in, label (and optionally a confidence) out.
- Route: label in, destination or action out.
Keeping them separate matters. You can test the classifier on its own, and you can change where labels go without touching the prompt.
When an LLM fits
An LLM is one way to build a classifier. It is not always the best one. Compare the common options:
| Approach | Strengths | Weaknesses |
|---|---|---|
| Rules (keywords, regex, metadata) | Fast, free, predictable, easy to explain | Break on paraphrase and new wording |
| Trained classifier (for example logistic regression) | Cheap per item, fast, consistent | Needs many labeled examples and retraining |
| LLM with a prompt | Works with few or no examples, handles varied wording, labels change by editing text | Costs per call, slower, can be inconsistent, can be manipulated by input text |
An LLM is a good fit when:
- The text is free-form and varied, so rules would be long and brittle.
- You have few labeled examples, or none yet.
- The label set changes often, and editing a prompt is easier than retraining.
- The decision depends on meaning, not on specific words.
Another approach is better when:
- A field already decides the answer, such as a form dropdown or a sender address. Use a rule.
- Volume is very high and latency must be a few milliseconds. Use a trained model or rules.
- A wrong label has serious consequences and no human checks it. Add review, or avoid the LLM for that step.
Many real systems combine these. Rules handle the obvious cases, and the LLM handles the rest. Chapter 9 covers that pattern.
A framing checklist
Before writing any prompt, answer these questions in writing:
- What is one item? A whole email, a single message, or a ticket with its metadata?
- What are the labels, and does every item get exactly one?
- What should happen to an item that fits no label?
- What does each label trigger downstream?
- What does a wrong label cost, and which mistakes are worse?
- How will you measure success? You need some labeled examples, even a few dozen.
Question 5 shapes later choices. Sending a billing dispute to the technical queue wastes a day. Sending a security report to the other queue could be much worse. Those two mistakes should not be treated as equal.
Common errors at this stage
- Skipping the fallback. Without an explicit "other" or "unsure" outcome, the model must force every item into a label, and wrong guesses look just like right ones.
- Mixing classification with other work. Asking the model to classify, summarize, and draft a reply in one call makes failures hard to locate.
- Using an LLM where a field would do. If the customer already chose "Billing" in a form, trust that field.
- Having no measurement plan. Without labeled examples, you can't tell whether a prompt change helped.
Exercise
A company receives emails to one address. Each email must go to sales, support, or legal. Some emails come from a web form that has a required "Department" dropdown. Which emails should an LLM classify, and what should happen to emails that fit none of the three?
Show answer
The LLM should classify the emails that did not come through the form, since those have free-form text and no reliable field. For form emails, route directly on the dropdown value with a rule. Emails that fit none of the three departments, or that the classifier is unsure about, should go to an explicit fallback such as a human-triage queue instead of being forced into a label.