Skip to content

What is an LLM router and how does it dynamically direct queries based on complexity?

Beginner · 2 min read · AI System Design

Open in study mode to save this question and track progress

Short answer: An LLM router evaluates incoming user prompts (using lightweight classification models, intent embeddings, or rule-based heuristics) to dynamically route low-complexity queries to fast, cheap models (e.g. GPT-4o-mini) and high-complexity queries to frontier reasoning models (e.g. o3-mini or GPT-4o), reducing overall API costs by 60–80%.

Detail

Routing 100% of user queries to frontier flagship models causes excessive cost and unnecessary latency.

Incoming User Query
         │
         ▼
[LLM Router (Intent / Embedding Classifier)]
         │
         ├─► Simple Q&A / Classification ──► Route to `gpt-4o-mini` / `llama-3-8b` ($0.15 / 1M tokens)
         │
         └─► Complex Math / Code Generation ──► Route to `o3-mini` / `gpt-4o` ($5.00 / 1M tokens)

Routing Mechanisms

  1. Lightweight Classification Model: A fast BERT or 1B model trained to categorize intent (simple, coding, reasoning).
  2. Embedding Distance Router: Measuring cosine similarity between query embeddings and exemplar cluster vectors for different complexity tiers.
  3. Cascade Routing (Fallback): Attempting completion on a small cheap model first; if quality evaluation checks fail, falling back to a larger model.

Example

Python concept illustrating intent-based LLM routing:

python
def route_user_query(prompt: str) -> str:
    COMPLEX_KEYWORDS = {"code", "python", "refactor", "proof", "math", "architect"}
    words = set(prompt.lower().split())

    # Check if query contains high-complexity technical triggers
    if words.intersection(COMPLEX_KEYWORDS) or len(prompt.split()) > 200:
        return "models/flagship-reasoning-model" # High complexity -> Route to o3-mini / GPT-4o

    return "models/lightweight-fast-model" # Low complexity -> Route to GPT-4o-mini

print("Routing Simple Query:", route_user_query("What is the capital of France?"))
print("Routing Complex Query:", route_user_query("Write a Python script to compute async graph deadlocks."))

Interview tips

  • Discuss RouteLLM (open-source benchmark and routing library).
  • Emphasize tracking accuracy vs cost Pareto frontiers when tuning router confidence thresholds.

Related Concepts


⬅ Back to AI System Design · All topics