Sponsored by

Staying ahead with AI

Say you're building an AI app that summarizes documents, extracts information, answers questions, and writes code.

You connect it to a powerful LLM, everything works.

Then you check your API bill.

Turns out, you've been using the same expensive model to summarize a two-paragraph email and debug a 200-line Python function.

Unnecessary, right?

What if your app could automatically choose a cheaper model for simple tasks and reserve the stronger one for harder requests?

That's what we're building today: an LLM router that picks the cheapest suitable model for each task.

Before the router, a quick word from two tools worth keeping open.

Presented by

State of Product reveals what AI still hasn’t solved

80% of product professionals say AI helps them ship faster, but customers aren’t seeing value any sooner. Atlassian’s State of Product 2027 explores the gains AI is delivering and the challenges that remain, from decision-making that hasn’t kept pace to gut instinct overriding customer evidence.

Presented by

Why are so many brands shifting ad spend to CTV?

For marketers who want a presence in streaming, Roku is the obvious place to go. With 47% of all streaming time in the US, it has more reach than every competitor. Discover how Roku Ads Manager can spread your message across 100+ premium apps in a single, self-serve ad buy.

Tools of the Week

The idea

Different requests need different levels of intelligence.

  • Summarizing a short email? A lightweight model should be your first choice.

  • Extracting names from text? Probably the same.

  • Debugging complicated code? A stronger model might be worth it.

  • Solving a multi-step reasoning problem? Again, consider the stronger model.

Our router will use the task type to shortlist suitable models, estimate their costs, and select the cheapest.

Here's the workflow:

One important detail: the router needs some idea of what each model is good at. Price alone can't tell us that.

For this demo, we'll define those rules ourselves. In a real application, you'd refine them using evaluations on your own tasks.

Step 1: Install the dependency

We'll use Python and the OpenAI API.

pip install openai

Then set your API key:

export OPENAI_API_KEY="your-api-key"

Step 2: Configure our models

We'll start with two models and their standard API prices per million tokens.

Model

Input

Output

GPT-6 Luna

$0.10

$0.50

GPT-6.1 Terra

$1.00

$5.00

These are the published rates as of November 2026. Pricing can change, so check the official pricing page before deploying.

MODELS = {
    "gpt-6-luna": {
        "input": 0.10,
        "output": 0.50,
    },
    "gpt-6.1-terra": {
        "input": 1.00,
        "output": 5.00,
    },
}

TASKS = {
    "summary": ["gpt-6-luna", "gpt-6.1-terra"],
    "extraction": ["gpt-6-luna", "gpt-6.1-terra"],
    "reasoning": ["gpt-6.1-terra"],
    "coding": ["gpt-6.1-terra"],
}

Notice that we allow both models for summarization and extraction, but only Terra for reasoning and coding.

This is our initial routing policy, not a universal rule about model capabilities.

Step 3: Estimate the cost

LLM pricing usually depends on input and output tokens. We can calculate an estimated request cost with:

def estimate_cost(model, input_tokens, output_tokens):
    price = MODELS[model]

    return (
        input_tokens * price["input"]
        + output_tokens * price["output"]
    ) / 1_000_000

We don't know the exact output length before generating a response, so we'll use an expected output budget.

Step 4: Build the router

Now for the actual model selection.

def pick_model(task, input_tokens, output_tokens=300):
    candidates = TASKS[task]

    return min(
        candidates,
        key=lambda model: estimate_cost(
            model, input_tokens, output_tokens
        )
    )

That's our router.

It looks at the models approved for a particular task, estimates their costs, and selects the cheapest.

No separate classification model or additional API request.

Step 5: Connect it to an LLM

Let's make the router actually generate responses.

from openai import OpenAI

client = OpenAI()

def routed_completion(prompt, task):
    # Rough estimate: 1 token per 4 characters
    estimated_tokens = max(1, len(prompt) // 4)

    model = pick_model(task, estimated_tokens)

    response = client.responses.create(
        model=model,
        input=prompt,
        reasoning={"effort": "low"},
        max_output_tokens=2000,
    )

    usage = response.usage

    cost = estimate_cost(
        model,
        usage.input_tokens,
        usage.output_tokens,
    )

    print(f"Model: {model}")
    print(f"Cost: ${cost:.6f}")

    return response.output_text

The router estimates input size to make its selection, then uses the API's actual token counts to calculate the request cost.

Both models are reasoning models, and their thinking counts toward max_output_tokens (and is billed as output), so we keep reasoning effort low and leave enough room for the answer.

With too small a limit, the model can run out of tokens before it writes anything.

The cost calculation is simplified and doesn't account for cached-token discounts or additional billable features.

Step 6: Test it

Try two different requests:

print(routed_completion(
    "Summarize: Python is a programming language "
    "widely used in AI and automation.",
    task="summary"
))

print(routed_completion(
    "Explain how to debug a deadlock in "
    "a multithreaded Python application.",
    task="reasoning"
))

The first request should route to Luna. The second will go to Terra because we've restricted reasoning tasks to that model.

Try changing the available models or their prices and see how the selection changes.

How much can we save?

Let's compare a request containing 1,000 input tokens and 500 output tokens.

Model

Cost per request

GPT-6 Luna

$0.00035

GPT-6.1 Terra

$0.00350

If 10,000 requests could be handled by Luna instead of Terra:

  • Terra for everything: $35.00

  • Luna for those requests: $3.50

  • Potential savings: 90%

That's a theoretical comparison for identical token usage, before retries, caching, and any differences in response quality.

The actual savings depend on your workload.

My take

Model routing becomes more useful as an AI product grows. At 50 API calls a day, the savings barely matter. At thousands of requests, they start adding up.

Our router is still basic. It relies on predefined task types, but we could add a lightweight classifier to handle arbitrary requests.

The tricky part is that the cheapest model isn't always the cheapest in practice. Multiple failed attempts can cost more than using a stronger model from the start.

I'd add output validation, fallbacks, and cost tracking, then test both models on real requests to improve the routing rules.

For production, LiteLLM already offers cost-based routing and fallbacks. But building your own is a useful way to understand where your LLM budget goes.

Until next time,
Vaibhav 🤝

Reply

Avatar

or to participate