Strait Docs
Concepts

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:

FieldTypeDescription
idstringUnique identifier (UUIDv7).
run_idstringReference to the Job Run.
providerstringAI provider (e.g., openai, anthropic).
modelstringModel name (e.g., gpt-4o).
prompt_tokensintNumber of input tokens.
completion_tokensintNumber of output tokens.
total_tokensintSum of prompt and completion tokens.
cost_microusdint64Calculated 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:

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.

Was this page helpful?

On this page