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
impl Agent
Sourcepub fn new(client: Arc<dyn LlmClient>) -> Self
pub fn new(client: Arc<dyn LlmClient>) -> Self
Start building an agent around a provider client.
Sourcepub fn tools(self, tools: Vec<Arc<dyn Tool>>) -> Self
pub fn tools(self, tools: Vec<Arc<dyn Tool>>) -> Self
Tools the model may call. Replaces any earlier list.
Sourcepub fn system_prompt(self, prompt: impl Into<String>) -> Self
pub fn system_prompt(self, prompt: impl Into<String>) -> Self
System prompt prepended to every run.
Sourcepub const fn max_steps(self, max_steps: u32) -> Self
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.
Sourcepub const fn max_tokens(self, max_tokens: u32) -> Self
pub const fn max_tokens(self, max_tokens: u32) -> Self
Token budget for the whole run (estimated + provider-reported).
Sourcepub const fn max_duration(self, limit: Duration) -> Self
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.
Sourcepub const fn per_tool_output_limit(self, limit: usize) -> Self
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.
Sourcepub const fn temperature(self, temperature: f32) -> Self
pub const fn temperature(self, temperature: f32) -> Self
Sampling temperature for model calls.
Sourcepub fn hook(self, hook: Arc<dyn AgentHooks>) -> Self
pub fn hook(self, hook: Arc<dyn AgentHooks>) -> Self
Add a lifecycle hook. Hooks run in the order they were added.
Sourcepub fn policy(self, policy: Arc<dyn ToolPolicy>) -> Self
pub fn policy(self, policy: Arc<dyn ToolPolicy>) -> Self
Decide per call whether the agent may act. Defaults to
AllowAll.
Sourcepub const fn loop_guard(self, guard: LoopGuard) -> Self
pub const fn loop_guard(self, guard: LoopGuard) -> Self
Loop-detection thresholds. Use LoopGuard::disabled to turn it off.
Sourcepub const fn parallel_tool_calls(self, parallel: bool) -> Self
pub const fn parallel_tool_calls(self, parallel: bool) -> Self
Run the calls of one round concurrently (the default) or one by one.
Sourcepub fn memory(self, store: Arc<dyn MemoryStore>, scope: MemoryScope) -> Self
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.
Sourcepub fn skills(self, skills: impl Into<Arc<[Skill]>>) -> Self
pub fn skills(self, skills: impl Into<Arc<[Skill]>>) -> Self
Offer skills: an index in the system prompt plus the load_skill
tool.
Sourcepub const fn compaction(self, compaction: Compaction) -> Self
pub const fn compaction(self, compaction: Compaction) -> Self
Summarize long session transcripts before a run. Applies to
Agent::run_in_session.
Sourcepub fn run_id(self, run_id: RunId) -> Self
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.
Sourcepub async fn run(&self, user_input: &str) -> Result<AgentOutcome, AgentError>
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.
Sourcepub async fn run_turn(
&self,
history: Vec<ChatMessage>,
user_input: &str,
) -> Result<AgentTurn, AgentError>
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.
Sourcepub async fn run_in_session(
&self,
store: &dyn SessionStore,
session_id: &SessionId,
user_input: &str,
) -> Result<AgentTurn, AgentError>
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.
Sourcepub async fn resume(
&self,
state: RunState,
decisions: Vec<ApprovalDecision>,
) -> Result<AgentTurn, AgentError>
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.
Sourcepub async fn resume_in_session(
&self,
store: &dyn SessionStore,
state: RunState,
decisions: Vec<ApprovalDecision>,
) -> Result<AgentTurn, AgentError>
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§
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> 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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<T> Scoped for T
impl<T> Scoped for T
Source§fn scope(ctx: &PolicyContext) -> ScopeQuery<'_, Self>
fn scope(ctx: &PolicyContext) -> ScopeQuery<'_, Self>
ScopeQuery for this type. Resolves the
registered scope at .load() time, not here.