# How OmniRoute's Cost-Optimized Routing Strategy Minimizes API Costs

> Minimize LLM expenses with OmniRoute's cost-optimized routing. Discover how it intelligently dispatches requests to the cheapest providers first, ensuring maximum cost savings for your API.

- Repository: [Diego Rodrigues de Sa e Souza/OmniRoute](https://github.com/diegosouzapw/OmniRoute)
- Tags: deep-dive
- Published: 2026-08-21

---

**TLDR:** OmniRoute's *cost-optimized* routing strategy minimizes LLM expenses by sorting available provider-model pairs by input price per million tokens and dispatching requests to the cheapest healthy candidate first, with automatic fallback to the next cheapest if the primary connection fails.

The *cost-optimized* strategy is one of 19 combo-routing algorithms available in the diegosouzapw/OmniRoute open-source routing engine. It directly targets per-token expenses by prioritizing the lowest-priced provider connections without sacrificing request reliability or throughput.

## Pricing-First Ordering in the Target Sorter

At the heart of the cost-optimized strategy lies the **pricing-first ordering** mechanism implemented in [[`open-sse/services/combo/targetSorters.ts`](https://github.com/diegosouzapw/OmniRoute/blob/main/open-sse/services/combo/targetSorters.ts)](https://github.com/diegosouzapw/OmniRoute/blob/release/v3.8.50/open-sse/services/combo/targetSorters.ts). When a combo request enters the system, the routing engine constructs a list of candidate provider connections.

At line 62, the sorter arranges these candidates by **input price**—specifically the price per 1 million input tokens as reported in the provider catalog. The inline comment confirms this logic:

> "Sort models by pricing (cheapest first) for cost-optimized strategy"

This ensures that the provider offering the lowest rate appears first in the dispatch queue.

## Cheapest-First Dispatch Logic

After sorting, the combo engine dispatches requests to the first healthy connection in the list. The dispatch pathway is controlled by [[`open-sse/services/combo/applyStrategyOrdering.ts`](https://github.com/diegosouzapw/OmniRoute/blob/main/open-sse/services/combo/applyStrategyOrdering.ts)](https://github.com/diegosouzapw/OmniRoute/blob/release/v3.8.50/open-sse/services/combo/applyStrategyOrdering.ts), specifically at line 137, where the conditional branch recognizes the strategy:

```typescript
} else if (strategy === "cost-optimized") {
    // cheapest-first ordering already applied by targetSorters
}

```

This branch executes the dispatch knowing that the target sorter has already arranged candidates from lowest to highest cost.

## Automatic Fallback to Next Cheapest

Cost optimization does not compromise resilience. If the cheapest connection fails—whether due to a transient error or a triggered circuit-breaker—the fallback logic automatically retries with the next-cheapest candidate in the sorted list.

This behavior is validated in [[`tests/unit/combo-strategy-fallbacks.test.ts`](https://github.com/diegosouzapw/OmniRoute/blob/main/tests/unit/combo-strategy-fallbacks.test.ts)](https://github.com/diegosouzapw/OmniRoute/blob/release/v3.8.50/tests/unit/combo-strategy-fallbacks.test.ts) at line 202, ensuring that routing failures increment the pointer to the subsequent low-cost option rather than abandoning the cost-saving approach.

## Deterministic Handling of Price Ties

When multiple providers offer identical pricing, OmniRoute ensures **deterministic routing** to prevent erratic behavior. Rather than randomizing selections, the system preserves the original ordering—typically based on provider priority or insertion order—guaranteeing stable routing decisions.

The unit test at line 243 in [[`tests/unit/combo-strategy-fallbacks.test.ts`](https://github.com/diegosouzapw/OmniRoute/blob/main/tests/unit/combo-strategy-fallbacks.test.ts)](https://github.com/diegosouzapw/OmniRoute/blob/release/v3.8.50/tests/unit/combo-strategy-fallbacks.test.ts) explicitly asserts this property, confirming that ties do not disrupt the predictability of the routing path.

## Integration with Authentication and UI

The cost-optimized strategy integrates deeply with OmniRoute's authentication layer. At line 1979 in [[`src/sse/services/auth.ts`](https://github.com/diegosouzapw/OmniRoute/blob/main/src/sse/services/auth.ts)](https://github.com/diegosouzapw/OmniRoute/blob/release/v3.8.50/src/sse/services/auth.ts), the authentication service recognizes the *cost-optimized* option, ensuring that provider-level credential selection respects the strategy constraints.

Users can activate this routing mode through multiple interfaces:
- **API requests**: Specify the strategy in the request body
- **Copilot UI**: Configure via [[`src/lib/copilot/tools.ts`](https://github.com/diegosouzapw/OmniRoute/blob/main/src/lib/copilot/tools.ts)](https://github.com/diegosouzapw/OmniRoute/blob/release/v3.8.50/src/lib/copilot/tools.ts) (line 138) and the engine interface at [[`src/lib/copilot/engine.ts`](https://github.com/diegosouzapw/OmniRoute/blob/main/src/lib/copilot/engine.ts)](https://github.com/diegosouzapw/OmniRoute/blob/release/v3.8.50/src/lib/copilot/engine.ts) (line 72)

## Practical Implementation Examples

### API Request with Cost-Optimized Routing

To invoke the strategy via REST API, include the `comboStrategy` field in your request payload:

```json
POST /api/v1/chat/completions
{
  "model": "gpt-4o-mini",
  "messages": [{ "role": "user", "content": "Explain quantum tunneling." }],
  "comboStrategy": "cost-optimized"
}

```

The router automatically sorts providers by price and dispatches to the cheapest viable endpoint.

### Creating a Combo Preset in Copilot

For reusable configurations in the Copilot interface, define a combo preset with the strategy specified:

```javascript
const combo = {
  id: "combo-001",
  name: "Cheap-First",
  strategy: "cost-optimized",
  enabled: true,
};

await fetch("/api/mcp/tools/createCombo", {
  method: "POST",
  body: JSON.stringify({ combo }),
  headers: { "Content-Type": "application/json" },
});

```

This creates a persistent routing rule that always selects the lowest-cost provider for matching requests.

## Summary

- **Price-based sorting**: The strategy relies on [`targetSorters.ts`](https://github.com/diegosouzapw/OmniRoute/blob/main/targetSorters.ts) to arrange candidates by input price per million tokens, ensuring the cheapest option is evaluated first.
- **Sequential dispatch**: The [`applyStrategyOrdering.ts`](https://github.com/diegosouzapw/OmniRoute/blob/main/applyStrategyOrdering.ts) module dispatches to the first healthy candidate in the price-sorted list.
- **Resilient fallbacks**: Failed connections trigger automatic retries with the next-cheapest provider, verified in [`combo-strategy-fallbacks.test.ts`](https://github.com/diegosouzapw/OmniRoute/blob/main/combo-strategy-fallbacks.test.ts).
- **Deterministic ties**: Identical prices preserve original provider ordering, preventing routing instability.
- **Full-stack integration**: The strategy is recognized by the authentication service ([`auth.ts`](https://github.com/diegosouzapw/OmniRoute/blob/main/auth.ts)) and exposed through both API and Copilot UI interfaces.

## Frequently Asked Questions

### What is the cost-optimized routing strategy in OmniRoute?

The cost-optimized routing strategy is one of 19 combo-routing algorithms in OmniRoute designed to minimize LLM API costs. It sorts available provider-model pairs by their input token price (per million tokens) and always attempts to route requests to the cheapest healthy provider first.

### How does OmniRoute handle provider failures when using cost-optimized routing?

If the cheapest provider connection fails due to network errors or circuit-breaker states, OmniRoute automatically falls back to the next-cheapest candidate in the sorted list. This behavior is tested in [`tests/unit/combo-strategy-fallbacks.test.ts`](https://github.com/diegosouzapw/OmniRoute/blob/main/tests/unit/combo-strategy-fallbacks.test.ts) at line 202, ensuring cost optimization does not compromise request reliability.

### Can cost-optimized routing be configured through the OmniRoute Copilot UI?

Yes, the strategy is fully supported in the Copilot interface. Users can create combo presets specifying `"strategy": "cost-optimized"` via the tools endpoint defined in [`src/lib/copilot/tools.ts`](https://github.com/diegosouzapw/OmniRoute/blob/main/src/lib/copilot/tools.ts) (line 138), or configure it directly in the engine interface at [`src/lib/copilot/engine.ts`](https://github.com/diegosouzapw/OmniRoute/blob/main/src/lib/copilot/engine.ts) (line 72).

### What happens when two providers have identical pricing in cost-optimized mode?

When price ties occur, OmniRoute preserves the original candidate ordering—typically based on provider priority or configuration sequence—rather than randomizing selection. This deterministic approach is asserted at line 243 in [`tests/unit/combo-strategy-fallbacks.test.ts`](https://github.com/diegosouzapw/OmniRoute/blob/main/tests/unit/combo-strategy-fallbacks.test.ts), ensuring consistent routing behavior even when costs are equal.