A provider that issues POST {base_url}/v1/messages against
the native Anthropic Messages API. The request carries the mandatory
max_tokens field and the anthropic-version header; when a
schema is supplied it adds a tool-use input_schema block
(tools + tool_choice) to obtain structured output, and the
response extractor prefers that tool call's structured input while
falling back to the first text block — so the provider never crashes when the
model replies with plain text instead of a tool call.
The API key is resolved at CALL time from ANTHROPIC_API_KEY (never at
construction, never stored on the object) and attached with
req_headers_redacted("x-api-key" = key), which redacts the key in
every print/error path. Note req_auth_bearer_token does NOT redact an
x-api-key header — the redacted-headers helper is mandatory here. A
missing key, transport/timeout failure, non-2xx status, malformed body, or
missing completion text each degrades to exactly one warning() plus an
`es_provider_response` with text = NA_character_ — it never crashes the
session and never returns a fabricated completion.
httr2 is required only for this provider and is guarded by
requireNamespace() at the top of complete(), so the package
installs and R CMD checks cleanly with httr2 absent.
Super class
ProviderBase -> AnthropicProvider
Methods
AnthropicProvider$new()
Construct an AnthropicProvider. Stores non-secret config only;
the API key is resolved at call time inside complete().
Usage
AnthropicProvider$new(
model,
base_url = "https://api.anthropic.com",
max_tokens = 1024L,
...
)Arguments
modelCharacter model identifier, e.g. "claude-opus-4-8".
base_urlCharacter base URL. Defaults to the Anthropic cloud endpoint.
max_tokensInteger maximum number of tokens to generate. The Anthropic Messages API REQUIRES this field; defaults to 1024.
...Ignored; accepted for forward-compatibility.
AnthropicProvider$complete()
Complete a prompt via POST {base_url}/v1/messages.
Resolves the key at call time; on a missing key or any HTTP/parse failure
returns one warning + NA. Structured output is OPTIONAL: when
schema is supplied a tool-use input_schema block is added and
the extractor prefers the tool call's input, falling back to the
first text block.
Examples
if (FALSE) { # \dontrun{
# Reads ANTHROPIC_API_KEY from the environment at call time:
p <- AnthropicProvider$new(model = "claude-opus-4-8")
p$complete("Summarise these event-study diagnostics")
# Structured output via a tool-use input_schema:
schema <- list(type = "object",
properties = list(advice = list(type = "string")))
p$complete("Recommend a test statistic", schema = schema)
} # }