Skip to main content

AgentBuilder

Struct AgentBuilder 

Source
pub struct AgentBuilder<M, H = (), C = M, Schema = HandoffDocument> { /* private fields */ }
Expand description

Builder for constructing agents with custom configuration.

§Type Parameters

  • M: the conversation model
  • H: the composed hooks
  • C: the model compaction runs on, when compaction_model sets one
  • Schema: the handoff schema, see handoff

§Example

ⓘ
let agent = Agent::builder(claude)
    .request_parameters(parameters)
    .instruction("You are a helpful assistant.")
    .tool(FileSystemTool::read_only("."))
    .build();

Implementations§

Source§

impl<M: LanguageModel> AgentBuilder<M>

Source

pub fn new(model: M) -> Self

Creates a new agent builder with default configuration.

Source§

impl<M, H, C, Schema> AgentBuilder<M, H, C, Schema>
where M: LanguageModel + 'static, C: LanguageModel + 'static, H: Hooks, Schema: HandoffSchema,

Source

pub fn identity(self, identity: AgentIdentity) -> Self

Sets the identity every tool call of the built agent carries as the agent making it. An agent is the main agent unless an embedder builds it as a subagent.

Source

pub fn fork_source(self, source: ForkSource) -> Self

Publishes every request the built agent sends to source, which is where a fork starts from.

Source

pub const fn max_iterations(self, limit: usize) -> Self

Sets the maximum number of iterations (turns).

The agent will stop and return an error if this limit is exceeded.

Source

pub const fn context_policy(self, policy: ContextPolicy) -> Self

Sets the context management policy: when compaction starts, where the window pauses, and the budgets compaction and recovery work within.

Source

pub const fn config(self, config: AgentConfig) -> Self

Sets the full agent configuration.

Source

pub fn context_source<S: ContextSource>(self, source: S) -> Self

Registers a source rendered into the tail of every request.

Sources are rendered in registration order, and each is told when the request it contributed to was sent.

Source

pub fn live_source<S: LiveSource>(self, source: S) -> Self

Registers live content: a source rendered into the cacheable prefix, after the system prompt and before the handoff, for content too large to resend uncached in every request’s tail.

Blocks render in registration order. A change after a request carried the block reaches the model as the diff alert the source writes for it, and the new block replaces the old one at the next reassembly or compaction (see LiveSource).

§Panics

Panics if a live source with the same TAG is already registered.

Source

pub fn subagents(self, runs: SubagentRuns) -> Self

Sets the registry of children the agent’s tools run, whose events join the agent’s stream (see SubagentRuns).

Source

pub fn approver(self, approver: impl Approver) -> Self

Sets who answers the approval requests of tool calls: a ToolDecision::Ask from a hook, and the requests tools make of their own.

Without one, a request fails the run with AgentError::Approval. The run emits Paused while an approval is pending.

Source

pub fn instruction(self, content: impl Into<String>) -> Self

Appends an instruction to the system prompt, verbatim. Instructions render in the order they are added; aither supplies none of its own.

Source

pub fn tagged_instruction(self, tag: &'static str, content: &str) -> Self

Appends an instruction to the system prompt wrapped in a tag tag.

Source

pub fn external_content( self, name: &'static str, content: ExternalContent, ) -> Self

Appends the external-content block name: memory, a repository’s instructions, a fetched page or a file, rendered after every instruction inside a boundary it cannot close.

Source

pub fn system_layout(self, layout: SystemLayout) -> Self

Lays the system prompt out in layout’s order, for a prompt whose blocks are placed by name instead of by the order they are added in.

§Panics

Panics if a block already added has no slot in layout.

Source

pub fn request_parameters(self, parameters: Parameters) -> Self

Sets the parameters of every request aither sends the conversation model: the agent’s turns, its compactions on that model, its cache keep-alives and the first request of a fork.

aither sets the fields it owns per request over them — the prompt- cache controls, structured output on a compaction, and the token cap of a keep-alive — and sends every other field as given here. The compaction_model takes its own.

Source

pub fn prompt_cache(self, cache: PromptCache) -> Self

Overrides the session’s prompt-cache controller.

The default derives a fresh session key per agent, so every rebuild asks for a new cache shard; a host that rebuilds its agent supplies the session’s identity (and the replaced agent’s epoch, through PromptCache::with_epoch) so the key stays the same across rebuilds.

Source

pub fn claude_cache_ttl(self, ttl: ClaudePromptCacheTtl) -> Self

Sets the Claude cache retention of the session’s prompt cache, keeping its session key; the cache lifetime follows the retention.

Source

pub fn cache_lifetime(self, lifetime: CacheLifetime) -> Self

Sets the conversation’s cache lifetime, keeping the rest of its prompt cache. Idle compaction and idle reassembly follow it.

Source

pub fn clock(self, clock: impl Clock) -> Self

Sets the time source idle compaction, idle reassembly and cache warmth read; the system clock by default.

Source

pub fn restore_dir(self, path: impl Into<PathBuf>) -> Self

Sets the directory files are restored from after a compaction.

Source

pub fn file_references(self, references: impl FileReferences) -> Self

Sets how a compaction’s file index finds the files tool results name.

Source

pub fn handoff<S2: HandoffSchema>(self) -> AgentBuilder<M, H, C, S2>

Sets the handoff schema every compaction asks the model for, in place of HandoffDocument.

The schema’s type is the structured output the model fills; its HandoffSchema implementation gives the instruction, the retry, the validation and the text the conversation reads after the handoff.

Source

pub fn compaction_model<C2: LanguageModel>( self, model: C2, parameters: Parameters, ) -> AgentBuilder<M, H, C2, Schema>

Sets the model compaction runs on, instead of the session’s own.

Without one, compaction reads the conversation’s prompt cache on the session’s model, and an idle session is compacted shortly before the cache expires (Agent::compact_when_idle). A different model cannot read that cache, so with one set compaction requests go to it, idle compaction never runs, and an idle gap past the cache lifetime reassembles the window instead.

Compaction requests to it carry parameters, the model’s own, in place of the session model’s, under the cache controls and structured output aither sets on every compaction.

Source

pub fn hooks<NH: Hooks>(self, hooks: NH) -> AgentBuilder<M, NH, C, Schema>

Sets the embedder’s decisions at the loop’s control points, in place of any set before (see Hooks).

Source

pub fn tool<T: Tool + 'static>(self, tool: T) -> Self

Registers a tool.

§Panics

Panics if the tool cannot be registered: another tool already uses its name, or it carries no description for the model to read. Both are mistakes in the calling program.

Source

pub fn forked_from(self, fork: &ForkSnapshot) -> Self

Makes the built agent a fork of the main agent that sent fork.

The agent starts from the main agent’s request: its window, with the live tail as the last message, its request parameters and its prompt-cache identity, so its first request is the main agent’s followed by the prompt it is given and reads the main agent’s cache. Nothing is initialized on top of that, since initialization would rewrite the window.

The tools registered on this builder must present exactly ForkSnapshot::definitions, or the first request would diverge from the cached prefix at the tool definitions.

Source

pub fn tool_definitions(&self) -> Vec<ToolDefinition>

Returns the definitions of the tools registered on this builder so far.

These are the definitions the built agent sends to the model, in the order the tool registry holds them.

Source

pub fn build(self) -> Agent<M, H, C, Schema>

Builds the agent.

§Panics

Panics when the agent is a fork and its tools do not present the definitions the main agent’s request carried.

Trait Implementations§

Source§

impl<M, H, C, Schema> Debug for AgentBuilder<M, H, C, Schema>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<M, H = (), C = M, Schema = HandoffDocument> !RefUnwindSafe for AgentBuilder<M, H, C, Schema>

§

impl<M, H = (), C = M, Schema = HandoffDocument> !UnwindSafe for AgentBuilder<M, H, C, Schema>

§

impl<M, H, C, Schema> Forget for AgentBuilder<M, H, C, Schema>

§

impl<M, H, C, Schema> Freeze for AgentBuilder<M, H, C, Schema>
where M: Freeze, Option<WithParameters<C>>: Freeze, H: Freeze, PhantomData<fn() -> Schema>: Freeze,

§

impl<M, H, C, Schema> Move for AgentBuilder<M, H, C, Schema>

§

impl<M, H, C, Schema> Send for AgentBuilder<M, H, C, Schema>
where M: Send, Option<WithParameters<C>>: Send, H: Send, PhantomData<fn() -> Schema>: Send,

§

impl<M, H, C, Schema> Sync for AgentBuilder<M, H, C, Schema>
where M: Sync, Option<WithParameters<C>>: Sync, H: Sync, PhantomData<fn() -> Schema>: Sync,

§

impl<M, H, C, Schema> Unpin for AgentBuilder<M, H, C, Schema>
where M: Unpin, Option<WithParameters<C>>: Unpin, H: Unpin, PhantomData<fn() -> Schema>: Unpin,

§

impl<M, H, C, Schema> UnsafeUnpin for AgentBuilder<M, H, C, Schema>
where M: UnsafeUnpin, Option<WithParameters<C>>: UnsafeUnpin, H: UnsafeUnpin, PhantomData<fn() -> Schema>: UnsafeUnpin,

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> 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 = !

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

fn try_from(value: U) -> Result<T, !>

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