Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
25 changes: 25 additions & 0 deletions crates/core/src/observability/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,31 @@ pub mod openinference;
pub mod otel;
pub mod plugin_component;

#[cfg(any(feature = "otel", feature = "openinference"))]
pub(crate) fn estimate_cost_for_response_or_requested_model(
event: &crate::api::event::Event,
response_model: Option<&str>,
usage: &crate::codec::response::Usage,
) -> Option<crate::codec::response::CostEstimate> {
// Prefer the provider-echoed model, but fall back to the requested model
// when pricing does not recognize the echoed model alias.
if let Some(model_name) = response_model
&& let Some(cost) = crate::codec::response::estimate_cost_for_provider(
Some(event.name()),
model_name,
usage,
)
{
return Some(cost);
}

let event_model = event.model_name()?;
if response_model == Some(event_model) {
return None;
}
crate::codec::response::estimate_cost_for_provider(Some(event.name()), event_model, usage)
}

#[cfg(any(feature = "otel", feature = "openinference"))]
pub(crate) fn set_span_status_from_event_metadata<S>(span: &mut S, event: &crate::api::event::Event)
where
Expand Down
22 changes: 15 additions & 7 deletions crates/core/src/observability/openinference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use std::collections::{HashMap, VecDeque};
use std::sync::{Arc, Mutex};
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use super::manual;
use super::{estimate_cost_for_response_or_requested_model, manual};
use crate::api::event::{Event, ScopeCategory};
use crate::api::runtime::EventSubscriberFn;
use crate::api::scope::ScopeType;
Expand Down 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,22 +1177,27 @@ 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() {
return cost.total_or_component_sum_for_currency("USD");
}
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_for_currency("USD"));
if let Some(cost) =
estimate_cost_for_response_or_requested_model(event, response.model.as_deref(), usage)
{
return cost.total_for_currency("USD");
}
}

Expand Down
24 changes: 14 additions & 10 deletions crates/core/src/observability/otel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use std::collections::{HashMap, VecDeque};
use std::sync::{Arc, Mutex};
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use super::manual;
use super::{estimate_cost_for_response_or_requested_model, manual};
use crate::api::event::Event;
use crate::api::event::ScopeCategory;
use crate::api::runtime::EventSubscriberFn;
Expand Down Expand Up @@ -716,15 +716,19 @@ 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(cost) = estimate_cost_for_response_or_requested_model(
event,
response.model.as_deref(),
usage,
) {
return cost_total_and_currency(&cost);
}
}
}
let usage = manual::usage_from_manual_llm_output(event.output())?;
Expand Down
132 changes: 132 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,79 @@ 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_unpriced_response_model_uses_requested_model_cost_attribute() {
let _pricing_guard = pricing_test_mutex().lock().unwrap();
install_openai_disambiguation_pricing("priced-model");
let _reset_guard = ResetPricingResolverGuard;

let event = make_scope_event_with_profile(
ScopeCategory::End,
Uuid::now_v7(),
None,
"openai",
ScopeType::Llm,
Some(openai_chat_provider_response("api-echoed-model")),
Some(
CategoryProfile::builder()
.model_name("priced-model")
.build(),
),
);

assert!(event.annotated_response().is_none());
let normalized = event.normalized_llm_response().unwrap();
assert_eq!(normalized.model.as_deref(), Some("api-echoed-model"));

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
Loading
Loading