Skip to main content

Routing: Building Step-by-Step AI Reasoning

7 min read

Prompting an LLM with a single question gets you only so far. Modern AI products rely on structured reasoning workflows where each step builds on the previous one, with clearly defined roles and goals. One of the most practical of these workflows is routing: deciding what kind of query is being asked, then directing it to the right handler.

We’ll walk through routing using a fun example: a magical wand support system inspired by Harry Potter (Ollivander’s AI Helpdesk). We’ll cover:

  • What routing is in an LLM-driven app
  • How schema-guided prompting structures AI responses
  • A full code walkthrough (with fake wands and magical issues)
  • Why modular routing improves reasoning and maintainability

What is Routing in an LLM System?

Routing is the process of classifying a user’s input so you can:

  • Send it to the correct sub-agent or subsystem (e.g., technical vs. product support)
  • Select the right prompt or reasoning template
  • Invoke specific tools or APIs (e.g., weather API, product database)

Think of routing as the AI equivalent of a customer support switchboard operator. Its job isn’t to answer everything directly; it’s to direct each request to the best destination.

This matters most when building multi-functional assistants or agentic systems where multiple tools or skills are available.

Why Routing Matters

Imagine building an AI assistant for a support desk. Without routing:

User: My wand won't stop casting water spells!
AI: That's interesting! Did you know the Elder Wand is made of elder wood?

That’s a product info answer to a technical support question. Without a routing step, your assistant picks the wrong reasoning strategy, leaving the user confused.

If you first classify the input as either product_info or technical_support, you can ensure the LLM responds within the correct frame of reference.

Building a Magical Support Router (Code Walkthrough)

We’ll build a wand support system with two departments:

  • product_info: wand types, features, materials
  • technical_support: malfunctioning spells, reversed transformations

We use Google’s @google/genai Node.js SDK to interact with Gemini, with JSON schema validation to enforce structure.

1. The Wand Product Catalogue

We simulate a fake in-memory database:

const wandCatalogue = [
  {
    name: 'Elder Wand',
    core: 'Thestral tail hair',
    wood: 'Elder',
    length: '15 inches',
  },
  {
    name: 'Holly Phoenix',
    core: 'Phoenix feather',
    wood: 'Holly',
    length: '11 inches',
  },
  // ...
];

This lets the AI answer product questions with actual reference data.

2. The Routing Schema

This defines what a valid routing decision looks like. We use @google/genai’s Type.OBJECT to define the expected structure:

const routingDecisionSchema = {
  type: Type.OBJECT,
  properties: {
    category: {
      type: Type.STRING,
      enum: ['product_info', 'technical_support', 'unknown'],
    },
    reasoning: {
      type: Type.STRING,
    },
  },
  required: ['category', 'reasoning'],
};

By enforcing this schema, we get structured output instead of ambiguous text. That’s the key to reliable, parseable routing.

3. The Routing Prompt

Instead of asking the AI to do everything, we ask it only to determine the category:

const promptRouter = `
You are a magical support router at Ollivander's AI Helpdesk. Classify the query into one of:

- product_info: Wand types, cores, woods, sizes, general info
- technical_support: Wand not working, spells going wrong, magical anomalies
- unknown: If the query doesn't clearly fit

Explain your choice.

Query: ${userQuery}
`;

The AI focuses only on classification first, not solving the problem. This keeps reasoning simple and targeted.

4. Handling the Routed Query

Once the AI returns the category and reasoning (as a valid object per our schema), we dynamically craft the real prompt based on the routing result:

const routerRequest = {
  model,
  contents: promptRouter,
  config: {
    responseMimeType: 'application/json',
    responseSchema: routingDecisionSchema,
  },
};

const resultRouter = await ai.models.generateContent(routerRequest);
routingDecision = JSON.parse(resultRouter.text);
switch (routingDecision.category) {
  case 'product_info':
    prompt = `You are a wand specialist. The user asked: "${userQuery}". ...`;
    /*
    For this case you also want to include the available products, which you could do by the following:

    Here are the wand listings: ${wandCatalogue.map(w => `- ${w.name}: ${w.wood} wood, ${w.core} core, ${w.length}`).join('\n')}
    */
    break;

  case 'technical_support':
    prompt = `You are a magical technician. Help the user with: "${userQuery}" ...`;
    break;

  default:
    prompt = `The user's message was: "${userQuery}". Ask them to rephrase politely.`;
    break;
}

Each query type now gets answered in context: with the correct tone, expertise, and reference data.

5. Sample Outputs

Let’s test with a few magical queries:

const queries = [
  'What\'s the difference between a phoenix feather core and unicorn hair?',
  'My wand randomly casts Aguamenti when I try Lumos!',
  'I dropped my wand into a cauldron and now it smells like basilisk.',
  'Where can I find Lord Voldermort?',
];

You’ll get expert wand lore answers for product questions, detailed magical troubleshooting for technical issues, and friendly fallback messages for unclear input.

$

Why Schema-Guided Routing Is Powerful

Schema-based routing delivers several benefits:

  • Modularity: cleanly separates classification from response generation
  • Interpretability: you get structured insight into why a query was classified
  • Maintainability: easy to update prompt logic per category
  • Robustness: can gracefully handle unknown queries

Simple pattern. Powerful results.

When to Use Routing

Consider routing when:

  • Your assistant has multiple skills, agents, or domains
  • You want to separate concerns (classify first, then respond)
  • You need detailed control over prompts, tools, or model strategies
  • You want to log and audit model decisions for debugging or analytics

It’s a core design pattern for agentic AI systems, and a lightweight alternative to full-blown orchestration frameworks.

Closing Thoughts

LLMs are versatile, but their effectiveness depends on how you guide them. Bolting on a routing step gives your AI the ability to reason more clearly and act more appropriately in multi-domain settings.

Whether you’re building a whimsical wand assistant or a real-world customer support bot, routing is a foundational capability for intelligent, human-aligned behaviour.