llm_cost: Cost of an ellmer chat

View source: R/prices.R

llm_costR Documentation

Cost of an ellmer chat

Description

The cost in US dollars of the conversation held by an ellmer chat object, as used by agents. When a price was registered for the chat's model with [set_prices()], the cost is computed from the token usage of each assistant turn with that price. Otherwise it is ellmer's own estimate (the cost ellmer records on each assistant turn), which is 'NA' for models ellmer has no price for. Turns without a recorded cost, such as turns rebuilt from an agent's edited history, count as zero, so the cost is 'NA' only when no turn has a known cost.

An agent's own cost ('$cost', '$get_usage_stats()') also keeps what was spent on turns that were later removed from its history.

Usage

llm_cost(llm_object, include = c("all", "last"))

Arguments

llm_object

An ellmer chat object (eg. an agent's '$llm_object').

include

'"all"' (default) for the whole conversation, or '"last"' for the last assistant turn only.

Value

A number: the cost in USD, or 'NA' if it is unknown.

Examples

## Not run: 
chat <- ellmer::chat("openai/gpt-5.6", echo = "none")
chat$chat("Hello")
llm_cost(chat) # NA: ellmer has no price for this model
set_prices("gpt-5.6", input = 1.25, output = 10)
llm_cost(chat)

## End(Not run)

mini007 documentation built on Dec. 3, 2026, 5:10 p.m.