Skip to main content

adk_runner/
prewarm.rs

1//! Prewarming: readying a runner's agent, and optionally a session, before the first message.
2//!
3//! [`Runner::prewarm`](crate::Runner::prewarm) does what a first turn would otherwise do
4//! before its first model token: it opens the model's pooled connection
5//! ([`Llm::prewarm`](adk_core::Llm::prewarm)), starts the servers an agent starts lazily, such
6//! as a harness's MCP servers ([`Agent::prewarm`](adk_core::Agent::prewarm)), and reads the
7//! session, which opens the session store's connection and refuses an archived session early.
8//! It sends no prompt and spends no tokens.
9//!
10//! | Bound | What it keeps |
11//! |---|---|
12//! | Kill switch | A frozen runner refuses to prewarm, as it refuses a turn |
13//! | Budget | The whole prewarm stops at [`Prewarm::budget`] (10 seconds by default) with `runner.prewarm.timeout` |
14//! | Expiry | A warm model connection lasts as long as the client keeps it idle, 90 seconds for `adk-anthropic`; [`PrewarmReport::warm_until`] says when to prewarm again |
15
16use std::time::Duration;
17
18use adk_core::identity::{SessionId, UserId};
19use chrono::{DateTime, Utc};
20
21/// How long a model client keeps an idle connection open: `adk-anthropic`'s and `adk-model`'s
22/// pool idle timeout.
23pub const WARM_FOR: Duration = Duration::from_secs(90);
24
25/// What [`Runner::prewarm`](crate::Runner::prewarm) readies.
26///
27/// # Example
28///
29/// ```rust
30/// use std::time::Duration;
31/// use adk_core::{SessionId, UserId};
32/// use adk_runner::Prewarm;
33///
34/// let request = Prewarm::new()
35///     .session(UserId::new("ana").unwrap(), SessionId::new("trip").unwrap())
36///     .budget(Duration::from_secs(5));
37/// assert_eq!(request.budget_or_default(), Duration::from_secs(5));
38/// ```
39#[derive(Debug, Clone, Default)]
40pub struct Prewarm {
41    pub(crate) session: Option<(UserId, SessionId)>,
42    pub(crate) budget: Option<Duration>,
43}
44
45impl Prewarm {
46    /// The longest a prewarm runs when no [`budget`](Self::budget) is set.
47    pub const DEFAULT_BUDGET: Duration = Duration::from_secs(10);
48
49    /// Prewarms the agent tree only.
50    pub fn new() -> Self {
51        Self::default()
52    }
53
54    /// Also reads the session `session_id` of `user_id`, so its store's connection is open and
55    /// an archived session is refused before anyone types.
56    #[must_use]
57    pub fn session(mut self, user_id: UserId, session_id: SessionId) -> Self {
58        self.session = Some((user_id, session_id));
59        self
60    }
61
62    /// Stops the prewarm after `budget`.
63    #[must_use]
64    pub fn budget(mut self, budget: Duration) -> Self {
65        self.budget = Some(budget);
66        self
67    }
68
69    /// The budget, or [`DEFAULT_BUDGET`](Self::DEFAULT_BUDGET).
70    pub fn budget_or_default(&self) -> Duration {
71        self.budget.unwrap_or(Self::DEFAULT_BUDGET)
72    }
73}
74
75/// What a [`Runner::prewarm`](crate::Runner::prewarm) did.
76#[derive(Debug, Clone, PartialEq, Eq)]
77#[non_exhaustive]
78pub struct PrewarmReport {
79    /// How long the prewarm took.
80    pub elapsed: Duration,
81    /// Whether a session was read.
82    pub session_read: bool,
83    /// When the model connection it opened is likely closed again: [`WARM_FOR`] from now.
84    pub warm_until: DateTime<Utc>,
85}