Repository navigation
fix(autorouter): compare historical and new savings consistently #43348
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
+484
−138
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
43b611d
fix(autorouter): compare historical and new savings consistently
tin-berri 2f3f36a
fix(autorouter): reject comparisons if request counts changed
tin-berri 234cb06
fix(router): restore eligible LLM and classification breakdown
tin-berri 5a40335
fix(router): avoid ambiguous baseline labels for partial comparisons
tin-berri 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| from collections.abc import Mapping | ||
| from contextlib import AbstractAsyncContextManager | ||
| from datetime import timedelta | ||
| from math import isclose | ||
| from types import MappingProxyType | ||
| from typing import TYPE_CHECKING, Final, Protocol, cast | ||
|
|
||
| from pydantic import BaseModel, ConfigDict, TypeAdapter | ||
|
|
||
| from litellm._logging import verbose_proxy_logger | ||
| from litellm.constants import MAX_SPENDLOG_ROWS_TO_QUERY | ||
| from litellm.proxy.db.autorouter_session_rollup import AUTOROUTER_SESSION_WINDOW_SQL | ||
| from litellm.proxy.db.create_views import SupportsRawQueries | ||
|
|
||
| if TYPE_CHECKING: | ||
| from litellm.proxy.utils import PrismaClient | ||
|
|
||
|
|
||
| class SessionSavingsComparison(BaseModel): | ||
| model_config = ConfigDict(frozen=True, allow_inf_nan=False) | ||
|
|
||
| router_name: str | ||
| router_type: str | ||
| turns: int | ||
| estimated_turns: int | ||
| actual_spend: float | ||
| classifier_cost: float | None | ||
| saved_spend: float | ||
| complete: bool | ||
|
|
||
| def coverage_fields(self, recorded_savings: float, recorded_turns: int) -> Mapping[str, float | int]: | ||
| if self.turns != recorded_turns or not self.complete: | ||
| return MappingProxyType({}) | ||
| if not isclose(self.saved_spend, recorded_savings, rel_tol=1e-9, abs_tol=1e-9): | ||
| return MappingProxyType({}) | ||
| return MappingProxyType( | ||
| { | ||
| "savings_estimated_turns": self.estimated_turns, | ||
| "savings_estimated_actual_spend": self.actual_spend, | ||
| "savings_estimated_saved_spend": self.saved_spend, | ||
| } | ||
| ) | ||
|
|
||
|
|
||
| class _ReadTransactions(Protocol): | ||
| def tx(self, *, timeout: timedelta, max_wait: timedelta) -> AbstractAsyncContextManager[SupportsRawQueries]: ... | ||
|
|
||
|
|
||
| _COMPARISONS: Final = TypeAdapter(tuple[SessionSavingsComparison, ...]) | ||
|
|
||
|
|
||
| async def historical_session_comparisons( | ||
| prisma_client: "PrismaClient", | ||
| start_date: str, | ||
| end_date: str, | ||
| api_key: str | None, | ||
| user_id: str | None, | ||
| session_id: str | None = None, | ||
| ) -> Mapping[tuple[str, str], SessionSavingsComparison]: | ||
| try: | ||
| reader: Final = cast(_ReadTransactions, prisma_client.read_db) # cast-ok: untyped Prisma transaction delegate | ||
| async with reader.tx(timeout=timedelta(seconds=3), max_wait=timedelta(seconds=1)) as transaction: | ||
| await transaction.execute_raw("SET TRANSACTION READ ONLY") | ||
| await transaction.execute_raw("SET LOCAL statement_timeout = 2000") | ||
| rows: Final = await transaction.query_raw( | ||
| HISTORICAL_SESSION_COMPARISONS_SQL, | ||
| start_date, | ||
| end_date, | ||
| api_key, | ||
| user_id, | ||
| session_id, | ||
| ) | ||
| comparisons: Final = _COMPARISONS.validate_python(rows or ()) | ||
| return MappingProxyType({(row.router_name, row.router_type): row for row in comparisons}) | ||
| except Exception: # noqa: BLE001 # missing retained logs must not discard recorded dollar savings | ||
| verbose_proxy_logger.warning("Historical auto-router cost comparison unavailable; preserving recorded savings") | ||
| return MappingProxyType({}) | ||
|
|
||
|
|
||
| HISTORICAL_SESSION_COMPARISONS_SQL: Final = f""" | ||
| WITH {AUTOROUTER_SESSION_WINDOW_SQL}, scoped AS MATERIALIZED ( | ||
| SELECT * FROM windowed WHERE $5::text IS NULL OR session_id = $5::text | ||
| ), limited_logs AS MATERIALIZED ( | ||
| SELECT session.api_key, session.session_id, session.router_name, session.router_type, session.comparison_user_id, | ||
| session.classifier_cost_recorded_turns = session.turns AS classifier_cost_tracked, | ||
| logs.spend, logs.prompt_tokens + logs.completion_tokens AS tokens, | ||
| logs.metadata::jsonb -> 'routing_decision' AS decision, | ||
| logs.metadata::jsonb -> 'autorouter_savings' AS savings, | ||
| logs.metadata::jsonb -> 'autorouter_savings_estimate' AS estimate | ||
| FROM scoped AS session JOIN "LiteLLM_SpendLogs" AS logs | ||
| ON logs.api_key = session.api_key | ||
| AND CASE WHEN char_length(logs.session_id) > 256 | ||
| THEN 'sha256:' || encode(sha256(convert_to(logs.session_id, 'UTF8')), 'hex') | ||
| ELSE logs.session_id END = session.session_id | ||
| AND (session.comparison_user_id IS NULL OR logs."user" = session.comparison_user_id) | ||
| AND logs."startTime" BETWEEN session.first_turn_at AND session.last_turn_at | ||
| AND COALESCE(logs.metadata::jsonb #>> '{{routing_decision,router_model_name}}', logs.model_group) | ||
| = session.router_name | ||
| WHERE session.savings_estimated_turns < session.turns | ||
| AND logs.status = 'success' AND COALESCE(logs.metadata::jsonb ->> 'internal_call_origin', '') = '' | ||
| LIMIT {MAX_SPENDLOG_ROWS_TO_QUERY + 1} | ||
| ), facts AS ( | ||
| SELECT *, | ||
| CASE WHEN jsonb_typeof(decision -> 'classifier_cost') = 'number' | ||
| THEN (decision ->> 'classifier_cost')::float8 | ||
| WHEN classifier_cost_tracked THEN 0 END AS classifier, | ||
| CASE WHEN jsonb_typeof(savings) = 'number' AND ( | ||
| estimate IS NULL OR estimate = 'null'::jsonb OR ( | ||
| jsonb_typeof(estimate -> 'version') = 'number' AND estimate ->> 'version' IN ('1', '2', '3') | ||
| AND estimate ->> 'status' = 'estimated' | ||
| ) | ||
| ) THEN savings::text::float8 END AS saved | ||
| FROM limited_logs | ||
| ), compared AS ( | ||
| SELECT api_key, session_id, router_name, router_type, comparison_user_id, | ||
| COUNT(*) AS turns, SUM(spend + COALESCE(classifier, 0)) AS spend, SUM(tokens) AS total_tokens, | ||
| COUNT(saved) AS estimated_turns, | ||
| COALESCE(SUM(spend + COALESCE(classifier, 0)) FILTER (WHERE saved IS NOT NULL), 0)::float8 AS actual_spend, | ||
| CASE WHEN COUNT(saved) = COUNT(classifier) FILTER (WHERE saved IS NOT NULL) | ||
| THEN COALESCE(SUM(classifier) FILTER (WHERE saved IS NOT NULL), 0)::float8 | ||
| END AS estimated_classifier_cost, | ||
| COALESCE(SUM(saved), 0)::float8 AS saved_spend | ||
| FROM facts GROUP BY 1, 2, 3, 4, 5 | ||
| ), reconciled AS ( | ||
| SELECT session.*, logs.estimated_turns, logs.actual_spend, logs.estimated_classifier_cost, | ||
| COALESCE((SELECT COUNT(*) FROM limited_logs) <= {MAX_SPENDLOG_ROWS_TO_QUERY} | ||
| AND logs.turns = session.turns AND logs.total_tokens = session.total_tokens | ||
| AND ABS(logs.spend - session.spend) <= GREATEST(1e-9, ABS(session.spend) * 1e-9) | ||
| AND ABS(logs.saved_spend - session.saved_spend) <= GREATEST(1e-9, ABS(session.saved_spend) * 1e-9), FALSE | ||
| ) AS recovered | ||
| FROM scoped AS session LEFT JOIN compared AS logs | ||
| ON logs.api_key = session.api_key AND logs.session_id = session.session_id | ||
| AND logs.router_name = session.router_name AND logs.router_type = session.router_type | ||
| AND logs.comparison_user_id IS NOT DISTINCT FROM session.comparison_user_id | ||
| ) | ||
| SELECT router_name, router_type, | ||
| SUM(turns)::bigint AS turns, | ||
| SUM(CASE WHEN recovered THEN estimated_turns ELSE savings_estimated_turns END)::bigint AS estimated_turns, | ||
| SUM(CASE WHEN recovered THEN actual_spend ELSE savings_estimated_actual_spend END)::float8 AS actual_spend, | ||
| CASE WHEN BOOL_AND(CASE WHEN recovered THEN estimated_classifier_cost IS NOT NULL | ||
| ELSE savings_estimated_turns = turns AND classifier_cost_recorded_turns = turns END) | ||
| THEN SUM(CASE WHEN recovered THEN estimated_classifier_cost ELSE classifier_cost END)::float8 | ||
| END AS classifier_cost, | ||
| SUM(saved_spend)::float8 AS saved_spend, | ||
| BOOL_AND(recovered OR savings_estimated_turns = turns) AS complete | ||
| FROM reconciled GROUP BY router_name, router_type | ||
| """ |
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.