Skip to main content

AgentHandle

Struct AgentHandle 

Source
pub struct AgentHandle<R: Runtime + 'static> { /* private fields */ }
Expand description

Handle to a running agent.

Wraps the agent’s lifecycle: creation, chat, and shutdown.

Call shutdown() for a clean, error-reported shutdown. If the handle is dropped without calling shutdown(), a best-effort background shutdown is spawned via tokio::spawn — the Python agent will be cleaned up, but errors are only logged, not returned.

Most methods take &self — interior mutability is used where needed so multiple concurrent operations can share a single handle.

§Mutex choice

This type uses std::sync::Mutex rather than tokio::sync::Mutex because every lock acquisition is a brief, synchronous operation (pointer swap or clone) that never spans an .await point. For these microsecond critical sections, std::sync::Mutex is both simpler and lower-overhead than the async alternative.

Implementations§

Source§

impl<R: Runtime> AgentHandle<R>

Source

pub async fn new( runtime: Arc<R>, config: AgentConfig, registry: Option<Arc<ToolRegistry>>, hook_runner: Option<Arc<Hooks>>, policy_handler: Option<Arc<dyn AskUserHandler>>, ) -> Result<Self, Error>

Create a new agent from the given runtime and configuration.

This sends a CreateAgent command to the Python runtime, waits for quota availability, and returns the handle.

§Errors

Returns a Error if agent creation fails (e.g. invalid config, Python error, or quota exceeded).

Source

pub async fn chat( &self, content: impl Into<Content>, ) -> Result<ChatResponseHandle, Error>

Send a message and receive a streaming response.

Accepts any type that converts into Content: &str, String, Image, Document, Audio, Video, or a Vec<ContentPrimitive> for multimodal input.

Automatically backs off on quota limits (HTTP 429).

§Errors

Returns a Error on chat failure (Python error, timeout, etc.).

Source

pub async fn chat_text( &self, message: impl Into<Content>, ) -> Result<String, Error>

Send a message and return the final text response.

This is a convenience wrapper around chat that drains the streaming response into a single String. If tools were associated with the agent at creation time, the Python runtime handles tool execution automatically.

§Errors

Returns Error if the chat turn fails or stream errors occur.

Source

pub fn conversation_id(&self) -> Option<String>

Return the current conversation ID, if one has been set.

Returns a cloned String because the underlying value is behind a Mutex (interior mutability for &self access).

Source

pub fn set_conversation_id(&self, id: String)

Set the conversation ID (called when the SDK assigns one).

Takes &self rather than &mut self so the handle can be shared across concurrent tasks.

Source

pub fn is_started(&self) -> bool

Check whether the agent has been started and is not yet shut down.

Source

pub const fn id(&self) -> AgentId

Return the agent’s unique identifier.

Source

pub const fn config(&self) -> &AgentConfig

Return a reference to the agent’s configuration.

Source

pub async fn cancel(&self) -> Result<(), Error>

Interrupt the active chat prompt execution.

§Errors

Returns a Error if the cancellation call fails.

Source

pub async fn wait_for_idle(&self) -> Result<(), Error>

Wait for the conversation or active run to stabilize and become idle.

§Errors

Returns a Error if the wait call fails.

Source

pub async fn history(&self) -> Result<Vec<ConversationMessage>, Error>

Retrieve the conversation’s message history.

§Errors

Returns Error if the query fails.

Source

pub async fn turn_count(&self) -> Result<u32, Error>

Return the number of completed turns in the conversation.

§Errors

Returns Error if the query fails.

Source

pub async fn total_usage(&self) -> Result<UsageMetadata, Error>

Return cumulative token usage across all turns.

§Errors

Returns Error if the query fails.

Source

pub async fn last_turn_usage(&self) -> Result<UsageMetadata, Error>

Return token usage from the most recent turn only.

§Errors

Returns Error if the query fails.

Source

pub async fn clear_history(&self) -> Result<(), Error>

Clear the conversation history and reset state.

§Errors

Returns Error if the operation fails.

Source

pub async fn last_response(&self) -> Result<Option<String>, Error>

Return the text of the last model response, if any.

§Errors

Returns Error if the query fails.

Source

pub async fn compaction_indices(&self) -> Result<Vec<u32>, Error>

Return the step indices at which conversation compaction occurred.

§Errors

Returns Error if the query fails.

Source

pub async fn delete(&self) -> Result<(), Error>

Delete the conversation and all associated state.

After calling this method, the agent handle is no longer usable for chat operations. This also marks the agent as shut down.

§Errors

Returns Error if the delete operation fails.

Source

pub async fn disconnect(&self) -> Result<(), Error>

Disconnect from the agent without deleting its state.

The agent’s conversation state is preserved but this handle can no longer send messages. Marks the agent as shut down.

§Errors

Returns Error if the disconnect operation fails.

Source

pub async fn is_idle(&self) -> Result<bool, Error>

Check whether the agent is currently idle (not running a turn).

§Errors

Returns Error if the query fails.

Source

pub fn get_last_structured_output(&self) -> Option<Value>

Return the structured output from the last chat response, if any.

Only populated after a chat() round-trip when the agent was configured with a response_schema and the model returned a valid JSON payload.

Source

pub fn get_last_usage(&self) -> Option<UsageMetadata>

Return the usage metadata from the last chat response, if any.

Source

pub async fn send(&self, content: impl Into<Content>) -> Result<(), Error>

Send a message without waiting for a response.

Fire-and-forget: the message is delivered to the agent but no streaming response is produced.

§Errors

Returns a Error if sending fails.

Source

pub async fn signal_idle(&self) -> Result<(), Error>

Signal that this agent is idle and ready to receive input.

§Errors

Returns a Error if the signal call fails.

Source

pub async fn wait_for_wakeup(&self, timeout: Duration) -> Result<bool, Error>

Wait for the agent to wake up, returning true if woken or false if the timeout elapsed.

§Errors

Returns a Error if the wait call fails.

Source

pub async fn shutdown(&self) -> Result<(), Error>

Gracefully shut down the agent.

This sends a ShutdownAgent command to the Python runtime, which calls __aexit__() on the SDK agent. The handle remains usable for read-only queries (e.g. is_started()) after shutdown.

§Errors

Returns a Error if shutdown fails. The is_shutdown flag is always set so the Drop impl will not emit a warning.

Source

pub async fn spawn_subagent( &self, config: AgentConfig, registry: impl Into<Option<ToolRegistry>>, ) -> Result<Self, Error>

Spawn a subagent from the given config, sharing this agent’s runtime.

If a ToolRegistry is provided and config.tools is empty, the registry’s definitions are automatically applied.

§Errors

Returns a Error if agent creation fails.

Trait Implementations§

Source§

impl<R: Runtime> Drop for AgentHandle<R>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

§

impl<R> !Freeze for AgentHandle<R>

§

impl<R> !RefUnwindSafe for AgentHandle<R>

§

impl<R> !UnwindSafe for AgentHandle<R>

§

impl<R> Send for AgentHandle<R>

§

impl<R> Sync for AgentHandle<R>

§

impl<R> Unpin for AgentHandle<R>

§

impl<R> UnsafeUnpin for AgentHandle<R>

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> Ungil for T
where T: Send,

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