pub struct Messages<'a> { /* private fields */ }async only.Expand description
Namespace handle for the Messages API.
Obtained via Client::messages; cheap to
construct (just borrows the client).
Implementations§
Source§impl<'a> Messages<'a>
impl<'a> Messages<'a>
Sourcepub async fn create(&self, request: CreateMessageRequest) -> Result<Message>
pub async fn create(&self, request: CreateMessageRequest) -> Result<Message>
Send a request to POST /v1/messages and return the full response.
Retries are governed by the RetryPolicy
configured on the client.
Sourcepub async fn create_with_beta(
&self,
request: CreateMessageRequest,
betas: &[&str],
) -> Result<Message>
pub async fn create_with_beta( &self, request: CreateMessageRequest, betas: &[&str], ) -> Result<Message>
Like Self::create but with additional per-request beta headers
merged into anthropic-beta.
Sourcepub async fn count_tokens(
&self,
request: CountTokensRequest,
) -> Result<CountTokensResponse>
pub async fn count_tokens( &self, request: CountTokensRequest, ) -> Result<CountTokensResponse>
Count the input tokens that would be billed for the given request, without invoking the model.
Sourcepub async fn count_tokens_with_beta(
&self,
request: CountTokensRequest,
betas: &[&str],
) -> Result<CountTokensResponse>
pub async fn count_tokens_with_beta( &self, request: CountTokensRequest, betas: &[&str], ) -> Result<CountTokensResponse>
Like Self::count_tokens but with per-request beta headers.
Sourcepub fn dry_run(&self, request: &CreateMessageRequest) -> Result<DryRun>
pub fn dry_run(&self, request: &CreateMessageRequest) -> Result<DryRun>
Render the HTTP request that Self::create would send, without
firing it. Returns a DryRun containing the rendered method, URL,
headers, and JSON body. Useful for tests, debugging, and reproducing
requests as curl commands.
Sourcepub fn dry_run_with_beta(
&self,
request: &CreateMessageRequest,
betas: &[&str],
) -> Result<DryRun>
pub fn dry_run_with_beta( &self, request: &CreateMessageRequest, betas: &[&str], ) -> Result<DryRun>
Like Self::dry_run but with per-request beta headers merged in,
matching Self::create_with_beta.
Sourcepub async fn cost_preview(
&self,
request: &CreateMessageRequest,
pricing: &PricingTable,
) -> Result<CostPreview>
Available on crate feature pricing only.
pub async fn cost_preview( &self, request: &CreateMessageRequest, pricing: &PricingTable, ) -> Result<CostPreview>
pricing only.Estimate the USD cost of a request before sending it.
Hits /v1/messages/count_tokens to get the exact server-counted
input-token total, then uses request.max_tokens as the output upper
bound. The returned CostPreview
exposes input / max-output / max-total cost components.
Pre-flight charge from this call: one count-tokens request (significantly cheaper than a full generation, but not free).
Sourcepub async fn cost_preview_cached(
&self,
request: &CreateMessageRequest,
pricing: &PricingTable,
cache: &CountTokensCache,
) -> Result<CostPreview>
Available on crate feature pricing only.
pub async fn cost_preview_cached( &self, request: &CreateMessageRequest, pricing: &PricingTable, cache: &CountTokensCache, ) -> Result<CostPreview>
pricing only.Like Self::cost_preview but consults a
CountTokensCache
before issuing the network call. Cache hits avoid the
count_tokens round-trip entirely. Useful for IDE integrations
and long-running agents that preview the same prompt many times.
Sourcepub fn dry_run_count_tokens(
&self,
request: &CountTokensRequest,
) -> Result<DryRun>
pub fn dry_run_count_tokens( &self, request: &CountTokensRequest, ) -> Result<DryRun>
Render the HTTP request that Self::count_tokens would send.
Sourcepub fn dry_run_count_tokens_with_beta(
&self,
request: &CountTokensRequest,
betas: &[&str],
) -> Result<DryRun>
pub fn dry_run_count_tokens_with_beta( &self, request: &CountTokensRequest, betas: &[&str], ) -> Result<DryRun>
Like Self::dry_run_count_tokens but with per-request beta headers.
Sourcepub async fn create_stream(
&self,
request: CreateMessageRequest,
) -> Result<EventStream>
Available on crate feature streaming only.
pub async fn create_stream( &self, request: CreateMessageRequest, ) -> Result<EventStream>
streaming only.Open a streaming connection to POST /v1/messages and return an
EventStream.
The returned stream yields StreamEvents
as they arrive; call EventStream::aggregate to drive it to
completion and reconstruct the final Message.
Streaming requests are not retried.
Sourcepub async fn create_stream_with_beta(
&self,
request: CreateMessageRequest,
betas: &[&str],
) -> Result<EventStream>
Available on crate feature streaming only.
pub async fn create_stream_with_beta( &self, request: CreateMessageRequest, betas: &[&str], ) -> Result<EventStream>
streaming only.Like Self::create_stream but with per-request beta headers.
Auto Trait Implementations§
impl<'a> Freeze for Messages<'a>
impl<'a> !RefUnwindSafe for Messages<'a>
impl<'a> Send for Messages<'a>
impl<'a> Sync for Messages<'a>
impl<'a> Unpin for Messages<'a>
impl<'a> UnsafeUnpin for Messages<'a>
impl<'a> !UnwindSafe for Messages<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more