pub struct AgentBuilder<M, P = (), ToolState = NoToolConfig>where
M: CompletionModel,
P: PromptHook<M>,{ /* private fields */ }Expand description
A builder for creating an agent
The builder uses a typestate pattern to enforce that tool configuration
is done in a mutually exclusive way: either provide a pre-existing
ToolServerHandle, or add tools via the builder API, but not both.
§Example
use rig_core::{agent::AgentBuilder, client::{CompletionClient, ProviderClient}, providers::openai};
let openai = openai::Client::from_env()?;
let model = openai.completion_model(openai::GPT_5_2);
// Configure the agent
let agent = AgentBuilder::new(model)
.preamble("System prompt")
.context("Context document 1")
.context("Context document 2")
.temperature(0.8)
.build();Implementations§
Source§impl<M, P, ToolState> AgentBuilder<M, P, ToolState>where
M: CompletionModel,
P: PromptHook<M>,
impl<M, P, ToolState> AgentBuilder<M, P, ToolState>where
M: CompletionModel,
P: PromptHook<M>,
Sourcepub fn description(self, description: &str) -> Self
pub fn description(self, description: &str) -> Self
Set the description of the agent
Sourcepub fn without_preamble(self) -> Self
pub fn without_preamble(self) -> Self
Remove the system prompt
Sourcepub fn append_preamble(self, doc: &str) -> Self
pub fn append_preamble(self, doc: &str) -> Self
Append to the preamble of the agent
Sourcepub fn dynamic_context(
self,
sample: usize,
dynamic_context: impl VectorStoreIndexDyn + Send + Sync + 'static,
) -> Self
pub fn dynamic_context( self, sample: usize, dynamic_context: impl VectorStoreIndexDyn + Send + Sync + 'static, ) -> Self
Add some dynamic context to the agent. On each prompt, sample documents from the
dynamic context will be inserted in the request.
Sourcepub fn tool_choice(self, tool_choice: ToolChoice) -> Self
pub fn tool_choice(self, tool_choice: ToolChoice) -> Self
Set the tool choice for the agent
Sourcepub fn default_max_turns(self, default_max_turns: usize) -> Self
pub fn default_max_turns(self, default_max_turns: usize) -> Self
Set the default maximum depth that an agent will use for multi-turn.
Sourcepub fn temperature(self, temperature: f64) -> Self
pub fn temperature(self, temperature: f64) -> Self
Set the temperature of the model
Sourcepub fn max_tokens(self, max_tokens: u64) -> Self
pub fn max_tokens(self, max_tokens: u64) -> Self
Set the maximum number of tokens for the completion
Sourcepub fn additional_params(self, params: Value) -> Self
pub fn additional_params(self, params: Value) -> Self
Set additional parameters to be passed to the model
Sourcepub fn output_schema<T>(self) -> Selfwhere
T: JsonSchema,
pub fn output_schema<T>(self) -> Selfwhere
T: JsonSchema,
Set the output schema for structured output. When set, providers that support native structured outputs will constrain the model’s response to match this schema.
Sourcepub fn output_schema_raw(self, schema: Schema) -> Self
pub fn output_schema_raw(self, schema: Schema) -> Self
Set the output schema for structured output. In comparison to AgentBuilder::schema() which requires type annotation, you can put in any schema you’d like here.
Sourcepub fn memory<B>(self, memory: B) -> Selfwhere
B: ConversationMemory + 'static,
pub fn memory<B>(self, memory: B) -> Selfwhere
B: ConversationMemory + 'static,
Attach a ConversationMemory backend.
When set, the agent will automatically load prior conversation history before
each prompt and append the new turn after a successful response. A
conversation_id must be supplied either via AgentBuilder::conversation_id
or per-request via crate::agent::prompt_request::PromptRequest::conversation.
If neither is set, memory is silently bypassed.
Sourcepub fn conversation_id(self, id: impl Into<String>) -> Self
pub fn conversation_id(self, id: impl Into<String>) -> Self
Set a default conversation id used when none is provided per-request.
Most agents are reused across users or threads; prefer setting the id
per-request via crate::agent::prompt_request::PromptRequest::conversation.
Source§impl<M> AgentBuilder<M, (), NoToolConfig>where
M: CompletionModel,
impl<M> AgentBuilder<M, (), NoToolConfig>where
M: CompletionModel,
Source§impl<M, P> AgentBuilder<M, P, NoToolConfig>where
M: CompletionModel,
P: PromptHook<M>,
impl<M, P> AgentBuilder<M, P, NoToolConfig>where
M: CompletionModel,
P: PromptHook<M>,
Sourcepub fn tool_server_handle(
self,
handle: ToolServerHandle,
) -> AgentBuilder<M, P, WithToolServerHandle>
pub fn tool_server_handle( self, handle: ToolServerHandle, ) -> AgentBuilder<M, P, WithToolServerHandle>
Set a pre-existing ToolServerHandle for the agent.
After calling this method, tool-adding methods (.tool(), .tools(), etc.)
will not be available. Use this when you want to share a ToolServer
between multiple agents or have pre-configured tools.
Sourcepub fn tool(
self,
tool: impl Tool + 'static,
) -> AgentBuilder<M, P, WithBuilderTools>
pub fn tool( self, tool: impl Tool + 'static, ) -> AgentBuilder<M, P, WithBuilderTools>
Add a static tool to the agent.
This transitions the builder to the WithBuilderTools state, where
additional tools can be added but tool_server_handle() is no longer available.
Sourcepub fn tools(
self,
tools: Vec<Box<dyn ToolDyn>>,
) -> AgentBuilder<M, P, WithBuilderTools>
pub fn tools( self, tools: Vec<Box<dyn ToolDyn>>, ) -> AgentBuilder<M, P, WithBuilderTools>
Add a vector of boxed static tools to the agent.
This is useful when you need to dynamically add static tools to the agent.
Transitions the builder to the WithBuilderTools state.
Sourcepub fn rmcp_tool(
self,
tool: Tool,
client: ServerSink,
) -> AgentBuilder<M, P, WithBuilderTools>
Available on crate feature rmcp only.
pub fn rmcp_tool( self, tool: Tool, client: ServerSink, ) -> AgentBuilder<M, P, WithBuilderTools>
rmcp only.Add an MCP tool (from rmcp) to the agent.
Transitions the builder to the WithBuilderTools state.
Sourcepub fn rmcp_tools(
self,
tools: Vec<Tool>,
client: ServerSink,
) -> AgentBuilder<M, P, WithBuilderTools>
Available on crate feature rmcp only.
pub fn rmcp_tools( self, tools: Vec<Tool>, client: ServerSink, ) -> AgentBuilder<M, P, WithBuilderTools>
rmcp only.Add an array of MCP tools (from rmcp) to the agent.
Transitions the builder to the WithBuilderTools state.
Sourcepub fn dynamic_tools(
self,
sample: usize,
dynamic_tools: impl VectorStoreIndexDyn + Send + Sync + 'static,
toolset: ToolSet,
) -> AgentBuilder<M, P, WithBuilderTools>
pub fn dynamic_tools( self, sample: usize, dynamic_tools: impl VectorStoreIndexDyn + Send + Sync + 'static, toolset: ToolSet, ) -> AgentBuilder<M, P, WithBuilderTools>
Add some dynamic tools to the agent. On each prompt, sample tools from the
dynamic toolset will be inserted in the request.
Transitions the builder to the WithBuilderTools state.
Sourcepub fn hook<P2>(self, hook: P2) -> AgentBuilder<M, P2, NoToolConfig>where
P2: PromptHook<M>,
pub fn hook<P2>(self, hook: P2) -> AgentBuilder<M, P2, NoToolConfig>where
P2: PromptHook<M>,
Set the default hook for the agent.
This hook will be used for all prompt requests unless overridden
via .with_hook() on the request.
Source§impl<M, P> AgentBuilder<M, P, WithToolServerHandle>where
M: CompletionModel,
P: PromptHook<M>,
impl<M, P> AgentBuilder<M, P, WithToolServerHandle>where
M: CompletionModel,
P: PromptHook<M>,
Source§impl<M, P> AgentBuilder<M, P, WithBuilderTools>where
M: CompletionModel,
P: PromptHook<M>,
impl<M, P> AgentBuilder<M, P, WithBuilderTools>where
M: CompletionModel,
P: PromptHook<M>,
Sourcepub fn tools(self, tools: Vec<Box<dyn ToolDyn>>) -> Self
pub fn tools(self, tools: Vec<Box<dyn ToolDyn>>) -> Self
Add a vector of boxed static tools to the agent.
Sourcepub fn rmcp_tools(self, tools: Vec<Tool>, client: ServerSink) -> Self
Available on crate feature rmcp only.
pub fn rmcp_tools(self, tools: Vec<Tool>, client: ServerSink) -> Self
rmcp only.Add an array of MCP tools (from rmcp) to the agent.
Sourcepub fn dynamic_tools(
self,
sample: usize,
dynamic_tools: impl VectorStoreIndexDyn + Send + Sync + 'static,
toolset: ToolSet,
) -> Self
pub fn dynamic_tools( self, sample: usize, dynamic_tools: impl VectorStoreIndexDyn + Send + Sync + 'static, toolset: ToolSet, ) -> Self
Add some dynamic tools to the agent. On each prompt, sample tools from the
dynamic toolset will be inserted in the request.
Auto Trait Implementations§
impl<M, P, ToolState> Freeze for AgentBuilder<M, P, ToolState>
impl<M, P = (), ToolState = NoToolConfig> !RefUnwindSafe for AgentBuilder<M, P, ToolState>
impl<M, P, ToolState> Send for AgentBuilder<M, P, ToolState>where
ToolState: Send,
impl<M, P, ToolState> Sync for AgentBuilder<M, P, ToolState>where
ToolState: Sync,
impl<M, P, ToolState> Unpin for AgentBuilder<M, P, ToolState>
impl<M, P, ToolState> UnsafeUnpin for AgentBuilder<M, P, ToolState>
impl<M, P = (), ToolState = NoToolConfig> !UnwindSafe for AgentBuilder<M, P, ToolState>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more