Anthropic SDK Environment Overrides
Relevant source files
The following files were used as context for generating this wiki page:
The Anthropic SDK Environment Overrides in the ollacc project represent a specialized configuration mechanism designed to redirect the claude CLI (Claude Code) from its default Anthropic API targets to the Ollama Cloud API. By manipulating specific environment variables, ollacc ensures that the Anthropic SDK communicates with Ollama's OpenAI-compatible endpoint at https://ollama.com rather than the standard Anthropic backend.
This system functions as a thin wrapper that manages authentication tokens, base URLs, and model selection, ultimately executing the claude command with a modified environment. This approach allows users to leverage Ollama Cloud models without requiring a local Ollama server installation.
Sources: README.md:14-25, README.md:158-166
Environment Variable Architecture
The integration relies on three primary environment variables that dictate the behavior of the Anthropic SDK. The ollacc launcher ensures these are set correctly before executing the claude process.
Required Environment Variables
| Variable | Value/Purpose | Technical Rationale |
|---|---|---|
ANTHROPIC_BASE_URL | https://ollama.com | Redirects SDK requests to Ollama Cloud. The SDK appends /v1/messages automatically. |
ANTHROPIC_AUTH_TOKEN | User's Ollama API Key | Acts as the Bearer token for authentication with Ollama Cloud. |
ANTHROPIC_API_KEY | "" (Empty String) | CRITICAL: Must be empty. If set, the SDK ignores ANTHROPIC_AUTH_TOKEN. |
Sources: README.md:158-166, lib/auth.sh:54-58
Initialization Flow
The project uses a structured sequence to prepare these overrides, starting from configuration loading to the final exec of the SDK client.
The diagram shows the logic flow from initial execution to the final hand-off to the Anthropic SDK.
Sources: lib/auth.sh:8-58, README.md:31-34
Configuration Management
Environment overrides are persisted in a local configuration file, typically located at ~/.ollacc/config. This file is sourced as a shell script by the launcher.
Storage and Security
- File Location:
~/.ollacc/config - Permissions: The directory is set to
chmod 700and the config file tochmod 600to ensure only the user can read the sensitive API key. - Persistence: Configuration is updated using the
config_setfunction, which utilizesbash_escapeto ensure characters like$,\, and"do not break the shell sourcing mechanism.
Sources: install.sh:41-43, lib/config.sh:37-43, lib/config.sh:13-26
Key Functions for Environment Control
| Function | Source File | Description |
|---|---|---|
auth_load_config | lib/auth.sh | Sources the configuration file and sets the default ANTHROPIC_BASE_URL. |
auth_export_sdk_env | lib/auth.sh | Exports the final environment variables to the current shell. |
config_set | lib/config.sh | Safely writes environment variable assignments to the config file. |
flags_handle | lib/flags.sh | Processes --key-clear to unset ANTHROPIC_AUTH_TOKEN. |
Sources: lib/auth.sh:8-58, lib/config.sh:37-65, lib/flags.sh:11-30
Implementation Details
The override logic is implemented in the lib/auth.sh file, which specifically handles the export of variables required by the Anthropic SDK.
# lib/auth.sh:54-58
auth_export_sdk_env() {
export ANTHROPIC_BASE_URL
export ANTHROPIC_AUTH_TOKEN
export ANTHROPIC_API_KEY=""
}
Sources: lib/auth.sh:54-58
Integration with Model Selection
The environment overrides are used in conjunction with a model selection process. The catalog_resolve function uses the ANTHROPIC_AUTH_TOKEN to fetch the authorized catalog from https://ollama.com/api/tags via curl.
This sequence illustrates how the authenticated environment enables both model discovery and SDK execution.
Sources: lib/catalog.sh:22-45, README.md:158-166
Summary
The Anthropic SDK Environment Overrides provide the necessary glue between Claude Code and Ollama Cloud. By intercepting the SDK's configuration through shell environment variables, ollacc successfully redirects traffic to an alternative provider while maintaining compatibility with the official claude command-line tools. This system emphasizes security through restrictive file permissions and technical precision by accounting for SDK-specific behaviors regarding token priority.
Sources: README.md:14-34, lib/auth.sh:54-58