Skip to main content

Agent

Struct Agent 

Source
pub struct Agent { /* private fields */ }
Expand description

The agent loop.

match agent.run("What is the weather in Chicago?").await? {
    AgentOutcome::Completed { text, .. } => println!("answer: {text}"),
    other => println!("stopped: {other:?}"),
}

Implementations§

Source§

impl Agent

Source

pub fn new(client: Arc<dyn LlmClient>) -> Self

Start building an agent around a provider client.

Source

pub fn tools(self, tools: Vec<Arc<dyn Tool>>) -> Self

Tools the model may call. Replaces any earlier list.

Source

pub fn tool(self, tool: Arc<dyn Tool>) -> Self

Add one tool to the list.

Source

pub fn system_prompt(self, prompt: impl Into<String>) -> Self

System prompt prepended to every run.

Source

pub const fn max_steps(self, max_steps: u32) -> Self

Maximum tool-execution rounds before the run stops with BudgetKind::Steps.

A step is one round in which the model requested tool calls; the final answering call does not consume a step.

Source

pub const fn max_tokens(self, max_tokens: u32) -> Self

Token budget for the whole run (estimated + provider-reported).

Source

pub const fn max_duration(self, limit: Duration) -> Self

Wall-clock limit for one run (or one resume). Model and tool calls still in flight at the deadline are cancelled; the run ends with BudgetKind::Deadline.

Source

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

Truncate any single tool output to this many characters.

One chatty tool must not eat the whole context window.

Source

pub const fn temperature(self, temperature: f32) -> Self

Sampling temperature for model calls.

Source

pub fn hook(self, hook: Arc<dyn AgentHooks>) -> Self

Add a lifecycle hook. Hooks run in the order they were added.

Source

pub fn policy(self, policy: Arc<dyn ToolPolicy>) -> Self

Decide per call whether the agent may act. Defaults to AllowAll.

Source

pub const fn loop_guard(self, guard: LoopGuard) -> Self

Loop-detection thresholds. Use LoopGuard::disabled to turn it off.

Source

pub const fn parallel_tool_calls(self, parallel: bool) -> Self

Run the calls of one round concurrently (the default) or one by one.

Source

pub fn memory(self, store: Arc<dyn MemoryStore>, scope: MemoryScope) -> Self

Give the agent persistent memory: a frozen snapshot in the system prompt plus the memory tool, both bound to scope.

Source

pub fn skills(self, skills: impl Into<Arc<[Skill]>>) -> Self

Offer skills: an index in the system prompt plus the load_skill tool.

Source

pub const fn compaction(self, compaction: Compaction) -> Self

Summarize long session transcripts before a run. Applies to Agent::run_in_session.

Source

pub fn run_id(self, run_id: RunId) -> Self

Use this id for the next run instead of generating one. Background jobs set it so a retried job keeps the same run id.

Source

pub async fn run(&self, user_input: &str) -> Result<AgentOutcome, AgentError>

Run the agent loop against one user message, with no prior history.

§Errors

Returns AgentError when a provider call fails (transport, auth, rate limit, malformed response) or memory cannot load. Budget exhaustion, loops, and approval pauses are not errors: they come back as AgentOutcome variants.

Source

pub async fn run_turn( &self, history: Vec<ChatMessage>, user_input: &str, ) -> Result<AgentTurn, AgentError>

Continue a transcript with one user message.

history is a transcript from an earlier AgentTurn::messages (no system messages needed: the agent adds its own).

§Errors

See Agent::run.

Source

pub async fn run_in_session( &self, store: &dyn SessionStore, session_id: &SessionId, user_input: &str, ) -> Result<AgentTurn, AgentError>

Run one turn inside a persisted session.

Loads the transcript, compacts it when Agent::compaction is set and the transcript is long, runs, then saves the new transcript. A run that pauses for approval saves nothing: Agent::resume_in_session saves once the run finishes. A new message sent to the session while a run waits for approval continues from the last saved transcript.

§Errors

See Agent::run; also fails when the store or the compaction call fails.

Source

pub async fn resume( &self, state: RunState, decisions: Vec<ApprovalDecision>, ) -> Result<AgentTurn, AgentError>

Continue a run that paused with AgentOutcome::AwaitingApproval.

Calls the policy allowed run; calls it denied report their reason; calls that needed approval follow decisions — a call with no decision counts as rejected. The deadline restarts on resume.

§Errors

Returns ErrorKind::Config when a decision names a call that is not waiting for approval, plus every error of Agent::run.

Source

pub async fn resume_in_session( &self, store: &dyn SessionStore, state: RunState, decisions: Vec<ApprovalDecision>, ) -> Result<AgentTurn, AgentError>

Agent::resume, then save the transcript to the run’s session.

§Errors

See Agent::resume; also fails when the store fails.

Trait Implementations§

Source§

impl Clone for Agent

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Agent

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for Agent

§

impl !UnwindSafe for Agent

§

impl Forget for Agent

§

impl Freeze for Agent

§

impl Send for Agent

§

impl Sync for Agent

§

impl Unpin for Agent

§

impl UnsafeUnpin for Agent

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
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: Sized + 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: Sized + 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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> Scoped for T
where T: Send + Sync + 'static,

Source§

fn scope(ctx: &PolicyContext) -> ScopeQuery<'_, Self>

Open a deferred ScopeQuery for this type. Resolves the registered scope at .load() time, not here.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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