/** * Model rate registry — pure token-to-USD cost estimation. * * ## CODEX COST GAP * * The `codex-runner` (packages/codex-taskflow) emits NO cost field in its usage * response, so `usage.cost` stays 0 on Codex even when tokens are accurately * reported. This makes `budget.maxUSD` silently non-functional for Codex-hosted * flows. This module fills the gap: batch 2 of the runner layer (or a post-hoc * cost fill) can call `estimateCost()` to compute USD from the known model + * token counts, making Codex cost-aware without changing the runner protocol. * * ## Link to verify.ts * * `verify.ts` defines `ESTIMATED_COST_PER_PHASE = 0.001` as a crude lower-bound * budget guard. `FLAT_FALLBACK_PER_TOKEN` serves the same role for unknown models * — a rough-order-of-magnitude per-token default (~$1/1M tokens, consistent with * $0.001/1000-tokens at verify's granularity). * * @module */ import type { UsageStats } from "./usage.ts"; /** * Per-model pricing in USD per 1,000,000 tokens. * * Rates mirror the provider's published API pricing (input, output, cached * input read, cached input write). Missing fields default to zero. */ export interface ModelRate { /** USD per 1,000,000 input (prompt) tokens. */ inputPer1M: number; /** USD per 1,000,000 output (completion) tokens. */ outputPer1M: number; /** USD per 1,000,000 cached input tokens read (if applicable). */ cacheReadPer1M?: number; /** USD per 1,000,000 cached input tokens written (if applicable). */ cacheWritePer1M?: number; } /** * Built-in rate table keyed by **model-family prefix** (case-sensitive lookup * key — matching in `resolveRate` is case-insensitive). * * This is a small, illustrative, SPARSE table. Hosts can inject a richer table * at runtime via the optional `table` parameter to `resolveRate`/`estimateCost`. * * Prices are approximate and may drift from provider publishing. For production * billing, always use the provider's official cost API. * * ```ts * // Example override from a host adapter: * const myRates = { ...DEFAULT_RATES, "my-model": { inputPer1M: 1, outputPer1M: 4 } }; * const cost = estimateCost(usage, "my-model", myRates); * ``` */ export declare const DEFAULT_RATES: Record; /** * Flat per-token USD fallback used when no model rate can be resolved. * * Currently set to $1 per 1,000,000 tokens (~$0.001 per 1K tokens), consistent * with `verify.ts`'s `ESTIMATED_COST_PER_PHASE = 0.001` rough-order-of-magnitude * estimate. This avoids silent zero-cost for unknown models while staying in the * right ballpark for most hosted LLM APIs. */ export declare const FLAT_FALLBACK_PER_TOKEN = 0.000001; /** * Resolve a model identifier to a `ModelRate` via case-insensitive prefix match * against the provided table (defaults to `DEFAULT_RATES`). * * Matching strategy: * 1. Exact case-insensitive key match. * 2. Longest prefix match (case-insensitive), e.g. "gpt-4o-2024-08-06" matches * the "gpt-4o" key. * 3. Falls back to single-word prefix: "claude-sonnet-4-20241022" matches * "claude-sonnet-4" (best prefix). * * Returns `undefined` if no match is found. Never throws. * * @param model - The model identifier to look up (e.g. "gpt-4o-2024-08-06") * @param table - Optional rate table (defaults to DEFAULT_RATES) * @returns The matching ModelRate, or undefined on miss */ export declare function resolveRate(model: string, table?: Record): ModelRate | undefined; /** * Compute estimated USD cost from token usage. * * ## Cache accounting contract * * Hosts disagree on whether `usage.input` already includes cached tokens: * - **Codex**: `input_tokens` includes `cached_input_tokens` (overlap). * - **Claude / OpenCode (typical)**: `input` is non-cached; `cacheRead` is * disjoint and additional. * * Heuristic (never double-bill, never under-bill the common cases): * - If `0 < cacheRead <= input`, treat cache as a **subset** of input * (Codex-style): bill `(input - cacheRead)` at the input rate and * `cacheRead` at the cache-read rate. * - Otherwise treat input and cacheRead as **disjoint**: bill full `input` * at the input rate plus `cacheRead` at the cache-read rate. * * Cache-write tokens are always billed separately when a write rate exists * (they are never part of `input`). * * Formula (known rate): * nonCachedInput = (cacheRead > 0 && cacheRead <= input) * ? input - cacheRead : input * cost = (nonCachedInput * inputRate + output * outputRate + * cacheRead * cacheReadRate + cacheWrite * cacheWriteRate) / 1_000_000 * * If the model cannot be resolved, falls back to * `FLAT_FALLBACK_PER_TOKEN * (input + output)`. Cache tokens are *not* counted * in the fallback (avoids double-count when input already includes them). * * Pure function — no I/O, no side effects. Never throws. * * @param usage - Token counts (input, output, cacheRead, cacheWrite) * @param model - Model identifier used to resolve the rate * @param table - Optional rate table override (passed through to resolveRate) * @returns Estimated cost in USD */ export declare function estimateCost(usage: UsageStats, model: string, table?: Record): number; //# sourceMappingURL=rates.d.ts.map