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 /// Tool names called in this turn iteration.
36 pub tools_used: Vec<String>,
37 /// Total number of tool calls made.
38 pub tool_call_count: u32,
39 /// Number of tools that succeeded.
40 pub tool_success: u32,
41 /// Number of tools that failed.
42 pub tool_failed: u32,
43 /// Outcome of this turn iteration (Completed / Failed / Cancelled / MaxTurnsExceeded).
44 pub outcome: RunOutcome,
45 /// Error message if the turn errored.
46 pub error_message: Option<String>,
47 /// The user's input text (may be truncated).
48 pub user_input: String,
49 /// Model name used for the LLM call.
50 pub model: String,
51 /// Plan-update events emitted during this turn iteration (taken from EventBus).
52 pub plan_updates: u32,
53 /// Approval-request events emitted during this turn iteration (taken from EventBus).
54 pub approval_count: u32,
55 /// Number of LLM calls made (≥ 1; includes retries).
56 pub llm_calls: u32,
57}