How do you classify AI agent messages by intent? Store a few example messages for each intent (billing, support, sales, cancel), embed them, and label each new message with the intent of its nearest examples. The steps below do that in plain SQL, with no model training: adding an intent means inserting rows, and a confidence gate sends unclear messages back to the user as a question instead of a guess.
Objective
Before an agent can act, it has to know what the user wants: is this a billing question, a bug report, a sales lead, or a cancellation? Brittle keyword rules break on paraphrase, and spinning up a classifier service is overkill. Here you'll classify intent by meaning: store a few labeled examples per intent, embed them, and match an incoming message to the nearest examples — a k-NN intent classifier that needs no training job and adds new intents by inserting rows. The same classifier works from any framework or a voice agent — see Use it from your agent at the end.
Step 1: Create the labeled-examples store
Each row is an example utterance tagged with its intent label and embedded for matching.
CREATE TABLE IF NOT EXISTS recipe_intent_examples (
example_id INTEGER PRIMARY KEY,
intent TEXT, -- the label this example teaches
utterance TEXT, -- a sample way users phrase that intent
embedding VECTOR(384)
);
Step 2: Seed a few examples per intent
A handful of phrasings per intent is enough — the embedding model generalizes to unseen wording.
INSERT INTO recipe_intent_examples (example_id, intent, utterance) VALUES
(1,'billing','Why was I charged twice this month?'),
(2,'billing','I need a copy of my last invoice.'),
(3,'billing','How do I update my credit card?'),
(4,'tech_support','The app crashes when I open the dashboard.'),
(5,'tech_support','I keep getting a 500 error on upload.'),
(6,'tech_support','The export button does nothing.'),
(7,'sales','Do you offer a plan for large teams?'),
(8,'sales','Can I get a demo before buying?'),
(9,'sales','What''s included in the enterprise tier?'),
(10,'cancel','I want to close my account.'),
(11,'cancel','How do I stop my subscription?'),
(12,'cancel','Please cancel my plan and refund me.');
Step 3: Embed the examples
The embedding model runs in-database; this is the labeled index the classifier searches.
UPDATE recipe_intent_examples SET embedding = EMBED(utterance);
Step 4: Classify a new message by nearest examples
Find the closest labeled examples to the incoming message — their intent is the prediction.
SELECT intent, utterance,
COSINE_SIMILARITY(embedding, EMBED('my card got declined, can you help me pay?')) AS similarity
FROM recipe_intent_examples
ORDER BY similarity DESC
LIMIT 3;
Step 5: Vote across the top-k for a robust label
Average similarity per intent over the nearest examples — majority-by-meaning beats a single match.
SELECT intent, AVG(similarity) AS confidence, COUNT(*) AS votes
FROM (
SELECT intent,
COSINE_SIMILARITY(embedding, EMBED('the page keeps throwing an error when I click save')) AS similarity
FROM recipe_intent_examples
ORDER BY similarity DESC
LIMIT 5
)
GROUP BY intent
ORDER BY confidence DESC
LIMIT 1;
Step 6: Gate low-confidence intents to a fallback
Return the predicted intent only when it clears a threshold; otherwise the agent should ask, not assume.
SELECT intent, confidence,
CASE WHEN confidence >= 0.30 THEN intent ELSE 'unknown_ask_clarify' END AS routed_intent
FROM (
SELECT intent, AVG(similarity) AS confidence
FROM (
SELECT intent,
COSINE_SIMILARITY(embedding, EMBED('hi there, hope you are well')) AS similarity
FROM recipe_intent_examples
ORDER BY similarity DESC
LIMIT 5
)
GROUP BY intent
ORDER BY confidence DESC
LIMIT 1
);