AgentConfig

Struct AgentConfig 

Source
pub struct AgentConfig {
    pub base_url: Option<String>,
    pub api_key: Option<String>,
    pub model: Option<String>,
    pub small_fast_model: Option<String>,
    pub max_thinking_tokens: Option<u32>,
}
Expand description

Agent configuration loaded from environment variables and settings files

Configuration priority (highest to lowest):

  1. Environment variables (e.g., ANTHROPIC_MODEL)
  2. Settings files - Top-level fields (e.g., model)
  3. Settings files - env object (e.g., env.ANTHROPIC_MODEL)
  4. Defaults

Settings files are loaded from:

  • ~/.claude/settings.json (user settings)
  • <project_dir>/.claude/settings.json (project settings)
  • <project_dir>/.claude/settings.local.json (local settings)

Supports configuring alternative AI model providers (e.g., domestic providers in China) through environment variables or settings files.

Fields§

§base_url: Option<String>

Anthropic API base URL Environment variable: ANTHROPIC_BASE_URL Settings field: apiBaseUrl

§api_key: Option<String>

API key for authentication Environment variable: ANTHROPIC_API_KEY (preferred) or ANTHROPIC_AUTH_TOKEN (legacy) Note: Not supported in settings files for security reasons

§model: Option<String>

Primary model name Environment variable: ANTHROPIC_MODEL Settings field: model

§small_fast_model: Option<String>

Small/fast model name (fallback) Environment variable: ANTHROPIC_SMALL_FAST_MODEL Settings field: smallFastModel

§max_thinking_tokens: Option<u32>

Maximum tokens for thinking blocks (extended thinking mode)

Can be set via:

  • Environment variable: MAX_THINKING_TOKENS
  • Settings field: alwaysThinkingEnabled (sets to default 20000)
  • Settings env object: env.MAX_THINKING_TOKENS

When alwaysThinkingEnabled is true in settings, this defaults to 20000. Typical values: 4096, 8000, 16000, 20000

Implementations§

Source§

impl AgentConfig

Source

pub fn new() -> Self

Create a new empty configuration

Source

pub fn from_env() -> Self

Load configuration from environment variables

Reads the following environment variables:

  • ANTHROPIC_BASE_URL: API base URL
  • ANTHROPIC_API_KEY: API key (preferred)
  • ANTHROPIC_AUTH_TOKEN: Auth token (legacy, fallback if API_KEY not set)
  • ANTHROPIC_MODEL: Primary model name
  • ANTHROPIC_SMALL_FAST_MODEL: Small/fast model name
  • MAX_THINKING_TOKENS: Maximum tokens for thinking blocks
Source

pub fn from_settings_or_env(project_dir: &Path) -> Self

Load configuration from settings files and environment variables

Configuration priority (highest to lowest):

  1. Environment variables (e.g., ANTHROPIC_MODEL)
  2. Settings files - Top-level fields (e.g., model)
  3. Settings files - env object (e.g., env.ANTHROPIC_MODEL)
  4. Defaults (including alwaysThinkingEnabled → default MAX_THINKING_TOKENS)

Settings files are loaded in this order (later ones override earlier):

  • ~/.claude/settings.json (user settings)
  • <project_dir>/.claude/settings.json (project settings)
  • <project_dir>/.claude/settings.local.json (local settings)
§Arguments
  • project_dir - The project working directory
§Example settings.json

Using top-level fields:

{
  "model": "claude-opus-4-20250514",
  "smallFastModel": "claude-haiku-4-20250514",
  "apiBaseUrl": "https://api.anthropic.com"
}

Using env object (compatible with Claude Code CLI):

{
  "env": {
    "ANTHROPIC_MODEL": "claude-opus-4-20250514",
    "ANTHROPIC_SMALL_FAST_MODEL": "claude-haiku-4-20250514",
    "ANTHROPIC_BASE_URL": "https://api.anthropic.com"
  }
}

Enabling extended thinking mode with alwaysThinkingEnabled:

{
  "model": "claude-sonnet-4-20250514",
  "alwaysThinkingEnabled": true
}

This will set MAX_THINKING_TOKENS to 20000 by default. You can still override it with the MAX_THINKING_TOKENS environment variable.

Source

pub fn is_configured(&self) -> bool

Check if any configuration is set

Source

pub fn to_env_vars(&self) -> HashMap<String, String>

Get environment variables to pass to Claude Code CLI

Returns a HashMap of environment variable names and values that should be passed to the subprocess.

Source

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

Get a masked version of the API key for logging

Shows first 4 and last 4 characters with the middle masked by asterisks. For example: sk-ant-api03-xxx... becomes sk-a***xxx

Returns None if no API key is set.

Source

pub fn apply_to_options(&self, options: &mut ClaudeAgentOptions)

Apply configuration to ClaudeAgentOptions

Sets the model and environment variables on the options.

Trait Implementations§

Source§

impl Clone for AgentConfig

Source§

fn clone(&self) -> AgentConfig

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for AgentConfig

Source§

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

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

impl Default for AgentConfig

Source§

fn default() -> AgentConfig

Returns the “default value” for a type. 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<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> 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> FutureExt for T

Source§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
Source§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
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> IntoMaybeUndefined<T> for T

Source§

impl<T> IntoOption<T> for T

Source§

impl<T> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<L> LayerExt<L> for L

Source§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in Layered.
Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: 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: 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> 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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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