Recipe: NL → tool selection router

Route natural-language queries to the correct tool with a single classification call.

Overview

This recipe classifies a user's natural-language request into one of N predefined tool categories, then dispatches to the appropriate handler. It uses a single LLM call with a constrained output schema — no multi-step reasoning, no agent loop.

Tool Manifest

Tool IDDescriptionExample Query
search_kbSemantic search over docs"How do I configure SSO?"
run_sqlGenerate and execute SQL"Show me MAU by month for 2025"
escalateCreate a support ticket"I need to talk to a human"

Router Prompt

You are a tool router. Given a user query, output
exactly one JSON object with keys:
- "tool": one of [search_kb, run_sql, escalate]
- "confidence": float 0-1

Do not explain. Do not wrap in markdown.

Dispatch Logic

const result = await classify(query);

switch (result.tool) {
  case "search_kb":
    return searchKnowledgeBase(query);
  case "run_sql":
    return executeSQL(query);
  case "escalate":
    return createTicket(query);
  default:
    throw new Error("Unknown tool");
}

Confidence Threshold

If confidence is below 0.7, fall back to a clarification prompt asking the user to rephrase. This prevents silent misrouting on ambiguous queries.