mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-18 09:45:23 +00:00
fix(cost): count search queries and resolve the search row like completion_cost
An empty query list bills zero queries instead of one, and a bare search model
reads the {provider}/search row, as completion_cost does
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
c7c1e4be91
commit
c31c4acf13
2 changed files with 59 additions and 0 deletions
|
|
@ -30,6 +30,7 @@ use crate::realtime_cost::{
|
|||
partition_results_by_service_tier,
|
||||
};
|
||||
use crate::responses_usage::ChatUsage;
|
||||
use crate::search_cost::search_provider_cost_per_query;
|
||||
use crate::speech_cost::count_characters;
|
||||
use crate::together_cost::together_pricing_model;
|
||||
use crate::tool_call_cost_tracking::{DefaultToolRates, ResponseKind as ToolResponseKind};
|
||||
|
|
@ -811,6 +812,25 @@ pub fn completion_cost_from_response(
|
|||
request.response_time_ms,
|
||||
),
|
||||
};
|
||||
if let CostCall::Search {
|
||||
number_of_queries,
|
||||
optional_params,
|
||||
} = call
|
||||
{
|
||||
let search_model = match provider {
|
||||
Some(provider) if !provider.is_empty() && !model.contains('/') => {
|
||||
format!("{provider}/search")
|
||||
}
|
||||
_ => model.to_owned(),
|
||||
};
|
||||
return search_provider_cost_per_query(
|
||||
catalog,
|
||||
&search_model,
|
||||
provider,
|
||||
number_of_queries.unwrap_or(1),
|
||||
optional_params,
|
||||
);
|
||||
}
|
||||
cost_per_token_for_call(catalog, cost_request, call)
|
||||
},
|
||||
)?;
|
||||
|
|
|
|||
|
|
@ -935,6 +935,45 @@ fn search_response_uses_query_list_and_skips_tool_and_additional_charges() {
|
|||
assert!((result.cost.total - 0.0033).abs() < 1e-12);
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
#[case::empty_query_list_bills_nothing("exa_ai", "search", json!({"query": []}), 0.0)]
|
||||
#[case::single_query_string("exa_ai", "search", json!({"query": "a"}), 0.5)]
|
||||
#[case::bare_model_reads_the_provider_search_row("tavily", "tavily-search", json!({"query": ["a", "b"]}), 1.0)]
|
||||
#[case::prefixed_model_keeps_its_own_row("exa_ai", "exa_ai/search", json!({}), 0.5)]
|
||||
fn search_response_counts_queries_and_resolves_the_search_row_like_python(
|
||||
#[case] provider: &str,
|
||||
#[case] model: &str,
|
||||
#[case] optional_params: Value,
|
||||
#[case] expected: f64,
|
||||
) {
|
||||
let catalog = ModelInfoCatalog::new(HashMap::from([
|
||||
(
|
||||
"exa_ai/search".to_owned(),
|
||||
json!({"input_cost_per_query": 0.5}),
|
||||
),
|
||||
(
|
||||
"tavily/search".to_owned(),
|
||||
json!({"input_cost_per_query": 0.5}),
|
||||
),
|
||||
]));
|
||||
let response = json!({"model": model});
|
||||
let empty = json!({});
|
||||
let base = request(Some(&response), Some(model), Some(provider), &empty, &empty);
|
||||
let result = completion_cost_from_response(
|
||||
&catalog,
|
||||
CompletionResponseCostRequest {
|
||||
input: CompletionInputRequest {
|
||||
call_type: Some("search"),
|
||||
optional_params: Some(&optional_params),
|
||||
..base.input
|
||||
},
|
||||
..base
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
assert!((result.cost.total - expected).abs() < 1e-12);
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
#[case("vector_store_search", "vertex_ai/search_api", json!({}), 0.25)]
|
||||
#[case("avector_store_search", "vertex_ai/search_api", json!({}), 0.25)]
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue