Skip to content

Conversation

@hadley
Copy link
Member

@hadley hadley commented Oct 29, 2025

Now we compute the cost once, when we create the turn. The makes a lot of things simpler, e.g. we no longer need to track the variant in the$tokens, and unit tests get simpler. I also introduce Chat$get_cost_details() (ported from #810) to make it easier to inspect the raw details of a chat.

Includes a fix for #804 by logging as part of Chat$add_turn(). This breaks #743 in a new way: for now batch chat usage isn't logged at all. But I think it makes sense to fix that in another PR.

I've also added map_tokens() helper to hide the awkward use of vapply() in a few places (and to make the output more consistent)

Now we compute the cost once, when we create the turn. The makes a lot of things simpler, e.g. we no longer need to track the variant in `the$tokens`, and unit tests get simpler. I also introduce `Chat$get_cost_details()` (ported from #810) to make it easier to inspect the raw details of a chat.

Includes a fix for #804 by logging as part of `Chat$add_turn()`. This breaks #743 in a new way: for now batch chat usage isn't logged at all. But I think it makes sense to fix that in another PR.

I've also added `map_tokens()` helper to hide the awkward use of `vapply()` in a few places (and to make the output more consistent)
@hadley hadley requested a review from cpsievert October 29, 2025 14:17
R/chat.R Outdated
private$provider,
# TODO: store better representation in Turn object
exec(tokens, !!!as.list(system@tokens)),
system@cost
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

system here really means assistant, right? Worth fixing that naming now?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah that confused me too. Might as well fix now.

R/turns.R Outdated
Comment on lines 63 to 72
tokens = c(0, 0, 0),
duration = NA_real_
duration = NA_real_,
cost = NA_real_
Copy link
Collaborator

@cpsievert cpsievert Oct 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer tokens and cost to be next to one another

Copy link
Collaborator

@cpsievert cpsievert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM once the minor comments are addressed

@hadley hadley merged commit d6bfac4 into main Oct 29, 2025
11 checks passed
@hadley hadley deleted the refactor-cost-computation branch October 29, 2025 18:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants