llama-index llms anthropic integration
Project Links
Meta
Author: Your Name
Requires Python: <4.0,>=3.10
Classifiers
LlamaIndex LLM Integration: Anthropic
Anthropic is an AI research company focused on developing advanced language models, notably the Claude series. Their flagship model, Claude, is designed to generate human-like text while prioritizing safety and alignment with human intentions. Anthropic aims to create AI systems that are not only powerful but also responsible, addressing potential risks associated with artificial intelligence.
Installation
%pip install llama-index-llms-anthropic
!pip install llama-index
# Set Tokenizer
# First we want to set the tokenizer, which is slightly different than TikToken.
# NOTE: The Claude 3 tokenizer has not been updated yet; using the existing Anthropic tokenizer leads
# to context overflow errors for 200k tokens. We've temporarily set the max tokens for Claude 3 to 180k.
Basic Usage
import os
from llama_index.llms.anthropic import Anthropic
from llama_index.core import Settings
os.environ["ANTHROPIC_API_KEY"] = "YOUR ANTHROPIC API KEY"
from llama_index.llms.anthropic import Anthropic
# To customize your API key, do this
# otherwise it will lookup ANTHROPIC_API_KEY from your env variable
# llm = Anthropic(api_key="<api_key>")
llm = Anthropic(model="claude-3-opus-20240229")
Settings.tokenizer = llm.tokenizer
resp = llm.complete("Paul Graham is ")
print(resp)
# Sample response
# Paul Graham is a well-known entrepreneur, programmer, venture capitalist, and essayist.
# He is best known for co-founding Viaweb, one of the first web application companies, which was later
# sold to Yahoo! in 1998 and became Yahoo! Store. Graham is also the co-founder of Y Combinator, a highly
# successful startup accelerator that has helped launch numerous successful companies, such as Dropbox,
# Airbnb, and Reddit.
Using Anthropic model through Vertex AI
import os
os.environ["ANTHROPIC_PROJECT_ID"] = "YOUR PROJECT ID HERE"
os.environ["ANTHROPIC_REGION"] = "YOUR PROJECT REGION HERE"
# Set region and project_id to make Anthropic use the Vertex AI client
llm = Anthropic(
model="claude-3-5-sonnet@20240620",
region=os.getenv("ANTHROPIC_REGION"),
project_id=os.getenv("ANTHROPIC_PROJECT_ID"),
)
resp = llm.complete("Paul Graham is ")
print(resp)
Chat example with a list of messages
from llama_index.core.llms import ChatMessage
from llama_index.llms.anthropic import Anthropic
messages = [
ChatMessage(
role="system", content="You are a pirate with a colorful personality"
),
ChatMessage(role="user", content="Tell me a story"),
]
resp = Anthropic(model="claude-3-opus-20240229").chat(messages)
print(resp)
Streaming example
from llama_index.llms.anthropic import Anthropic
llm = Anthropic(model="claude-3-opus-20240229", max_tokens=100)
resp = llm.stream_complete("Paul Graham is ")
for r in resp:
print(r.delta, end="")
Chat streaming with pirate story
llm = Anthropic(model="claude-3-opus-20240229")
messages = [
ChatMessage(
role="system", content="You are a pirate with a colorful personality"
),
ChatMessage(role="user", content="Tell me a story"),
]
resp = llm.stream_chat(messages)
for r in resp:
print(r.delta, end="")
Configure Model
from llama_index.llms.anthropic import Anthropic
llm = Anthropic(model="claude-3-sonnet-20240229")
resp = llm.stream_complete("Paul Graham is ")
for r in resp:
print(r.delta, end="")
Async completion
from llama_index.llms.anthropic import Anthropic
llm = Anthropic("claude-3-sonnet-20240229")
resp = await llm.acomplete("Paul Graham is ")
print(resp)
Using Anthropic Tools (Web Search)
from llama_index.llms.anthropic import Anthropic
# Initialize with web search tool
llm = Anthropic(
model="claude-3-7-sonnet-latest", # Must be a tool-supported model
max_tokens=1024,
tools=[
{
"type": "web_search_20250305",
"name": "web_search",
"max_uses": 3, # Limit to 3 searches
}
],
)
# Get response with citations
response = llm.complete("What are the latest AI research trends?")
# Access the main response content
print(response.text)
# Access citations if available
for citation in response.citations:
print(f"Source: {citation.get('url')} - {citation.get('cited_text')}")
Structured Prediction Example
from llama_index.llms.anthropic import Anthropic
from llama_index.core.prompts import PromptTemplate
from llama_index.core.bridge.pydantic import BaseModel
from typing import List
class MenuItem(BaseModel):
"""A menu item in a restaurant."""
course_name: str
is_vegetarian: bool
class Restaurant(BaseModel):
"""A restaurant with name, city, and cuisine."""
name: str
city: str
cuisine: str
menu_items: List[MenuItem]
llm = Anthropic("claude-3-5-sonnet-20240620")
prompt_tmpl = PromptTemplate(
"Generate a restaurant in a given city {city_name}"
)
# Option 1: Use `as_structured_llm`
restaurant_obj = (
llm.as_structured_llm(Restaurant)
.complete(prompt_tmpl.format(city_name="Miami"))
.raw
)
print(restaurant_obj)
# Option 2: Use `structured_predict`
# restaurant_obj = llm.structured_predict(Restaurant, prompt_tmpl, city_name="Miami")
# Streaming Structured Prediction
from llama_index.core.llms import ChatMessage
from IPython.display import clear_output
from pprint import pprint
input_msg = ChatMessage.from_str("Generate a restaurant in San Francisco")
sllm = llm.as_structured_llm(Restaurant)
stream_output = sllm.stream_chat([input_msg])
for partial_output in stream_output:
clear_output(wait=True)
pprint(partial_output.raw.dict())
LLM Implementation example
https://docs.llamaindex.ai/en/stable/examples/llm/anthropic/
0.11.2
Mar 28, 2026
0.11.1
Mar 16, 2026
0.11.0
Mar 12, 2026
0.10.11
Mar 09, 2026
0.10.10
Feb 23, 2026
0.10.9
Feb 17, 2026
0.10.8
Feb 05, 2026
0.10.7
Jan 30, 2026
0.10.6
Jan 21, 2026
0.10.5
Jan 02, 2026
0.10.4
Dec 23, 2025
0.10.3
Nov 27, 2025
0.10.2
Nov 10, 2025
0.10.1
Nov 05, 2025
0.10.0
Oct 27, 2025
0.9.7
Oct 18, 2025
0.9.6
Oct 17, 2025
0.9.5
Oct 15, 2025
0.9.4
Oct 13, 2025
0.9.3
Sep 29, 2025
0.9.2
Sep 29, 2025
0.9.1
Sep 25, 2025
0.9.0
Sep 24, 2025
0.8.6
Sep 08, 2025
0.8.5
Aug 21, 2025
0.8.4
Aug 14, 2025
0.8.3
Aug 12, 2025
0.8.2
Aug 08, 2025
0.8.1
Aug 05, 2025
0.8.0
Jul 30, 2025
0.7.6
Jul 03, 2025
0.7.5
Jun 26, 2025
0.7.4
Jun 26, 2025
0.7.3
Jun 21, 2025
0.7.2
Jun 02, 2025
0.7.1
Jun 01, 2025
0.7.0
May 30, 2025
0.6.19
May 25, 2025
0.6.18
May 23, 2025
0.6.17
May 22, 2025
0.6.16
May 22, 2025
0.6.15
May 22, 2025
0.6.14
May 19, 2025
0.6.13
May 15, 2025
0.6.12
May 08, 2025
0.6.11
May 07, 2025
0.6.10
Mar 06, 2025
0.6.9
Mar 06, 2025
0.6.8
Mar 06, 2025
0.6.7
Feb 27, 2025
0.6.6
Feb 24, 2025
0.6.5
Feb 14, 2025
0.6.4
Jan 17, 2025
0.6.3
Dec 17, 2024
0.6.1
Dec 15, 2024
0.6.0
Dec 15, 2024
0.5.0
Nov 18, 2024
0.4.1
Nov 12, 2024
0.4.0
Nov 11, 2024
0.3.9
Nov 05, 2024
0.3.8
Oct 25, 2024
0.3.7
Oct 22, 2024
0.3.6
Oct 11, 2024
0.3.5
Oct 08, 2024
0.3.4
Sep 29, 2024
0.3.3
Sep 27, 2024
0.3.2
Sep 27, 2024
0.3.1
Sep 13, 2024
0.3.0
Aug 27, 2024
0.2.1
Aug 22, 2024
0.2.0
Aug 22, 2024
0.1.17
Aug 08, 2024
0.1.16
Jul 23, 2024
0.1.15
Jun 28, 2024
0.1.14
Jun 20, 2024
0.1.13
Jun 12, 2024
0.1.12
Jun 12, 2024
0.1.11
Apr 26, 2024
0.1.10
Apr 15, 2024
0.1.9
Apr 05, 2024
0.1.8
Apr 04, 2024
0.1.7
Mar 19, 2024
0.1.6
Mar 13, 2024
0.1.5
Mar 05, 2024
0.1.4
Mar 04, 2024
0.1.3
Feb 21, 2024
0.1.2
Feb 20, 2024
0.1.1
Feb 12, 2024
0.1.0
Feb 10, 2024
0.0.1
Feb 03, 2024