Skip to main content

AgentLoopBuilder

Struct AgentLoopBuilder 

Source
pub struct AgentLoopBuilder<Ctx, P, H, M, S> { /* private fields */ }
Expand description

Builder for constructing an AgentLoop.

§Example

let agent = AgentLoop::builder()
    .provider(my_provider)
    .tools(my_tools)
    .config(AgentConfig::default())
    .build();

Implementations§

Source§

impl<Ctx> AgentLoopBuilder<Ctx, (), (), (), ()>

Source

pub fn new() -> Self

Create a new builder with no components set.

Source§

impl<Ctx, P, H, M, S> AgentLoopBuilder<Ctx, P, H, M, S>

Source

pub fn provider<P2: LlmProvider>( self, provider: P2, ) -> AgentLoopBuilder<Ctx, P2, H, M, S>

Set the LLM provider.

Source

pub fn tools(self, tools: ToolRegistry<Ctx>) -> Self

Set the tool registry.

Source

pub fn hooks<H2: AgentHooks>( self, hooks: H2, ) -> AgentLoopBuilder<Ctx, P, H2, M, S>

Set the agent hooks.

Source

pub fn message_store<M2: MessageStore>( self, message_store: M2, ) -> AgentLoopBuilder<Ctx, P, H, M2, S>

Set the message store.

Source

pub fn state_store<S2: StateStore>( self, state_store: S2, ) -> AgentLoopBuilder<Ctx, P, H, M, S2>

Set the state store.

Source

pub fn event_store(self, store: Arc<dyn EventStore>) -> Self

Set the authoritative event store for the loop lifecycle.

Source

pub fn event_authority(self, authority: Arc<dyn EventAuthority>) -> Self

Set the event authority for envelope creation.

When set, the authority governs how events are wrapped in envelopes (sequence numbers, event IDs, timestamps). In server mode the authority seeds sequences from durable storage so ordering is continuous across turns within a thread.

When not set, a fresh LocalEventAuthority starting at sequence 0 is created for each run.

Source

pub fn execution_store(self, store: impl ToolExecutionStore + 'static) -> Self

Set the execution store for tool idempotency.

When set, tool executions will be tracked using a write-ahead pattern:

  1. Record execution intent BEFORE calling the tool
  2. Update with result AFTER completion
  3. On retry, return cached result if execution already completed
§Example
use agent_sdk::{builder, stores::InMemoryExecutionStore};

let agent = builder()
    .provider(my_provider)
    .execution_store(InMemoryExecutionStore::new())
    .build();
Source

pub fn execution_store_shared(self, store: Arc<dyn ToolExecutionStore>) -> Self

Set the execution store from a shared Arc.

Use this when the caller needs to retain a handle to the store (for inspection, pre-population, or sharing across loops). See Self::execution_store for the standard owned form.

Source

pub fn audit_sink(self, sink: impl ToolAuditSink + 'static) -> Self

Set the authoritative tool audit sink.

When set, the agent loop emits a ToolAuditRecord at every tool lifecycle transition — blocked, requires-confirmation, cached, replayed, invalidated, completed, and persistence-failed. This gives servers a complete audit trail without relying on the weaker post_tool_use hook.

Defaults to NoopAuditSink when not set.

Source

pub fn audit_sink_shared(self, sink: Arc<dyn ToolAuditSink>) -> Self

Set the audit sink from a shared Arc.

Use this when the caller needs to retain a handle to the sink (e.g. to inspect captured records from tests, or to share a single durable sink across multiple agent loops). Passing an Arc<dyn ToolAuditSink> here avoids the Arc<Arc<S>> double wrap that happens when callers Arc::clone(&sink) a sink they already wrapped and hand it to Self::audit_sink.

See Self::audit_sink for the standard owned form.

Source

pub fn observability_store( self, store: impl ObservabilityStore + 'static, ) -> Self

Available on crate feature otel only.

Set the observability store for GenAI payload capture.

Source

pub fn config(self, config: AgentConfig) -> Self

Set the agent configuration.

Source

pub const fn with_compaction(self, config: CompactionConfig) -> Self

Enable context compaction with the given configuration.

When enabled, the agent will automatically compact conversation history when it exceeds the configured token threshold.

§Example
use agent_sdk::{builder, context::CompactionConfig};

let agent = builder()
    .provider(my_provider)
    .with_compaction(CompactionConfig::default())
    .build();
Source

pub fn with_auto_compaction(self) -> Self

Enable context compaction with default settings.

This is a convenience method equivalent to:

builder.with_compaction(CompactionConfig::default())
Source

pub fn with_custom_compactor( self, compactor: impl ContextCompactor + 'static, ) -> Self

Override the default compactor with a custom implementation.

Source

pub fn with_skill(self, skill: Skill) -> Self
where Ctx: Send + Sync + 'static,

Available on crate feature skills only.

Apply a skill configuration.

This merges the skill’s system prompt with the existing configuration and filters tools based on the skill’s allowed/denied lists.

Available when the skills feature is enabled.

§Example
let skill = Skill::new("code-review", "You are a code reviewer...")
    .with_denied_tools(vec!["bash".into()]);

let agent = builder()
    .provider(provider)
    .tools(tools)
    .with_skill(skill)
    .build();
Source§

impl<Ctx, P> AgentLoopBuilder<Ctx, P, (), (), ()>
where Ctx: Send + Sync + 'static, P: LlmProvider + 'static,

Source

pub fn build( self, ) -> AgentLoop<Ctx, P, DefaultHooks, InMemoryStore, InMemoryStore>

Build the agent loop with default hooks and in-memory message/state stores.

This is a convenience method that uses:

  • DefaultHooks for hooks
  • InMemoryStore for message store
  • InMemoryStore for state store
  • InMemoryEventStore for the event store, when none was set
  • AgentConfig::default() if no config is set

Supplying an event_store is optional for this convenience build — a fresh InMemoryEventStore is used by default so the 30-second path needs no Arc ceremony. Wire a durable store explicitly when you need persistence across process restarts.

§Panics

Panics if a provider has not been set.

Source§

impl<Ctx, P, H, M, S> AgentLoopBuilder<Ctx, P, H, M, S>
where Ctx: Send + Sync + 'static, P: LlmProvider + 'static, H: AgentHooks + 'static, M: MessageStore + 'static, S: StateStore + 'static,

Source

pub fn build_with_stores(self) -> AgentLoop<Ctx, P, H, M, S>

Build the agent loop with all custom components.

§Panics

Panics if any of the following have not been set:

  • provider
  • hooks
  • message_store
  • state_store
  • event_store

Trait Implementations§

Source§

impl<Ctx> Default for AgentLoopBuilder<Ctx, (), (), (), ()>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<Ctx, P, H, M, S> !RefUnwindSafe for AgentLoopBuilder<Ctx, P, H, M, S>

§

impl<Ctx, P, H, M, S> !UnwindSafe for AgentLoopBuilder<Ctx, P, H, M, S>

§

impl<Ctx, P, H, M, S> Freeze for AgentLoopBuilder<Ctx, P, H, M, S>
where P: Freeze, H: Freeze, M: Freeze, S: Freeze,

§

impl<Ctx, P, H, M, S> Send for AgentLoopBuilder<Ctx, P, H, M, S>
where P: Send, H: Send, M: Send, S: Send,

§

impl<Ctx, P, H, M, S> Sync for AgentLoopBuilder<Ctx, P, H, M, S>
where P: Sync, H: Sync, M: Sync, S: Sync,

§

impl<Ctx, P, H, M, S> Unpin for AgentLoopBuilder<Ctx, P, H, M, S>
where P: Unpin, H: Unpin, M: Unpin, S: Unpin,

§

impl<Ctx, P, H, M, S> UnsafeUnpin for AgentLoopBuilder<Ctx, P, H, M, S>

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FutureExt for T

Source§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
Source§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
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, 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<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