pub struct AgentBuilder { /* private fields */ }Expand description
Fluent builder for constructing crate::Agent instances with custom configuration.
Use crate::Agent::builder() to create a new builder instance.
Implementations§
Source§impl AgentBuilder
impl AgentBuilder
Sourcepub fn agent_config(self, config: AgentConfig) -> Self
pub fn agent_config(self, config: AgentConfig) -> Self
Sets the complete agent configuration, replacing all defaults.
Sourcepub fn provider_config(self, config: ProviderConfig) -> Self
pub fn provider_config(self, config: ProviderConfig) -> Self
Sets the API provider configuration (timeouts, beta features, etc.).
Sourcepub async fn auth(self, auth: impl Into<Auth>) -> Result<Self>
pub async fn auth(self, auth: impl Into<Auth>) -> Result<Self>
Configures authentication for the API.
§Supported Methods
Auth::from_env()- UsesANTHROPIC_API_KEYenvironment variableAuth::api_key("sk-...")- Explicit API keyAuth::claude_cli()- Uses Claude CLI OAuth (requirescli-integrationfeature)Auth::bedrock("region")- AWS Bedrock (requiresawsfeature)Auth::vertex("project", "region")- GCP Vertex AI (requiresgcpfeature)
§Example
let agent = Agent::builder()
.auth(Auth::from_env()).await?
.build().await?;Sourcepub fn oauth_config(self, config: OAuthConfig) -> Self
pub fn oauth_config(self, config: OAuthConfig) -> Self
Sets OAuth configuration for token refresh.
Sourcepub fn supports_server_tools(&self) -> bool
pub fn supports_server_tools(&self) -> bool
Returns whether the current auth method supports server-side tools.
Sourcepub fn models(self, config: ModelConfig) -> Self
pub fn models(self, config: ModelConfig) -> Self
Sets both primary and small model configurations.
Sourcepub fn model(self, model: impl Into<String>) -> Self
pub fn model(self, model: impl Into<String>) -> Self
Sets the primary model for main operations.
Default: claude-sonnet-4-5-20250514
Sourcepub fn small_model(self, model: impl Into<String>) -> Self
pub fn small_model(self, model: impl Into<String>) -> Self
Sets the smaller model for quick operations (e.g., subagents).
Default: claude-haiku-4-5-20251001
Sourcepub fn max_tokens(self, tokens: u32) -> Self
pub fn max_tokens(self, tokens: u32) -> Self
Sets the maximum tokens per response.
Default: 8192. Values exceeding this require the 128k beta feature,
which is automatically enabled when using ProviderConfig::with_max_tokens.
Sourcepub fn extended_context(self, enabled: bool) -> Self
pub fn extended_context(self, enabled: bool) -> Self
Enables extended context window (1M tokens for supported models).
Requires the context-1m-2025-08-07 beta feature.
Currently supported: claude-sonnet-4-5-20250929
Sourcepub fn tools(self, access: ToolAccess) -> Self
pub fn tools(self, access: ToolAccess) -> Self
Sets tool access policy.
§Options
ToolAccess::all()- Enable all built-in toolsToolAccess::none()- Disable all toolsToolAccess::only(["Read", "Write"])- Enable specific toolsToolAccess::except(["Bash"])- Enable all except specific tools
Sourcepub fn working_dir(self, path: impl Into<PathBuf>) -> Self
pub fn working_dir(self, path: impl Into<PathBuf>) -> Self
Sets the working directory for file operations.
Sourcepub fn max_iterations(self, max: usize) -> Self
pub fn max_iterations(self, max: usize) -> Self
Sets the maximum number of agentic loop iterations.
Default: 100
Sourcepub fn timeout(self, timeout: Duration) -> Self
pub fn timeout(self, timeout: Duration) -> Self
Sets the overall execution timeout.
Default: 300 seconds
Sourcepub fn chunk_timeout(self, timeout: Duration) -> Self
pub fn chunk_timeout(self, timeout: Duration) -> Self
Sets the timeout between streaming chunks.
This timeout detects stalled connections when no data is received for the specified duration during streaming responses.
Default: 60 seconds
For large projects or slow network conditions, consider increasing this value (e.g., 180 seconds).
Sourcepub fn auto_compact(self, enabled: bool) -> Self
pub fn auto_compact(self, enabled: bool) -> Self
Enables or disables automatic context compaction.
Default: true
Sourcepub fn compact_keep_messages(self, count: usize) -> Self
pub fn compact_keep_messages(self, count: usize) -> Self
Sets the number of messages to preserve during compaction.
Default: 4
Sourcepub fn system_prompt(self, prompt: impl Into<String>) -> Self
pub fn system_prompt(self, prompt: impl Into<String>) -> Self
Sets a custom system prompt, replacing the default.
Sourcepub fn system_prompt_mode(self, mode: SystemPromptMode) -> Self
pub fn system_prompt_mode(self, mode: SystemPromptMode) -> Self
Sets how the system prompt is applied.
Sourcepub fn append_system_prompt(self, prompt: impl Into<String>) -> Self
pub fn append_system_prompt(self, prompt: impl Into<String>) -> Self
Appends to the default system prompt instead of replacing it.
Sourcepub fn output_style(self, style: OutputStyle) -> Self
pub fn output_style(self, style: OutputStyle) -> Self
Sets the output style for response formatting.
Sourcepub fn output_style_name(self, name: impl Into<String>) -> Self
pub fn output_style_name(self, name: impl Into<String>) -> Self
Sets the output style by name (loaded from configuration).
Sourcepub fn output_schema(self, schema: Value) -> Self
pub fn output_schema(self, schema: Value) -> Self
Sets a JSON schema for structured output.
Sourcepub fn structured_output<T: JsonSchema>(self) -> Self
pub fn structured_output<T: JsonSchema>(self) -> Self
Enables structured output with automatic schema generation.
Sourcepub fn permission_policy(self, policy: PermissionPolicy) -> Self
pub fn permission_policy(self, policy: PermissionPolicy) -> Self
Sets the complete permission policy.
Sourcepub fn permission_mode(self, mode: PermissionMode) -> Self
pub fn permission_mode(self, mode: PermissionMode) -> Self
Sets the permission mode (permissive, default, or strict).
Sourcepub fn allow_tool(self, pattern: impl Into<String>) -> Self
pub fn allow_tool(self, pattern: impl Into<String>) -> Self
Adds a rule to allow a tool or pattern.
Sourcepub fn deny_tool(self, pattern: impl Into<String>) -> Self
pub fn deny_tool(self, pattern: impl Into<String>) -> Self
Adds a rule to deny a tool or pattern.
Sourcepub fn env(self, key: impl Into<String>, value: impl Into<String>) -> Self
pub fn env(self, key: impl Into<String>, value: impl Into<String>) -> Self
Sets an environment variable for tool execution.
Sourcepub fn envs(
self,
vars: impl IntoIterator<Item = (impl Into<String>, impl Into<String>)>,
) -> Self
pub fn envs( self, vars: impl IntoIterator<Item = (impl Into<String>, impl Into<String>)>, ) -> Self
Sets multiple environment variables for tool execution.
Sourcepub fn allow_domain(self, domain: impl Into<String>) -> Self
pub fn allow_domain(self, domain: impl Into<String>) -> Self
Adds a domain to the network allowlist.
Sourcepub fn deny_domain(self, domain: impl Into<String>) -> Self
pub fn deny_domain(self, domain: impl Into<String>) -> Self
Adds a domain to the network blocklist.
Sourcepub fn sandbox_enabled(self, enabled: bool) -> Self
pub fn sandbox_enabled(self, enabled: bool) -> Self
Enables or disables sandbox isolation.
Sourcepub fn exclude_command(self, command: impl Into<String>) -> Self
pub fn exclude_command(self, command: impl Into<String>) -> Self
Excludes a command from sandbox restrictions.
Sourcepub fn max_budget_usd(self, amount: f64) -> Self
pub fn max_budget_usd(self, amount: f64) -> Self
Sets the maximum budget in USD.
Sourcepub fn tenant_id(self, id: impl Into<String>) -> Self
pub fn tenant_id(self, id: impl Into<String>) -> Self
Sets the tenant ID for multi-tenant budget tracking.
Sourcepub fn tenant_budget_manager(self, manager: TenantBudgetManager) -> Self
pub fn tenant_budget_manager(self, manager: TenantBudgetManager) -> Self
Sets a shared tenant budget manager.
Sourcepub fn fallback_model(self, model: impl Into<String>) -> Self
pub fn fallback_model(self, model: impl Into<String>) -> Self
Sets the model to fall back to when budget is exceeded.
Sourcepub fn fallback(self, config: FallbackConfig) -> Self
pub fn fallback(self, config: FallbackConfig) -> Self
Sets the complete fallback configuration.
Sourcepub fn session_manager(self, manager: SessionManager) -> Self
pub fn session_manager(self, manager: SessionManager) -> Self
Sets a custom session manager for persistence.
Sourcepub async fn fork_session(self, session_id: impl Into<String>) -> Result<Self>
pub async fn fork_session(self, session_id: impl Into<String>) -> Result<Self>
Forks an existing session, creating a new branch.
Sourcepub async fn resume_session(self, session_id: impl Into<String>) -> Result<Self>
pub async fn resume_session(self, session_id: impl Into<String>) -> Result<Self>
Resumes an existing session by ID.
Sourcepub fn messages(self, messages: Vec<Message>) -> Self
pub fn messages(self, messages: Vec<Message>) -> Self
Sets initial messages for the conversation.
Sourcepub fn mcp_server(
self,
name: impl Into<String>,
config: McpServerConfig,
) -> Self
pub fn mcp_server( self, name: impl Into<String>, config: McpServerConfig, ) -> Self
Adds an MCP server configuration.
Sourcepub fn mcp_stdio(
self,
name: impl Into<String>,
command: impl Into<String>,
args: Vec<String>,
) -> Self
pub fn mcp_stdio( self, name: impl Into<String>, command: impl Into<String>, args: Vec<String>, ) -> Self
Adds an MCP server using stdio transport.
Sourcepub fn mcp_manager(self, manager: McpManager) -> Self
pub fn mcp_manager(self, manager: McpManager) -> Self
Sets an owned MCP manager.
Sets a shared MCP manager (for multi-agent scenarios).
Sourcepub fn mcp_toolset(self, toolset: McpToolset) -> Self
pub fn mcp_toolset(self, toolset: McpToolset) -> Self
Registers an MCP toolset configuration for deferred loading.
Sourcepub fn with_tool_search(self) -> Self
pub fn with_tool_search(self) -> Self
Enables tool search with default configuration.
Sourcepub fn tool_search_config(self, config: ToolSearchConfig) -> Self
pub fn tool_search_config(self, config: ToolSearchConfig) -> Self
Sets the tool search configuration.
Sourcepub fn tool_search_threshold(self, threshold: f64) -> Self
pub fn tool_search_threshold(self, threshold: f64) -> Self
Sets the tool search threshold as a fraction of context window (0.0 - 1.0).
Sourcepub fn tool_search_mode(self, mode: SearchMode) -> Self
pub fn tool_search_mode(self, mode: SearchMode) -> Self
Sets the search mode for tool search.
Sourcepub fn always_load_tools(
self,
tools: impl IntoIterator<Item = impl Into<String>>,
) -> Self
pub fn always_load_tools( self, tools: impl IntoIterator<Item = impl Into<String>>, ) -> Self
Sets tools that should always be loaded immediately (never deferred).
Sets a shared tool search manager.
Sourcepub fn skill_registry(self, registry: IndexRegistry<SkillIndex>) -> Self
pub fn skill_registry(self, registry: IndexRegistry<SkillIndex>) -> Self
Sets a complete skill registry.
Sourcepub fn skill(self, skill: SkillIndex) -> Self
pub fn skill(self, skill: SkillIndex) -> Self
Registers a single skill index.
Sourcepub fn rule_index(self, index: RuleIndex) -> Self
pub fn rule_index(self, index: RuleIndex) -> Self
Adds a rule index for rule discovery.
Sourcepub fn memory_content(self, content: impl Into<String>) -> Self
pub fn memory_content(self, content: impl Into<String>) -> Self
Adds memory content (CLAUDE.md style).
Sourcepub fn local_memory_content(self, content: impl Into<String>) -> Self
pub fn local_memory_content(self, content: impl Into<String>) -> Self
Adds local memory content (CLAUDE.local.md style).
Sourcepub fn subagent_registry(self, registry: IndexRegistry<SubagentIndex>) -> Self
pub fn subagent_registry(self, registry: IndexRegistry<SubagentIndex>) -> Self
Sets a complete subagent registry.
Sourcepub fn subagent(self, subagent: SubagentIndex) -> Self
pub fn subagent(self, subagent: SubagentIndex) -> Self
Registers a single subagent.
Source§impl AgentBuilder
impl AgentBuilder
Sourcepub async fn from_claude_code(self, path: impl AsRef<Path>) -> Result<Self>
Available on crate feature cli-integration only.
pub async fn from_claude_code(self, path: impl AsRef<Path>) -> Result<Self>
cli-integration only.Initializes Claude Code CLI authentication and working directory.
This is the minimal setup. Use with_*_resources() methods to enable
loading from specific levels. Resources are loaded during build() in
a fixed order: Enterprise → User → Project → Local.
§Example
let agent = Agent::builder()
.from_claude_code("./project").await?
.with_user_resources()
.with_project_resources()
.build()
.await?;Sourcepub fn with_enterprise_resources(self) -> Self
Available on crate feature cli-integration only.
pub fn with_enterprise_resources(self) -> Self
cli-integration only.Enables loading of enterprise-level resources during build.
Resources are loaded from system configuration paths:
- macOS:
/Library/Application Support/ClaudeCode/ - Linux:
/etc/claude-code/
This method only sets a flag; actual loading happens during build()
in the fixed order: Enterprise → User → Project → Local.
Sourcepub fn with_user_resources(self) -> Self
Available on crate feature cli-integration only.
pub fn with_user_resources(self) -> Self
cli-integration only.Enables loading of user-level resources during build.
Resources are loaded from ~/.claude/.
This method only sets a flag; actual loading happens during build()
in the fixed order: Enterprise → User → Project → Local.
Sourcepub fn with_project_resources(self) -> Self
Available on crate feature cli-integration only.
pub fn with_project_resources(self) -> Self
cli-integration only.Enables loading of project-level resources during build.
Resources are loaded from {working_dir}/.claude/.
Requires from_claude_code() to be called first to set the working directory.
This method only sets a flag; actual loading happens during build()
in the fixed order: Enterprise → User → Project → Local.
Sourcepub fn with_local_resources(self) -> Self
Available on crate feature cli-integration only.
pub fn with_local_resources(self) -> Self
cli-integration only.Enables loading of local-level resources during build.
Loads CLAUDE.local.md and settings.local.json from the project directory.
These are typically gitignored and contain personal/machine-specific settings.
Requires from_claude_code() to be called first to set the working directory.
This method only sets a flag; actual loading happens during build()
in the fixed order: Enterprise → User → Project → Local.
Trait Implementations§
Source§impl Default for AgentBuilder
impl Default for AgentBuilder
Source§fn default() -> AgentBuilder
fn default() -> AgentBuilder
Auto Trait Implementations§
impl Freeze for AgentBuilder
impl !RefUnwindSafe for AgentBuilder
impl Send for AgentBuilder
impl Sync for AgentBuilder
impl Unpin for AgentBuilder
impl !UnwindSafe for AgentBuilder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more