The router pattern

The router pattern: use an LLM to classify the request into a category, then route it to a specialist handler. Each handler has a focused system prompt and limited tools. Think of it like a phone menu: "Press 1 for lighting, 2 for climate..."

06-ai-workflows.ipynb
python
def smart_home_router(query: str) -> str:
    """Classify the request and return category."""
    prompt = (
        "Classify the smart home request into "
        "one of these categories: "
        "[LIGHTING, CLIMATE, SECURITY].\n"
        "Return ONLY a JSON object: "
        '{"category": "CATEGORY_NAME"}'
    )

    response = completion(
        model=DEFAULT_MODEL,
        messages=[
            {"role": "system", "content": prompt},
            {"role": "user", "content": query}
        ],
        response_format={"type": "json_object"}
    )
    result = json.loads(
        response.choices[0].message.content
    )
    return result["category"]

The router uses JSON mode to reliably classify requests into categories.

Router dispatching to specialists

06-ai-workflows.ipynb
python
# Specialized handlers, each focused on one domain
def handle_lighting(query):
    return f"Lighting Specialist: Executing -> {query}"

def handle_climate(query):
    return f"Climate Specialist: Executing -> {query}"

def handle_security(query):
    return f"Security Specialist: Executing -> {query}"

# Route and execute
request = "Turn off the kitchen lights"
category = smart_home_router(request)

handlers = {
    "LIGHTING": handle_lighting,
    "CLIMATE": handle_climate,
    "SECURITY": handle_security
}
result = handlers[category](request)
print(result)

Each handler is a focused function (or agent). The router picks the right one based on classification.

Before we build more complex workflows, let us create a reusable helper that simplifies LLM calls throughout the rest of this module.

06-ai-workflows.ipynb
python
def ask_llm(system_message, user_message,
            json_mode=False):
    """Helper: send a system + user message to LLM."""
    messages = [
        {"role": "system", "content": system_message},
        {"role": "user", "content": user_message}
    ]
    response_format = (
        {"type": "json_object"} if json_mode else None
    )
    response = completion(
        model=DEFAULT_MODEL,
        messages=messages,
        response_format=response_format
    )
    return response.choices[0].message.content

A reusable helper function for simple LLM calls with optional JSON mode.

AI prompt: Try it with AI

Loading practiceโ€ฆ

Fill in the blanks: Complete the router logic

Loading practiceโ€ฆ

Good thinking! Misclassification is a real risk. You can mitigate it with: (1) clear category descriptions in the prompt, (2) a "GENERAL" fallback category, (3) confidence scores where low-confidence requests go to a human, (4) testing with diverse inputs. Later, we cover error handling patterns that help here too.