Reference Client
AnthropicConfig
The client's settings. Start from an API key or the environment and adjust with withX; one call
overrides any of them with RequestOptions.
Reference#
AnthropicConfig#
final case class AnthropicConfig private (
credentials: Credentials,
baseUrl: String,
apiVersion: String,
timeout: FiniteDuration,
retryPolicy: RetryPolicy,
betas: Set[Beta],
headers: List[(String, String)]
)
Fields
credentials: an API key, sent asx-api-key, or an auth token, sent as a bearer token.baseUrl: where requests go;https://api.anthropic.comby default.apiVersion: theanthropic-versionheader.timeout: how long one request may take; 10 minutes by default.retryPolicy: when to send a request again; seeRetryPolicy.betas: beta features turned on for every request.headers: extra headers sent with every request.
val DefaultBaseUrl: String = "https://api.anthropic.com"
val DefaultApiVersion: String = "2026-10-21"
val DefaultTimeout: FiniteDuration = 10.minutes
AnthropicConfig(apiKey)#
def apply(credentials: Credentials): AnthropicConfig
def apply(apiKey: String): AnthropicConfig
sealed trait Credentials extends Product with Serializable
final case class ApiKey(key: Secret[String]) extends Credentials
final case class AuthToken(token: Secret[String]) extends Credentials
Caveats
- A
Secretprints as***, so a logged configuration does not leak the key.
AnthropicConfig.fromEnv#
def fromEnv[F[_]](implicit F: MonadThrow[F], env: Env[F]): F[AnthropicConfig]
Reads ANTHROPIC_API_KEY, or else ANTHROPIC_AUTH_TOKEN, and ANTHROPIC_BASE_URL when it is set.
Caveats
- With neither credential set it fails with
AnthropicError.Configuration.
withX methods#
def withCredentials(c: Credentials): AnthropicConfig
def withBaseUrl(url: String): AnthropicConfig
def withApiVersion(v: String): AnthropicConfig
def withRetryPolicy(p: RetryPolicy): AnthropicConfig
def withMaxRetries(n: Int): AnthropicConfig
def withTimeout(d: FiniteDuration): AnthropicConfig
def withBetas(bs: Beta*): AnthropicConfig
def withHeader(name: String, value: String): AnthropicConfig
Caveats
withBetasandwithHeaderadd to what is there; the others replace.
RetryPolicy#
final case class RetryPolicy private (
maxRetries: Int,
initialDelay: FiniteDuration,
maxDelay: FiniteDuration,
jitter: Double
)
def withMaxRetries(n: Int): RetryPolicy
def withInitialDelay(d: FiniteDuration): RetryPolicy
def withMaxDelay(d: FiniteDuration): RetryPolicy
val default: RetryPolicy = new RetryPolicy(2, 500.millis, 8.seconds, 0.25)
/** Never retry. */
val none: RetryPolicy = default.withMaxRetries(0)
Retries connection failures, timeouts, 408, 409, 429 and 5xx. The wait doubles from initialDelay, is capped at maxDelay and is shortened by up to jitter.
Caveats
- The server has the last word:
x-should-retryoverrides the status rule, andretry-after-msorretry-aftersets the wait when it is between 0 and 60 seconds. withMaxRetriesnever goes below 0.
Usage#
Adjusting the defaults#
AnthropicConfig.fromEnv[IO].map(_.withTimeout(2.minutes).withBetas(Beta.ServerSideFallback))
Errors, retries and timeouts explains when to change them.
Every signature and example on this page is copied, character for character, from the public source of anthropic4s and its Scaladoc. The complete API, every type included, is in the Scaladoc of each module.