Topic Routing

Route by detected topic using a custom topic guard and cost-optimized models.

Route requests to specialized models based on the detected topic of the conversation. Use semantic analysis to match content with the most appropriate model.

Use Case

  • Content-specific optimization

  • Specialized model selection

  • Domain expertise routing

Prerequisites

Before using this routing configuration, you need to create a Topic Adherence Guardrail in your LangDB dashboard:

  1. Go to Guardrails section in your LangDB dashboard

  2. Create a new guardrail with type "Topic Adherence"

  3. Configure the topics you want to classify (e.g., FAQ, support, technical, etc.)

  4. Copy the generated guardrail slug

  5. Replace {{topic_guard_slug}} in the configuration below with your actual guardrail slug

Configuration

{
  "model": "router/dynamic",
  "router": {
    "type": "conditional",
    "pre_request": [
      {
        "name": "topic_guard",
        "type": "guard",
        "guard_id": "{{topic_guard_slug}}"
      }
    ],
    "routes": [
      {
        "conditions": {
          "all": [
            {
              "pre_request.topic_guard.passed": {
                "$eq": true
              }
            }
          ]
        },
        "name": "faq_route",
        "targets": {
          "$any": [
            "openai/gpt-4.1",
            "openai/gpt-4.1-nano",
            "openai/gpt-4.1-mini"
          ],
          "sort_by": "price",
          "sort_order": "min"
        }
      },
      {
        "conditions": {},
        "name": "fallback_route",
        "targets": {
          "$any": [
            "gemini/*"
          ],
          "sort_by": "ttft",
          "sort_order": "min"
        }
      }
    ]
  }
}

How It Works

  1. Topic Guard: Uses a custom guard to classify the request content

  2. FAQ Route: Routes topic-classified requests to cost-optimized OpenAI models (GPT-4.1 variants)

  3. Fallback Route: Routes all other requests to any Gemini model, sorted by fastest response time

Variables Used

  • pre_request.topic_guard.passed: Topic guard classification result

Customization

  • Add more topic categories

  • Include specialized models for different domains

  • Adjust topic detection thresholds

  • Combine with user tier routing

Last updated

Was this helpful?