pub struct CompletionModel<T = Client> {
pub model: String,
pub default_max_tokens: Option<u64>,
pub prompt_caching: bool,
pub automatic_caching: bool,
pub automatic_caching_ttl: Option<CacheTtl>,
/* private fields */
}Fields§
§model: String§default_max_tokens: Option<u64>§prompt_caching: boolEnable automatic prompt caching (adds cache_control breakpoints to system prompt and messages)
automatic_caching: boolEnable Anthropic’s automatic prompt caching (adds a top-level cache_control field to the
request). The API automatically places the breakpoint on the last cacheable block and moves
it forward as the conversation grows. No beta header is required.
automatic_caching_ttl: Option<CacheTtl>TTL for automatic caching. None uses the API default (5 minutes).
Set to Some(CacheTtl::OneHour) for a 1-hour TTL (requires the
extended-cache-ttl-2025-04-11 beta header).
Implementations§
Source§impl<T> CompletionModel<T>where
T: HttpClientExt,
impl<T> CompletionModel<T>where
T: HttpClientExt,
pub fn new(client: Client<T>, model: impl Into<String>) -> Self
pub fn with_model(client: Client<T>, model: &str) -> Self
Sourcepub fn with_prompt_caching(self) -> Self
pub fn with_prompt_caching(self) -> Self
Enable automatic prompt caching.
When enabled, cache_control breakpoints are automatically added to:
- The system prompt (marked with ephemeral cache)
- The last content block of the last message (marked with ephemeral cache)
This allows Anthropic to cache the conversation history for cost savings.
Sourcepub fn with_automatic_caching(self) -> Self
pub fn with_automatic_caching(self) -> Self
Enable Anthropic’s automatic prompt caching.
When enabled, a top-level cache_control: { "type": "ephemeral" } field is added to every
request. Anthropic’s API automatically applies the cache breakpoint to the last cacheable
block and moves it forward as the conversation grows — no beta header and no manual
breakpoint management are required.
This is the recommended approach for multi-turn conversations. Use with_prompt_caching
instead when you need fine-grained, per-block control over what is cached.
To use a one-hour TTL instead of the default five minutes, pass ttl: "1h" via
additional_params or combine with an explicit block-level breakpoint that carries the
extended TTL.
let model = client.completion_model(anthropic::completion::CLAUDE_SONNET_4_6)
.with_automatic_caching();§Minimum cacheable prompt length
The combined prompt (tools + system + messages up to the automatically chosen breakpoint) must meet the model-specific minimum or caching is silently skipped by the API:
| Model | Minimum tokens |
|---|---|
claude-opus-4-6, claude-opus-4-5 | 4 096 |
claude-sonnet-4-6 | 2 048 |
claude-sonnet-4-5, claude-opus-4-1, claude-opus-4, claude-sonnet-4 | 1 024 |
claude-haiku-4-5 | 4 096 |
Sourcepub fn with_automatic_caching_1h(self) -> Self
pub fn with_automatic_caching_1h(self) -> Self
Enable Anthropic’s automatic prompt caching with a 1-hour TTL.
Identical to with_automatic_caching but sets ttl: "1h" on the
top-level cache_control field. Requires the
extended-cache-ttl-2025-04-11 beta header to be sent with the client:
let client = anthropic::Client::builder()
.api_key(std::env::var("ANTHROPIC_API_KEY").unwrap())
.anthropic_beta("extended-cache-ttl-2025-04-11")
.build()?;
let model = client.completion_model(anthropic::completion::CLAUDE_SONNET_4_6)
.with_automatic_caching_1h();Trait Implementations§
Source§impl<T: Clone> Clone for CompletionModel<T>
impl<T: Clone> Clone for CompletionModel<T>
Source§fn clone(&self) -> CompletionModel<T>
fn clone(&self) -> CompletionModel<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<T> CompletionModel for CompletionModel<T>
impl<T> CompletionModel for CompletionModel<T>
Source§type Response = CompletionResponse
type Response = CompletionResponse
Source§type StreamingResponse = StreamingCompletionResponse
type StreamingResponse = StreamingCompletionResponse
type Client = Client<AnthropicExt, T>
fn make(client: &Self::Client, model: impl Into<String>) -> Self
Source§async fn completion(
&self,
completion_request: CompletionRequest,
) -> Result<CompletionResponse<CompletionResponse>, CompletionError>
async fn completion( &self, completion_request: CompletionRequest, ) -> Result<CompletionResponse<CompletionResponse>, CompletionError>
async fn stream( &self, request: CompletionRequest, ) -> Result<StreamingCompletionResponse<Self::StreamingResponse>, CompletionError>
Source§fn completion_request(
&self,
prompt: impl Into<Message>,
) -> CompletionRequestBuilder<Self>
fn completion_request( &self, prompt: impl Into<Message>, ) -> CompletionRequestBuilder<Self>
prompt.Auto Trait Implementations§
impl<T> Freeze for CompletionModel<T>where
T: Freeze,
impl<T> RefUnwindSafe for CompletionModel<T>where
T: RefUnwindSafe,
impl<T> Send for CompletionModel<T>where
T: Send,
impl<T> Sync for CompletionModel<T>where
T: Sync,
impl<T> Unpin for CompletionModel<T>where
T: Unpin,
impl<T> UnsafeUnpin for CompletionModel<T>where
T: UnsafeUnpin,
impl<T> UnwindSafe for CompletionModel<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T, R> CompletionModelDyn for T
impl<T, R> CompletionModelDyn for T
Source§fn completion_request(
&self,
prompt: Message,
) -> CompletionRequestBuilder<CompletionModelHandle<'_>>
👎Deprecated since 0.25.0: DynClientBuilder and related features have been deprecated and will be removed in a future release. In this case, use CompletionModel instead.
fn completion_request( &self, prompt: Message, ) -> CompletionRequestBuilder<CompletionModelHandle<'_>>
DynClientBuilder and related features have been deprecated and will be removed in a future release. In this case, use CompletionModel instead.
Generates a completion request builder for the given prompt.
Source§fn completion(
&self,
request: CompletionRequest,
) -> Pin<Box<dyn Future<Output = Result<CompletionResponse<()>, CompletionError>> + Send + '_>>
fn completion( &self, request: CompletionRequest, ) -> Pin<Box<dyn Future<Output = Result<CompletionResponse<()>, CompletionError>> + Send + '_>>
DynClientBuilder and related features have been deprecated and will be removed in a future release. In this case, use CompletionModel instead.
Source§fn stream(
&self,
request: CompletionRequest,
) -> Pin<Box<dyn Future<Output = Result<StreamingCompletionResponse<FinalCompletionResponse>, CompletionError>> + Send + '_>>
fn stream( &self, request: CompletionRequest, ) -> Pin<Box<dyn Future<Output = Result<StreamingCompletionResponse<FinalCompletionResponse>, CompletionError>> + Send + '_>>
DynClientBuilder and related features have been deprecated and will be removed in a future release. In this case, use CompletionModel instead.
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> CloneableStorage for T
impl<T> CloneableStorage for T
fn clone_storage(&self) -> Box<dyn CloneableStorage>
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> 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