Skip to content

Anthropic Setup

Configuration guide for the Anthropic/Claude integration.


Prerequisites


Environment Variables

# Required
ANTHROPIC_API_KEY="sk-ant-..."

Backend Configuration

The Anthropic API key is configured in the backend settings:

# core/app/core/config.py
class Settings(BaseSettings):
    ANTHROPIC_API_KEY: str | None = None

Dependencies

# pyproject.toml
[project.dependencies]
anthropic = "^0.40.0"
litellm = "^1.75.8"   # Multi-provider abstraction
pillow = "^10.4.0"    # Image processing
aiohttp = "^3.10.6"   # HTTP requests

Client Initialization

Direct API Usage (Captioning Service)

import anthropic

class AnthropicAPI:
    def __init__(self, api_key: str):
        self.client = anthropic.Client(api_key=api_key)

    def create_message(self, system_prompt: str, content: list):
        return self.client.messages.create(
            model="claude-sonnet-4-20250514",
            max_tokens=1500,
            temperature=0.5,
            system=system_prompt,
            messages=[{"role": "user", "content": content}],
        )

Via AI Core Provider

from ai_core.providers import AnthropicProvider
from ai_core.client import AICore

provider = AnthropicProvider(api_key=settings.ANTHROPIC_API_KEY)
client = AICore(provider=provider)

Default Parameters

General Defaults

Parameter Value Description
max_tokens 4096 Maximum response tokens
temperature 0.7 Response creativity
api_base https://api.anthropic.com API endpoint

Captioning Defaults

Parameter Value Description
max_tokens 1500 Shorter for captions
temperature 0.5 More deterministic
model claude-sonnet-4-20250514 Vision-capable

Model Aliases

model_aliases = {
    "claude": "claude-sonnet-4-20250514",
    "claude-4": "claude-sonnet-4-20250514",
    "sonnet": "claude-sonnet-4-20250514",
    "opus": "claude-opus-4-20250514",
    "haiku": "claude-3-haiku-20240307",
    "claude-3-5-sonnet": "claude-3-5-sonnet-20241022",
}

Image Requirements

For vision features:

Requirement Value
Max Size 5 MB (after base64 encoding)
Formats JPEG, PNG, WebP, GIF
Encoding Base64 data URL

The integration automatically handles:

  • Format detection and conversion to WebP if needed
  • Size optimization
  • MIME type detection
  • Base64 encoding

Verification

Test your configuration:

import anthropic
from app.core.config import settings

client = anthropic.Client(api_key=settings.ANTHROPIC_API_KEY)

response = client.messages.create(
    model="claude-sonnet-4-20250514",
    max_tokens=100,
    messages=[{"role": "user", "content": "Hello, Claude!"}]
)

print(response.content[0].text)

Troubleshooting

Invalid API Key

Error: Invalid API key
  • Verify ANTHROPIC_API_KEY is set correctly
  • Check key hasn't expired
  • Ensure key has appropriate permissions

Rate Limits

Error: Rate limit exceeded
  • Use Claude Haiku for lighter workloads
  • Implement exponential backoff
  • Check your API tier limits

Context Length Exceeded

Error: Context length exceeded
  • Reduce input size
  • Use summarization for long content
  • Consider chunking large documents