Skip to main content
OpenCode uses environment variables for configuration, especially for API keys and provider credentials. This page documents all environment variables that OpenCode recognizes.

API provider credentials

These environment variables configure authentication for various AI providers.

Anthropic (Claude)

string
API key for Anthropic’s Claude models.Provider: Anthropic
Models: Claude 4, Claude 3.5, Claude 3.7, and all Claude model variants
Get your API key from the Anthropic Console.
If this key is present and valid, OpenCode will default to using Claude 4 Sonnet for all agents.

OpenAI

string
API key for OpenAI models.Provider: OpenAI
Models: GPT-4.1, GPT-4.5, GPT-4o, O1, O3, O4, and all OpenAI variants
Get your API key from the OpenAI Platform.
OpenCode prioritizes providers in this order: Copilot > Anthropic > OpenAI. If multiple keys are present, Anthropic will be used by default.

Google Gemini

string
API key for Google Gemini models via Google AI Studio.Provider: Google Gemini
Models: Gemini 2.5, Gemini 2.5 Flash, Gemini 2.0 Flash, Gemini 2.0 Flash Lite
Get your API key from Google AI Studio.
This is different from Google Cloud VertexAI. See VERTEXAI_PROJECT for VertexAI configuration.

GitHub Copilot

string
GitHub token with Copilot permissions for accessing GitHub Copilot models.Provider: GitHub Copilot (Experimental)
Models: GPT-4o, GPT-4.1, Claude 3.5/3.7/4 Sonnet, O1, O3-mini, O4-mini, Gemini 2.0/2.5
Requirements:
  • Copilot chat in the IDE enabled in GitHub settings
  • One of:
    • VSCode GitHub Copilot chat extension
    • GitHub gh CLI
    • Neovim GitHub Copilot plugin (copilot.vim or copilot.lua)
    • GitHub token with copilot permissions
If using one of the above tools, authenticate the tool with your GitHub account. This creates a token at:
  • ~/.config/github-copilot/hosts.json
  • ~/.config/github-copilot/apps.json
  • $XDG_CONFIG_HOME/github-copilot/hosts.json
Alternatively, set the token explicitly:
Copilot support is currently experimental. Some features may not work as expected.

Groq

string
API key for Groq cloud inference.Provider: Groq
Models: Llama 4, QWEN QWQ-32b, Deepseek R1 distill Llama 70b, Llama 3.3 70b
Get your API key from Groq Console.

OpenRouter

string
API key for OpenRouter, which provides access to multiple AI models through a unified API.Provider: OpenRouter
Models: Various models with openrouter. prefix
Get your API key from OpenRouter.

xAI (Grok)

string
API key for xAI’s Grok models.Provider: xAI
Models: Grok-3 Beta, Grok-3 Mini, Grok-3 Fast variants
Get your API key from xAI Console.

Cloud provider credentials

These environment variables configure access to cloud-based AI services.

AWS Bedrock

AWS Bedrock provides access to Claude models through Amazon’s infrastructure.
string
AWS access key ID for authenticating with AWS services.
string
AWS secret access key corresponding to the access key ID.
string
AWS region for Bedrock API calls.
If not set, OpenCode will check AWS_DEFAULT_REGION.
string
Fallback AWS region if AWS_REGION is not set.
string
AWS profile name to use from ~/.aws/credentials and ~/.aws/config.
string
Fallback AWS profile if AWS_PROFILE is not set.
string
Used when running on ECS with task IAM roles. Set automatically by AWS.
This is typically set automatically when running in AWS ECS. You don’t need to set it manually.
string
Alternative credentials endpoint for ECS tasks. Set automatically by AWS.
This is typically set automatically when running in AWS ECS. You don’t need to set it manually.

Azure OpenAI

Azure OpenAI Service provides OpenAI models through Microsoft Azure.
string
Azure OpenAI service endpoint URL.Format: https://<resource-name>.openai.azure.com
This variable must be set for Azure OpenAI to be enabled, even when using Entra ID authentication.
string
API key for Azure OpenAI service. Optional when using Entra ID authentication.
This can be omitted when using Azure Entra ID (formerly Azure AD) credentials for authentication.
string
Azure OpenAI API version to use.Format: YYYY-MM-DD-preview or YYYY-MM-DD

Google Cloud VertexAI

VertexAI provides access to Gemini models through Google Cloud Platform.
string
Google Cloud project ID for VertexAI.
Both VERTEXAI_PROJECT and VERTEXAI_LOCATION must be set for VertexAI to be enabled.
string
Google Cloud location/region for VertexAI API calls.Common values: us-central1, us-east1, europe-west1, asia-northeast1
string
Alternative to VERTEXAI_PROJECT. Google Cloud project ID.
string
Alternative to VERTEXAI_LOCATION. Google Cloud region.
string
Alternative to VERTEXAI_LOCATION and GOOGLE_CLOUD_REGION.

Self-hosted models

string
Endpoint URL for self-hosted OpenAI-compatible model servers.Use this to connect to locally running models or custom inference servers that implement the OpenAI API specification.Compatible servers:
  • llama.cpp server
  • Ollama (with OpenAI compatibility)
  • vLLM
  • Text Generation Inference (TGI)
  • LocalAI
  • Any OpenAI-compatible API
Example with Ollama:
Example configuration:

System configuration

string
Default shell to use for the bash tool. If not specified, OpenCode defaults to /bin/bash.This variable is typically set automatically by your system but can be overridden.
You can also configure the shell in .opencode.json:
string
Enable development debug mode. When set to "true", OpenCode writes debug logs to a file instead of using the TUI logger.Values: "true" or unset
When enabled:
  • Logs written to <data-directory>/debug.log
  • Messages saved to <data-directory>/messages/
  • Useful for debugging OpenCode itself
This is primarily for OpenCode development and debugging. Regular users should use the --debug flag instead.

Configuration precedence

OpenCode loads configuration from multiple sources in this order (highest to lowest priority):
  1. Command-line flags (e.g., --debug)
  2. Environment variables (documented on this page)
  3. Local config file (.opencode.json in working directory)
  4. Global config file:
    • $HOME/.opencode.json
    • $XDG_CONFIG_HOME/opencode/.opencode.json
    • $HOME/.config/opencode/.opencode.json
  5. Default values

Provider priority

When multiple API keys are present, OpenCode selects the default provider in this order:
  1. GitHub Copilot (GITHUB_TOKEN)
  2. Anthropic (ANTHROPIC_API_KEY)
  3. OpenAI (OPENAI_API_KEY)
  4. Google Gemini (GEMINI_API_KEY)
  5. Groq (GROQ_API_KEY)
  6. OpenRouter (OPENROUTER_API_KEY)
  7. xAI (XAI_API_KEY)
  8. AWS Bedrock (AWS credentials)
  9. Azure OpenAI (AZURE_OPENAI_ENDPOINT)
  10. Google Cloud VertexAI (VERTEXAI_PROJECT + VERTEXAI_LOCATION)
You can override this default by explicitly configuring models in .opencode.json.

Security best practices

Never commit API keys to version control. Always use environment variables or secure secret management systems.
  1. Use environment variables:
  2. Use a secrets manager:
  3. Use project-specific env files (not committed):
    Then source before running:
  4. Use shell-specific secure storage:
  5. Restrict file permissions:

Verification

To verify which provider OpenCode will use:
The debug output will show:
  • Which API keys were found
  • Which provider was selected as default
  • Any configuration issues

Troubleshooting

No valid provider available

If you see “no valid provider available”, ensure:
  1. At least one API key environment variable is set
  2. The API key is valid and has not expired
  3. For cloud providers (AWS, Azure, VertexAI), all required variables are set

Provider not working as expected

Check:
  1. API key format is correct
  2. No typos in environment variable names
  3. Environment variables are exported (use export)
  4. Variables are set in the same shell session

Cloud provider credentials

For AWS Bedrock:
For Azure OpenAI:
For Google Cloud VertexAI: