diff --git a/.env.example b/.env.example index eb4cb901..2aa2d0fd 100644 --- a/.env.example +++ b/.env.example @@ -1,5 +1,6 @@ # LLM Providers (set the one you use) ANTHROPIC_API_KEY= +ANTHROPIC_BASE_URL= OPENAI_API_KEY= GOOGLE_API_KEY= XAI_API_KEY= diff --git a/README.md b/README.md index 0958d00a..b7c6a50e 100644 --- a/README.md +++ b/README.md @@ -130,6 +130,11 @@ export OPENROUTER_API_KEY=... # OpenRouter export ALPHA_VANTAGE_API_KEY=... # Alpha Vantage ``` +To use a Claude account subscription (or proxy) instead of a direct API key, set a custom base URL: +```bash +export ANTHROPIC_BASE_URL=https://your-custom-endpoint.example.com +``` + For local models, configure Ollama with `llm_provider: "ollama"` in your config. Alternatively, copy `.env.example` to `.env` and fill in your keys: diff --git a/tradingagents/llm_clients/anthropic_client.py b/tradingagents/llm_clients/anthropic_client.py index 2c1e5a67..1f2c7d4a 100644 --- a/tradingagents/llm_clients/anthropic_client.py +++ b/tradingagents/llm_clients/anthropic_client.py @@ -1,3 +1,4 @@ +import os from typing import Any, Optional from langchain_anthropic import ChatAnthropic @@ -24,7 +25,12 @@ class NormalizedChatAnthropic(ChatAnthropic): class AnthropicClient(BaseLLMClient): - """Client for Anthropic Claude models.""" + """Client for Anthropic Claude models. + + Supports custom base URLs via the base_url parameter or ANTHROPIC_BASE_URL + environment variable, enabling use with Claude account subscriptions + or proxy endpoints instead of direct API key access. + """ def __init__(self, model: str, base_url: Optional[str] = None, **kwargs): super().__init__(model, base_url, **kwargs) @@ -33,6 +39,11 @@ class AnthropicClient(BaseLLMClient): """Return configured ChatAnthropic instance.""" llm_kwargs = {"model": self.model} + # Resolve base URL: explicit param > env var > default Anthropic API + base_url = self.base_url or os.environ.get("ANTHROPIC_BASE_URL") + if base_url: + llm_kwargs["anthropic_api_url"] = base_url + for key in _PASSTHROUGH_KWARGS: if key in self.kwargs: llm_kwargs[key] = self.kwargs[key]