pub struct Agent<O: Serialize + Send + Sync + 'static = ()> { /* private fields */ }Expand description
Builder for configuring and constructing a runnable agent.
Type parameter O is the optional structured output type (use () for
unstructured text).
Implementations§
Source§impl<O: Serialize + Send + Sync + 'static> Agent<O>
impl<O: Serialize + Send + Sync + 'static> Agent<O>
Sourcepub fn new(name: impl Into<String>, model: impl Into<String>) -> Self
pub fn new(name: impl Into<String>, model: impl Into<String>) -> Self
Create a new agent builder.
Sourcepub fn description(self, description: impl Into<String>) -> Self
pub fn description(self, description: impl Into<String>) -> Self
Set a human-readable description.
Sourcepub fn fallback_model(self, model: impl Into<String>) -> Self
pub fn fallback_model(self, model: impl Into<String>) -> Self
Set a fallback model used when the primary is rate-limited or unavailable.
Sourcepub const fn effort(self, effort: EffortLevel) -> Self
pub const fn effort(self, effort: EffortLevel) -> Self
Set the reasoning effort level.
Sourcepub const fn thinking(self, thinking: ThinkingConfig) -> Self
pub const fn thinking(self, thinking: ThinkingConfig) -> Self
Configure extended thinking / chain-of-thought.
Sourcepub fn allowed_tools(
self,
tools: impl IntoIterator<Item = impl Into<String>>,
) -> Self
pub fn allowed_tools( self, tools: impl IntoIterator<Item = impl Into<String>>, ) -> Self
Restrict the agent to only these tool names (allowlist).
Sourcepub fn exclude_tool(self, tool_name: impl Into<String>) -> Self
pub fn exclude_tool(self, tool_name: impl Into<String>) -> Self
Exclude specific tools by name (denylist within allowlist).
Sourcepub fn middleware(self, mw: impl Middleware + 'static) -> Self
pub fn middleware(self, mw: impl Middleware + 'static) -> Self
Add a middleware component.
Sourcepub fn hooks(self, hooks: HookRegistry) -> Self
pub fn hooks(self, hooks: HookRegistry) -> Self
Configure hooks.
Sourcepub const fn output_mode(self, mode: OutputMode) -> Self
pub const fn output_mode(self, mode: OutputMode) -> Self
Set the structured output mode.
Sourcepub fn output_schema(self, schema: Value) -> Self
pub fn output_schema(self, schema: Value) -> Self
Provide a JSON Schema for structured output validation.
Sourcepub const fn tool_output_format(self, format: OutputFormat) -> Self
pub const fn tool_output_format(self, format: OutputFormat) -> Self
Set the default serialization format for tool and VFS output.
Controls how structured data is rendered before being passed back
to the LLM as tool results. Defaults to OutputFormat::Json.
Individual tools can override this per-call.
Sourcepub const fn get_tool_output_format(&self) -> OutputFormat
pub const fn get_tool_output_format(&self) -> OutputFormat
Returns the configured tool output format.
Sourcepub const fn max_turns(self, turns: u32) -> Self
pub const fn max_turns(self, turns: u32) -> Self
Set the maximum number of agent turns per run.
Sourcepub const fn max_budget(self, budget_usd: f64) -> Self
pub const fn max_budget(self, budget_usd: f64) -> Self
Set the maximum cumulative cost budget (USD).
Sourcepub fn system_prompt(self, config: SystemPromptConfig) -> Self
pub fn system_prompt(self, config: SystemPromptConfig) -> Self
Configure the system prompt.
Sourcepub const fn permission_mode(self, mode: PermissionMode) -> Self
pub const fn permission_mode(self, mode: PermissionMode) -> Self
Set the permission mode.
Sourcepub fn permission_rule(self, rule: PermissionRule) -> Self
pub fn permission_rule(self, rule: PermissionRule) -> Self
Add a permission rule.
Sourcepub fn sandbox(self, config: SandboxConfig) -> Self
pub fn sandbox(self, config: SandboxConfig) -> Self
Configure the sandbox.
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
Set an environment variable available to the agent.
Sourcepub fn debug_file(self, path: impl Into<String>) -> Self
pub fn debug_file(self, path: impl Into<String>) -> Self
Write debug output to a file.
Sourcepub fn mcp_server(self, server_name: impl Into<String>) -> Self
pub fn mcp_server(self, server_name: impl Into<String>) -> Self
Register an MCP server by name.
Sourcepub fn before_agent<F>(self, f: F) -> Self
pub fn before_agent<F>(self, f: F) -> Self
Register a before-agent callback.
Sourcepub fn after_agent<F>(self, f: F) -> Selfwhere
F: Fn(&RunContext, &Result<(), AgentError>) -> BoxFuture<'static, ()> + Send + Sync + 'static,
pub fn after_agent<F>(self, f: F) -> Selfwhere
F: Fn(&RunContext, &Result<(), AgentError>) -> BoxFuture<'static, ()> + Send + Sync + 'static,
Register an after-agent callback.
Sourcepub fn on_model_error<F>(self, f: F) -> Selfwhere
F: Fn(&RunContext, &AgentError) -> BoxFuture<'static, ModelErrorAction> + Send + Sync + 'static,
pub fn on_model_error<F>(self, f: F) -> Selfwhere
F: Fn(&RunContext, &AgentError) -> BoxFuture<'static, ModelErrorAction> + Send + Sync + 'static,
Register a model error callback.
Sourcepub fn agent_name(&self) -> &str
pub fn agent_name(&self) -> &str
Agent name.
Sourcepub fn agent_description(&self) -> &str
pub fn agent_description(&self) -> &str
Agent description.
Sourcepub fn model_name(&self) -> &str
pub fn model_name(&self) -> &str
Primary model identifier.
Sourcepub fn fallback_model_name(&self) -> Option<&str>
pub fn fallback_model_name(&self) -> Option<&str>
Fallback model identifier, if configured.
Sourcepub const fn max_turn_count(&self) -> Option<u32>
pub const fn max_turn_count(&self) -> Option<u32>
Maximum turns, if set.
Sourcepub const fn budget_limit(&self) -> Option<f64>
pub const fn budget_limit(&self) -> Option<f64>
Maximum budget (USD), if set.