Environment variables¶
The module reads the variables below, and only under the conditions given. Everything else in chat.Config is set in code, because the module owns
no config system. A host maps its own configuration into chat.Config and
passes it in.
The variables¶
| Variable | Constant | Read when | Effect |
|---|---|---|---|
AI_PROVIDER |
chat.EnvAIProvider |
Config.Provider is empty |
names the provider; an unregistered name is then fatal with unsupported provider: <name> |
ANTHROPIC_API_KEY |
chatanthropic.EnvClaudeKey |
neither Config.Credential nor Config.Token is set, on ProviderClaude |
supplies the API key |
OPENAI_API_KEY |
chatopenai.EnvOpenAIKey |
neither Config.Credential nor Config.Token is set, and Config.BaseURL is empty, on ProviderOpenAI |
supplies the API key, sent to OpenAI or to an https OPENAI_BASE_URL. See OpenAI's own variables |
OPENAI_BASE_URL |
chatopenai.EnvOpenAIBaseURL |
Config.BaseURL is empty, on ProviderOpenAI or openai-compatible |
read by the SDK, and the requests go there, with OPENAI_API_KEY under ProviderOpenAI; anything but an https URL fails construction |
GEMINI_API_KEY |
chatgemini.EnvGeminiKey |
neither Config.Credential nor Config.Token is set, on ProviderGemini |
supplies the API key |
VLLM_API_KEY |
chatvllm.EnvVLLMKey |
neither Config.Credential nor Config.Token is set, on ProviderVLLM |
supplies the key a server started with --api-key expects; unset, no key is sent. chat-vllm is not released yet |
Two providers authenticate by something other than an API key, so they read a
different set. Neither takes a Config.Token, and supplying one is an error
rather than an alternative.
| Variable | Read when | Effect |
|---|---|---|
GOOGLE_CLOUD_PROJECT |
Config.Project is empty, on ProviderGeminiVertex |
names the GCP project |
GOOGLE_CLOUD_LOCATION |
Config.Location is empty, on ProviderGeminiVertex |
names the region |
GOOGLE_GENAI_USE_VERTEXAI |
always, on ProviderGemini |
refuses construction. The SDK reads it and would switch backend silently, so a client configured for the Gemini API fails rather than being moved somewhere it cannot report |
AWS_REGION, AWS_DEFAULT_REGION |
Config.Location is empty, on ProviderBedrock |
names the region, through the standard AWS chain |
| the AWS credential chain | always, on ProviderBedrock |
supplies credentials: environment, profile, instance role, SSO, or a web identity token. chat-bedrock resolves it through awsclient rather than reading any of these directly |
| none | ProviderAzureOpenAI |
reads no environment variable at all, including OPENAI_API_KEY. Everything comes from Config, or from a credential passed to chatopenaiazure.WithTokenCredential. See below |
Azure reads nothing from the environment, and that is worth stating because
the SDK underneath would. openai-go falls back to OPENAI_API_KEY by
default; the Azure request options disable that fallback, so an OpenAI key
exported in the shell cannot reach an Azure deployment. It would also be refused
if it did, because the SDK will not send an Authorization header alongside an
Azure credential.
For ambient Azure credential discovery, resolve it yourself with
azureclient and pass the
result to WithTokenCredential. chat-openai-azure takes a credential rather
than resolving one, so it depends on azcore and not on azidentity.
OpenAI's own variables¶
openai-go reads OpenAI's variables itself, and ProviderOpenAI limits where
the key goes. From chat-openai v0.22.2:
- A
Config.BaseURLwith noConfig.TokenorConfig.Credentialfails construction withchatopenai.ErrBaseURLNeedsKey.OPENAI_API_KEYis never sent to aConfig.BaseURL. OPENAI_BASE_URLmust be an https URL, andAllowInsecureBaseURLdoes not loosen that. To use any other URL, pass it asConfig.BaseURL.OPENAI_API_KEYdoes go to an httpsOPENAI_BASE_URL. Setting both is how a host keeps the endpoint and the key in its environment, so a gateway you name there receives the key.
Up to v0.22.2, WithExternalAuth with no Config.BaseURL still sends
OPENAI_API_KEY. From the chat-openai release after it, WithExternalAuth sends no
OPENAI_API_KEY to any host. A caller who relied on it passes the key among
its options with option.WithAPIKey, or the requests come back 401.
OPENAI_ORG_ID, OPENAI_PROJECT_ID and OPENAI_CUSTOM_HEADERS follow a
custom host. Under ProviderOpenAI, the SDK sends them as the
OpenAI-Organization, OpenAI-Project and custom headers on every request,
to Config.BaseURL or OPENAI_BASE_URL as much as to OpenAI. A gateway you
point the client at sees them, so don't export secrets in
OPENAI_CUSTOM_HEADERS for a client aimed at one.
An option.WithBaseURL passed inside WithExternalAuth is the caller's own
request option. It does not go through the base-URL check that Config.BaseURL
and OPENAI_BASE_URL meet, so prefer Config.BaseURL.
openai-compatible reads none of OpenAI's account variables either, from
chat-openai v0.17.3. Its key comes from Config.Credential or Config.Token,
and with neither no key is sent. OPENAI_API_KEY, OPENAI_ADMIN_KEY,
OPENAI_ORG_ID, OPENAI_PROJECT_ID and OPENAI_CUSTOM_HEADERS are cleared
from what the SDK would otherwise send, because the endpoint is not OpenAI's.
OPENAI_BASE_URL is the exception: with Config.BaseURL empty, the SDK still
reads it, and it must be https.
Earlier versions sent OPENAI_API_KEY to Config.BaseURL when no token was
set.
Plus any variable you name: Config.Credential: chat.EnvCredential("NAME")
reads the key from that variable, which is how a host keeps the secret out of
its config file while keeping the reference in it.
Where each sits in the credential cascade¶
This applies to the API-key providers. A set Config.Credential is the only
source read, and the well-known variables are read only through it, if the
source names them. Without one, the first non-empty value wins:
Config.Token- the provider's well-known variable from the table above
Both are whitespace-trimmed, so a blank token cannot mask the variable.
ProviderGeminiVertex and ProviderBedrock do not use this cascade at all.
They authenticate through their cloud's own credential chain, which is a
resolution order rather than a value, and neither accepts a Config.Token.
Why AI_PROVIDER has no prefix¶
AI_PROVIDER is deliberately unprefixed and deliberately narrow. It is an
ecosystem-wide default (the same variable across tools built on this module)
and it is consulted only when Config.Provider is empty, so it can never
override a provider a caller actually set. There is no AI_MODEL,
AI_BASE_URL or CHAT_* equivalent: everything else is host configuration, and
inventing a parallel environment schema here would fight whatever the host
already has.
Variables used only when running this module's own tests¶
| Variable | Effect |
|---|---|
INT_TEST=1 |
runs every integration test |
INT_TEST_CHAT=1 |
runs the integration tests tagged chat |
Integration tests are gated by environment variable rather than build tag, so they stay compiled and discoverable in an IDE. Without one of these set they skip.
Related¶
- Configuration fields: everything else, which is set in code.
- Choose & configure a provider: the credential cascade in practice.
- Provider-endpoint & credential security: why the reference is in config and the secret is not.