Skip to main content

Config

Struct Config 

Source
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

Source

pub fn new() -> Self

Create a new empty configuration.

Source

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.

Source

pub fn with_openai_key(self, key: impl Into<String>) -> Self

Set the OpenAI API key.

Source

pub fn with_openai_base_url(self, url: impl Into<String>) -> Self

Set the OpenAI base URL, for proxies or Azure OpenAI deployments.

Source

pub fn with_anthropic_key(self, key: impl Into<String>) -> Self

Set the Anthropic API key.

Source

pub fn with_anthropic_base_url(self, url: impl Into<String>) -> Self

Set the Anthropic base URL, for proxies.

Source

pub fn with_openrouter_key(self, key: impl Into<String>) -> Self

Set the OpenRouter API key.

Source

pub fn with_openrouter_base_url(self, url: impl Into<String>) -> Self

Set the OpenRouter base URL, for proxies.

Source

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.

Source

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.

Source

pub fn with_openai_compatible_capabilities( self, capabilities: EndpointCapabilities, ) -> Self

Declare what the OpenAI-compatible endpoint supports.

Source

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.

Source

pub fn with_openrouter_http_referer(self, referer: impl Into<String>) -> Self

Set the OpenRouter HTTP-Referer attribution header.

Source

pub fn with_openrouter_title(self, title: impl Into<String>) -> Self

Set the OpenRouter app title attribution header.

Source

pub fn with_openrouter_categories(self, categories: Vec<String>) -> Self

Set the OpenRouter app categories attribution header.

Source

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.

Source

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.

Source

pub fn with_timeout(self, seconds: u64) -> Self

Set the HTTP request timeout, in seconds.

Source

pub fn with_default_max_tokens(self, max_tokens: i32) -> Self

Set the default max_tokens used when a request does not specify one.

Source

pub fn with_retry_config(self, retry_config: RetryConfig) -> Self

Set the retry policy applied to transient errors.

Source

pub fn openai_key(&self) -> Option<String>

The OpenAI API key, falling back to OPENAI_API_KEY.

Source

pub fn anthropic_key(&self) -> Option<String>

The Anthropic API key, falling back to ANTHROPIC_API_KEY.

Source

pub fn openrouter_key(&self) -> Option<String>

The OpenRouter API key, falling back to OPENROUTER_API_KEY.

Source

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.

Source

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.

Source

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.

Source

pub fn openai_compatible_capabilities(&self) -> EndpointCapabilities

What the OpenAI-compatible endpoint was declared to support, defaulting to EndpointCapabilities::default.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn openrouter_app_title(&self) -> Option<String>

Deprecated alias for Config::openrouter_title, kept for callers written against the older attribution field names.

Source

pub fn retry_config(&self) -> RetryConfig

The effective retry policy, or RetryConfig::default when unset.

Source

pub fn timeout(&self) -> u64

The effective HTTP timeout in seconds (defaults to 120).

Source

pub fn validate_openai(&self) -> Result<()>

Available on crate feature openai only.

Check that OpenAI is usable.

§Errors

Returns Error::Config if no OpenAI API key is set programmatically or in OPENAI_API_KEY.

Source

pub fn validate_anthropic(&self) -> Result<()>

Available on crate feature anthropic only.

Check that Anthropic is usable.

§Errors

Returns Error::Config if no Anthropic API key is set programmatically or in ANTHROPIC_API_KEY.

Source

pub fn validate_openrouter(&self) -> Result<()>

Available on crate feature openrouter only.

Check that OpenRouter is usable.

§Errors

Returns Error::Config if no OpenRouter API key is set programmatically or in OPENROUTER_API_KEY.

Trait Implementations§

Source§

impl Clone for Config

Source§

fn clone(&self) -> Config

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Config

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Config

Source§

fn default() -> Config

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for Config

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for Config

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more