Estimate the USD cost of a set of calls
Usage
hr_estimate_cost(
model,
input_tokens,
output_tokens = 0,
cached_input_tokens = 0,
detail = FALSE
)Arguments
- model
Model id.
- input_tokens, output_tokens
Token counts (scalars or vectors; vectors are summed).
- cached_input_tokens
How many of
input_tokensthe provider served from its prompt cache. They are priced at the model'scached_input_usdwhen it has one, and as other input tokens when it does not.- detail
FALSE(the default) for the figure alone;TRUEfor a data frame that also says where the prices came from.
Value
A single numeric USD figure, or NA_real_ when the model has no
pricing in the registry, or no output price and output_tokens above zero
(an embedding model's missing output price counts as zero, since it
produces no output tokens). harnessr ships no prices: set them with
hr_register_model() or hr_load_models(). Treat the figure as an
estimate. When the model's prices are older than
hr_options("price_max_age_days") (120 days by default, counted from the
first day of their as_of month), the first figure computed from them in
a session warns with class hr_stale_price.
With detail = TRUE, a one-row data frame instead: model, usd (the
same figure), priced (whether the model has a price), as_of (the
month of its prices, NA without one) and certain (FALSE when the
model's registry entry is a family guess or the default; see
hr_model_info()).
See also
hr_model_info(), hr_register_model(), hr_trace_summary()
Other cost and token functions:
hr_budget(),
hr_context_fit(),
hr_context_policy(),
hr_count_tokens(),
hr_load_models(),
hr_model_info(),
hr_model_limits(),
hr_models(),
hr_register_model(),
hr_set_tokenizer(),
hr_tokenizer(),
hr_truncate(),
hr_truncate_tokens()
Examples
# harnessr ships no prices, so a built-in model's cost is NA until you set
# them. Not 0: nothing says the run is free.
hr_estimate_cost("gpt-4o", input_tokens = 120000, output_tokens = 4000)
#> [1] NA
# Set the prices you pay. These figures are illustrative: use your
# provider's current prices, in USD per million tokens.
hr_register_model("gpt-4o", input_usd = 2.50, output_usd = 10)
hr_estimate_cost("gpt-4o", input_tokens = 120000, output_tokens = 4000)
#> [1] 0.34
# Where the figure's prices came from.
hr_estimate_cost("gpt-4o", 120000, 4000, detail = TRUE)
#> model usd priced as_of certain
#> 1 gpt-4o 0.34 TRUE 2026-11 TRUE