agent_base/types/turn_context.rs
1//! Turn context — pure data struct passed to turn-end hooks.
2//!
3//! agent-base does not build, aggregate, or persist metrics. It only exposes
4//! raw turn data through the [`on_turn_end`](crate::AgentRuntime::on_turn_end)
5//! hook. Consumers (e.g. phi-telemetry) build their own metrics from this context.
6
7use crate::llm::UsageInfo;
8use crate::types::RunOutcome;
9
10/// Pure-data snapshot of a completed turn iteration, passed to turn-end hooks.
11///
12/// Contains all raw data a consumer needs to build turn-level metrics —
13/// agent-base itself performs no aggregation, no persistence, and no
14/// business-level interpretation of this data.
15#[derive(Clone, Debug)]
16pub struct TurnContext {
17 /// Numeric session identifier (agent-base internal).
18 pub session_id: u64,
19 /// 1-based turn number within the session.
20 pub turn_number: u32,
21 /// Time-to-first-token in milliseconds (user-perceived latency).
22 pub ttft_ms: u64,
23 /// LLM stream duration in milliseconds.
24 pub llm_duration_ms: u64,
25 /// Total wall-clock turn duration in milliseconds (llm + tool + overhead).
26 pub duration_ms: u64,
27 /// Total tool execution duration in milliseconds.
28 pub tool_duration_ms: u64,
29 /// Token usage from the LLM response, if available.
30 pub usage: Option<UsageInfo>,
31 /// Length of the full assistant text response in bytes.
32 pub full_text_len: u64,
33 /// Whether the response included thinking/reasoning content.
34 pub has_thinking: bool,
35 /// Byte length of reasoning/thinking content. Always available even when
36 /// the provider does not report reasoning token counts (e.g. mimo-v2.5-pro).
37 pub thinking_bytes: u64,
38 /// Tool names called in this turn iteration.
39 pub tools_used: Vec<String>,
40 /// Total number of tool calls made.
41 pub tool_call_count: u32,
42 /// Number of tools that succeeded.
43 pub tool_success: u32,
44 /// Number of tools that failed.
45 pub tool_failed: u32,
46 /// Outcome of this turn iteration (Completed / Failed / Cancelled / MaxTurnsExceeded).
47 pub outcome: RunOutcome,
48 /// Error message if the turn errored.
49 pub error_message: Option<String>,
50 /// The user's input text (may be truncated).
51 pub user_input: String,
52 /// Model name used for the LLM call.
53 pub model: String,
54 /// Plan-update events emitted during this turn iteration (taken from EventBus).
55 pub plan_updates: u32,
56 /// Approval-request events emitted during this turn iteration (taken from EventBus).
57 pub approval_count: u32,
58 /// Number of LLM calls made (≥ 1; includes retries).
59 pub llm_calls: u32,
60}