> ## Documentation Index
> Fetch the complete documentation index at: https://docs.auriko.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Cost Optimization

> Reduce inference costs with cache-aware routing

Auriko's proprietary cost model computes the *expected cost* (the predicted cost accounting for caching, pricing tiers, and your usage patterns) of each request at every available provider and routes to the cheapest one.

## Prerequisites

* An [Auriko API key](https://auriko.ai/signup?redirectTo=%2Fdashboard%3Ftab%3Dapi-keys)
* Python 3.10+ with the OpenAI SDK (`pip install openai`) or the auriko SDK (`pip install auriko`)
  * OR Node.js 18+ with the OpenAI SDK (`npm install openai`) or `@auriko/sdk` (`npm install @auriko/sdk`)

## Enable cost optimization

To route by cost, set `optimize` to `"cost"`:

<CodeGroup>
  ```python Python OpenAI theme={null}
  import os
  from openai import OpenAI

  client = OpenAI(
      api_key=os.environ["AURIKO_API_KEY"],
      base_url="https://api.auriko.ai/v1"
  )

  response = client.chat.completions.create(
      model="gpt-5.4",
      messages=[{"role": "user", "content": "Hello!"}],
      extra_body={"gateway": {"routing": {"optimize": "cost"}}}
  )
  ```

  ```typescript TypeScript OpenAI theme={null}
  import OpenAI from "openai";

  const client = new OpenAI({
      apiKey: process.env.AURIKO_API_KEY,
      baseURL: "https://api.auriko.ai/v1",
  });

  const response = await client.chat.completions.create({
      model: "gpt-5.4",
      messages: [{ role: "user", content: "Hello!" }],
      gateway: { routing: { optimize: "cost" } },
  });

  console.log(response.choices[0].message.content);
  ```

  ```python Python Auriko theme={null}
  import os
  from auriko import Client

  client = Client(
      api_key=os.environ["AURIKO_API_KEY"],
      base_url="https://api.auriko.ai/v1"
  )

  response = client.chat.completions.create(
      model="gpt-5.4",
      messages=[{"role": "user", "content": "Hello!"}],
      gateway={
          "routing": {
              "optimize": "cost",
          },
      }
  )

  # See the actual cost
  print(f"Cost: ${response.routing_metadata.cost.usd:.6f}")
  print(f"Provider: {response.routing_metadata.provider}")
  ```

  ```typescript TypeScript Auriko theme={null}
  import { Client } from "@auriko/sdk";

  const client = new Client({
      apiKey: process.env.AURIKO_API_KEY,
      baseUrl: "https://api.auriko.ai/v1",
  });

  const response = await client.chat.completions.create({
      model: "gpt-5.4",
      messages: [{ role: "user", content: "Hello!" }],
      gateway: {
          routing: {
              optimize: "cost",
          },
      },
  });

  console.log(`Provider: ${response.routing_metadata?.provider}`);
  ```

  ```bash cURL theme={null}
  curl https://api.auriko.ai/v1/chat/completions \
    -H "Authorization: Bearer $AURIKO_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "gpt-5.4",
      "messages": [{"role": "user", "content": "Hello!"}],
      "gateway": {"routing": {"optimize": "cost"}}
    }'
  ```
</CodeGroup>

## Understand the cost model

Pricing page rates show what a cached token costs if it gets cached. They don't tell you which tokens get cached or under what conditions. Two providers quoting identical rates can produce different bills on the same workload.

Auriko maintains a proprietary data pipeline and cost model that tracks provider-side caching mechanics, estimates your usage patterns, and predicts the expected cost of each request at every available provider.

### Provider tracking

Auriko's data pipeline tracks each provider's caching mechanics: discount depths, minimum token thresholds, block granularity, write costs, expiration windows, and pricing tiers that shift with context length. This data updates as providers change infrastructure.

### Usage estimation

Auriko estimates request-level variables from your usage patterns: prefix length, reuse frequency, request timing, conversation depth, and output volume. This predicts how each provider's caching performs for your specific traffic.

Auriko is a zero data retention proxy. Pattern estimation uses usage metadata only. Read the [Privacy Policy](https://www.auriko.ai/privacy) for details.

### Per-request cost prediction

For each request, the cost model combines provider data and usage estimates to compute the expected cost at every available provider. It routes to the cheapest one.

This is a per-request decision, not a static ranking. A provider with higher list prices can be cheaper over a multi-turn conversation if its caching mechanics produce more cache hits for your workload.

Cached tokens cost less than uncached tokens. Cache reads cost less than regular input, but writing to cache can cost more. The cost model accounts for these differences.

## Set latency constraints

To optimize for cost while enforcing a latency ceiling, add `max_ttft_ms`:

<CodeGroup>
  ```python Python OpenAI theme={null}
  response = client.chat.completions.create(
      model="gpt-5.4",
      messages=[{"role": "user", "content": "Hello!"}],
      extra_body={"gateway": {"routing": {
          "optimize": "cost",
          "max_ttft_ms": 1000,
      }}}
  )
  ```

  ```typescript TypeScript OpenAI theme={null}
  const response = await client.chat.completions.create({
      model: "gpt-5.4",
      messages: [{ role: "user", content: "Hello!" }],
      gateway: { routing: {
          optimize: "cost",
          max_ttft_ms: 1000,
      } },
  });

  console.log(response.choices[0].message.content);
  ```

  ```python Python Auriko theme={null}
  response = client.chat.completions.create(
      model="gpt-5.4",
      messages=[{"role": "user", "content": "Hello!"}],
      gateway={
          "routing": {
              "optimize": "cost",
              "max_ttft_ms": 1000,  # Max 1s to first token
          },
      }
  )
  ```

  ```typescript TypeScript Auriko theme={null}
  const response = await client.chat.completions.create({
      model: "gpt-5.4",
      messages: [{ role: "user", content: "Hello!" }],
      gateway: {
          routing: {
              optimize: "cost",
              max_ttft_ms: 1000, // Max 1s to first token
          },
      },
  });
  ```

  ```bash cURL theme={null}
  curl https://api.auriko.ai/v1/chat/completions \
    -H "Authorization: Bearer $AURIKO_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "gpt-5.4",
      "messages": [{"role": "user", "content": "Hello!"}],
      "gateway": {"routing": {"optimize": "cost", "max_ttft_ms": 1000}}
    }'
  ```
</CodeGroup>

## Maximize savings with cost-focus

`cost-focus` aggressively minimizes cost with minimal weight on other factors:

<CodeGroup>
  ```python Python OpenAI theme={null}
  response = client.chat.completions.create(
      model="gpt-5.4",
      messages=[{"role": "user", "content": "Summarize this document..."}],
      extra_body={"gateway": {"routing": {"optimize": "cost-focus"}}}
  )
  ```

  ```typescript TypeScript OpenAI theme={null}
  const response = await client.chat.completions.create({
      model: "gpt-5.4",
      messages: [{ role: "user", content: "Summarize this document..." }],
      gateway: { routing: { optimize: "cost-focus" } },
  });

  console.log(response.choices[0].message.content);
  ```

  ```python Python Auriko theme={null}
  response = client.chat.completions.create(
      model="gpt-5.4",
      messages=[{"role": "user", "content": "Summarize this document..."}],
      gateway={
          "routing": {
              "optimize": "cost-focus",
          },
      }
  )
  ```

  ```typescript TypeScript Auriko theme={null}
  const response = await client.chat.completions.create({
      model: "gpt-5.4",
      messages: [{ role: "user", content: "Summarize this document..." }],
      gateway: {
          routing: {
              optimize: "cost-focus",
          },
      },
  });
  ```

  ```bash cURL theme={null}
  curl https://api.auriko.ai/v1/chat/completions \
    -H "Authorization: Bearer $AURIKO_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "gpt-5.4",
      "messages": [{"role": "user", "content": "Summarize this document..."}],
      "gateway": {"routing": {"optimize": "cost-focus"}}
    }'
  ```
</CodeGroup>

You can also use the suffix shortcut:

```python theme={null}
response = client.chat.completions.create(
    model="gpt-5.4:cost-focus",
    messages=[{"role": "user", "content": "Summarize this document..."}]
)
```

| Strategy     | Behavior                                                             |
| ------------ | -------------------------------------------------------------------- |
| `cost`       | Favors cheaper providers while considering performance and latency   |
| `cost-focus` | Routes to the cheapest provider with minimal weight on other factors |

Both strategies account for cache economics. `cost-focus` weights cost more aggressively.

For the general base vs. focus explanation, see [Base vs. focus](/guides/routing-options#base-vs-focus).

## Set cost ceilings

To exclude providers above a price threshold, set `max_cost_per_1m`:

<CodeGroup>
  ```python Python OpenAI theme={null}
  response = client.chat.completions.create(
      model="gpt-5.4",
      messages=[{"role": "user", "content": "Hello!"}],
      extra_body={"gateway": {"routing": {
          "optimize": "cost",
          "max_cost_per_1m": 10.00,
      }}}
  )
  ```

  ```typescript TypeScript OpenAI theme={null}
  const response = await client.chat.completions.create({
      model: "gpt-5.4",
      messages: [{ role: "user", content: "Hello!" }],
      gateway: { routing: {
          optimize: "cost",
          max_cost_per_1m: 10.00,
      } },
  });

  console.log(response.choices[0].message.content);
  ```

  ```python Python Auriko theme={null}
  import os
  from auriko import Client

  client = Client(
      api_key=os.environ["AURIKO_API_KEY"],
      base_url="https://api.auriko.ai/v1"
  )

  response = client.chat.completions.create(
      model="gpt-5.4",
      messages=[{"role": "user", "content": "Hello!"}],
      gateway={
          "routing": {
              "optimize": "cost",
              "max_cost_per_1m": 10.00,  # Max $10 per 1M tokens (average of input + output)
          },
      }
  )
  ```

  ```typescript TypeScript Auriko theme={null}
  const response = await client.chat.completions.create({
      model: "gpt-5.4",
      messages: [{ role: "user", content: "Hello!" }],
      gateway: {
          routing: {
              optimize: "cost",
              max_cost_per_1m: 10.0, // Max $10 per 1M tokens (average of input + output)
          },
      },
  });
  ```

  ```bash cURL theme={null}
  curl https://api.auriko.ai/v1/chat/completions \
    -H "Authorization: Bearer $AURIKO_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "gpt-5.4",
      "messages": [{"role": "user", "content": "Hello!"}],
      "gateway": {"routing": {"optimize": "cost", "max_cost_per_1m": 10.00}}
    }'
  ```
</CodeGroup>

Auriko calculates cost as the average of input and output price per 1M tokens. Providers exceeding this ceiling are excluded from routing. For fine-grained quality and cost constraints, see [Advanced routing](/guides/advanced-routing).

## Restrict key source

If you have negotiated provider rates through your own API keys, force requests to use only BYOK keys for cost control:

<CodeGroup>
  ```python Python OpenAI theme={null}
  response = client.chat.completions.create(
      model="gpt-5.4",
      messages=[{"role": "user", "content": "Hello!"}],
      extra_body={"gateway": {"routing": {
          "optimize": "cost",
          "only_byok": True,
      }}}
  )
  ```

  ```typescript TypeScript OpenAI theme={null}
  const response = await client.chat.completions.create({
      model: "gpt-5.4",
      messages: [{ role: "user", content: "Hello!" }],
      gateway: { routing: {
          optimize: "cost",
          only_byok: true,
      } },
  });

  console.log(response.choices[0].message.content);
  ```

  ```python Python Auriko theme={null}
  import os
  from auriko import Client

  client = Client(
      api_key=os.environ["AURIKO_API_KEY"],
      base_url="https://api.auriko.ai/v1"
  )

  response = client.chat.completions.create(
      model="gpt-5.4",
      messages=[{"role": "user", "content": "Hello!"}],
      gateway={
          "routing": {
              "optimize": "cost",
              "only_byok": True,  # Use only your own provider keys
          },
      }
  )
  ```

  ```typescript TypeScript Auriko theme={null}
  const response = await client.chat.completions.create({
      model: "gpt-5.4",
      messages: [{ role: "user", content: "Hello!" }],
      gateway: {
          routing: {
              optimize: "cost",
              only_byok: true, // Use only your own provider keys
          },
      },
  });
  ```

  ```bash cURL theme={null}
  curl https://api.auriko.ai/v1/chat/completions \
    -H "Authorization: Bearer $AURIKO_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "gpt-5.4",
      "messages": [{"role": "user", "content": "Hello!"}],
      "gateway": {"routing": {"optimize": "cost", "only_byok": true}}
    }'
  ```
</CodeGroup>

See [Advanced routing](/guides/advanced-routing#set-quality-constraints) for the full constraint API and [Bring Your Own Key](/platform/byok) for BYOK setup.

## Track cost and savings

Every response includes the billable cost in `cost.usd`. The usage breakdown shows `prompt_tokens`, `cached_tokens`, and `completion_tokens`.

<CodeGroup>
  ```python Python Auriko theme={null}
  cost = response.routing_metadata.cost
  print(f"Total cost: ${cost.usd:.6f}")

  # Check cache usage
  usage = response.usage
  if hasattr(usage, "prompt_tokens_details") and usage.prompt_tokens_details:
      cached = getattr(usage.prompt_tokens_details, "cached_tokens", 0)
      print(f"Cached tokens: {cached}")
  print(f"Total prompt tokens: {usage.prompt_tokens}")
  ```

  ```typescript TypeScript Auriko theme={null}
  const cost = response.routing_metadata?.cost;
  console.log(`Total cost: $${cost?.usd}`);

  // Check cache usage
  const cached = response.usage?.prompt_tokens_details?.cached_tokens ?? 0;
  console.log(`Cached tokens: ${cached}`);
  console.log(`Total prompt tokens: ${response.usage?.prompt_tokens}`);
  ```
</CodeGroup>

Auriko normalizes cache reporting across all providers. Regardless of which provider served your request, you read `cached_tokens` from `usage.prompt_tokens_details`.

When cost-optimized routing triggers a failover, Auriko falls back in cost order to the next cheapest eligible provider.

The cost and savings data in each response reflect the output of Auriko's cost model, not list-price arithmetic.

## Optimize your workload

Structure your workload to maximize cost savings.

* **Long, stable system prompts:** Maximize cache reuse across requests.
* **Consistent conversation IDs:** These help providers maintain cache affinity.
* **Steady request cadence:** Bursty traffic can defeat cache expiration windows.
* **Prompt length:** Prompts below provider minimum token thresholds get zero cache discount.
* **Strategy choice:** `cost-focus` aggressively minimizes cost. `cost` adds weight to latency and performance.
* **Monitor:** Track cost and savings in the dashboard.

## Apply to use cases

### Background processing

Batch processing with `cost-focus` routing:

<CodeGroup>
  ```python Python OpenAI theme={null}
  for doc in documents:
      response = client.chat.completions.create(
          model="gpt-4o",
          messages=[{"role": "user", "content": f"Summarize: {doc}"}],
          extra_body={"gateway": {"routing": {"optimize": "cost-focus"}}}
      )
      save_summary(doc.id, response.choices[0].message.content)
  ```

  ```typescript TypeScript OpenAI theme={null}
  for (const doc of documents) {
      const response = await client.chat.completions.create({
          model: "gpt-4o",
          messages: [{ role: "user", content: `Summarize: ${doc}` }],
          gateway: { routing: { optimize: "cost-focus" } },
      });
      saveSummary(doc.id, response.choices[0].message.content);
  }
  ```

  ```python Python Auriko theme={null}
  for doc in documents:
      response = client.chat.completions.create(
          model="gpt-4o",
          messages=[{"role": "user", "content": f"Summarize: {doc}"}],
          gateway={"routing": {"optimize": "cost-focus"}}
      )
      save_summary(doc.id, response.choices[0].message.content)
  ```

  ```typescript TypeScript Auriko theme={null}
  for (const doc of documents) {
      const response = await client.chat.completions.create({
          model: "gpt-4o",
          messages: [{ role: "user", content: `Summarize: ${doc}` }],
          gateway: { routing: { optimize: "cost-focus" } },
      });
      await saveSummary(doc.id, response.choices[0].message.content);
  }
  ```
</CodeGroup>

### With latency budget

Cost routing with a latency constraint:

<CodeGroup>
  ```python Python OpenAI theme={null}
  response = client.chat.completions.create(
      model="gpt-5.4",
      messages=conversation,
      extra_body={"gateway": {"routing": {
          "optimize": "cost",
          "max_ttft_ms": 1000,
      }}}
  )
  ```

  ```typescript TypeScript OpenAI theme={null}
  const response = await client.chat.completions.create({
      model: "gpt-5.4",
      messages: conversation,
      gateway: { routing: {
          optimize: "cost",
          max_ttft_ms: 1000,
      } },
  });

  console.log(response.choices[0].message.content);
  ```

  ```python Python Auriko theme={null}
  response = client.chat.completions.create(
      model="gpt-5.4",
      messages=conversation,
      gateway={
          "routing": {
              "optimize": "cost",
              "max_ttft_ms": 1000,
          },
      }
  )
  ```

  ```typescript TypeScript Auriko theme={null}
  const response = await client.chat.completions.create({
      model: "gpt-5.4",
      messages: conversation,
      gateway: {
          routing: {
              optimize: "cost",
              max_ttft_ms: 1000,
          },
      },
  });
  ```

  ```bash cURL theme={null}
  curl https://api.auriko.ai/v1/chat/completions \
    -H "Authorization: Bearer $AURIKO_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "gpt-5.4",
      "messages": [{"role": "user", "content": "Hello!"}],
      "gateway": {"routing": {"optimize": "cost", "max_ttft_ms": 1000}}
    }'
  ```
</CodeGroup>

## Monitor costs

Track your cost savings in the Auriko dashboard:

* Total spend by day/week/month
* Cost per model
* Cost per provider
* Savings vs. single-provider baseline

<Card title="View Dashboard" icon="chart-bar" href="https://auriko.ai/dashboard">
  Monitor your usage and costs in real-time
</Card>
