-
Notifications
You must be signed in to change notification settings - Fork 1
fix(cost_router): record unavailable race-loser usage instead of dropping it #955
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
6 commits
Select commit
Hold shift + click to select a range
d8f713a
fix(cost_router): record unavailable race-loser usage instead of drop…
claude b7f1946
Merge remote-tracking branch 'origin/main' into fix/race-endpoint-usa…
seonghobae d51ad6b
fix(cost_router): propagate unavailable status into aggregate cost
claude 70c96ab
fix(cost): suppress incomplete currency components
seonghobae de6c995
fix(cost): propagate unavailable usage through ledger surfaces
seonghobae d90c1cd
Merge branch 'main' into fix/race-endpoint-usage-unavailable-record
seonghobae 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -190,23 +190,42 @@ def _record_race_endpoint_usage(self, endpoint_id: str, value: Any) -> None: | |
| usage = value[2] | ||
| elif isinstance(value, dict): | ||
| usage = value.get("usage") | ||
| counts = self._provider_usage(usage) | ||
| if counts is None: | ||
| return | ||
| agent = next( | ||
| (item for item in self.orchestrator.candidates if item.id == endpoint_id), | ||
| None, | ||
| ) | ||
| if agent is None: # pragma: no cover - endpoint came from the current pool | ||
| return | ||
| counts = self._provider_usage(usage) | ||
| provider_model = self._agent_provider_model(agent, context["model_name"]) | ||
| if counts is None: | ||
| # The provider call genuinely completed and is billable, but its | ||
| # usage payload could not be parsed. Record an honest | ||
| # "unavailable" row rather than silently dropping this spend — | ||
| # mirrors record_stream_usage's measurement_status="unavailable" | ||
| # fallback for the same "call happened, can't measure it" case. | ||
| provider, model = provider_model | ||
| record = self.ledger.record_usage( | ||
| provider=provider, | ||
| model=model, | ||
| prompt_tokens=0, | ||
| completion_tokens=0, | ||
| request_channel="sync", | ||
| route_mode=context["route_mode"], | ||
| workflow_run_id=context["workflow_run_id"], | ||
| attribution=context["attribution"], | ||
| measurement_status="unavailable", | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| ) | ||
| context["records"].append(record) | ||
| return | ||
| record = self._record_completion( | ||
| messages=[], | ||
| answer="", | ||
| route_mode=context["route_mode"], | ||
| request_channel="sync", | ||
| attribution=context["attribution"], | ||
| model_name=context["model_name"], | ||
| provider_model=self._agent_provider_model(agent, context["model_name"]), | ||
| provider_model=provider_model, | ||
| workflow_run_id=context["workflow_run_id"], | ||
| prompt_tokens=counts[0], | ||
| completion_tokens=counts[1], | ||
|
|
@@ -374,20 +393,27 @@ def complete( | |
| provider_response["usage_record_ids"] = [ | ||
| record.usage_record_id for record in records | ||
| ] | ||
| # "unavailable" (a billable race-loser call whose usage payload | ||
| # could not be parsed, see _record_race_endpoint_usage) outranks | ||
| # "estimated": a completion combining a measured winner with an | ||
| # unavailable loser must not present a confident-looking summed | ||
| # total, the same honesty precedence record_stream_usage uses. | ||
| statuses = {record.measurement_status for record in records} | ||
| aggregate_measurement_status = ( | ||
| "unavailable" if "unavailable" in statuses | ||
| else "estimated" if "estimated" in statuses | ||
| else "measured" | ||
|
Comment on lines
+401
to
+405
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔴 Late losers leave costs measured When a loser finishes after winner publication, Prompt for agentsWas this helpful? React with 👍 or 👎 to provide feedback. |
||
| ) | ||
| provider_response["cost"] = { | ||
| "cost_amount": ( | ||
| round(sum(record.cost_amount for record in records), 6) | ||
| if len(currencies) == 1 | ||
| if len(currencies) == 1 and aggregate_measurement_status != "unavailable" | ||
|
seonghobae marked this conversation as resolved.
|
||
| else None | ||
| ), | ||
| "currency_code": next(iter(currencies)) if len(currencies) == 1 else "MIXED", | ||
| "measurement_status": ( | ||
| "estimated" | ||
| if any(record.measurement_status == "estimated" for record in records) | ||
| else "measured" | ||
| ), | ||
| "measurement_status": aggregate_measurement_status, | ||
|
seonghobae marked this conversation as resolved.
|
||
| } | ||
| if len(currencies) > 1: | ||
| if len(currencies) > 1 and aggregate_measurement_status != "unavailable": | ||
| provider_response["cost"]["currency_components"] = [ | ||
| { | ||
| "currency_code": currency, | ||
|
|
@@ -506,20 +532,25 @@ def complete( | |
| "total_tokens": sum(item.total_tokens for item in client_usage_records), | ||
| } | ||
| currencies = {item.currency_code for item in records} | ||
| # Same honesty precedence as the provider_request path above and | ||
| # record_stream_usage: a billable race-loser call recorded | ||
| # "unavailable" must not be summed into a confident-looking total. | ||
| statuses = {item.measurement_status for item in records} | ||
| aggregate_measurement_status = ( | ||
| "unavailable" if "unavailable" in statuses | ||
| else "estimated" if "estimated" in statuses | ||
| else "measured" | ||
| ) | ||
| result["cost"] = { | ||
| "cost_amount": ( | ||
| round(sum(item.cost_amount for item in records), 6) | ||
| if len(currencies) == 1 | ||
| if len(currencies) == 1 and aggregate_measurement_status != "unavailable" | ||
| else None | ||
| ), | ||
| "currency_code": next(iter(currencies)) if len(currencies) == 1 else "MIXED", | ||
| "measurement_status": ( | ||
| "estimated" | ||
| if any(item.measurement_status == "estimated" for item in records) | ||
| else "measured" | ||
| ), | ||
| "measurement_status": aggregate_measurement_status, | ||
|
seonghobae marked this conversation as resolved.
|
||
| } | ||
| if len(currencies) > 1: | ||
| if len(currencies) > 1 and aggregate_measurement_status != "unavailable": | ||
| result["cost"]["currency_components"] = [ | ||
| { | ||
| "currency_code": currency, | ||
|
|
||
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
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.