Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions crates/core/src/observability/openinference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,10 @@ fn end_attributes(event: &Event) -> Vec<KeyValue> {
if is_llm {
push_llm_usage_attributes(&mut attributes, usage.as_ref());
}
if is_llm && let Some(cost_total) = cost_total_from_llm_event(event, fallback_usage.as_ref()) {
if is_llm
&& let Some(cost_total) =
cost_total_from_llm_event(event, normalized.as_deref(), fallback_usage.as_ref())
{
attributes.push(KeyValue::new(oi::llm::cost::TOTAL, cost_total));
}
if is_llm {
Expand Down Expand Up @@ -1174,14 +1177,18 @@ fn finish_reason_value(reason: &FinishReason) -> String {
}
}

fn cost_total_from_llm_event(event: &Event, fallback_usage: Option<&Usage>) -> Option<f64> {
fn cost_total_from_llm_event(
event: &Event,
normalized_response: Option<&AnnotatedLlmResponse>,
fallback_usage: Option<&Usage>,
) -> Option<f64> {
if let Some(cost) =
manual::cost_from_manual_llm_output(event.output(), true).map(|(total, _)| total)
{
return Some(cost);
}

if let Some(response) = event.annotated_response()
if let Some(response) = normalized_response
&& let Some(usage) = response.usage.as_ref()
{
if let Some(cost) = usage.cost.as_ref() {
Expand Down
19 changes: 10 additions & 9 deletions crates/core/src/observability/otel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -716,15 +716,16 @@ fn cost_from_llm_event(event: &Event) -> Option<(f64, String)> {
if let Some(cost) = manual::cost_from_manual_llm_output(event.output(), false) {
return Some(cost);
}
if let Some(response) = event.annotated_response()
&& let Some(usage) = response.usage.as_ref()
{
if let Some(cost) = usage.cost.as_ref() {
return cost_total_and_currency(cost);
}
if let Some(model_name) = response.model.as_deref().or_else(|| event.model_name()) {
return estimate_cost_for_provider(Some(event.name()), model_name, usage)
.and_then(|cost| cost_total_and_currency(&cost));
if let Some(response) = event.normalized_llm_response() {
let response = response.as_ref();
if let Some(usage) = response.usage.as_ref() {
if let Some(cost) = usage.cost.as_ref() {
return cost_total_and_currency(cost);
}
if let Some(model_name) = response.model.as_deref().or_else(|| event.model_name()) {
return estimate_cost_for_provider(Some(event.name()), model_name, usage)
.and_then(|cost| cost_total_and_currency(&cost));
Comment thread
mnajafian-nv marked this conversation as resolved.
Outdated
}
}
}
let usage = manual::usage_from_manual_llm_output(event.output())?;
Expand Down
101 changes: 101 additions & 0 deletions crates/core/tests/unit/observability/openinference_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,65 @@ fn install_test_pricing(model_id: &str) {
set_active_pricing_resolver(PricingResolver::from_catalogs(vec![catalog])).unwrap();
}

fn install_openai_disambiguation_pricing(model_id: &str) {
let catalog = PricingCatalog::from_json_str(
&json!({
"version": 1,
"entries": [
{
"provider": "other",
"model_id": model_id,
"pricing_as_of": "2026-06-05",
"pricing_source": "test",
"rates": {
"input_per_million": 1000.0,
"output_per_million": 1000.0
},
"prompt_cache": {
"read_accounting": "included_in_prompt_tokens"
}
},
{
"provider": "openai",
"model_id": model_id,
"pricing_as_of": "2026-06-05",
"pricing_source": "test",
"rates": {
"input_per_million": 0.15,
"output_per_million": 0.60,
"cache_read_per_million": 0.075
},
"prompt_cache": {
"read_accounting": "included_in_prompt_tokens"
}
}
]
})
.to_string(),
)
.unwrap();
set_active_pricing_resolver(PricingResolver::from_catalogs(vec![catalog])).unwrap();
}

fn openai_chat_provider_response(model_id: &str) -> Json {
json!({
"id": "chatcmpl-test",
"object": "chat.completion",
"model": model_id,
"choices": [{
"index": 0,
"message": {"role": "assistant", "content": "hello"},
"finish_reason": "stop"
}],
"usage": {
"prompt_tokens": 1_000,
"completion_tokens": 500,
"total_tokens": 1_500,
"prompt_tokens_details": {"cached_tokens": 200}
}
})
}

fn sample_openinference_annotated_request() -> AnnotatedLlmRequest {
AnnotatedLlmRequest {
messages: vec![
Expand Down Expand Up @@ -2655,6 +2714,48 @@ fn llm_end_with_known_model_usage_emits_derived_cost_attribute() {
);
}

#[test]
fn llm_end_with_unannotated_openai_response_uses_codec_cost_attribute() {
let _pricing_guard = pricing_test_mutex().lock().unwrap();
install_openai_disambiguation_pricing("priced-model");
let _reset_guard = ResetPricingResolverGuard;

let event = make_end_event(
Uuid::now_v7(),
None,
"other",
ScopeType::Llm,
Some(openai_chat_provider_response("priced-model")),
);

assert!(event.annotated_response().is_none());
assert!(event.normalized_llm_response().is_some());

let attributes = attr_map(&end_attributes(&event));
assert_eq!(
attributes.get("llm.cost.total"),
Some(&"0.000435".to_string())
);
}

#[test]
fn llm_end_with_unannotated_openai_response_without_usage_omits_cost_attribute() {
let _pricing_guard = pricing_test_mutex().lock().unwrap();
reset_active_pricing_resolver().unwrap();
let _reset_guard = ResetPricingResolverGuard;

let mut output = openai_chat_provider_response("priced-model");
output.as_object_mut().unwrap().remove("usage");
let event = make_end_event(Uuid::now_v7(), None, "openai", ScopeType::Llm, Some(output));

assert!(event.annotated_response().is_none());
let normalized = event.normalized_llm_response().unwrap();
assert!(normalized.usage.is_none());

let attributes = attr_map(&end_attributes(&event));
assert!(!attributes.contains_key("llm.cost.total"));
}

#[test]
fn llm_end_with_manual_usage_and_output_model_emits_derived_cost_attribute() {
let _pricing_guard = pricing_test_mutex().lock().unwrap();
Expand Down
76 changes: 75 additions & 1 deletion crates/core/tests/unit/observability/otel_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ fn install_test_pricing(model_id: &str) {
}

fn install_provider_disambiguation_pricing(model_id: &str) {
install_disambiguation_pricing(model_id, "test");
}

fn install_openai_disambiguation_pricing(model_id: &str) {
install_disambiguation_pricing(model_id, "openai");
}

fn install_disambiguation_pricing(model_id: &str, preferred_provider: &str) {
let catalog = PricingCatalog::from_json_str(
&json!({
"version": 1,
Expand All @@ -96,7 +104,7 @@ fn install_provider_disambiguation_pricing(model_id: &str) {
}
},
{
"provider": "test",
"provider": preferred_provider,
"model_id": model_id,
"pricing_as_of": "2026-06-05",
"pricing_source": "test",
Expand All @@ -117,6 +125,25 @@ fn install_provider_disambiguation_pricing(model_id: &str) {
set_active_pricing_resolver(PricingResolver::from_catalogs(vec![catalog])).unwrap();
}

fn openai_chat_provider_response(model_id: &str) -> Json {
json!({
"id": "chatcmpl-test",
"object": "chat.completion",
"model": model_id,
"choices": [{
"index": 0,
"message": {"role": "assistant", "content": "hello"},
"finish_reason": "stop"
}],
"usage": {
"prompt_tokens": 1_000,
"completion_tokens": 500,
"total_tokens": 1_500,
"prompt_tokens_details": {"cached_tokens": 200}
}
})
}

fn reset_global() {
crate::shared_runtime::reset_runtime_owner_for_tests();
let context = global_context();
Expand Down Expand Up @@ -916,6 +943,53 @@ fn pre_epoch_timestamps_round_trip_through_system_time() {
);
}

#[test]
fn llm_end_with_unannotated_openai_response_uses_codec_cost() {
let _pricing_guard = pricing_test_mutex().lock().unwrap();
install_openai_disambiguation_pricing("priced-model");
let _reset_guard = ResetPricingResolverGuard;

let event = make_end_event(
Uuid::now_v7(),
None,
"other",
ScopeType::Llm,
Some(openai_chat_provider_response("priced-model")),
);

assert!(event.annotated_response().is_none());
assert!(event.normalized_llm_response().is_some());

let attributes = attr_map(&end_attributes(&event));
assert_eq!(
attributes.get("nemo_relay.llm.cost.total"),
Some(&"0.000435".to_string())
);
assert_eq!(
attributes.get("nemo_relay.llm.cost.currency"),
Some(&"USD".to_string())
);
}

#[test]
fn llm_end_with_unannotated_openai_response_without_usage_omits_cost() {
let _pricing_guard = pricing_test_mutex().lock().unwrap();
reset_active_pricing_resolver().unwrap();
let _reset_guard = ResetPricingResolverGuard;

let mut output = openai_chat_provider_response("priced-model");
output.as_object_mut().unwrap().remove("usage");
let event = make_end_event(Uuid::now_v7(), None, "openai", ScopeType::Llm, Some(output));

assert!(event.annotated_response().is_none());
let normalized = event.normalized_llm_response().unwrap();
assert!(normalized.usage.is_none());

let attributes = attr_map(&end_attributes(&event));
assert!(!attributes.contains_key("nemo_relay.llm.cost.total"));
assert!(!attributes.contains_key("nemo_relay.llm.cost.currency"));
}

#[test]
fn helper_functions_cover_additional_otel_branches() {
let function_end = make_end_event(Uuid::now_v7(), None, "fn-scope", ScopeType::Function, None);
Expand Down
Loading