Skip to content
Get Started

Anthropic

The Anthropic provider (rig::providers::anthropic) connects Rig to Anthropic’s Claude models. It supports completion models with tool use, prompt caching, and detailed token-usage reporting. Two things set it apart from other providers: it is configured through a ClientBuilder (to pin the API version and opt into beta features), and it requires max_tokens on every request.

CapabilitySupportedModel / flag
CompletionYesAny Claude model id, e.g. claude-sonnet-4-5, claude-opus-4-8max_tokens is required
StreamingYesStreaming completion and agent responses
ToolsYesRig Tool trait; tool_choice supported
Structured outputYesVia Rig’s extractor (built on tool use); no native JSON mode
Vision / image inputYesImage content blocks — JPEG, PNG, GIF, WebP
Extended thinking / reasoningYesThinking and redacted-thinking blocks; enable a thinking object through additional_params
EmbeddingsNoNot offered — pair Anthropic completions with OpenAI or Cohere embedding models for vector search
use rig::prelude::*;
use rig::providers::anthropic::{Client, completion::ANTHROPIC_VERSION_LATEST};
// Build a client, pinning the API version and opting into a beta feature
let client = Client::builder()
.api_key("your-api-key")
.anthropic_version(ANTHROPIC_VERSION_LATEST)
.anthropic_beta("prompt-caching-2024-07-31")
.build()?;
// Build an agent — max_tokens is required for Anthropic
let agent = client
.agent("claude-sonnet-4-5")
.preamble("You are a helpful assistant.")
.max_tokens(1024)
.build();

Prompting the agent above — let reply = agent.prompt("Hello!").await?; — returns a plain-text completion:

Hello! How can I help you today?

Unlike most providers, the Anthropic client is created through ClientBuilder so you can set the API version and beta headers:

use rig::prelude::*;
use rig::providers::anthropic::Client;
let client = Client::builder()
.api_key("your-api-key")
.anthropic_version("2023-06-01")
.anthropic_beta("prompt-caching-2024-07-31")
.build()?;

A from_env constructor is also available for the common case of reading ANTHROPIC_API_KEY from the environment.

Anthropic requires an explicit API version. The provider exports constants so you don’t have to hardcode the string:

  • ANTHROPIC_VERSION_2023_01_01
  • ANTHROPIC_VERSION_2023_06_01
  • ANTHROPIC_VERSION_LATEST — currently an alias for 2023-06-01

Prefer ANTHROPIC_VERSION_LATEST unless you need to pin an older version.

Pass any Claude model id as a string to agent or completion_model:

let agent = client
.agent("claude-sonnet-4-5")
.max_tokens(1024)
.build();

Claude is organized into three families, from most capable to fastest: Opus, Sonnet, and Haiku.

With the prompt-caching beta enabled, Anthropic reports cache statistics alongside the usual token counts. Rig surfaces these on the response’s Usage:

FieldMeaning
input_tokensTokens processed at full price
cache_read_input_tokensTokens served from the prompt cache (optional)
cache_creation_input_tokensTokens written to the cache this request (optional)
output_tokensTokens generated in the response

The two cache fields are optional — they are populated only when prompt caching is active for the request.

Claude responses can contain more than plain text. Rig models this with an untagged Content enum whose main variants are:

  • Text — a text block from the model
  • ToolUse — a request to call a tool, carrying an id, name, and JSON input

When you use an agent with tools, Rig interprets these variants for you: text becomes the response, and tool-use blocks are dispatched to your registered tools. You only need to work with Content directly if you consume the raw completion response.

Anthropic models support tool use with Anthropic’s own formatting. As with other providers, you define tools once via Rig’s Tool trait and register them on an agent — Rig converts them to Anthropic’s expected tool-definition format and parses tool-call responses back into its own types. See Tools for details.