> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/getzep/graphiti/llms.txt
> Use this file to discover all available pages before exploring further.

# Anthropic Integration

> Use Anthropic's Claude models for LLM inference in Graphiti

Anthropic's Claude models provide powerful language understanding with extended context windows and strong reasoning capabilities.

## Installation

Install Graphiti with Anthropic support:

<CodeGroup>
  ```bash pip theme={null}
  pip install graphiti-core[anthropic]
  ```

  ```bash uv theme={null}
  uv add graphiti-core[anthropic]
  ```
</CodeGroup>

## Configuration

### Environment Variables

```bash .env theme={null}
ANTHROPIC_API_KEY=sk-ant-...
```

## Basic Setup

Initialize Graphiti with Anthropic:

```python theme={null}
import os
from graphiti_core import Graphiti
from graphiti_core.llm_client.anthropic_client import AnthropicClient
from graphiti_core.llm_client.config import LLMConfig

# Create Anthropic LLM client
llm_client = AnthropicClient(
    config=LLMConfig(
        api_key=os.environ["ANTHROPIC_API_KEY"],
        model="claude-haiku-4-5-latest",
        temperature=0.7,
        max_tokens=8192
    )
)

# Initialize Graphiti
graphiti = Graphiti(
    "bolt://localhost:7687",
    "neo4j",
    "password",
    llm_client=llm_client
)
```

## Supported Models

### Claude 4.5 Models

* **claude-haiku-4-5-latest** (recommended): Fast, cost-effective, 64K output tokens
* **claude-sonnet-4-5-latest**: Balanced performance, 64K output tokens
* **claude-sonnet-4-5-20250929**: Specific version of Sonnet 4.5

### Claude 3.7 Models

* **claude-3-7-sonnet-latest**: Advanced reasoning, 64K output tokens
* **claude-3-7-sonnet-20250219**: Specific version

### Claude 3.5 Models

* **claude-3-5-haiku-latest**: Fast, 8K output tokens
* **claude-3-5-sonnet-latest**: Balanced, 8K output tokens
* **claude-3-5-haiku-20241022**: Specific version
* **claude-3-5-sonnet-20241022**: Specific version

### Legacy Models

* **claude-3-opus-latest**: Highest capability, 4K output tokens
* **claude-3-sonnet-20240229**: Previous generation
* **claude-3-haiku-20240307**: Previous generation

## Model Selection

Graphiti uses two models:

* **Primary model**: For complex entity extraction and relationship detection
* **Small model**: For simpler classification tasks

```python theme={null}
llm_client = AnthropicClient(
    config=LLMConfig(
        model="claude-sonnet-4-5-latest",        # Primary model
        small_model="claude-haiku-4-5-latest",   # Small model
    )
)
```

## Configuration Options

| Parameter     | Type  | Default                     | Description                |
| ------------- | ----- | --------------------------- | -------------------------- |
| `api_key`     | str   | From env                    | Anthropic API key          |
| `model`       | str   | `"claude-haiku-4-5-latest"` | Primary LLM model          |
| `small_model` | str   | Same as model               | Model for simpler tasks    |
| `temperature` | float | `0.7`                       | Sampling temperature (0-1) |
| `max_tokens`  | int   | Model-specific              | Maximum tokens to generate |

## Maximum Output Tokens

Anthropic models have different max output token limits:

| Model Family | Max Output Tokens |
| ------------ | ----------------- |
| Claude 4.5   | 65,536 (64K)      |
| Claude 3.7   | 65,536 (64K)      |
| Claude 3.5   | 8,192 (8K)        |
| Claude 3     | 4,096 (4K)        |
| Claude 2     | 4,096 (4K)        |

Graphiti automatically selects appropriate limits based on the model.

## Structured Output

Anthropic doesn't have native structured output like OpenAI. Graphiti uses a **tool-based approach** to ensure valid JSON responses:

```python theme={null}
# Graphiti automatically:
# 1. Defines a tool with your Pydantic schema
# 2. Forces tool use via tool_choice
# 3. Extracts structured data from tool arguments
# 4. Falls back to JSON extraction if needed
```

**Benefits:**

* More reliable structured outputs
* Automatic retry on validation errors
* Graceful fallback handling

## Complete Example

```python theme={null}
import asyncio
import os
from datetime import datetime, timezone
from graphiti_core import Graphiti
from graphiti_core.llm_client.anthropic_client import AnthropicClient
from graphiti_core.llm_client.config import LLMConfig
from graphiti_core.nodes import EpisodeType

async def main():
    # Configure Anthropic LLM
    llm_client = AnthropicClient(
        config=LLMConfig(
            api_key=os.environ["ANTHROPIC_API_KEY"],
            model="claude-haiku-4-5-latest",
            temperature=0.7
        ),
        max_tokens=16384  # Override default
    )
    
    # Initialize Graphiti
    graphiti = Graphiti(
        "bolt://localhost:7687",
        "neo4j",
        "password",
        llm_client=llm_client
    )
    
    try:
        # Add an episode
        await graphiti.add_episode(
            name="AI Research 1",
            episode_body="Anthropic released Claude 4.5, featuring extended context and reasoning.",
            source=EpisodeType.text,
            reference_time=datetime.now(timezone.utc)
        )
        
        # Search the graph
        results = await graphiti.search("What are Claude 4.5's capabilities?")
        for result in results:
            print(f"Fact: {result.fact}")
    
    finally:
        await graphiti.close()

if __name__ == "__main__":
    asyncio.run(main())
```

## Error Handling

Graphiti automatically handles:

* **Rate Limit Errors**: Exponential backoff and retry
* **Content Policy Violations**: Converted to `RefusalError` (no retry)
* **API Errors**: Automatic retry with error context
* **Validation Errors**: Retry with schema hints

## Rate Limiting

Adjust concurrency to avoid rate limits:

```bash .env theme={null}
SEMAPHORE_LIMIT=10  # Default: 10 concurrent operations
```

If you encounter rate limit errors, reduce this value.

## When to Use Anthropic

**Choose Anthropic if you:**

* Need extended context windows (200K tokens for Claude 3)
* Want strong reasoning and analysis capabilities
* Prefer Claude's conversational style
* Need specific safety and content filtering

**Choose OpenAI if you:**

* Need native structured output support
* Want the latest GPT-5 reasoning models
* Prefer function calling over tool use
* Need faster response times

## Cost Optimization

* **Use Haiku Models**: Claude Haiku is cost-effective for most tasks
* **Batch Operations**: Process multiple items together
* **Token Limits**: Set appropriate `max_tokens` for your use case
* **Model Selection**: Use cheaper models for simpler tasks

## Best Practices

1. **Start with Haiku 4.5**: Best cost/performance ratio
2. **Use Sonnet for Complex Tasks**: When you need deeper reasoning
3. **Monitor Token Usage**: Track costs via Anthropic dashboard
4. **Set Appropriate Limits**: Configure `max_tokens` based on task complexity
5. **Handle Refusals**: Catch `RefusalError` for content policy violations

## Token Usage Tracking

Graphiti tracks token usage automatically:

```python theme={null}
# Token usage is logged automatically
# Check your application logs for:
# - Input tokens
# - Output tokens
# - Total token usage per prompt
```

## Related Resources

* [Anthropic API Documentation](https://docs.anthropic.com/)
* [Claude Model Comparison](https://docs.anthropic.com/en/docs/models-overview)
* [Anthropic Pricing](https://www.anthropic.com/pricing)
* [Claude Safety Best Practices](https://docs.anthropic.com/en/docs/safety-best-practices)
