Skip to main content

AgentBuilder

Struct AgentBuilder 

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

Implementations§

Source§

impl AgentBuilder

Source

pub fn new(llm: Arc<dyn StreamingModelProvider>) -> Self

Source

pub async fn from_spec( spec: &AgentSpec, base_prompts: Vec<Prompt>, ) -> Result<Self>

Create a builder from a resolved AgentSpec.

The LLM provider is derived from spec.model via ModelProviderParser. base_prompts are prepended before the spec’s own prompts.

Source

pub fn system_prompt(self, prompt: Prompt) -> Self

Add a prompt to the system prompt.

Multiple prompts are concatenated with double newlines.

Source

pub fn tools(self, tx: Sender<McpCommand>, tools: Vec<ToolDefinition>) -> Self

Source

pub fn tool_timeout(self, timeout: Duration) -> Self

Set the timeout for tool execution

If a tool does not return a result within this duration, it will be marked as failed and the agent will continue processing.

Default: 20 minutes

Source

pub fn compaction(self, config: CompactionConfig) -> Self

Configure context compaction settings.

By default, agents automatically compact context when token usage exceeds 85% of the context window, preventing overflow during long-running tasks.

§Examples
// Custom threshold
agent(llm).compaction(CompactionConfig::with_threshold(0.9))

// Disable compaction entirely
agent(llm).compaction(CompactionConfig::disabled())

// Full customization
agent(llm).compaction(
    CompactionConfig::with_threshold(0.85)
        .keep_recent_tool_results(3)
        .min_messages(20)
)
Source

pub fn disable_compaction(self) -> Self

Disable context compaction entirely.

Overflow errors from the model will be surfaced directly to callers.

Source

pub fn max_auto_continues(self, max: u32) -> Self

Configure the maximum number of auto-continue attempts.

When the LLM stops without making tool calls, the agent may inject a continuation prompt and restart the LLM stream for resumable stop reasons (for example, token length limits).

This setting limits how many times the agent will attempt to continue before giving up and returning AgentMessage::Done.

Default: 3

§Example
// Allow up to 5 auto-continue attempts
agent(llm).max_auto_continues(5)

// Disable auto-continue entirely
agent(llm).max_auto_continues(0)
Source

pub fn prompt_cache_key(self, key: String) -> Self

Set a prompt cache key for LLM provider request routing.

This is typically a session ID (UUID) that remains stable across all turns within a conversation, improving prompt cache hit rates.

Source

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

Pre-populate the context with conversation history (e.g. from a restored session).

These messages are inserted after the system prompt.

Source

pub async fn spawn( self, ) -> Result<(Sender<UserMessage>, Receiver<AgentMessage>, AgentHandle)>

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> IntoMaybeUndefined<T> for T

Source§

impl<T> IntoOption<T> for T

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> Same for T

Source§

type Output = T

Should always be Self
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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