How to Intermediate · 3 min read

How to use LlamaIndex with Claude

Quick answer
Use LlamaIndex with Anthropic's claude-3-5-sonnet-20241022 by configuring the Anthropic client as the LLM backend. Pass your documents to LlamaIndex and query it to get responses powered by Claude's advanced language understanding.

PREREQUISITES

  • Python 3.8+
  • Anthropic API key
  • pip install anthropic llama-index

Setup

Install the required packages and set your Anthropic API key as an environment variable.

bash
pip install anthropic llama-index

Step by step

This example shows how to create a simple LlamaIndex index with documents and query it using Anthropic Claude.

python
import os
from anthropic import Anthropic
from llama_index import GPTVectorStoreIndex, SimpleDirectoryReader, LLMPredictor, PromptHelper

# Initialize Anthropic client
client = Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])

# Define a wrapper class to adapt Anthropic client to LlamaIndex's LLMPredictor interface
class ClaudeLLM:
    def __init__(self, client, model="claude-3-5-sonnet-20241022", max_tokens=1024):
        self.client = client
        self.model = model
        self.max_tokens = max_tokens

    def __call__(self, prompt: str) -> str:
        response = self.client.messages.create(
            model=self.model,
            max_tokens=self.max_tokens,
            system="You are a helpful assistant.",
            messages=[{"role": "user", "content": prompt}]
        )
        return response.content

# Instantiate the LLM predictor with Claude
llm_predictor = LLMPredictor(llm=ClaudeLLM(client))

# Load documents from a directory (replace 'data' with your folder)
loader = SimpleDirectoryReader('data')
documents = loader.load_data()

# Create the index
index = GPTVectorStoreIndex(documents, llm_predictor=llm_predictor)

# Query the index
query = "What is the main topic of the documents?"
response = index.query(query)
print("Response:", response.response)
output
Response: The main topic of the documents is ...

Common variations

You can customize the Claude model variant by changing the model parameter in ClaudeLLM. For async usage, wrap calls with async Anthropic client methods. You can also use other LlamaIndex index types like GPTListIndex or GPTTreeIndex depending on your use case.

Troubleshooting

  • If you get authentication errors, verify your ANTHROPIC_API_KEY environment variable is set correctly.
  • If responses are empty or incomplete, increase max_tokens in the ClaudeLLM wrapper.
  • For rate limits, check Anthropic API usage and consider retry logic.

Key Takeaways

  • Use Anthropic's official SDK with claude-3-5-sonnet-20241022 for best coding and reasoning results.
  • LlamaIndex requires an LLM wrapper; create a simple adapter for Claude's API.
  • Load documents with SimpleDirectoryReader and query with GPTVectorStoreIndex for vector-based search.
  • Adjust max_tokens and model variant to optimize response quality and cost.
  • Always set your API keys securely via environment variables.
Verified 2026-04 · claude-3-5-sonnet-20241022
Verify ↗