How to Configure Environment Variables for Provider Overrides in Kimi Code
Kimi Code recognizes a specific set of environment variables—including KIMI_CODE_BASE_URL, KIMI_CODE_OAUTH_HOST, and KIMI_MODEL_REASONING_KEY—that override corresponding fields in ~/.kimi-code/config.toml before the provider adapters initialize.
The MoonshotAI/kimi-code repository implements a three-layer configuration system where environment variables serve as surgical overrides for provider-specific settings. Unlike generic credential injection, these variables replace exact TOML fields early in the boot process, allowing you to redirect API endpoints or modify model behavior without editing persistent configuration files.
Recognized Provider Override Variables
Kimi Code explicitly reads five environment variables to override provider configuration. These are not generic fallbacks; each maps to a specific field in the configuration schema:
KIMI_CODE_BASE_URL– Overrides[providers.<name>.base_url], forcing the API endpoint used for the provider.KIMI_CODE_OAUTH_HOST– Overrides[providers.<name>.oauth_host], redirecting the OAuth flow to a custom authentication server.KIMI_MODEL_REASONING_KEY– Overrides[models."<alias>".reasoning_key], replacing the default reasoning field name for OpenAI-compatible providers.KIMI_MODEL_MAX_OUTPUT_SIZE– Overrides[models."<alias>".max_output_size], capping output tokens for Anthropic-only models.KIMI_MODEL_THINKING_KEEP– Overrides[thinking].keep], controlling whether thinking blocks are retained (values:all,none).
These variables are parsed before the TOML config file is loaded, ensuring they take precedence over file-based settings.
Source Code Implementation
The override logic is hardcoded in specific source files within the packages/ directory.
Base URL Resolution
In packages/oauth/src/managed-usage.ts at line 38, the kimiCodeEnvBaseUrl function reads the base URL override:
export function kimiCodeEnvBaseUrl(env: ManagedKimiEnv = process.env): string | undefined {
return (env['KIMI_CODE_BASE_URL'] ?? DEFAULT_KIMI_CODE_BASE_URL).replace(/\/+$/, '');
}
This function strips trailing slashes and returns the custom URL early in the provider initialization chain.
OAuth Host Configuration
In packages/oauth/src/managed-kimi-code.ts at line 8, the kimiCodeEnvOAuthHost function retrieves the OAuth endpoint:
export function kimiCodeEnvOAuthHost(env: ManagedKimiEnv = process.env): string | undefined {
return env['KIMI_CODE_OAUTH_HOST'] ?? env['KIMI_OAUTH_HOST'];
}
This allows corporate deployments to redirect authentication to internal identity providers.
Model Alias Overrides
Model-level variables like KIMI_MODEL_REASONING_KEY are applied when constructing model aliases in packages/kosong/src/model/aliases.ts. The logic reads env['KIMI_MODEL_REASONING_KEY'] and injects it into the model's configuration object, overriding any value defined in the [models."<alias>"] TOML table.
Configuration Priority Chain
Kimi Code resolves settings using a strict hierarchy where command-line arguments sit at the top and defaults at the bottom:
Command-line > Environment-variable override > Config file > Default
Setting KIMI_CODE_BASE_URL will always win over any base_url entry in config.toml for the matching provider, but a command-line flag passed at runtime would supersede the environment variable.
Practical Configuration Examples
Set a custom base URL for the OpenAI provider:
export KIMI_CODE_BASE_URL="https://my-proxy.example.com/v1"
kimi --model openai/gpt-4o
Override the OAuth host for the managed "kimi" provider:
export KIMI_CODE_OAUTH_HOST="https://auth.mycompany.com"
kimi
Force a non-default reasoning field name:
export KIMI_MODEL_REASONING_KEY="my_reasoning_field"
kimi -m openai/gpt-4o
Cap output size for an Anthropic model:
export KIMI_MODEL_MAX_OUTPUT_SIZE=1024
kimi -m anthropic/claude-3-opus-20240229
Summary
- Kimi Code supports five specific environment variables (
KIMI_CODE_BASE_URL,KIMI_CODE_OAUTH_HOST,KIMI_MODEL_REASONING_KEY,KIMI_MODEL_MAX_OUTPUT_SIZE,KIMI_MODEL_THINKING_KEEP) that override TOML configuration fields. - These variables are parsed in
packages/oauth/src/managed-usage.ts,packages/oauth/src/managed-kimi-code.ts, andpackages/kosong/src/model/aliases.tsbefore the config file loads. - The priority chain places environment variables between command-line arguments and the config file, making them ideal for temporary redirects or containerized deployments.
- Provider credentials themselves are not set via these overrides; they must be defined in the config file or the
[providers.<name>.env]sub-table.
Frequently Asked Questions
Do environment variables override command-line arguments in Kimi Code?
No. The configuration priority chain places command-line arguments at the highest level, followed by environment variables. If you pass a --base-url flag to the CLI, it will supersede any KIMI_CODE_BASE_URL value.
Can I use environment variables to inject API keys for providers?
No. The recognized environment variables specifically control endpoint URLs, OAuth hosts, and model behavior flags. According to the documentation in docs/en/configuration/providers.md (lines 20-22), provider credentials must be supplied in the config file or its [providers.<name>.env] sub-table; generic environment variables are not used as a credential fallback.
Which variable should I use to proxy all OpenAI-compatible requests?
Set KIMI_CODE_BASE_URL to your proxy endpoint. This variable is read by packages/oauth/src/managed-usage.ts and replaces the base_url field for the relevant provider, ensuring all API traffic routes through your specified proxy.
Are these environment variables case-sensitive?
Yes. The source code explicitly checks for uppercase keys such as KIMI_CODE_BASE_URL and KIMI_MODEL_REASONING_KEY. Using lowercase variants like kimi_code_base_url will not trigger the override mechanism.
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 →