POST /api/v1/route-model

Select the optimal model for a given input based on your routing rules. Routes by complexity, token count, or keywords — no LLM calls needed, so it adds near-zero latency.

Request

POST /api/v1/route-model
Authorization: Bearer ts_...
Content-Type: application/json

{
  "input": "What is your refund policy?",
  "configName": "production-router"
}

curl Example

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

Parameters

FieldTypeRequiredDescription
inputstringYesText to analyze for routing
configNamestringNoRouting config to use (default config if omitted)

Response (200)

{
  "model": "gpt-4o-mini",
  "configName": "production-router",
  "complexity": 0.23
}
FieldDescription
modelThe selected model name
configNameWhich routing config was used
complexityComputed complexity score (0–1) for the input

Routing Rules

Configure routing rules in the dashboard or via the CLI. Each rule has a condition type:

ConditionDescriptionExample
complexityRoutes based on a 0–1 complexity score (heuristic)If complexity > 0.7 → use gpt-4o
token_countRoutes based on estimated input token countIf tokens > 2000 → use gpt-4o
keywordRoutes if input contains specific keywordsIf contains "code" → use gpt-4o
alwaysAlways matches — use as a fallback ruleDefault → gpt-4o-mini

Rule Evaluation

  1. Rules are evaluated in priority order (highest priority first)
  2. The first matching rule determines the model
  3. If no rule matches, the config's default model is used

Complexity Scoring

The complexity score is computed heuristically based on:

  • Input length
  • Vocabulary diversity
  • Sentence structure
  • Presence of technical terms, code, or multi-step instructions

No LLM calls are involved — the scoring is deterministic and takes under 1ms.

Example: Using in Your Agent

import httpx

def route_and_call(query: str) -> str:
    # 1. Get the optimal model
    route = httpx.post(
        "https://api.2signal.dev/api/v1/route-model",
        headers={"Authorization": "Bearer ts_..."},
        json={"input": query},
    ).json()

    # 2. Call the selected model
    response = openai_client.chat.completions.create(
        model=route["model"],
        messages=[{"role": "user", "content": query}],
    )
    return response.choices[0].message.content

Error Responses

StatusWhen
400Missing input field
401Invalid or missing API key
404Routing config not found

Have questions? Join our community!

Connect with other developers and the 2Signal team.

Join Discord