pub struct Anthropic { /* private fields */ }Expand description
Client for the Anthropic API with performance optimizations.
Implementations§
Source§impl Anthropic
impl Anthropic
Sourcepub fn new(api_key: Option<String>) -> Result<Self>
pub fn new(api_key: Option<String>) -> Result<Self>
Create a new Anthropic client.
The API key can be provided directly or read from the CLAUDIUS_API_KEY or ANTHROPIC_API_KEY environment variables. If an environment variable value starts with “file://”, it will be treated as a file path and the API key will be read from that file.
Sourcepub fn with_base_url(self, base_url: String) -> Self
pub fn with_base_url(self, base_url: String) -> Self
Set a custom base URL for this client.
This method allows you to specify a different API endpoint for the client.
Sourcepub fn with_timeout(self, timeout: Duration) -> Result<Self>
pub fn with_timeout(self, timeout: Duration) -> Result<Self>
Set a custom timeout for this client.
This method allows you to specify a different timeout for API requests.
Sourcepub fn with_max_retries(self, max_retries: usize) -> Self
pub fn with_max_retries(self, max_retries: usize) -> Self
Set the maximum number of retries for this client.
This method allows you to specify how many times to retry failed requests.
Sourcepub fn with_backoff_params(
self,
throughput_ops_sec: f64,
reserve_capacity: f64,
) -> Self
pub fn with_backoff_params( self, throughput_ops_sec: f64, reserve_capacity: f64, ) -> Self
Set the backoff parameters for this client.
This method allows you to configure the exponential backoff algorithm.
Sourcepub fn with_base_url_and_timeout(
self,
base_url: String,
timeout: Duration,
) -> Result<Self>
pub fn with_base_url_and_timeout( self, base_url: String, timeout: Duration, ) -> Result<Self>
Set both a custom base URL and timeout for this client.
This is a convenience method that chains with_base_url and with_timeout.
Sourcepub async fn send(&self, params: MessageCreateParams) -> Result<Message>
pub async fn send(&self, params: MessageCreateParams) -> Result<Message>
Send a message to the API and get a non-streaming response.
Sourcepub async fn stream(
&self,
params: MessageCreateParams,
) -> Result<impl Stream<Item = Result<MessageStreamEvent>>>
pub async fn stream( &self, params: MessageCreateParams, ) -> Result<impl Stream<Item = Result<MessageStreamEvent>>>
Send a message to the API and get a streaming response.
Returns a stream of MessageStreamEvent objects that can be processed incrementally.
Sourcepub async fn count_tokens(
&self,
params: MessageCountTokensParams,
) -> Result<MessageTokensCount>
pub async fn count_tokens( &self, params: MessageCountTokensParams, ) -> Result<MessageTokensCount>
Count tokens for a message.
This method counts the number of tokens that would be used by a message with the given parameters. It’s useful for estimating costs or making sure your messages fit within the model’s context window.
Sourcepub async fn list_models(
&self,
params: Option<ModelListParams>,
) -> Result<ModelListResponse>
pub async fn list_models( &self, params: Option<ModelListParams>, ) -> Result<ModelListResponse>
List available models from the API.
Returns a paginated list of all available models. Use the parameters to control pagination and filter results.