How to Set Up Environment Variables for Custom Model Providers in Claudian
Claudian parses plain-text KEY=VALUE entries from the Settings → Environment panel to configure custom AI endpoints and model selections, applying these variables through parseEnvironmentVariables in src/utils/env.ts and reconciling changes via applyEnvironmentVariables in src/main.ts.
Claudian, an Obsidian plugin maintained at YishenTu/claudian, allows users to override default Anthropic models and API endpoints using custom environment variables. Understanding how to set up environment variables for custom model providers enables integration with third-party AI services or self-hosted Claude-compatible APIs without modifying core plugin files.
Understanding the Environment Variable Parser
The plugin processes raw environment text through parseEnvironmentVariables defined in src/utils/env.ts. This function accepts a string input and returns a Record<string, string> by splitting lines on =, trimming whitespace, ignoring comments, and stripping optional export prefixes.
When you click Apply in the settings interface, the plugin invokes this parser to transform your custom variables into a runtime configuration object used by the Claude SDK.
Supported Custom Model Configuration Keys
Claudian recognizes four specific environment variables for model selection, defined in the CUSTOM_MODEL_ENV_KEYS constant in src/utils/env.ts:
ANTHROPIC_MODEL– Sets the default model across all tiersANTHROPIC_DEFAULT_OPUS_MODEL– Configures the Opus tier specificallyANTHROPIC_DEFAULT_SONNET_MODEL– Configures the Sonnet tier specificallyANTHROPIC_DEFAULT_HAIKU_MODEL– Configures the Haiku tier specifically
The function getModelTypeFromEnvKey maps each key to its corresponding model type (model, opus, sonnet, haiku), while getModelsFromEnvironment builds a sorted list of available models from these values for the UI selector.
Configuring Custom Variables in the UI
The environment input interface resides in src/features/settings/ClaudianSettings.ts. Navigate to Settings → Environment → Custom variables to access the textarea where you paste your configuration.
When you modify the text and click Apply, the plugin stores the raw string in this.settings.environmentVariables and triggers the application flow. This design allows immediate testing of different provider endpoints without restarting Obsidian.
Runtime Application and Model Reconciliation
The core application logic lives in applyEnvironmentVariables within src/main.ts. This method performs the following sequence:
- Parse: Calls
parseEnvironmentVariables(envText)to generate the env object - Detect Changes: Compares the new text against
this.runtimeEnvironmentVariablesusingcomputeEnvHash(lines 38-56 insrc/main.ts), which hashes relevant keys includingANTHROPIC_MODEL, the threeANTHROPIC_DEFAULT_*_MODELvariables, andANTHROPIC_BASE_URL - Reconcile: Invokes
reconcileModelWithEnvironment, which usesgetModelsFromEnvironmentto extract custom model IDs andgetCurrentModelFromEnvironmentto determine the active default - Apply: Updates
this.settings.modeland persists changes, restarting conversation tabs if the model configuration changed
Pointing to Custom Provider Endpoints
To route requests to a non-Anthropic API, define ANTHROPIC_BASE_URL in your environment variables:
ANTHROPIC_BASE_URL=https://my-custom-api.example.com
The plugin reads this value during the hash computation phase and passes it to src/utils/claudeCli.ts, which constructs the Claude SDK client using your custom endpoint. This enables compatibility with proxy services, local LLM servers, or third-party Claude API providers.
Practical Implementation Examples
Minimal Custom Provider Setup
Paste this block into Settings → Environment → Custom variables and click Apply:
# Custom provider configuration
ANTHROPIC_MODEL=anthropic/claude-3-opus
ANTHROPIC_BASE_URL=https://api.my-provider.com
This configuration routes all requests to api.my-provider.com while selecting the Opus model variant.
Programmatic Environment Parsing
For plugin extensions or vault scripts, import the utility functions directly:
import { parseEnvironmentVariables, getModelsFromEnvironment } from '@/utils/env';
// Read environment from a vault file
const envText = await app.vault.read('.env.custom');
const env = parseEnvironmentVariables(envText);
// Extract available models
const customModels = getModelsFromEnvironment(env);
console.log('Detected models:', customModels.map(m => m.label));
This approach leverages the same parsing logic the core plugin uses, ensuring consistency with the UI behavior.
Runtime Model Override
To dynamically select models based on current environment variables:
import { parseEnvironmentVariables, getCurrentModelFromEnvironment } from '@/utils/env';
const envText = plugin.settings.environmentVariables;
const env = parseEnvironmentVariables(envText);
const preferredModel = getCurrentModelFromEnvironment(env);
if (preferredModel) {
plugin.settings.model = preferredModel;
}
The getCurrentModelFromEnvironment function returns the first non-empty value, preferring ANTHROPIC_MODEL over tier-specific variables when multiple are defined.
Summary
- Configuration Location: Set variables in Settings → Environment within the Claudian plugin interface, stored in
src/features/settings/ClaudianSettings.ts - Core Parsing:
parseEnvironmentVariablesinsrc/utils/env.tshandles the KEY=VALUE syntax, supporting comments and optionalexportprefixes - Model Variables: Use
ANTHROPIC_MODEL,ANTHROPIC_DEFAULT_OPUS_MODEL,ANTHROPIC_DEFAULT_SONNET_MODEL, orANTHROPIC_DEFAULT_HAIKU_MODELto define custom model IDs - Custom Endpoints: Set
ANTHROPIC_BASE_URLto redirect the Claude SDK to alternative API providers, processed insrc/utils/claudeCli.ts - Application Flow:
applyEnvironmentVariablesinsrc/main.tsorchestrates parsing, change detection viacomputeEnvHash, and model reconciliation without requiring an Obsidian restart
Frequently Asked Questions
What environment variables does Claudian support for custom model providers?
Claudian recognizes four model-specific variables—ANTHROPIC_MODEL, ANTHROPIC_DEFAULT_OPUS_MODEL, ANTHROPIC_DEFAULT_SONNET_MODEL, and ANTHROPIC_DEFAULT_HAIKU_MODEL—plus ANTHROPIC_BASE_URL for custom endpoints. These are defined in the CUSTOM_MODEL_ENV_KEYS constant in src/utils/env.ts and processed by getModelsFromEnvironment to populate the model selector.
How do I point Claudian to a custom API endpoint instead of Anthropic's servers?
Define ANTHROPIC_BASE_URL=https://your-endpoint.com in the Custom variables text area under Settings → Environment, then click Apply. The plugin stores this value and passes it to the Claude SDK constructor in src/utils/claudeCli.ts, redirecting all API traffic to your specified URL while maintaining the same request format.
Why isn't my custom model appearing in the model selector dropdown?
Verify that your environment variable uses one of the four supported keys (ANTHROPIC_MODEL or the tier-specific variants) and that the value contains no whitespace or comment characters on the same line. The getModelsFromEnvironment function in src/utils/env.ts only includes non-empty values from these specific keys in the dropdown list.
Do I need to restart Obsidian after changing environment variables?
No. The applyEnvironmentVariables method in src/main.ts immediately applies changes when you click Apply, computes a hash of the relevant variables via computeEnvHash to detect modifications, and reconciles the active model. Conversation tabs restart automatically if the model configuration changed, but the application itself continues running.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →