Skip to main content
Anthropic’s Claude models provide powerful language understanding with extended context windows and strong reasoning capabilities.

Installation

Install Graphiti with Anthropic support:
pip install graphiti-core[anthropic]

Configuration

Environment Variables

.env
ANTHROPIC_API_KEY=sk-ant-...

Basic Setup

Initialize Graphiti with Anthropic:
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
llm_client = AnthropicClient(
    config=LLMConfig(
        model="claude-sonnet-4-5-latest",        # Primary model
        small_model="claude-haiku-4-5-latest",   # Small model
    )
)

Configuration Options

ParameterTypeDefaultDescription
api_keystrFrom envAnthropic API key
modelstr"claude-haiku-4-5-latest"Primary LLM model
small_modelstrSame as modelModel for simpler tasks
temperaturefloat0.7Sampling temperature (0-1)
max_tokensintModel-specificMaximum tokens to generate

Maximum Output Tokens

Anthropic models have different max output token limits:
Model FamilyMax Output Tokens
Claude 4.565,536 (64K)
Claude 3.765,536 (64K)
Claude 3.58,192 (8K)
Claude 34,096 (4K)
Claude 24,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:
# 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

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:
.env
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:
# Token usage is logged automatically
# Check your application logs for:
# - Input tokens
# - Output tokens
# - Total token usage per prompt

Build docs developers (and LLMs) love