Skip to main content

Agent

Struct Agent 

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

The main agent struct.

An agent combines an LLM provider, tools, memory, and steering into a single runtime that processes user input and produces output.

Use Agent::builder() to construct an agent.

Implementations§

Source§

impl Agent

Source

pub fn builder() -> AgentBuilder

Create a builder for constructing an agent.

Source

pub fn with_system(provider: impl Provider, system: impl Into<String>) -> Self

Create an agent with just a provider and system prompt.

This is a convenience shorthand equivalent to:

Agent::builder()
    .provider(provider)
    .system(system)
    .build()
    .unwrap()

All other settings use their defaults (in-memory memory, no tools, no guards, etc.). Use Agent::builder() for full customization.

§Example
use traitclaw_core::prelude::*;

let agent = Agent::with_system(provider, "You are a helpful assistant.");
§Panics

This method cannot panic under normal usage — the internal build() call only fails when no provider is set, and with_system always provides one.

Source

pub fn session(&self, id: impl Into<String>) -> AgentSession<'_>

Create a session bound to a specific session ID.

The returned AgentSession routes all memory operations through this session ID, providing conversation isolation.

let session = agent.session("user-123");
let output = session.say("Hello!").await?;
Source

pub fn session_auto(&self) -> AgentSession<'_>

Create a session with an auto-generated UUID v4 session ID.

Useful when you want isolated conversations without managing IDs.

Source

pub fn memory(&self) -> &dyn Memory

Returns a reference to the agent’s memory store.

Useful for inspecting conversation history in tests or building custom conversation management on top of the agent.

Source

pub async fn run(&self, input: &str) -> Result<AgentOutput>

Run the agent with user input and return the final output.

This uses the "default" session for backward compatibility. For session isolation, use Agent::session() or Agent::session_auto().

§Errors

Returns an error if the provider fails, tool execution fails, memory operations fail, or max iterations are reached.

Source

pub fn stream( &self, input: &str, ) -> Pin<Box<dyn Stream<Item = Result<StreamEvent>> + Send>>

Run the agent and return a streaming response.

This uses the "default" session for backward compatibility.

Returns an AgentStream that yields StreamEvents incrementally, providing real-time output from the LLM.

Source

pub async fn run_structured<T>(&self, input: &str) -> Result<T>

Run the agent and return a structured output.

The LLM is instructed to return JSON matching type T’s schema. If deserialization fails, retries up to 3 times with feedback.

When the provider’s model supports native structured-output (model_info.supports_structured == true), the response_format is set on the CompletionRequest for guaranteed valid JSON. Otherwise, schema instructions are injected into the system prompt.

§⚠️ Stateless Mode

This method calls the provider directly, bypassing the agent runtime loop. Memory, guards, hints, context strategy, and usage tracking are not used. Use run() for full agent behavior.

§Errors

Returns an error if the provider fails or deserialization fails after retries.

Trait Implementations§

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 Freeze for Agent

§

impl !RefUnwindSafe for Agent

§

impl Send for Agent

§

impl Sync for Agent

§

impl Unpin for Agent

§

impl UnsafeUnpin for Agent

§

impl !UnwindSafe 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<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, 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