Anthropic Setup¶
Configuration guide for the Anthropic/Claude integration.
Prerequisites¶
- Anthropic API account
- API key from console.anthropic.com
Environment Variables¶
Backend Configuration¶
The Anthropic API key is configured in the backend settings:
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¶
- Verify
ANTHROPIC_API_KEYis set correctly - Check key hasn't expired
- Ensure key has appropriate permissions
Rate Limits¶
- Use Claude Haiku for lighter workloads
- Implement exponential backoff
- Check your API tier limits
Context Length Exceeded¶
- Reduce input size
- Use summarization for long content
- Consider chunking large documents