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):
- Environment variables (e.g.,
ANTHROPIC_MODEL) - Settings files - Top-level fields (e.g.,
model) - Settings files -
envobject (e.g.,env.ANTHROPIC_MODEL) - 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
envobject:env.MAX_THINKING_TOKENS
When alwaysThinkingEnabled is true in settings, this defaults to 20000.
Typical values: 4096, 8000, 16000, 20000
Implementations§
Source§impl AgentConfig
impl AgentConfig
Sourcepub fn from_env() -> Self
pub fn from_env() -> Self
Load configuration from environment variables
Reads the following environment variables:
ANTHROPIC_BASE_URL: API base URLANTHROPIC_API_KEY: API key (preferred)ANTHROPIC_AUTH_TOKEN: Auth token (legacy, fallback if API_KEY not set)ANTHROPIC_MODEL: Primary model nameANTHROPIC_SMALL_FAST_MODEL: Small/fast model nameMAX_THINKING_TOKENS: Maximum tokens for thinking blocks
Sourcepub fn from_settings_or_env(project_dir: &Path) -> Self
pub fn from_settings_or_env(project_dir: &Path) -> Self
Load configuration from settings files and environment variables
Configuration priority (highest to lowest):
- Environment variables (e.g.,
ANTHROPIC_MODEL) - Settings files - Top-level fields (e.g.,
model) - Settings files -
envobject (e.g.,env.ANTHROPIC_MODEL) - 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.
Sourcepub fn is_configured(&self) -> bool
pub fn is_configured(&self) -> bool
Check if any configuration is set
Sourcepub fn to_env_vars(&self) -> HashMap<String, String>
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.
Sourcepub fn masked_api_key(&self) -> Option<String>
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.
Sourcepub fn apply_to_options(&self, options: &mut ClaudeAgentOptions)
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
impl Clone for AgentConfig
Source§fn clone(&self) -> AgentConfig
fn clone(&self) -> AgentConfig
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for AgentConfig
impl Debug for AgentConfig
Source§impl Default for AgentConfig
impl Default for AgentConfig
Source§fn default() -> AgentConfig
fn default() -> AgentConfig
Auto Trait Implementations§
impl Freeze for AgentConfig
impl RefUnwindSafe for AgentConfig
impl Send for AgentConfig
impl Sync for AgentConfig
impl Unpin for AgentConfig
impl UnwindSafe for AgentConfig
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoMaybeUndefined<T> for T
impl<T> IntoMaybeUndefined<T> for T
fn into_maybe_undefined(self) -> MaybeUndefined<T>
Source§impl<T> IntoOption<T> for T
impl<T> IntoOption<T> for T
fn into_option(self) -> Option<T>
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request