pub struct AgentBuilder<M, ToolState = NoToolConfig>where
M: CompletionModel,{ /* 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, ToolState> AgentBuilder<M, ToolState>where
M: CompletionModel,
impl<M, ToolState> AgentBuilder<M, ToolState>where
M: CompletionModel,
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 total model-call budget, including the initial call and every retry or continuation. Zero permits no model calls.
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 output_mode(self, mode: OutputMode) -> Self
pub fn output_mode(self, mode: OutputMode) -> Self
Set how output_schema is enforced — OutputMode::Tool (output as a
tool call, the default when the agent has tools), OutputMode::Native
(provider structured output), or OutputMode::Prompted (see #1928).
Has no effect unless output_schema/output_schema_raw is also set.
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
or per-request via crate::agent::prompt_request::PromptRequest::conversation.
If neither is set, memory is silently bypassed.
Sourcepub fn conversation(self, id: impl Into<String>) -> Self
pub fn conversation(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.
Sourcepub fn add_hook<H>(self, hook: H) -> Selfwhere
H: AgentHook<M> + 'static,
pub fn add_hook<H>(self, hook: H) -> Selfwhere
H: AgentHook<M> + 'static,
Attach a default hook to the agent. Each call appends to the agent’s hook
stack; hooks run for every prompt request (unless more are added per
request) in registration order. How their results compose is
event-dependent: CompletionCall request patches accumulate and merge,
ToolCall/ToolResult rewrites chain, and only observe-only/recovery
events use first-non-Continue-wins. See the
hook module docs.
Source§impl<M> AgentBuilder<M, NoToolConfig>where
M: CompletionModel,
impl<M> AgentBuilder<M, NoToolConfig>where
M: CompletionModel,
Source§impl<M> AgentBuilder<M, NoToolConfig>where
M: CompletionModel,
impl<M> AgentBuilder<M, NoToolConfig>where
M: CompletionModel,
Sourcepub fn tool_server_handle(
self,
handle: ToolServerHandle,
) -> AgentBuilder<M, WithToolServerHandle>
pub fn tool_server_handle( self, handle: ToolServerHandle, ) -> AgentBuilder<M, 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, WithBuilderTools>
pub fn tool( self, tool: impl Tool + 'static, ) -> AgentBuilder<M, 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, WithBuilderTools>
pub fn tools( self, tools: Vec<Box<dyn ToolDyn>>, ) -> AgentBuilder<M, 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, WithBuilderTools>
Available on crate feature rmcp only.
pub fn rmcp_tool( self, tool: Tool, client: ServerSink, ) -> AgentBuilder<M, WithBuilderTools>
rmcp only.Add an MCP tool (from rmcp) to the agent, bounded by
DEFAULT_MCP_TOOL_TIMEOUT
(see issue #1914). Use rmcp_tool_with_timeout
to change or disable it.
Transitions the builder to the WithBuilderTools state.
Sourcepub fn rmcp_tool_with_timeout(
self,
tool: Tool,
client: ServerSink,
timeout: impl Into<Option<Duration>>,
) -> AgentBuilder<M, WithBuilderTools>
Available on crate feature rmcp only.
pub fn rmcp_tool_with_timeout( self, tool: Tool, client: ServerSink, timeout: impl Into<Option<Duration>>, ) -> AgentBuilder<M, WithBuilderTools>
rmcp only.Add an MCP tool (from rmcp) with a per-call timeout (see issue #1914).
Pass a Duration to bound the call, or None to
disable the timeout (unbounded). On timeout the call resolves to a tool
error the agent can recover from instead of blocking forever.
Transitions the builder to the WithBuilderTools state.
Sourcepub fn rmcp_tools(
self,
tools: Vec<Tool>,
client: ServerSink,
) -> AgentBuilder<M, WithBuilderTools>
Available on crate feature rmcp only.
pub fn rmcp_tools( self, tools: Vec<Tool>, client: ServerSink, ) -> AgentBuilder<M, WithBuilderTools>
rmcp only.Add an array of MCP tools (from rmcp) to the agent, each bounded by
DEFAULT_MCP_TOOL_TIMEOUT
(see issue #1914). Use rmcp_tools_with_timeout
to change or disable it.
Transitions the builder to the WithBuilderTools state.
Sourcepub fn rmcp_tools_with_timeout(
self,
tools: Vec<Tool>,
client: ServerSink,
timeout: impl Into<Option<Duration>>,
) -> AgentBuilder<M, WithBuilderTools>
Available on crate feature rmcp only.
pub fn rmcp_tools_with_timeout( self, tools: Vec<Tool>, client: ServerSink, timeout: impl Into<Option<Duration>>, ) -> AgentBuilder<M, WithBuilderTools>
rmcp only.Add an array of MCP tools (from rmcp) with a per-call timeout (see
issue #1914).
Pass a Duration to bound calls, or None to
disable the timeout (unbounded). On timeout a call resolves to a tool
error the agent can recover from instead of blocking forever.
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, WithBuilderTools>
pub fn dynamic_tools( self, sample: usize, dynamic_tools: impl VectorStoreIndexDyn + Send + Sync + 'static, toolset: ToolSet, ) -> AgentBuilder<M, 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.
Source§impl<M> AgentBuilder<M, WithToolServerHandle>where
M: CompletionModel,
impl<M> AgentBuilder<M, WithToolServerHandle>where
M: CompletionModel,
Source§impl<M> AgentBuilder<M, WithBuilderTools>where
M: CompletionModel,
impl<M> AgentBuilder<M, WithBuilderTools>where
M: CompletionModel,
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, each bounded by
DEFAULT_MCP_TOOL_TIMEOUT
(see issue #1914). Use rmcp_tools_with_timeout
to change or disable it.
Sourcepub fn rmcp_tools_with_timeout(
self,
tools: Vec<Tool>,
client: ServerSink,
timeout: impl Into<Option<Duration>>,
) -> Self
Available on crate feature rmcp only.
pub fn rmcp_tools_with_timeout( self, tools: Vec<Tool>, client: ServerSink, timeout: impl Into<Option<Duration>>, ) -> Self
rmcp only.Add an array of MCP tools (from rmcp) with a per-call timeout (see
issue #1914).
Pass a Duration to bound calls, or None to
disable the timeout (unbounded). On timeout a call resolves to a tool
error the agent can recover from instead of blocking forever.
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, ToolState = NoToolConfig> !RefUnwindSafe for AgentBuilder<M, ToolState>
impl<M, ToolState = NoToolConfig> !UnwindSafe for AgentBuilder<M, ToolState>
impl<M, ToolState> Freeze for AgentBuilder<M, ToolState>
impl<M, ToolState> Send for AgentBuilder<M, ToolState>where
ToolState: Send,
impl<M, ToolState> Sync for AgentBuilder<M, ToolState>where
ToolState: Sync,
impl<M, ToolState> Unpin for AgentBuilder<M, ToolState>
impl<M, ToolState> UnsafeUnpin for AgentBuilder<M, ToolState>where
M: UnsafeUnpin,
ToolState: UnsafeUnpin,
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
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