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 modelH: the composed hooksC: the model compaction runs on, whencompaction_modelsets oneSchema: the handoff schema, seehandoff
§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>
impl<M: LanguageModel> AgentBuilder<M>
Source§impl<M, H, C, Schema> AgentBuilder<M, H, C, Schema>
impl<M, H, C, Schema> AgentBuilder<M, H, C, Schema>
Sourcepub fn identity(self, identity: AgentIdentity) -> Self
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.
Sourcepub fn fork_source(self, source: ForkSource) -> Self
pub fn fork_source(self, source: ForkSource) -> Self
Publishes every request the built agent sends to source, which is
where a fork starts from.
Sourcepub const fn max_iterations(self, limit: usize) -> Self
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.
Sourcepub const fn context_policy(self, policy: ContextPolicy) -> Self
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.
Sourcepub const fn config(self, config: AgentConfig) -> Self
pub const fn config(self, config: AgentConfig) -> Self
Sets the full agent configuration.
Sourcepub fn context_source<S: ContextSource>(self, source: S) -> Self
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.
Sourcepub fn live_source<S: LiveSource>(self, source: S) -> Self
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.
Sourcepub fn subagents(self, runs: SubagentRuns) -> Self
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).
Sourcepub fn approver(self, approver: impl Approver) -> Self
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.
Sourcepub fn instruction(self, content: impl Into<String>) -> Self
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.
Sourcepub fn tagged_instruction(self, tag: &'static str, content: &str) -> Self
pub fn tagged_instruction(self, tag: &'static str, content: &str) -> Self
Appends an instruction to the system prompt wrapped in a tag tag.
Sourcepub fn external_content(
self,
name: &'static str,
content: ExternalContent,
) -> Self
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.
Sourcepub fn system_layout(self, layout: SystemLayout) -> Self
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.
Sourcepub fn request_parameters(self, parameters: Parameters) -> Self
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.
Sourcepub fn prompt_cache(self, cache: PromptCache) -> Self
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.
Sourcepub fn claude_cache_ttl(self, ttl: ClaudePromptCacheTtl) -> Self
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.
Sourcepub fn cache_lifetime(self, lifetime: CacheLifetime) -> Self
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.
Sourcepub fn clock(self, clock: impl Clock) -> Self
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.
Sourcepub fn restore_dir(self, path: impl Into<PathBuf>) -> Self
pub fn restore_dir(self, path: impl Into<PathBuf>) -> Self
Sets the directory files are restored from after a compaction.
Sourcepub fn file_references(self, references: impl FileReferences) -> Self
pub fn file_references(self, references: impl FileReferences) -> Self
Sets how a compaction’s file index finds the files tool results name.
Sourcepub fn handoff<S2: HandoffSchema>(self) -> AgentBuilder<M, H, C, S2>
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.
Sourcepub fn compaction_model<C2: LanguageModel>(
self,
model: C2,
parameters: Parameters,
) -> AgentBuilder<M, H, C2, Schema>
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.
Sourcepub fn hooks<NH: Hooks>(self, hooks: NH) -> AgentBuilder<M, NH, C, Schema>
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).
Sourcepub fn tool<T: Tool + 'static>(self, tool: T) -> Self
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.
Sourcepub fn forked_from(self, fork: &ForkSnapshot) -> Self
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.
Sourcepub fn tool_definitions(&self) -> Vec<ToolDefinition>
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.