From 51f4b8af01db1fb04e0cc6235444573ecceda716 Mon Sep 17 00:00:00 2001 From: jfoo-oai Date: Fri, 20 Nov 2026 17:33:07 +0000 Subject: [PATCH] Include native USD in ChatGPT turn cost telemetry (#62810) ## Why When a turn incurs usage priced directly in USD, reporting only the credit-converted estimate understates its cost. ## What changed Sum `estimated_usage_usd_micros` and `native_usage_usd_micros` for settled turns. Default an omitted native USD field to zero for legacy responses. Suppress the total when either component is unavailable (including explicit null), either is negative, or their sum overflows. ## Testing Cover omitted and null native USD values, zero costs, negative amounts, and overflow in backend client tests. Verify that the combined amount reaches the OTLP exporter in the app-server integration test. GitOrigin-RevId: 8709e48329db813cd66baf270ee3f1bcfd87cb03 --- .../src/turn_cost_worker_chatgpt.rs | 5 +- .../tests/suite/v2/turn_cost_otel.rs | 3 +- .../src/client/chatgpt_turn_cost.rs | 22 ++++++++- .../src/client/chatgpt_turn_cost_tests.rs | 49 ++++++++++++++++++- 4 files changed, 72 insertions(+), 7 deletions(-) diff --git a/codex-rs/app-server/src/turn_cost_worker_chatgpt.rs b/codex-rs/app-server/src/turn_cost_worker_chatgpt.rs index b79fe31401..d728fee4ed 100644 --- a/codex-rs/app-server/src/turn_cost_worker_chatgpt.rs +++ b/codex-rs/app-server/src/turn_cost_worker_chatgpt.rs @@ -53,14 +53,13 @@ impl WorkerRuntime { continue; }; let (Some(micros), Some(settled_ids)) = - (cost.estimated_usage_usd_micros, cost.settled_response_ids) + (cost.visible_total_usd_micros(), cost.settled_response_ids) else { continue; }; // Missing/hidden dollars are not zero. A projection can appear before // all responses settle, so require every locally observed response. - if micros < 0 - || entry.expected_response_ids.is_empty() + if entry.expected_response_ids.is_empty() || !entry .expected_response_ids .iter() diff --git a/codex-rs/app-server/tests/suite/v2/turn_cost_otel.rs b/codex-rs/app-server/tests/suite/v2/turn_cost_otel.rs index f9a06f2e47..5f5c5f173e 100644 --- a/codex-rs/app-server/tests/suite/v2/turn_cost_otel.rs +++ b/codex-rs/app-server/tests/suite/v2/turn_cost_otel.rs @@ -64,7 +64,8 @@ async fn chatgpt_turn_cost_reaches_otlp_on_success() -> Result<()> { assert_eq!(body["include_settled_response_ids"], true); let threads: Vec<_> = body["threads"].as_array().expect("threads").iter().map(|thread| { let turns: Vec<_> = thread["turn_ids"].as_array().expect("turns").iter().map(|turn_id| json!({ - "turn_id": turn_id, "model": "mock-model", "estimated_usage_usd_micros": 1250001, + "turn_id": turn_id, "model": "mock-model", "estimated_usage_usd_micros": 250001, + "native_usage_usd_micros": 1000000, "settled_response_ids": ["resp-cost"] })).collect(); json!({"thread_id": thread["thread_id"], "turns": turns}) diff --git a/codex-rs/backend-client/src/client/chatgpt_turn_cost.rs b/codex-rs/backend-client/src/client/chatgpt_turn_cost.rs index ab3db6bb8c..82a5bc92a5 100644 --- a/codex-rs/backend-client/src/client/chatgpt_turn_cost.rs +++ b/codex-rs/backend-client/src/client/chatgpt_turn_cost.rs @@ -1,5 +1,5 @@ //! Queries SiWC per-turn estimates using the client's Codex or WHAM route and auth. -//! Always request settled response IDs and retain thread/turn association and missing amounts. +//! Always request settled response IDs and retain thread/turn association and unavailable amounts. use super::Client; use super::PathStyle; @@ -15,9 +15,29 @@ pub struct ChatgptTurnCost { pub turn_id: String, pub model: Option, pub estimated_usage_usd_micros: Option, + // Older backends report only converted credits. An explicit null means native USD is unavailable. + #[serde(default = "legacy_native_usd_micros")] + pub native_usage_usd_micros: Option, pub settled_response_ids: Option>, } +fn legacy_native_usd_micros() -> Option { + Some(0) +} + +impl ChatgptTurnCost { + /// Returns the complete visible estimate, including credit conversion and native USD. + pub fn visible_total_usd_micros(&self) -> Option { + // The backend also uses a missing converted estimate to hide dollar amounts. + let converted = self.estimated_usage_usd_micros?; + let native = self.native_usage_usd_micros?; + if converted < 0 || native < 0 { + return None; + } + converted.checked_add(native) + } +} + #[derive(Debug, Clone, PartialEq, Eq, Deserialize)] pub struct ChatgptThreadTurnCosts { pub thread_id: String, diff --git a/codex-rs/backend-client/src/client/chatgpt_turn_cost_tests.rs b/codex-rs/backend-client/src/client/chatgpt_turn_cost_tests.rs index e6c8fadb35..3ed481873c 100644 --- a/codex-rs/backend-client/src/client/chatgpt_turn_cost_tests.rs +++ b/codex-rs/backend-client/src/client/chatgpt_turn_cost_tests.rs @@ -45,11 +45,11 @@ async fn check_query(base_path: &str, endpoint: &str) { "threads": [ {"thread_id": "thread-a", "turns": [{ "turn_id": "turn-a", "model": "gpt-5.6", - "estimated_usage_usd_micros": 1250001, + "estimated_usage_usd_micros": 1250001, "native_usage_usd_micros": 750000, "settled_response_ids": ["resp-a", "resp-b"] }]}, {"thread_id": "thread-b", "turns": [ - {"turn_id": "turn-b", "estimated_usage_usd_micros": null, + {"turn_id": "turn-b", "estimated_usage_usd_micros": null, "native_usage_usd_micros": null, "settled_response_ids": null}, {"turn_id": "turn-c", "estimated_usage_usd_micros": 0} ]} @@ -82,6 +82,7 @@ async fn check_query(base_path: &str, endpoint: &str) { turn_id: "turn-a".to_string(), model: Some("gpt-5.6".to_string()), estimated_usage_usd_micros: Some(1250001), + native_usage_usd_micros: Some(750000), settled_response_ids: Some(vec!["resp-a".to_string(), "resp-b".to_string()]), }], }, @@ -92,12 +93,14 @@ async fn check_query(base_path: &str, endpoint: &str) { turn_id: "turn-b".to_string(), model: None, estimated_usage_usd_micros: None, + native_usage_usd_micros: None, settled_response_ids: None, }, ChatgptTurnCost { turn_id: "turn-c".to_string(), model: None, estimated_usage_usd_micros: Some(0), + native_usage_usd_micros: Some(0), settled_response_ids: None, }, ], @@ -106,3 +109,45 @@ async fn check_query(base_path: &str, endpoint: &str) { ); server.verify().await; } + +#[test] +fn visible_turn_cost_requires_both_dollar_components() { + for (converted, native, expected) in [ + (Some(0), Some(4_668), Some(4_668)), + (Some(120), Some(35), Some(155)), + (Some(120), Some(0), Some(120)), + (Some(0), Some(0), Some(0)), + (None, Some(35), None), + (Some(120), None, None), + (Some(-1), Some(35), None), + (Some(120), Some(-1), None), + (Some(i64::MAX), Some(1), None), + ] { + let cost: ChatgptTurnCost = serde_json::from_value(serde_json::json!({ + "turn_id": "turn-a", + "estimated_usage_usd_micros": converted, + "native_usage_usd_micros": native, + })) + .expect("valid turn cost response"); + assert_eq!( + cost.visible_total_usd_micros(), + expected, + "converted={converted:?}, native={native:?}" + ); + } +} + +#[test] +fn settled_turn_cost_distinguishes_omitted_native_usd_from_null() { + let mut turn = serde_json::json!({ + "turn_id": "turn-a", + "estimated_usage_usd_micros": 120, + "settled_response_ids": ["resp-a"], + }); + let legacy: ChatgptTurnCost = serde_json::from_value(turn.clone()).unwrap(); + turn["native_usage_usd_micros"] = serde_json::Value::Null; + let unavailable: ChatgptTurnCost = serde_json::from_value(turn).unwrap(); + + assert_eq!(legacy.visible_total_usd_micros(), Some(120)); + assert_eq!(unavailable.visible_total_usd_micros(), None); +}