Skip to content

Commit 1d681bf

Browse files
committed
gracefully handle failure if cost not available for model
1 parent 52178b3 commit 1d681bf

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

eval_protocol/pytest/utils.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
import statistics
3434

3535

36+
logger = logging.getLogger(__name__)
37+
3638
AggregationMethod = Literal["mean", "max", "min", "bootstrap"]
3739

3840

@@ -495,10 +497,18 @@ def add_cost_metrics(row: EvaluationRow) -> None:
495497
input_tokens = usage.prompt_tokens or 0
496498
output_tokens = usage.completion_tokens or 0
497499

498-
input_cost, output_cost = cost_per_token(
499-
model=model_id, prompt_tokens=input_tokens, completion_tokens=output_tokens
500-
)
501-
total_cost = input_cost + output_cost
500+
# Try to calculate costs, but gracefully handle unknown models
501+
try:
502+
input_cost, output_cost = cost_per_token(
503+
model=model_id, prompt_tokens=input_tokens, completion_tokens=output_tokens
504+
)
505+
total_cost = input_cost + output_cost
506+
except Exception as e:
507+
# Model not in LiteLLM's database - set costs to 0 and continue
508+
logger.debug(f"Could not calculate cost for model '{model_id}': {e}")
509+
input_cost = 0.0
510+
output_cost = 0.0
511+
total_cost = 0.0
502512

503513
# Set all cost metrics on the row
504514
row.execution_metadata.cost_metrics = CostMetrics(

0 commit comments

Comments
 (0)