use crate::live::LiveRunConfig;
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum StreamingMode {
#[default]
None,
Sse,
Bidi,
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct RunConfig {
pub streaming_mode: StreamingMode,
pub save_input_blobs_as_artifacts: bool,
pub max_llm_calls: i32,
pub live: Option<LiveRunConfig>,
}
pub mod run_limits {
use std::sync::atomic::{AtomicI64, Ordering};
use crate::error::AgentError;
pub const DEFAULT_MAX_LLM_CALLS: i32 = 500;
pub const MAX_LLM_CALLS_ENV_VAR: &str = "ADK_MAX_LLM_CALLS";
pub fn resolve_max_llm_calls(configured: i32) -> i32 {
resolve_max_llm_calls_with(
configured,
std::env::var(MAX_LLM_CALLS_ENV_VAR).ok().as_deref(),
)
}
pub fn resolve_max_llm_calls_with(configured: i32, env: Option<&str>) -> i32 {
if configured != 0 {
return configured;
}
env.and_then(|raw| raw.parse::<i32>().ok())
.unwrap_or(DEFAULT_MAX_LLM_CALLS)
}
#[derive(Debug)]
pub struct LlmCallLimiter {
max_llm_calls: i32,
calls: AtomicI64,
}
impl LlmCallLimiter {
pub fn new(max_llm_calls: i32) -> Self {
Self {
max_llm_calls,
calls: AtomicI64::new(0),
}
}
pub fn max_llm_calls(&self) -> i32 {
self.max_llm_calls
}
pub fn increment_and_enforce(&self) -> Result<(), AgentError> {
let calls = self.calls.fetch_add(1, Ordering::SeqCst).saturating_add(1);
if self.max_llm_calls <= 0 || calls <= i64::from(self.max_llm_calls) {
return Ok(());
}
Err(AgentError::LlmCallsLimitExceeded {
limit: self.max_llm_calls,
})
}
}
}