Repository navigation
feat(cli): a run's usage and cost by role and model where runs end #908
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d8e5432
feat(cli): a run's usage and cost by role and model where runs end
earakely-scale af1f864
fix(cli): an eval counts each run's cost though runs share a tag, and…
earakely-scale b01976f
fix(cli): a cost priced on an assumed rate prints as at most, and wit…
earakely-scale 9390045
fix(cli): a share cut off mid-run prints its cost as at least, or est…
earakely-scale f5779fa
refactor(cli): one summary rule for a share's cost and the total's, e…
earakely-scale d8dd53f
refactor(cli): name the cost lines' display limits
earakely-scale 2442826
refactor(cli): wrap the dollars format
earakely-scale File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| """How the run summaries print a run's usage (``task.usage.summary``): one cost line per run and one closing line | ||
| naming where the costs came from.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from agent_env.config.model import ModelRole | ||
| from agent_env.config.model_pricing import ReportedBy | ||
| from agent_env.task.usage.summary import Share, UsageSummary | ||
|
|
||
| _ROLE_NAMES = {role.value: role.record_key for role in ModelRole} | ||
| # The cost sources that aren't the price map, in the order they're named. | ||
| _REPORTED = {ReportedBy.PROVIDER: "the endpoint's bills", ReportedBy.HARNESS: "agents' own figures"} | ||
| _DOLLAR_PLACES = 4 | ||
| _SMALLEST_DOLLARS = 0.0001 # the least a cost shows at _DOLLAR_PLACES; a smaller one reads "under" it | ||
| _THOUSAND, _MILLION = 1_000, 1_000_000 | ||
| _TENTHS_OF_THOUSANDS_BELOW = 10 * _THOUSAND # a count under it shows one decimal: 1.2k, not 1k | ||
|
|
||
|
|
||
| def cost_line(summary: UsageSummary | None, label: str = "cost", attempts: bool = True) -> str | None: | ||
| """``cost: $0.0532 (solver gpt-4.1-mini $0.0412, 115k in, 1.2k out; judge ...)``, each cost as ``_cost`` gives it | ||
| and, with ``attempts``, the attempts named when there were more than one; None for no usage.""" | ||
| if summary is None: | ||
| return None | ||
| parts = [f"{summary.attempts} attempts"] if attempts and summary.attempts > 1 else [] | ||
| return f"{label}: {_cost(summary)} ({'; '.join([*parts, *map(_share, summary.shares)])})" | ||
|
|
||
|
|
||
| def sources_line(summary: UsageSummary | None) -> str | None: | ||
| """Where ``summary``'s costs came from, as ``Costs priced from LiteLLM's price map, litellm@1.80.0.``; None when | ||
| nothing was priced.""" | ||
| sources = summary.sources if summary is not None else () | ||
| if not sources: | ||
| return None | ||
| named = [f"LiteLLM's price map, {source}" for source in sources if source.startswith("litellm@")] | ||
| named += [name for source, name in _REPORTED.items() if source in sources] | ||
| named += [source for source in sources if not source.startswith("litellm@") and source not in _REPORTED] | ||
| return f"Costs priced from {'; '.join(named)}." | ||
|
|
||
|
|
||
| def _share(share: Share) -> str: | ||
| words = [f"{_ROLE_NAMES.get(share.role, share.role)} {share.model or 'an unnamed model'} {_cost(share)}"] | ||
| if share.input_tokens is not None: | ||
| words.append(f"{_count(share.input_tokens)} in") | ||
| if share.output_tokens is not None: | ||
| words.append(f"{_count(share.output_tokens)} out") | ||
| if share.partial: | ||
| words.append("partial") | ||
| return ", ".join(words) | ||
|
|
||
|
|
||
| def _cost(of: UsageSummary | Share) -> str: | ||
| """``of``'s cost with what it is: ``at least`` when some usage went unpriced or uncounted, ``at most`` when some | ||
| was priced on an assumed rate, ``estimated`` when both, and ``unpriced`` when nothing was priced.""" | ||
| if of.cost_usd is None: | ||
| return "unpriced" | ||
| lower, upper = of.lower_bound, of.upper_bound | ||
| word = "estimated " if lower and upper else "at least " if lower else "at most " if upper else "" | ||
| return f"{word}{_dollars(of.cost_usd)}" | ||
|
|
||
|
|
||
| def _dollars(usd: float) -> str: | ||
| if usd == 0: | ||
| return "$0" | ||
| if usd < _SMALLEST_DOLLARS: | ||
| return f"under ${_SMALLEST_DOLLARS:.{_DOLLAR_PLACES}f}" | ||
| return f"${usd:.{_DOLLAR_PLACES}f}" | ||
|
|
||
|
|
||
| def _count(n: int) -> str: | ||
| if n < _THOUSAND: | ||
| return str(n) | ||
| if n < _TENTHS_OF_THOUSANDS_BELOW: | ||
| return f"{n / _THOUSAND:.1f}k" | ||
| if n < _MILLION: | ||
| return f"{round(n / _THOUSAND)}k" | ||
| return f"{n / _MILLION:.1f}M" |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.