Skip to main content

ModelProvider

Trait ModelProvider 

Source
pub trait ModelProvider: Send + Sync {
    // Required methods
    fn name(&self) -> &str;
    fn max_context_tokens(&self) -> usize;
    fn max_output_tokens(&self) -> usize;
    fn generate<'life0, 'async_trait>(
        &'life0 self,
        messages: Vec<Message>,
        tools: Vec<ToolDefinition>,
        system_prompt: Option<String>,
    ) -> Pin<Box<dyn Future<Output = Result<ModelResponse, ProviderError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;

    // Provided methods
    fn estimate_token_count(&self, text: &str) -> usize { ... }
    fn estimate_message_tokens(&self, messages: &[Message]) -> usize { ... }
    fn generate_stream<'life0, 'async_trait>(
        &'life0 self,
        messages: Vec<Message>,
        tools: Vec<ToolDefinition>,
        system_prompt: Option<String>,
    ) -> Pin<Box<dyn Future<Output = Result<BoxStream<'static, Result<StreamEvent, ProviderError>>, ProviderError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait { ... }
}
Expand description

Trait for model providers

This trait abstracts over different LLM providers (Bedrock, Anthropic, etc.) allowing the Agent to work with any provider implementation.

A provider combines model metadata (name, token limits) with API interaction (generate, streaming). Use the builder to create agents:

let agent = Agent::builder()
    .bedrock(ClaudeSonnet4_5)
    .build()
    .await?;

Required Methods§

Source

fn name(&self) -> &str

Get the model name for display (e.g., “Claude Sonnet 4.5”)

Source

fn max_context_tokens(&self) -> usize

Maximum input context tokens for this model

Source

fn max_output_tokens(&self) -> usize

Maximum output tokens this model can generate

Source

fn generate<'life0, 'async_trait>( &'life0 self, messages: Vec<Message>, tools: Vec<ToolDefinition>, system_prompt: Option<String>, ) -> Pin<Box<dyn Future<Output = Result<ModelResponse, ProviderError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Send a request to the model and get a response

§Arguments
  • messages - The conversation history
  • tools - Available tools for the model to use
  • system_prompt - Optional system prompt

Provided Methods§

Source

fn estimate_token_count(&self, text: &str) -> usize

Estimate token count for text

Providers should implement this to match their model’s tokenization. Default implementation uses ~4 characters per token heuristic.

Source

fn estimate_message_tokens(&self, messages: &[Message]) -> usize

Estimate token count for a conversation

Source

fn generate_stream<'life0, 'async_trait>( &'life0 self, messages: Vec<Message>, tools: Vec<ToolDefinition>, system_prompt: Option<String>, ) -> Pin<Box<dyn Future<Output = Result<BoxStream<'static, Result<StreamEvent, ProviderError>>, ProviderError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Send a request and stream the response token-by-token (optional)

§Arguments
  • messages - The conversation history
  • tools - Available tools for the model to use
  • system_prompt - Optional system prompt

Implementations on Foreign Types§

Source§

impl ModelProvider for Arc<dyn ModelProvider>

Source§

fn name(&self) -> &str

Source§

fn max_context_tokens(&self) -> usize

Source§

fn max_output_tokens(&self) -> usize

Source§

fn estimate_token_count(&self, text: &str) -> usize

Source§

fn estimate_message_tokens(&self, messages: &[Message]) -> usize

Source§

fn generate<'life0, 'async_trait>( &'life0 self, messages: Vec<Message>, tools: Vec<ToolDefinition>, system_prompt: Option<String>, ) -> Pin<Box<dyn Future<Output = Result<ModelResponse, ProviderError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

fn generate_stream<'life0, 'async_trait>( &'life0 self, messages: Vec<Message>, tools: Vec<ToolDefinition>, system_prompt: Option<String>, ) -> Pin<Box<dyn Future<Output = Result<BoxStream<'static, Result<StreamEvent, ProviderError>>, ProviderError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Implementors§