pub struct Config {Show 17 fields
pub openai_api_key: Option<String>,
pub openai_base_url: Option<String>,
pub anthropic_api_key: Option<String>,
pub anthropic_base_url: Option<String>,
pub openrouter_api_key: Option<String>,
pub openrouter_base_url: Option<String>,
pub openai_compatible_base_url: Option<String>,
pub openai_compatible_api_key: Option<String>,
pub openai_compatible_capabilities: Option<EndpointCapabilities>,
pub openrouter_http_referer: Option<String>,
pub openrouter_title: Option<String>,
pub openrouter_categories: Option<Vec<String>>,
pub openrouter_app_url: Option<String>,
pub openrouter_app_title: Option<String>,
pub timeout_seconds: Option<u64>,
pub default_max_tokens: Option<i32>,
pub retry_config: Option<RetryConfig>,
}Expand description
Configuration for the AI SDK client.
Every field is optional. Unset credentials simply mean the corresponding
provider is unavailable rather than an error at construction time; the
failure surfaces as Error::ProviderNotConfigured
when that provider is actually used.
Fields§
§openai_api_key: Option<String>OpenAI API key.
openai_base_url: Option<String>OpenAI API base URL (for proxies or Azure OpenAI).
anthropic_api_key: Option<String>Anthropic API key.
anthropic_base_url: Option<String>Anthropic API base URL (for proxies).
openrouter_api_key: Option<String>OpenRouter API key.
openrouter_base_url: Option<String>OpenRouter API base URL.
openai_compatible_base_url: Option<String>Base URL of an OpenAI-compatible endpoint, such as
http://localhost:11434/v1.
Setting this is what makes ProviderKind::OpenAICompatible available
on a client; there is no default endpoint and no environment variable,
because a process routinely talks to several at once. See
Config::openai_compatible_base_url.
openai_compatible_api_key: Option<String>Bearer token for the OpenAI-compatible endpoint.
Optional: endpoints that need no credential — the common case for a
local runtime — simply leave this unset, and no Authorization header
is sent.
openai_compatible_capabilities: Option<EndpointCapabilities>What the OpenAI-compatible endpoint supports beyond plain chat.
Declared by the caller, never probed. Defaults to
EndpointCapabilities::default, which assumes full compatibility.
openrouter_http_referer: Option<String>Optional app URL for OpenRouter attribution headers.
openrouter_title: Option<String>Optional app title for OpenRouter attribution headers.
openrouter_categories: Option<Vec<String>>Optional app categories for OpenRouter attribution headers.
openrouter_app_url: Option<String>OpenRouter App URL (sent in headers).
openrouter_app_title: Option<String>OpenRouter App Title (sent in headers).
timeout_seconds: Option<u64>Request timeout in seconds (default: 120).
default_max_tokens: Option<i32>Default max tokens for generation (can be overridden per request).
retry_config: Option<RetryConfig>Retry configuration for transient errors.
Implementations§
Source§impl Config
impl Config
Sourcepub fn from_env() -> Self
pub fn from_env() -> Self
Create configuration from environment variables.
Reads every variable listed in the module docs. Missing or
unparseable values are ignored, leaving the corresponding field unset
(and therefore at its default). The retry configuration is only
populated when at least one AI_RETRY_*/AI_MAX_RETRIES variable was
recognized.
Sourcepub fn with_openai_key(self, key: impl Into<String>) -> Self
pub fn with_openai_key(self, key: impl Into<String>) -> Self
Set the OpenAI API key.
Sourcepub fn with_openai_base_url(self, url: impl Into<String>) -> Self
pub fn with_openai_base_url(self, url: impl Into<String>) -> Self
Set the OpenAI base URL, for proxies or Azure OpenAI deployments.
Sourcepub fn with_anthropic_key(self, key: impl Into<String>) -> Self
pub fn with_anthropic_key(self, key: impl Into<String>) -> Self
Set the Anthropic API key.
Sourcepub fn with_anthropic_base_url(self, url: impl Into<String>) -> Self
pub fn with_anthropic_base_url(self, url: impl Into<String>) -> Self
Set the Anthropic base URL, for proxies.
Sourcepub fn with_openrouter_key(self, key: impl Into<String>) -> Self
pub fn with_openrouter_key(self, key: impl Into<String>) -> Self
Set the OpenRouter API key.
Sourcepub fn with_openrouter_base_url(self, url: impl Into<String>) -> Self
pub fn with_openrouter_base_url(self, url: impl Into<String>) -> Self
Set the OpenRouter base URL, for proxies.
Sourcepub fn with_openai_compatible_base_url(self, url: impl Into<String>) -> Self
pub fn with_openai_compatible_base_url(self, url: impl Into<String>) -> Self
Point this client at an OpenAI-compatible endpoint.
The URL is the API root that serves POST /chat/completions, so it
usually ends in /v1.
Sourcepub fn with_openai_compatible_key(self, key: impl Into<String>) -> Self
pub fn with_openai_compatible_key(self, key: impl Into<String>) -> Self
Set the bearer token for the OpenAI-compatible endpoint.
Leave it unset for endpoints that need no credential; no
Authorization header is sent then.
Sourcepub fn with_openai_compatible_capabilities(
self,
capabilities: EndpointCapabilities,
) -> Self
pub fn with_openai_compatible_capabilities( self, capabilities: EndpointCapabilities, ) -> Self
Declare what the OpenAI-compatible endpoint supports.
Sourcepub fn with_ollama(self) -> Self
pub fn with_ollama(self) -> Self
Point this client at a local Ollama server (OLLAMA_BASE_URL).
Shorthand for Config::with_openai_compatible_base_url with Ollama’s
default address; pass the URL explicitly for any other host or port.
Sourcepub fn with_openrouter_http_referer(self, referer: impl Into<String>) -> Self
pub fn with_openrouter_http_referer(self, referer: impl Into<String>) -> Self
Set the OpenRouter HTTP-Referer attribution header.
Sourcepub fn with_openrouter_title(self, title: impl Into<String>) -> Self
pub fn with_openrouter_title(self, title: impl Into<String>) -> Self
Set the OpenRouter app title attribution header.
Sourcepub fn with_openrouter_categories(self, categories: Vec<String>) -> Self
pub fn with_openrouter_categories(self, categories: Vec<String>) -> Self
Set the OpenRouter app categories attribution header.
Sourcepub fn with_openrouter_app_url(self, url: impl Into<String>) -> Self
pub fn with_openrouter_app_url(self, url: impl Into<String>) -> Self
Set the legacy OpenRouter app URL, which also sets the canonical
HTTP-Referer value.
Sourcepub fn with_openrouter_app_title(self, title: impl Into<String>) -> Self
pub fn with_openrouter_app_title(self, title: impl Into<String>) -> Self
Set the legacy OpenRouter app title, which also sets the canonical title value.
Sourcepub fn with_timeout(self, seconds: u64) -> Self
pub fn with_timeout(self, seconds: u64) -> Self
Set the HTTP request timeout, in seconds.
Sourcepub fn with_default_max_tokens(self, max_tokens: i32) -> Self
pub fn with_default_max_tokens(self, max_tokens: i32) -> Self
Set the default max_tokens used when a request does not specify one.
Sourcepub fn with_retry_config(self, retry_config: RetryConfig) -> Self
pub fn with_retry_config(self, retry_config: RetryConfig) -> Self
Set the retry policy applied to transient errors.
Sourcepub fn openai_key(&self) -> Option<String>
pub fn openai_key(&self) -> Option<String>
The OpenAI API key, falling back to OPENAI_API_KEY.
Sourcepub fn anthropic_key(&self) -> Option<String>
pub fn anthropic_key(&self) -> Option<String>
The Anthropic API key, falling back to ANTHROPIC_API_KEY.
Sourcepub fn openrouter_key(&self) -> Option<String>
pub fn openrouter_key(&self) -> Option<String>
The OpenRouter API key, falling back to OPENROUTER_API_KEY.
Sourcepub fn openrouter_base_url(&self) -> Option<String>
pub fn openrouter_base_url(&self) -> Option<String>
The OpenRouter base URL, falling back to OPENROUTER_BASE_URL.
None means the provider uses its built-in default endpoint.
Sourcepub fn openai_compatible_base_url(&self) -> Option<String>
pub fn openai_compatible_base_url(&self) -> Option<String>
The OpenAI-compatible endpoint’s base URL, or None when this client
has none configured.
Unlike every other getter here this one has no environment-variable
fallback, and that is deliberate. The other providers each name one
well-known service, so a process-wide *_BASE_URL is a sensible
override. “OpenAI-compatible” names no service at all: a single process
may talk to a local Ollama, a shared vLLM deployment, and a staging
gateway at the same time, each with its own credentials and
capabilities. That is per-client configuration, so it is set per client.
OPENAI_BASE_URL keeps its existing meaning and still applies only to
the real OpenAI provider.
Sourcepub fn openai_compatible_key(&self) -> Option<String>
pub fn openai_compatible_key(&self) -> Option<String>
The OpenAI-compatible endpoint’s bearer token, if one was set.
No environment-variable fallback, for the reasons given on
Config::openai_compatible_base_url.
Sourcepub fn openai_compatible_capabilities(&self) -> EndpointCapabilities
pub fn openai_compatible_capabilities(&self) -> EndpointCapabilities
What the OpenAI-compatible endpoint was declared to support, defaulting
to EndpointCapabilities::default.
Sourcepub fn openrouter_http_referer(&self) -> Option<String>
pub fn openrouter_http_referer(&self) -> Option<String>
The OpenRouter HTTP-Referer value.
Resolution order: the explicit referer, the legacy app URL,
OPENROUTER_HTTP_REFERER, then OPENROUTER_APP_URL.
Sourcepub fn openrouter_title(&self) -> Option<String>
pub fn openrouter_title(&self) -> Option<String>
The OpenRouter app title used for attribution headers.
Resolution order: the explicit title, the legacy app title,
OPENROUTER_TITLE, then OPENROUTER_APP_TITLE.
Sourcepub fn openrouter_categories(&self) -> Option<Vec<String>>
pub fn openrouter_categories(&self) -> Option<Vec<String>>
The OpenRouter app categories, falling back to the comma-separated
OPENROUTER_CATEGORIES variable. Empty lists are treated as unset.
Sourcepub fn openrouter_app_url(&self) -> Option<String>
pub fn openrouter_app_url(&self) -> Option<String>
Deprecated alias for Config::openrouter_http_referer, kept for
callers written against the older attribution field names.
Sourcepub fn openrouter_app_title(&self) -> Option<String>
pub fn openrouter_app_title(&self) -> Option<String>
Deprecated alias for Config::openrouter_title, kept for callers
written against the older attribution field names.
Sourcepub fn retry_config(&self) -> RetryConfig
pub fn retry_config(&self) -> RetryConfig
The effective retry policy, or RetryConfig::default when unset.
Sourcepub fn validate_openai(&self) -> Result<()>
Available on crate feature openai only.
pub fn validate_openai(&self) -> Result<()>
openai only.Check that OpenAI is usable.
§Errors
Returns Error::Config if no OpenAI API key is
set programmatically or in OPENAI_API_KEY.
Sourcepub fn validate_anthropic(&self) -> Result<()>
Available on crate feature anthropic only.
pub fn validate_anthropic(&self) -> Result<()>
anthropic only.Check that Anthropic is usable.
§Errors
Returns Error::Config if no Anthropic API key
is set programmatically or in ANTHROPIC_API_KEY.
Sourcepub fn validate_openrouter(&self) -> Result<()>
Available on crate feature openrouter only.
pub fn validate_openrouter(&self) -> Result<()>
openrouter only.Check that OpenRouter is usable.
§Errors
Returns Error::Config if no OpenRouter API key
is set programmatically or in OPENROUTER_API_KEY.