Skip to main content

AgentBuilder

Struct AgentBuilder 

Source
pub struct AgentBuilder { /* private fields */ }
Expand description

Builder for configuring and creating an agent.

Use Agent::builder() to start building. All methods are optional except llm().

§Example

let agent = Agent::builder()
    .llm(Arc::new(provider))
    .tools(registry)
    .system_prompt("You are a helpful assistant")
    .max_steps(50)
    .build()?;

Implementations§

Source§

impl AgentBuilder

Source

pub fn llm(self, llm: Arc<dyn LlmProvider>) -> Self

Set the LLM provider (required).

The provider handles all requests to the model. Must be provided before calling build(), otherwise build() will return an error.

Source

pub fn tools(self, tools: ToolRegistry) -> Self

Set the tool registry (optional, defaults to empty registry).

Tools are available to the model for execution during the run. If not set, the agent will only generate text. The registry is shared via Arc, so tool implementations must be thread-safe.

Source

pub fn system_prompt(self, prompt: impl Into<String>) -> Self

Set the system prompt (optional).

Prepended to the transcript before the first goal. Typically describes the agent’s role (e.g., “You are a code assistant”). If not set, no system message is added.

Source

pub fn max_steps(self, n: usize) -> Self

Set the maximum number of agent iterations (optional, defaults to 32).

Each iteration is one LLM call + tool execution round. Higher values allow more complex reasoning but increase cost and latency. If exceeded, the run finishes with FinishReason::BudgetExceeded.

Source

pub fn max_transcript_chars(self, n: usize) -> Self

Set the maximum transcript character size (optional).

Prevents the transcript from growing unbounded. When this limit is approached, Compactor (if configured) will summarize and trim old messages. If the transcript cannot fit within the limit, the run stops with FinishReason::TranscriptLimit.

§Note

This is a hard limit; if compaction cannot reduce the size enough, the run will terminate rather than add the next message.

Source

pub fn events(self, tx: UnboundedSender<StepEvent>) -> Self

Attach an event channel to observe agent progress (optional).

Events are sent through this channel as the agent runs. Non-blocking: if the receiver is dropped or the channel is full, sends are silently ignored. Allows UI layers to display real-time progress without coupling to the agent’s internals.

Source

pub fn seed_transcript(self, messages: Vec<Message>) -> Self

Seed the agent with a pre-existing transcript. Seeded messages are placed in the transcript after the system prompt but before the new goal. No StepEvents are emitted for seeded messages; only events produced during the new run are streamed.

Source

pub fn streaming(self, enabled: bool) -> Self

Enable token-by-token streaming from the provider (optional, defaults to false).

If enabled, the agent creates a channel and asks the provider to emit PartialToken events as they arrive. Requires provider support; some providers ignore this and emit the complete response at once.

Source

pub fn compactor(self, compactor: Compactor) -> Self

Attach a compactor to automatically trim old transcript content (optional).

When the transcript reaches max_transcript_chars, the compactor summarizes and removes old messages. Only active if both max_transcript_chars and compactor are set. Without a compactor, TranscriptLimit errors occur.

Source

pub fn permission_hook<F>(self, hook: F) -> Self
where F: Fn(&str, &Value) -> PermissionDecision + Send + Sync + 'static,

Attach a permission hook that is invoked before each tool execution (optional).

The hook can allow, deny, or transform tool arguments. If denied, the tool is not executed and the reason is returned as a tool error. If transformed, the new arguments are passed to the tool instead.

Source

pub fn permission_hook_opt(self, hook: Option<PermissionHook>) -> Self

Attach a pre-built permission hook (e.g. cloned from a parent agent).

This is useful when inheriting a permission hook from a parent agent into a sub-agent. Unlike permission_hook(), this accepts an Option<PermissionHook> directly, avoiding the need to re-wrap.

Source

pub fn hook(self, hook: Arc<dyn Hook>) -> Self

Register a lifecycle hook (optional).

Hooks are invoked at well-defined lifecycle points during the agent run. Multiple hooks are supported; they fire in registration order.

Source

pub fn planning_mode(self, mode: PlanningMode) -> Self

Set the planning mode (optional, defaults to Immediate).

When set to PlanFirst, the agent will buffer tool calls and emit a PlanProposed event instead of executing them immediately. The caller must then call confirm_plan() or reject_plan() to proceed.

Source

pub fn build(self) -> Result<Agent>

Trait Implementations§

Source§

impl Default for AgentBuilder

Source§

fn default() -> AgentBuilder

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more