pub struct LlmAgentBuilder { /* private fields */ }agents only.Expand description
Builder for constructing an LlmAgent with all configuration options.
Implementations§
Source§impl LlmAgentBuilder
impl LlmAgentBuilder
Sourcepub fn new(name: impl Into<String>) -> LlmAgentBuilder
pub fn new(name: impl Into<String>) -> LlmAgentBuilder
Create a new builder with the given agent name.
Sourcepub fn description(self, desc: impl Into<String>) -> LlmAgentBuilder
pub fn description(self, desc: impl Into<String>) -> LlmAgentBuilder
Set the agent description.
Sourcepub fn model(self, model: Arc<dyn Llm>) -> LlmAgentBuilder
pub fn model(self, model: Arc<dyn Llm>) -> LlmAgentBuilder
Set the LLM model for this agent.
Sourcepub fn instruction(self, instruction: impl Into<String>) -> LlmAgentBuilder
pub fn instruction(self, instruction: impl Into<String>) -> LlmAgentBuilder
Set the system instruction for this agent.
Sourcepub fn instruction_provider(
self,
provider: Box<dyn Fn(Arc<dyn ReadonlyContext>) -> Pin<Box<dyn Future<Output = Result<String, AdkError>> + Send>> + Send + Sync>,
) -> LlmAgentBuilder
pub fn instruction_provider( self, provider: Box<dyn Fn(Arc<dyn ReadonlyContext>) -> Pin<Box<dyn Future<Output = Result<String, AdkError>> + Send>> + Send + Sync>, ) -> LlmAgentBuilder
Set a dynamic instruction provider evaluated per invocation.
Sourcepub fn global_instruction(
self,
instruction: impl Into<String>,
) -> LlmAgentBuilder
pub fn global_instruction( self, instruction: impl Into<String>, ) -> LlmAgentBuilder
Set a global instruction prepended to all requests.
Sourcepub fn global_instruction_provider(
self,
provider: Box<dyn Fn(Arc<dyn ReadonlyContext>) -> Pin<Box<dyn Future<Output = Result<String, AdkError>> + Send>> + Send + Sync>,
) -> LlmAgentBuilder
pub fn global_instruction_provider( self, provider: Box<dyn Fn(Arc<dyn ReadonlyContext>) -> Pin<Box<dyn Future<Output = Result<String, AdkError>> + Send>> + Send + Sync>, ) -> LlmAgentBuilder
Set a dynamic global instruction provider evaluated per invocation.
Sourcepub fn with_skills(self, index: SkillIndex) -> LlmAgentBuilder
Available on crate feature skills only.
pub fn with_skills(self, index: SkillIndex) -> LlmAgentBuilder
skills only.Set a preloaded skills index for this agent.
Sourcepub fn with_auto_skills(self) -> Result<LlmAgentBuilder, AdkError>
Available on crate feature skills only.
pub fn with_auto_skills(self) -> Result<LlmAgentBuilder, AdkError>
skills only.Auto-load skills from .skills/ in the current working directory.
Sourcepub fn with_skills_from_root(
self,
root: impl AsRef<Path>,
) -> Result<LlmAgentBuilder, AdkError>
Available on crate feature skills only.
pub fn with_skills_from_root( self, root: impl AsRef<Path>, ) -> Result<LlmAgentBuilder, AdkError>
skills only.Auto-load skills from .skills/ under a custom root directory.
Sourcepub fn with_skill_policy(self, policy: SelectionPolicy) -> LlmAgentBuilder
Available on crate feature skills only.
pub fn with_skill_policy(self, policy: SelectionPolicy) -> LlmAgentBuilder
skills only.Customize skill selection behavior.
Sourcepub fn with_skill_budget(self, max_chars: usize) -> LlmAgentBuilder
Available on crate feature skills only.
pub fn with_skill_budget(self, max_chars: usize) -> LlmAgentBuilder
skills only.Limit injected skill content length.
Sourcepub fn input_schema(self, schema: Value) -> LlmAgentBuilder
pub fn input_schema(self, schema: Value) -> LlmAgentBuilder
Set a JSON schema for validating user input.
Sourcepub fn output_schema(self, schema: Value) -> LlmAgentBuilder
pub fn output_schema(self, schema: Value) -> LlmAgentBuilder
Set a JSON schema for structured output from the LLM.
Sourcepub fn disallow_transfer_to_parent(self, disallow: bool) -> LlmAgentBuilder
pub fn disallow_transfer_to_parent(self, disallow: bool) -> LlmAgentBuilder
Prevent this agent from transferring control back to its parent.
Sourcepub fn disallow_transfer_to_peers(self, disallow: bool) -> LlmAgentBuilder
pub fn disallow_transfer_to_peers(self, disallow: bool) -> LlmAgentBuilder
Prevent this agent from transferring control to peer agents.
Sourcepub fn include_contents(self, include: IncludeContents) -> LlmAgentBuilder
pub fn include_contents(self, include: IncludeContents) -> LlmAgentBuilder
Control which conversation history contents are included in LLM requests.
Sourcepub fn output_key(self, key: impl Into<String>) -> LlmAgentBuilder
pub fn output_key(self, key: impl Into<String>) -> LlmAgentBuilder
Set a state key where the agent’s final output will be stored.
Sourcepub fn generate_content_config(
self,
config: GenerateContentConfig,
) -> LlmAgentBuilder
pub fn generate_content_config( self, config: GenerateContentConfig, ) -> LlmAgentBuilder
Set default generation parameters (temperature, top_p, top_k, max_output_tokens) applied to every LLM request made by this agent.
These defaults are merged with any per-request config. If output_schema is also
set, the schema is preserved alongside these generation parameters.
§Example
use adk_core::GenerateContentConfig;
let agent = LlmAgentBuilder::new("my-agent")
.model(model)
.generate_content_config(GenerateContentConfig {
temperature: Some(0.7),
max_output_tokens: Some(2048),
..Default::default()
})
.build()?;Sourcepub fn temperature(self, temperature: f32) -> LlmAgentBuilder
pub fn temperature(self, temperature: f32) -> LlmAgentBuilder
Set the default temperature for LLM requests.
Shorthand for setting just temperature without a full GenerateContentConfig.
Sourcepub fn top_p(self, top_p: f32) -> LlmAgentBuilder
pub fn top_p(self, top_p: f32) -> LlmAgentBuilder
Set the default top_p for LLM requests.
Sourcepub fn top_k(self, top_k: i32) -> LlmAgentBuilder
pub fn top_k(self, top_k: i32) -> LlmAgentBuilder
Set the default top_k for LLM requests.
Sourcepub fn max_output_tokens(self, max_tokens: i32) -> LlmAgentBuilder
pub fn max_output_tokens(self, max_tokens: i32) -> LlmAgentBuilder
Set the default max output tokens for LLM requests.
Sourcepub fn max_iterations(self, max: u32) -> LlmAgentBuilder
pub fn max_iterations(self, max: u32) -> LlmAgentBuilder
Set the maximum number of LLM round-trips (iterations) before the agent stops. Default is 100.
Sourcepub fn tool_timeout(self, timeout: Duration) -> LlmAgentBuilder
pub fn tool_timeout(self, timeout: Duration) -> LlmAgentBuilder
Set the timeout for individual tool executions. Default is 5 minutes. Tools that exceed this timeout will return an error.
Sourcepub fn tool(self, tool: Arc<dyn Tool>) -> LlmAgentBuilder
pub fn tool(self, tool: Arc<dyn Tool>) -> LlmAgentBuilder
Add a tool to this agent’s toolbox.
Sourcepub fn toolset(self, toolset: Arc<dyn Toolset>) -> LlmAgentBuilder
pub fn toolset(self, toolset: Arc<dyn Toolset>) -> LlmAgentBuilder
Register a dynamic toolset for per-invocation tool resolution.
Toolsets are resolved at the start of each run() call using the
invocation’s ReadonlyContext. This enables context-dependent tools
like per-user browser sessions from a pool.
Sourcepub fn sub_agent(self, agent: Arc<dyn Agent>) -> LlmAgentBuilder
pub fn sub_agent(self, agent: Arc<dyn Agent>) -> LlmAgentBuilder
Add a sub-agent that this agent can delegate to.
Sourcepub fn before_callback(
self,
callback: Box<dyn Fn(Arc<dyn CallbackContext>) -> Pin<Box<dyn Future<Output = Result<Option<Content>, AdkError>> + Send>> + Send + Sync>,
) -> LlmAgentBuilder
pub fn before_callback( self, callback: Box<dyn Fn(Arc<dyn CallbackContext>) -> Pin<Box<dyn Future<Output = Result<Option<Content>, AdkError>> + Send>> + Send + Sync>, ) -> LlmAgentBuilder
Add a before-agent callback.
Sourcepub fn after_callback(
self,
callback: Box<dyn Fn(Arc<dyn CallbackContext>) -> Pin<Box<dyn Future<Output = Result<Option<Content>, AdkError>> + Send>> + Send + Sync>,
) -> LlmAgentBuilder
pub fn after_callback( self, callback: Box<dyn Fn(Arc<dyn CallbackContext>) -> Pin<Box<dyn Future<Output = Result<Option<Content>, AdkError>> + Send>> + Send + Sync>, ) -> LlmAgentBuilder
Add an after-agent callback.
Sourcepub fn before_model_callback(
self,
callback: Box<dyn Fn(Arc<dyn CallbackContext>, LlmRequest) -> Pin<Box<dyn Future<Output = Result<BeforeModelResult, AdkError>> + Send>> + Send + Sync>,
) -> LlmAgentBuilder
pub fn before_model_callback( self, callback: Box<dyn Fn(Arc<dyn CallbackContext>, LlmRequest) -> Pin<Box<dyn Future<Output = Result<BeforeModelResult, AdkError>> + Send>> + Send + Sync>, ) -> LlmAgentBuilder
Add a before-model callback invoked before each LLM request.
Sourcepub fn after_model_callback(
self,
callback: Box<dyn Fn(Arc<dyn CallbackContext>, LlmResponse) -> Pin<Box<dyn Future<Output = Result<Option<LlmResponse>, AdkError>> + Send>> + Send + Sync>,
) -> LlmAgentBuilder
pub fn after_model_callback( self, callback: Box<dyn Fn(Arc<dyn CallbackContext>, LlmResponse) -> Pin<Box<dyn Future<Output = Result<Option<LlmResponse>, AdkError>> + Send>> + Send + Sync>, ) -> LlmAgentBuilder
Add an after-model callback invoked after each LLM response.
Sourcepub fn before_tool_callback(
self,
callback: Box<dyn Fn(Arc<dyn CallbackContext>) -> Pin<Box<dyn Future<Output = Result<Option<Content>, AdkError>> + Send>> + Send + Sync>,
) -> LlmAgentBuilder
pub fn before_tool_callback( self, callback: Box<dyn Fn(Arc<dyn CallbackContext>) -> Pin<Box<dyn Future<Output = Result<Option<Content>, AdkError>> + Send>> + Send + Sync>, ) -> LlmAgentBuilder
Add a before-tool callback invoked before each tool execution.
Sourcepub fn after_tool_callback(
self,
callback: Box<dyn Fn(Arc<dyn CallbackContext>) -> Pin<Box<dyn Future<Output = Result<Option<Content>, AdkError>> + Send>> + Send + Sync>,
) -> LlmAgentBuilder
pub fn after_tool_callback( self, callback: Box<dyn Fn(Arc<dyn CallbackContext>) -> Pin<Box<dyn Future<Output = Result<Option<Content>, AdkError>> + Send>> + Send + Sync>, ) -> LlmAgentBuilder
Add an after-tool callback invoked after each tool execution.
Sourcepub fn after_tool_callback_full(
self,
callback: Box<dyn Fn(Arc<dyn CallbackContext>, Arc<dyn Tool>, Value, Value) -> Pin<Box<dyn Future<Output = Result<Option<Value>, AdkError>> + Send>> + Send + Sync>,
) -> LlmAgentBuilder
pub fn after_tool_callback_full( self, callback: Box<dyn Fn(Arc<dyn CallbackContext>, Arc<dyn Tool>, Value, Value) -> Pin<Box<dyn Future<Output = Result<Option<Value>, AdkError>> + Send>> + Send + Sync>, ) -> LlmAgentBuilder
Register a rich after-tool callback that receives the tool, arguments, and response value.
This is the V2 callback surface aligned with the Python/Go ADK model
where after_tool_callback receives the full tool execution context.
Unlike after_tool_callback (which only
receives CallbackContext), this callback can inspect and modify tool
results directly.
Return Ok(None) to keep the original response, or Ok(Some(value))
to replace the function response sent to the LLM.
These callbacks run after the legacy after_tool_callback chain.
ToolOutcome is available via ctx.tool_outcome().
Sourcepub fn on_tool_error(
self,
callback: Box<dyn Fn(Arc<dyn CallbackContext>, Arc<dyn Tool>, Value, String) -> Pin<Box<dyn Future<Output = Result<Option<Value>, AdkError>> + Send>> + Send + Sync>,
) -> LlmAgentBuilder
pub fn on_tool_error( self, callback: Box<dyn Fn(Arc<dyn CallbackContext>, Arc<dyn Tool>, Value, String) -> Pin<Box<dyn Future<Output = Result<Option<Value>, AdkError>> + Send>> + Send + Sync>, ) -> LlmAgentBuilder
Register a callback invoked when a tool execution fails (after retries are exhausted).
If the callback returns Ok(Some(value)), the value is used as a
fallback function response to the LLM. If it returns Ok(None),
the next callback in the chain is tried. If no callback provides a
fallback, the original error is reported to the LLM.
Sourcepub fn default_retry_budget(self, budget: RetryBudget) -> LlmAgentBuilder
pub fn default_retry_budget(self, budget: RetryBudget) -> LlmAgentBuilder
Set a default retry budget applied to all tools that do not have a per-tool override.
When a tool execution fails and a retry budget applies, the agent
retries up to budget.max_retries times with the configured delay
between attempts.
Sourcepub fn tool_retry_budget(
self,
tool_name: impl Into<String>,
budget: RetryBudget,
) -> LlmAgentBuilder
pub fn tool_retry_budget( self, tool_name: impl Into<String>, budget: RetryBudget, ) -> LlmAgentBuilder
Set a per-tool retry budget that overrides the default for the named tool.
Per-tool budgets take precedence over the default retry budget.
Sourcepub fn circuit_breaker_threshold(self, threshold: u32) -> LlmAgentBuilder
pub fn circuit_breaker_threshold(self, threshold: u32) -> LlmAgentBuilder
Configure a circuit breaker that temporarily disables tools after
threshold consecutive failures within a single invocation.
When a tool’s consecutive failure count reaches the threshold, subsequent calls to that tool are short-circuited with an immediate error response until the next invocation (which resets the state).
Sourcepub fn tool_confirmation_policy(
self,
policy: ToolConfirmationPolicy,
) -> LlmAgentBuilder
pub fn tool_confirmation_policy( self, policy: ToolConfirmationPolicy, ) -> LlmAgentBuilder
Configure tool confirmation requirements for this agent.
Sourcepub fn require_tool_confirmation(
self,
tool_name: impl Into<String>,
) -> LlmAgentBuilder
pub fn require_tool_confirmation( self, tool_name: impl Into<String>, ) -> LlmAgentBuilder
Require confirmation for a specific tool name.
Sourcepub fn require_tool_confirmation_for_all(self) -> LlmAgentBuilder
pub fn require_tool_confirmation_for_all(self) -> LlmAgentBuilder
Require confirmation for all tool calls.
Sourcepub fn tool_execution_strategy(
self,
strategy: ToolExecutionStrategy,
) -> LlmAgentBuilder
pub fn tool_execution_strategy( self, strategy: ToolExecutionStrategy, ) -> LlmAgentBuilder
Set the tool execution strategy for this agent.
When set, this overrides the RunConfig’s tool_execution_strategy
for this agent’s dispatch loop. When None (the default), the
RunConfig value is used.
Sourcepub fn input_guardrails(self, guardrails: GuardrailSet) -> LlmAgentBuilder
pub fn input_guardrails(self, guardrails: GuardrailSet) -> LlmAgentBuilder
Set input guardrails to validate user input before processing.
Input guardrails run before the agent processes the request and can:
- Block harmful or off-topic content
- Redact PII from user input
- Enforce input length limits
Requires the guardrails feature.
Sourcepub fn output_guardrails(self, guardrails: GuardrailSet) -> LlmAgentBuilder
pub fn output_guardrails(self, guardrails: GuardrailSet) -> LlmAgentBuilder
Set output guardrails to validate agent responses.
Output guardrails run after the agent generates a response and can:
- Enforce JSON schema compliance
- Redact PII from responses
- Block harmful content in responses
Requires the guardrails feature.