Objective
When a user says "I can't log in," the agent has to pick the right tool —
reset_password, not cancel_subscription. Hard-coded keyword routers break the moment
someone phrases it differently, and full LLM function-calling is slow and costly for every
turn. Here you'll route by meaning: store each tool's description as an embedding, match the
user's request to the nearest tool with one query, and confirm with a confidence gate. No
framework, no rules engine. The same router works from any framework or a voice agent — see
Use it from your agent at the end.
Step 1: Create the tool registry
Each row is a tool the agent can call, described in natural language and embedded for matching.
CREATE TABLE IF NOT EXISTS recipe_agent_tools (
tool_id INTEGER PRIMARY KEY,
tool_name TEXT, -- the function the agent would invoke
description TEXT, -- what the tool does, in plain language
embedding VECTOR(384)
);
Step 2: Register the agent's tools
A realistic toolbox for a SaaS support agent.
INSERT INTO recipe_agent_tools (tool_id, tool_name, description) VALUES
(1,'reset_password','Reset or recover a user account password; help when a user cannot log in or is locked out.'),
(2,'cancel_subscription','Cancel a paid subscription or stop recurring billing for the account.'),
(3,'check_order_status','Look up the shipping status and delivery estimate of an existing order.'),
(4,'update_billing','Update the credit card or billing address on file for the account.'),
(5,'create_support_ticket','Open a support ticket for a problem that needs a human agent.'),
(6,'check_usage','Report the account''s current API usage and remaining quota for the month.');
Step 3: Embed the tool descriptions
The embedding model runs in-database; this is the index the router searches.
UPDATE recipe_agent_tools SET embedding = EMBED(description);
Step 4: Route a request to the best tool
Match the user's message to the nearest tool by meaning — no shared keywords required.
SELECT tool_name, description,
COSINE_SIMILARITY(embedding, EMBED('I keep getting locked out and can''t sign in')) AS match_score
FROM recipe_agent_tools
ORDER BY match_score DESC
LIMIT 3;
Step 5: Pick the single tool to call, with a confidence gate
Return the top tool only when it clears a confidence threshold — otherwise the agent should ask a clarifying question.
SELECT tool_name, match_score,
CASE WHEN match_score >= 0.35 THEN 'CALL' ELSE 'ASK_CLARIFY' END AS decision
FROM (
SELECT tool_name,
COSINE_SIMILARITY(embedding, EMBED('I want to stop being charged every month')) AS match_score
FROM recipe_agent_tools
ORDER BY match_score DESC
LIMIT 1
);