Estimate and limit spending
Estimate token spending without making a model request. Save these files together and run node monetary-budget.ts. The rates below are illustrative, not current vendor prices.
Set prices and a limit
Section titled “Set prices and a limit”Rates are per million tokens in one currency. Model names must match your agent configuration exactly.
This task reports usage explicitly. Agent task helpers already do so; a custom dispatch needs prices: context.prices, and a queue worker must return usage attributed to each model.
The result estimates €0.60: €0.20 for input and €0.40 for output. The €20 limit covers all tasks and retries. To estimate without stopping execution, supply prices without cost.
Handle an incomplete estimate
Section titled “Handle an incomplete estimate”A strict cost budget stops with WorkflowCostUnavailable if usage, a model or a price is missing—even with an attempt limit. Reaching the cost limit cancels active attempts with WorkflowBudgetExceeded.
Load a public pricing catalog
Section titled “Load a public pricing catalog”To use Models.dev prices, map your model alias to its catalog ID. This EUR exchange rate is illustrative.
In monetary-budget.ts, import prices from ./catalog-prices.ts. Check the loaded rates; they do not refresh automatically. See loadModelPrices for OpenRouter, conversion and loading errors.
Keep estimates stable on resume
Section titled “Keep estimates stable on resume”Save and reuse the price table: a resumed workflow recomputes total cost from saved model counters. Old checkpoints without those counters cannot satisfy a strict cost budget. A durable candidate race requires its original price table.
API : ModelPriceTable · UsageCost · WorkflowBudget.

