Skip to main content

Anthropic

LiteLLM supports

  • claude-2.1
  • claude-2.1
  • claude-instant-1
  • claude-instant-1.2

API Keys​

import os 

os.environ["ANTHROPIC_API_KEY"] = "your-api-key"

Sample Usage​

import os
from litellm import completion

# set env - [OPTIONAL] replace with your anthropic key
os.environ["ANTHROPIC_API_KEY"] = "your-api-key"

messages = [{"role": "user", "content": "Hey! how's it going?"}]
response = completion(model="claude-instant-1", messages=messages)
print(response)

streaming​

Just set stream=True when calling completion.

import os
from litellm import completion

# set env
os.environ["ANTHROPIC_API_KEY"] = "your-api-key"

messages = [{"role": "user", "content": "Hey! how's it going?"}]
response = completion(model="claude-instant-1", messages=messages, stream=True)
for chunk in response:
print(chunk["choices"][0]["delta"]["content"]) # same as openai format

Model Details​

Model NameFunction CallRequired OS Variables
claude-2.1completion('claude-2.1', messages)os.environ['ANTHROPIC_API_KEY']
claude-2completion('claude-2', messages)os.environ['ANTHROPIC_API_KEY']
claude-instant-1completion('claude-instant-1', messages)os.environ['ANTHROPIC_API_KEY']
claude-instant-1.2completion('claude-instant-1.2', messages)os.environ['ANTHROPIC_API_KEY']