Skip to content

Anthropic Chat Provider

Anthropic API chat provider for the conversational interface.

Anthropic chat provider supporting four authentication methods.

Credentials are tried in priority order (first match wins): 1. Vertex AI -- GOOGLE_CLOUD_PROJECT / ANTHROPIC_VERTEX_PROJECT_ID 2. Bedrock -- AWS_REGION / AWS_DEFAULT_REGION 3. API key -- ANTHROPIC_API_KEY 4. Claude Code OAuth -- reads ~/.claude/.credentials.json

This fall-through design means the same deployment works unchanged across Google Cloud, AWS, direct API, and local development with Claude Code login.

Responses from the Anthropic SDK are converted into the normalized ChatResponse / TextBlock / ToolUseBlock types so the rest of the codebase stays provider-agnostic.

Classes

AnthropicChatProvider

AnthropicChatProvider(model: str = '')

Bases: ChatProvider

Chat provider using the Anthropic SDK (direct API, Vertex AI, Bedrock, or OAuth).

Source code in src/chat_providers/anthropic.py
def __init__(self, model: str = ""):
    import anthropic

    self._client = None
    self._model = model

    # Try Vertex AI first
    project_id = (
        os.environ.get("GOOGLE_CLOUD_PROJECT")
        or os.environ.get("ANTHROPIC_VERTEX_PROJECT_ID")
    )
    if project_id:
        from anthropic import AsyncAnthropicVertex

        region = (
            os.environ.get("GOOGLE_CLOUD_LOCATION")
            or os.environ.get("CLOUD_ML_REGION")
            or "us-east5"
        )
        self._client = AsyncAnthropicVertex(project_id=project_id, region=region)
        if not self._model:
            self._model = "claude-sonnet-4@20250514"
        return

    # Try Bedrock
    if os.environ.get("AWS_REGION") or os.environ.get("AWS_DEFAULT_REGION"):
        from anthropic import AsyncAnthropicBedrock

        self._client = AsyncAnthropicBedrock()
        if not self._model:
            self._model = "claude-sonnet-4-20250514"
        return

    # Try explicit API key
    api_key = os.environ.get("ANTHROPIC_API_KEY")
    if api_key:
        self._client = anthropic.AsyncAnthropic(api_key=api_key)
        if not self._model:
            self._model = "claude-sonnet-4-20250514"
        return

    # Try Claude Code OAuth credentials (~/.claude/.credentials.json)
    oauth_token = _load_claude_oauth_token()
    if oauth_token:
        self._client = anthropic.AsyncAnthropic(auth_token=oauth_token)
        if not self._model:
            self._model = "claude-sonnet-4-20250514"
        return