Cookbook

Route Models by Complexity

Not every query needs GPT-4. Simple questions can go to cheaper, faster models while complex reasoning tasks route to powerful ones. 2Signal analyzes input complexity and matches against your routing rules so you can cut costs without sacrificing quality.

How It Works

2Signal scores every input on a 0–1 complexity scale using four weighted signals:

  • Length (25%) — longer inputs score higher
  • Vocabulary richness (20%) — diverse word usage indicates complexity
  • Question complexity (35%) — multi-step or reasoning-heavy prompts score higher
  • Structural complexity (20%) — nested lists, code blocks, and structured data

The score is then matched against your routing rules in priority order. The first rule whose condition is satisfied determines the model.

Step 1: Call the Routing API

curl -X POST https://your-instance.2signal.dev/api/v1/route-model \
  -H "Authorization: Bearer your-api-key" \
  -H "Content-Type: application/json" \
  -d '{"input": "What is your return policy?"}'

Response:

{
  "data": {
    "model": "gpt-4o-mini",
    "configName": "default",
    "complexity": 0.18
  }
}

Step 2: Use the Result in Your Agent

Call the routing endpoint before each LLM request, then pass the returned model name to your provider client.

import httpx
import twosignal
from openai import OpenAI

client = twosignal.TwoSignal(api_key="your-api-key")
openai_client = twosignal.wrap_openai(OpenAI())

@client.observe(name="smart-router", span_type="AGENT")
def ask(question: str) -> str:
    # Get the recommended model
    resp = httpx.post(
        "https://your-instance.2signal.dev/api/v1/route-model",
        headers={"Authorization": "Bearer your-api-key"},
        json={"input": question},
    )
    model = resp.json()["data"]["model"]

    # Use the routed model
    response = openai_client.chat.completions.create(
        model=model,
        messages=[{"role": "user", "content": question}],
    )
    return response.choices[0].message.content

Step 3: Configure Routing Rules in the Dashboard

Go to your project's Model Routing page and create a routing configuration. Rules are evaluated in priority order — the first match wins.

PriorityNameConditionModel
1Simple queriescomplexity < 0.3gpt-4o-mini
2Medium queriescomplexity < 0.7gpt-4o
3Keyword: codekeyword: "code", "debug", "implement"gpt-4o
4Fallbackalwaysgpt-4o

Condition Types

  • complexity — match when the complexity score is below a threshold
  • token_count — match when the estimated token count is below a maximum
  • keyword — match when the input contains any of the specified words
  • always — always matches; use as a fallback rule

What's Next

See the Model Routing guide for the full reference on configuration options, condition syntax, and advanced routing strategies.

Have questions? Join our community!

Connect with other developers and the 2Signal team.

Join Discord