home / skills / jeremylongshore / claude-code-plugins-plus-skills / openrouter-routing-rules

This skill implements request-based routing to select optimal models by cost, speed, and quality, improving efficiency across diverse tasks.

npx playbooks add skill jeremylongshore/claude-code-plugins-plus-skills --skill openrouter-routing-rules

Review the files below or copy the command above to add this skill to your agents.

Files (8)
SKILL.md
1.8 KB
---
name: openrouter-routing-rules
description: |
  Implement intelligent model routing based on request characteristics. Use when optimizing for cost, speed, or quality per request. Trigger with phrases like 'openrouter routing', 'model selection', 'smart routing', 'dynamic model'.
allowed-tools: Read, Write, Edit, Grep
version: 1.0.0
license: MIT
author: Jeremy Longshore <[email protected]>
---
# OpenRouter Routing Rules

## Overview

This skill covers implementing request-based routing logic to select optimal models based on content, urgency, or cost constraints.

## Prerequisites

- OpenRouter integration
- Understanding of model capabilities and pricing

## Instructions

Follow these steps to implement this skill:

1. **Verify Prerequisites**: Ensure all prerequisites listed above are met
2. **Review the Implementation**: Study the code examples and patterns below
3. **Adapt to Your Environment**: Modify configuration values for your setup
4. **Test the Integration**: Run the verification steps to confirm functionality
5. **Monitor in Production**: Set up appropriate logging and monitoring

## Overview

This skill covers implementing request-based routing logic to select optimal models based on content, urgency, or cost constraints.

## Prerequisites

- OpenRouter integration
- Understanding of model capabilities and pricing

## Basic Routing Strategies

### Content-Based Routing
```python
def route_by_content(prompt: str) -> str:
    """Route to appropriate model based on content analysis."""
    prompt_lower = prompt.lower()

    # Code-related
    if any(word in prompt_lower for word in ["code", "function", "debug", "python", "javascript"]):
        return "anthropic/claude-3.5-sonnet"

## Detailed Reference

See `{baseDir}/references/implementation.md` for complete implementation guide.

Overview

This skill implements intelligent model routing for OpenRouter to pick the best model per request based on content, urgency, and cost. It helps teams balance quality, latency, and budget by applying simple, extensible routing rules. Use it to automate model selection without manual intervention for each request.

How this skill works

The skill inspects request metadata and prompt content, then evaluates routing rules such as content-based keywords, cost thresholds, and latency requirements. Rules map requests to specific models or tiers (e.g., fast/cheap vs. high-quality) and can be composed with overrides for special cases. The code integrates with an OpenRouter client to forward requests to the chosen model and returns the unified response.

When to use it

  • Optimize cost by routing non-critical requests to lower-cost models
  • Prioritize latency for time-sensitive requests by using faster models
  • Ensure high-quality outputs for complex or code-related prompts
  • Automatically enforce organizational model usage policies
  • Experiment with A/B routing to compare models in production

Best practices

  • Start with simple, transparent rules and add complexity only when needed
  • Maintain a model capability and cost catalog to inform routing decisions
  • Log routing decisions and outcomes for monitoring and iterative tuning
  • Provide a configurable fallback model for failed or ambiguous routing
  • Use rate and budget guards to prevent accidental overspend

Example use cases

  • Route bug reports and code generation prompts to a high-capability model while sending casual chat to a cheaper endpoint
  • Detect urgent customer support messages and route to low-latency, high-availability models
  • Apply regulatory or data-sensitivity tags to force on-prem or compliant models for protected content
  • Run experiments that split traffic across models to measure cost vs. quality tradeoffs

FAQ

How do I add a new routing rule?

Add a rule function that inspects request fields (prompt, metadata, urgency) and returns a model key, then register it in the routing chain configuration.

What if multiple rules match a request?

Rules should be ordered by priority; the first matching rule is applied. Alternatively implement scoring and choose the highest-scoring rule for more complex use cases.