feat: add metric for per-turn tool count and add tmp_mem flag (#13456)

This commit is contained in:
jif-oai
2026-03-04 11:25:58 +00:00
committed by GitHub
parent bda3c49dc4
commit e07eaff0d3
3 changed files with 31 additions and 5 deletions

View File

@@ -74,6 +74,7 @@ pub(crate) struct TurnState {
pending_user_input: HashMap<String, oneshot::Sender<RequestUserInputResponse>>,
pending_dynamic_tools: HashMap<String, oneshot::Sender<DynamicToolResponse>>,
pending_input: Vec<ResponseInputItem>,
pub(crate) tool_calls: u64,
pub(crate) token_usage_at_turn_start: TokenUsage,
}

View File

@@ -38,6 +38,7 @@ use codex_protocol::models::ResponseItem;
use codex_protocol::protocol::RolloutItem;
use codex_protocol::user_input::UserInput;
use crate::features::Feature;
pub(crate) use compact::CompactTask;
pub(crate) use ghost_snapshot::GhostSnapshotTask;
pub(crate) use regular::RegularTask;
@@ -200,11 +201,13 @@ impl Session {
let mut pending_input = Vec::<ResponseInputItem>::new();
let mut should_clear_active_turn = false;
let mut token_usage_at_turn_start = None;
let mut turn_tool_calls = 0_u64;
if let Some(at) = active.as_mut()
&& at.remove_task(&turn_context.sub_id)
{
let mut ts = at.turn_state.lock().await;
pending_input = ts.take_pending_input();
turn_tool_calls = ts.tool_calls;
token_usage_at_turn_start = Some(ts.token_usage_at_turn_start.clone());
should_clear_active_turn = true;
}
@@ -239,6 +242,20 @@ impl Session {
}
// Emit token usage metrics.
if let Some(token_usage_at_turn_start) = token_usage_at_turn_start {
// TODO(jif): drop this
let tmp_mem = (
"tmp_mem_enabled",
if self.enabled(Feature::MemoryTool) {
"true"
} else {
"false"
},
);
self.services.otel_manager.histogram(
"codex.turn.tool.call",
i64::try_from(turn_tool_calls).unwrap_or(i64::MAX),
&[tmp_mem],
);
let total_token_usage = self.total_token_usage().await.unwrap_or_default();
let turn_token_usage = crate::protocol::TokenUsage {
input_tokens: (total_token_usage.input_tokens
@@ -260,27 +277,27 @@ impl Session {
self.services.otel_manager.histogram(
"codex.turn.token_usage",
turn_token_usage.total_tokens,
&[("token_type", "total")],
&[("token_type", "total"), tmp_mem],
);
self.services.otel_manager.histogram(
"codex.turn.token_usage",
turn_token_usage.input_tokens,
&[("token_type", "input")],
&[("token_type", "input"), tmp_mem],
);
self.services.otel_manager.histogram(
"codex.turn.token_usage",
turn_token_usage.cached_input(),
&[("token_type", "cached_input")],
&[("token_type", "cached_input"), tmp_mem],
);
self.services.otel_manager.histogram(
"codex.turn.token_usage",
turn_token_usage.output_tokens,
&[("token_type", "output")],
&[("token_type", "output"), tmp_mem],
);
self.services.otel_manager.histogram(
"codex.turn.token_usage",
turn_token_usage.reasoning_output_tokens,
&[("token_type", "reasoning_output")],
&[("token_type", "reasoning_output"), tmp_mem],
);
}
let event = EventMsg::TurnComplete(TurnCompleteEvent {

View File

@@ -118,6 +118,14 @@ impl ToolRegistry {
let mcp_server_ref = mcp_server.as_deref();
let mcp_server_origin_ref = mcp_server_origin.as_deref();
{
let mut active = invocation.session.active_turn.lock().await;
if let Some(active_turn) = active.as_mut() {
let mut turn_state = active_turn.turn_state.lock().await;
turn_state.tool_calls = turn_state.tool_calls.saturating_add(1);
}
}
let handler = match self.handler(tool_name.as_ref()) {
Some(handler) => handler,
None => {