Skip to main content

Agent

Struct Agent 

Source
pub struct Agent<Deps = (), Output = String> { /* private fields */ }
Expand description

The main agent type.

An agent wraps a model and provides:

  • System prompts and instructions
  • Tool registration and execution
  • Structured output parsing and validation
  • Retry logic for failures
  • Usage tracking and limits

§Type Parameters

  • Deps: Dependencies injected into tools and instruction functions.
  • Output: The output type (default: String).

Implementations§

Source§

impl<Deps, Output> Agent<Deps, Output>
where Deps: Send + Sync + 'static, Output: Send + Sync + 'static,

Source

pub fn model(&self) -> &dyn Model

Get the model.

Source

pub fn model_arc(&self) -> Arc<dyn Model>

Get the model as an Arc (for cloning into spawned tasks).

Source

pub fn name(&self) -> Option<&str>

Get agent name.

Source

pub fn model_settings(&self) -> &ModelSettings

Get model settings.

Source

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

Get registered tools.

Source

pub fn output_mode(&self) -> OutputMode

Get the output mode.

Source

pub fn has_tools(&self) -> bool

Check if the agent has tools.

Source

pub fn usage_limits(&self) -> Option<&UsageLimits>

Get usage limits.

Source

pub fn parallel_tool_calls(&self) -> bool

Check if parallel tool execution is enabled.

Source

pub fn max_concurrent_tools(&self) -> Option<usize>

Get the maximum number of concurrent tool calls.

Source

pub async fn run( &self, prompt: impl Into<UserContent>, deps: Deps, ) -> Result<AgentRunResult<Output>, AgentRunError>

Run the agent with a prompt.

§Arguments
  • prompt - The user prompt to send to the model.
  • deps - Dependencies to inject into tools and instructions.
§Returns

The agent’s output after completing the conversation.

Source

pub async fn run_with_options( &self, prompt: impl Into<UserContent>, deps: Deps, options: RunOptions, ) -> Result<AgentRunResult<Output>, AgentRunError>

Run with options.

Source

pub fn run_sync( &self, prompt: impl Into<UserContent>, deps: Deps, ) -> Result<AgentRunResult<Output>, AgentRunError>

Run synchronously (blocking).

Note: This requires a Tokio runtime to be available.

Source

pub async fn start_run( &self, prompt: impl Into<UserContent>, deps: Deps, options: RunOptions, ) -> Result<AgentRun<'_, Deps, Output>, AgentRunError>

Start a run that can be iterated.

This allows stepping through the agent’s execution manually.

Source

pub async fn run_stream( &self, prompt: impl Into<UserContent>, deps: Deps, ) -> Result<AgentStream, AgentRunError>

Run with streaming output.

Source

pub async fn run_stream_with_options( &self, prompt: impl Into<UserContent>, deps: Deps, options: RunOptions, ) -> Result<AgentStream, AgentRunError>

Run stream with options.

Source

pub fn static_system_prompt(&self) -> &str

Get the static system prompt.

Trait Implementations§

Source§

impl<Deps, Output> Debug for Agent<Deps, Output>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<Deps: Send + Sync + 'static> Default for Agent<Deps, String>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<Deps, Output> Freeze for Agent<Deps, Output>

§

impl<Deps = (), Output = String> !RefUnwindSafe for Agent<Deps, Output>

§

impl<Deps, Output> Send for Agent<Deps, Output>
where Deps: Send, Output: Send,

§

impl<Deps, Output> Sync for Agent<Deps, Output>
where Deps: Sync, Output: Sync,

§

impl<Deps, Output> Unpin for Agent<Deps, Output>
where Deps: Unpin, Output: Unpin,

§

impl<Deps = (), Output = String> !UnwindSafe for Agent<Deps, Output>

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> 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, 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<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