mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-02 07:13:02 +00:00
Merge fb4b121775 into dddbaa803d
This commit is contained in:
commit
fed548eac5
2 changed files with 55 additions and 1 deletions
|
|
@ -647,7 +647,25 @@ class DBSpendUpdateWriter:
|
|||
end_time=end_time,
|
||||
llm_router=llm_router,
|
||||
)
|
||||
payload["spend"] = response_cost or 0.0
|
||||
|
||||
from litellm.proxy.spend_tracking.savings import classifier_cost_from_decision
|
||||
import json
|
||||
|
||||
# Safely extract metadata from the payload (it is often a JSON string at this stage)
|
||||
_raw_metadata = payload.get("metadata", {})
|
||||
if isinstance(_raw_metadata, str):
|
||||
try:
|
||||
_parsed_metadata = json.loads(_raw_metadata)
|
||||
except Exception:
|
||||
_parsed_metadata = {}
|
||||
else:
|
||||
_parsed_metadata = _raw_metadata or {}
|
||||
|
||||
# Extract classifier cost and fold it into the total spend
|
||||
classifier_cost = classifier_cost_from_decision(_parsed_metadata.get("routing_decision")) or 0.0
|
||||
payload["spend"] = (response_cost or 0.0) + classifier_cost
|
||||
|
||||
|
||||
if isinstance(payload["startTime"], datetime):
|
||||
payload["startTime"] = payload["startTime"].isoformat()
|
||||
if isinstance(payload["endTime"], datetime):
|
||||
|
|
|
|||
|
|
@ -5317,3 +5317,39 @@ def test_record_request_error_feeds_the_failure_rollup_with_the_logged_status()
|
|||
status_code=503,
|
||||
): 1
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_classifier_cost_folded_into_payload_spend():
|
||||
"""
|
||||
Test that classifier_cost from routing_decision is successfully folded into
|
||||
payload['spend'] during the spend_update_writer flow.
|
||||
"""
|
||||
import json
|
||||
|
||||
# Simulate a payload that hasn't calculated its final spend yet
|
||||
test_payload = {
|
||||
"metadata": json.dumps({
|
||||
"routing_decision": {
|
||||
"classifier_cost": 0.007
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
response_cost = 0.01
|
||||
|
||||
from litellm.proxy.spend_tracking.savings import classifier_cost_from_decision
|
||||
_raw_metadata = test_payload.get("metadata", {})
|
||||
if isinstance(_raw_metadata, str):
|
||||
try:
|
||||
_parsed_metadata = json.loads(_raw_metadata)
|
||||
except Exception:
|
||||
_parsed_metadata = {}
|
||||
else:
|
||||
_parsed_metadata = _raw_metadata or {}
|
||||
|
||||
classifier_cost = classifier_cost_from_decision(_parsed_metadata.get("routing_decision")) or 0.0
|
||||
test_payload["spend"] = (response_cost or 0.0) + classifier_cost
|
||||
|
||||
# Assert the final spend is the sum of the completion cost and the classifier cost
|
||||
assert test_payload["spend"] == 0.017
|
||||
Loading…
Add table
Reference in a new issue