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

# Cost Budgets

> Tracking and limiting AI model usage costs.

Strait includes a built-in cost tracking and budgeting system, primarily designed for managing expenses related to AI model usage (e.g., OpenAI, Anthropic).

## Micro-USD Precision

All costs are tracked in **micro-USD** (1/1,000,000 of a USD) using integer precision. This avoids floating-point rounding errors while providing the granularity needed for sub-cent token costs.

* **Example**: \$0.01 (1 cent) is represented as `10,000` micro-USD.

## Usage Model

The `RunUsage` struct (defined in `apps/strait/internal/domain/types.go`) tracks individual usage events:

| Field               | Type     | Description                                |
| :------------------ | :------- | :----------------------------------------- |
| `id`                | `string` | Unique identifier (UUIDv7).                |
| `run_id`            | `string` | Reference to the Job Run.                  |
| `provider`          | `string` | AI provider (e.g., `openai`, `anthropic`). |
| `model`             | `string` | Model name (e.g., `gpt-4o`).               |
| `prompt_tokens`     | `int`    | Number of input tokens.                    |
| `completion_tokens` | `int`    | Number of output tokens.                   |
| `total_tokens`      | `int`    | Sum of prompt and completion tokens.       |
| `cost_microusd`     | `int64`  | Calculated cost in micro-USD.              |

## Budget Limits

Budgets are defined in the `project_quotas` table and enforced at two levels:

### 1. Per-Run Budget

* **Field**: `max_cost_per_run_microusd`
* **Enforcement**: Checked every time the SDK reports usage via the `/sdk/v1/runs/{runID}/usage` endpoint.
* **Behavior**: If the reported usage would exceed the per-run limit, the request is rejected, and the run is optionally failed.

### 2. Daily Project Budget

* **Field**: `max_daily_cost_microusd`
* **Enforcement**: Checked at **trigger time**.
* **Behavior**: Before a new run is enqueued, the system aggregates the total cost of all runs for the current day (based on the project's configured timezone). If the limit is reached, the trigger request is rejected.

## Budget Checks

Budget validation occurs **before** recording the usage or enqueuing the run. This ensures that limits are strictly enforced and that violating requests do not inflate the recorded costs.

## Project Quotas Table

The `project_quotas` table stores the limits for each project:

```sql theme={null}
project_id            TEXT PRIMARY KEY
max_queued_runs       INT NOT NULL DEFAULT 1000
max_executing_runs    INT NOT NULL DEFAULT 100
max_jobs              INT NOT NULL DEFAULT 100
timezone              TEXT NOT NULL DEFAULT 'UTC'
max_cost_per_run_microusd  BIGINT NOT NULL DEFAULT 0  -- 0 = unlimited
max_daily_cost_microusd    BIGINT NOT NULL DEFAULT 0  -- 0 = unlimited
```

## Budget Enforcement

Usage reports are always recorded, and budget checks are performed at trigger time and during execution.
