# How the Cost-Optimized Combo Strategy Works in OmniRoute

> Discover how OmniRoute's cost-optimized combo strategy routes LLM requests to the cheapest provider first, falling back to expensive options only when necessary. Save on LLM calls.

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

---

**The cost-optimized combo strategy routes LLM requests to the cheapest available provider-model pair first, sequentially falling back to more expensive alternatives only when the primary target fails or is unavailable.**

The cost-optimized combo strategy is a core routing mode in the OmniRoute repository that prioritizes monetary efficiency over latency or raw performance. When this strategy is configured, the system automatically sorts all candidate targets by their per-token pricing and attempts the lowest-cost option before considering more expensive alternatives. This approach is ideal for batch processing, testing environments, or any workload where minimizing API spend is the primary objective.

## Sorting Targets by Input Price

At the heart of the cost-optimized combo strategy lies a price-based sorting mechanism that arranges provider-model pairs from lowest to highest cost. The strategy evaluates the **input price per million tokens** defined in each provider's catalog entry to establish this ordering.

### The Target Sorter Implementation

In [`open-sse/services/combo/targetSorters.ts`](https://github.com/diegosouzapw/OmniRoute/blob/main/open-sse/services/combo/targetSorters.ts), the sorting logic is explicitly documented with a comment stating it "*Sort models by pricing (cheapest first) for cost-optimized strategy*" at line 62. The implementation compares the `inputPricePerM` property of each target:

```typescript
function sortByCost(targets) {
  return targets.sort((a, b) => {
    const priceA = a.pricing.inputPricePerM;
    const priceB = b.pricing.inputPricePerM;
    return priceA - priceB;               // cheapest first
  });
}

```

### Handling Price Ties

When multiple targets share identical pricing, the cost-optimized combo strategy preserves the original catalog order rather than applying secondary sorting criteria. This behavior is verified in [`tests/unit/combo-strategy-fallbacks.test.ts`](https://github.com/diegosouzapw/OmniRoute/blob/main/tests/unit/combo-strategy-fallbacks.test.ts) at lines 243-256, ensuring deterministic and predictable routing even when providers offer competitive equivalent rates.

## Dispatch and Fallback Execution

Once sorted, the candidate list feeds into the dispatcher which executes a sequential fallback pattern. The system attempts the cheapest target first and only escalates to more expensive options upon failure.

### Sequential Attempt Order

The dispatcher processes the price-sorted array iteratively, as demonstrated in [`tests/unit/combo-strategy-fallbacks.test.ts`](https://github.com/diegosouzapw/OmniRoute/blob/main/tests/unit/combo-strategy-fallbacks.test.ts) lines 202-220. If the cheapest target fails due to an error, circuit-breaker state, or rate limiting, the strategy automatically proceeds to the next-cheapest available option:

```typescript
async function dispatchCostOptimized(sortedTargets) {
  for (const tgt of sortedTargets) {
    try {
      return await executeTarget(tgt);   // try cheapest available
    } catch (e) {
      // log and continue with next cheapest
    }
  }
  throw new Error("All cost-optimized targets failed");
}

```

Live integration tests in [`tests/integration/combo-matrix/cost-and-context.test.ts`](https://github.com/diegosouzapw/OmniRoute/blob/main/tests/integration/combo-matrix/cost-and-context.test.ts) (lines 46-52) confirm this behavior against real provider catalogs, verifying that cheaper models like `gpt-4o-mini` at $0.15 per million tokens are selected before more expensive alternatives such as `gemini-2.5-pro` at $2.00 per million tokens.

### Deterministic Caching Benefits

Because the cost-optimized combo strategy produces the exact same ordering every time for a given catalog, it is classified as deterministic in [`open-sse/utils/cacheControlPolicy.ts`](https://github.com/diegosouzapw/OmniRoute/blob/main/open-sse/utils/cacheControlPolicy.ts) at lines 74-75. This classification allows OmniRoute to apply aggressive caching policies and consistent routing decisions without worrying about stochastic behavior affecting cache hit rates.

## Authentication and Account Selection

The cost-optimized strategy extends beyond model selection into the authentication layer. In [`src/sse/services/auth.ts`](https://github.com/diegosouzapw/OmniRoute/blob/main/src/sse/services/auth.ts) at line 2051, the code path handling `strategy === "cost-optimized"` selects fallback accounts by choosing the credential with the lowest priority value—effectively treating account priority as a proxy for cost, consistent with the strategy's cost-minimization philosophy.

## Configuring the Cost-Optimized Combo Strategy

To implement this routing mode in your OmniRoute deployment, specify the strategy when creating a combo configuration:

```typescript
import { createCombo } from "@/open-sse/services/combo";
import { resolveRequest } from "@/open-sse/handlers/chatCore";

const combo = createCombo({
  name: "Cheap Combo",
  strategy: "cost-optimized",   // ← select the cost-optimised sorter
  targets: [
    { provider: "openai", model: "gpt-4o-mini" },
    { provider: "google", model: "gemini-2.5-pro" },
    // …other models…
  ],
});

const response = await resolveRequest({
  body: { model: combo.name, messages: [...] },
});

```

## Summary

- **Price-driven sorting**: The strategy arranges targets by `inputPricePerM` from lowest to highest in [`open-sse/services/combo/targetSorters.ts`](https://github.com/diegosouzapw/OmniRoute/blob/main/open-sse/services/combo/targetSorters.ts).
- **Sequential fallback**: The dispatcher attempts the cheapest target first, escalating to more expensive options only upon failure.
- **Tie preservation**: When prices are identical, the original catalog order is maintained for predictable behavior.
- **Deterministic classification**: The strategy is marked as deterministic in [`open-sse/utils/cacheControlPolicy.ts`](https://github.com/diegosouzapw/OmniRoute/blob/main/open-sse/utils/cacheControlPolicy.ts), enabling efficient caching.
- **Auth integration**: Account selection follows the same cost-minimization logic in [`src/sse/services/auth.ts`](https://github.com/diegosouzapw/OmniRoute/blob/main/src/sse/services/auth.ts).

## Frequently Asked Questions

### How does the cost-optimized strategy handle targets with identical pricing?

When multiple provider-model pairs have the same input price per million tokens, the cost-optimized combo strategy preserves their original catalog order rather than randomizing or applying secondary criteria. This behavior is explicitly tested in [`tests/unit/combo-strategy-fallbacks.test.ts`](https://github.com/diegosouzapw/OmniRoute/blob/main/tests/unit/combo-strategy-fallbacks.test.ts) lines 243-256.

### Is the cost-optimized combo strategy deterministic?

Yes. Because the sorting depends solely on static pricing data from the provider catalog, the strategy yields the exact same ordering every time for a given configuration. The caching policy module in [`open-sse/utils/cacheControlPolicy.ts`](https://github.com/diegosouzapw/OmniRoute/blob/main/open-sse/utils/cacheControlPolicy.ts) explicitly treats cost-optimized as a deterministic strategy at lines 74-75.

### What happens if the cheapest provider fails?

If the cheapest target fails due to network errors, circuit breaker states, or rate limits, the dispatcher automatically falls back to the next-cheapest target in the sorted list. This sequential retry mechanism continues until a successful response is obtained or all targets are exhausted, as verified in [`tests/unit/combo-strategy-fallbacks.test.ts`](https://github.com/diegosouzapw/OmniRoute/blob/main/tests/unit/combo-strategy-fallbacks.test.ts) lines 202-220.

### Can cost-optimized be used for authentication fallbacks?

Yes. The authentication service in [`src/sse/services/auth.ts`](https://github.com/diegosouzapw/OmniRoute/blob/main/src/sse/services/auth.ts) supports cost-optimized selection for account fallbacks at line 2051. When this strategy is specified, the system selects the account with the lowest priority value, effectively choosing the cheapest available credentials.