[codex-analytics] add token usage metadata

This commit is contained in:
rhan-oai
2026-04-05 16:08:28 -07:00
parent 4570e5d894
commit 5faf7cfe4f
5 changed files with 132 additions and 2 deletions
@@ -1367,8 +1367,14 @@ pub(crate) async fn apply_bespoke_event_handling(
.await;
}
EventMsg::TokenCount(token_count_event) => {
handle_token_count_event(conversation_id, event_turn_id, token_count_event, &outgoing)
.await;
handle_token_count_event(
conversation_id,
event_turn_id,
Some(&analytics_events_client),
token_count_event,
&outgoing,
)
.await;
}
EventMsg::Error(ev) => {
thread_watch_manager
@@ -2217,6 +2223,7 @@ async fn handle_thread_rollback_failed(
async fn handle_token_count_event(
conversation_id: ThreadId,
turn_id: String,
analytics_events_client: Option<&AnalyticsEventsClient>,
token_count_event: TokenCountEvent,
outgoing: &ThreadScopedOutgoingMessageSender,
) {
@@ -2227,6 +2234,11 @@ async fn handle_token_count_event(
turn_id,
token_usage,
};
if let Some(analytics_events_client) = analytics_events_client {
analytics_events_client.track_notification(
ServerNotification::ThreadTokenUsageUpdated(notification.clone()),
);
}
outgoing
.send_server_notification(ServerNotification::ThreadTokenUsageUpdated(notification))
.await;
@@ -3582,6 +3594,7 @@ mod tests {
handle_token_count_event(
conversation_id,
turn_id.clone(),
/*analytics_events_client*/ None,
TokenCountEvent {
info: Some(info),
rate_limits: Some(rate_limits),
@@ -3636,6 +3649,7 @@ mod tests {
handle_token_count_event(
conversation_id,
turn_id.clone(),
/*analytics_events_client*/ None,
TokenCountEvent {
info: None,
rate_limits: None,
@@ -309,6 +309,11 @@ async fn turn_start_tracks_turn_event_analytics() -> Result<()> {
assert!(event["event_params"]["created_at"].as_u64().is_some());
assert!(event["event_params"]["completed_at"].as_u64().is_some());
assert!(event["event_params"]["duration_ms"].as_u64().is_some());
assert_eq!(event["event_params"]["input_tokens"], 0);
assert_eq!(event["event_params"]["cached_input_tokens"], 0);
assert_eq!(event["event_params"]["output_tokens"], 0);
assert_eq!(event["event_params"]["reasoning_output_tokens"], 0);
assert_eq!(event["event_params"]["total_tokens"], 0);
Ok(())
}