Routing

The routing pattern classifies incoming requests and sends them to the right handler. Think of it as a smart dispatcher: instead of one-size-fits-all processing, each type of input gets specialized treatment.

Routing pattern: input classification

The classifier examines the input and routes to the appropriate specialist.

Whenever your inputs have distinct categories that benefit from different handling. A customer support bot, for example, routes billing questions to a specialist prompt and technical issues to another. One-size-fits-all prompts get mediocre results across the board.

patterns/02_routing.py
python
def classify_input(user_input, llm):
    """Classify user input into a category."""
    prompt = f"""
    Classify this input into one category:
    - technical: Programming, debugging, system design
    - creative: Writing, brainstorming, storytelling
    - analytical: Data analysis, research, comparison
    - general: Greetings, simple questions

    Input: {user_input}
    Respond with just the category name.
    """
    return llm.generate(prompt).content.strip().lower()

def route_to_handler(category, user_input, llm):
    """Route to the appropriate specialist handler."""
    handlers = {
        "technical": "You are a senior software engineer...",
        "creative": "You are a creative writing expert...",
        "analytical": "You are a data analyst...",
        "general": "You are a friendly assistant...",
    }
    system_prompt = handlers.get(category, handlers["general"])
    prompt = f"{system_prompt}\n\nUser request: {user_input}"
    return llm.generate(prompt).content

# Usage
category = classify_input("Help me debug this Python error", llm)
# category = "technical"
response = route_to_handler(category, user_input, llm)

The LLM classifies the input, then we route to a handler with a tailored system prompt.

The routing prompt describes each available handler and asks the LLM to classify the input. It is essentially a classification task. The LLM reads the descriptions and picks the best match based on the user's input. Clear, distinct handler descriptions are key to accurate routing.

Quiz: Quiz

Loading practice…

Matching exercise: Match routing concepts

Loading practice…

Ordering exercise: Order the routing steps

Loading practice…

Flashcards: Flashcards

Loading practice…

Routing completes the foundation. You now know prompt chaining for sequential workflows and routing for branching logic. These two patterns are the building blocks for everything that follows.