Anthropic
LiteLLM 支援所有 anthropic 模型。
claude-opus-4-6(claude-opus-4-6-20260205)claude-sonnet-4-6claude-sonnet-4-5-20250929claude-opus-4-5-20251101claude-opus-4-1-20250805claude-4(claude-opus-4-20250514,claude-sonnet-4-20250514)claude-3.7(claude-3-7-sonnet-20250219)claude-3.5(claude-3-5-sonnet-20240620)claude-3(claude-3-haiku-20240307,claude-3-opus-20240229,claude-3-sonnet-20240229)claude-2claude-2.1claude-instant-1.2
| 屬性 | 詳細資訊 |
|---|---|
| 說明 | Claude 是由 Anthropic 建立的高效能、值得信賴且智慧的 AI 平台。Claude 在涉及語言、推理、分析、程式撰寫等任務上表現出色。亦可透過 Azure Foundry 使用。 |
| LiteLLM 提供者路由 | anthropic/(將此前綴加到模型名稱前,即可將任何請求路由到 Anthropic - 例如 anthropic/claude-3-5-sonnet-20240620)。對於 Azure Foundry 部署,請使用 azure/claude-*(請參閱 Azure Anthropic 文件) |
| 提供者文件 | Anthropic ↗, Azure Foundry Claude ↗ |
| 提供者 API 端點 | https://api.anthropic.com(或 Azure Foundry endpoint:https://<resource-name>.services.ai.azure.com/anthropic) |
| 支援的端點 | /chat/completions, /v1/messages(passthrough) |
支援的 OpenAI 參數
請在程式碼中於 此處 查看
"stream",
"stop",
"temperature",
"top_p",
"max_tokens",
"max_completion_tokens",
"tools",
"tool_choice",
"extra_headers",
"parallel_tool_calls",
"response_format",
"user",
"reasoning_effort",
結構化輸出
LiteLLM 支援 Anthropic 的 結構化輸出功能,適用於 Claude Sonnet 4.5 與 Opus 4.1 模型。當您對這些模型使用 response_format 時,LiteLLM 會自動:
- 加入必要的
structured-outputs-2025-11-13beta 標頭 - 將 OpenAI 的
response_format轉換為 Anthropic 的output_format格式
支援的模型
sonnet-4-5或sonnet-4.5(所有 Sonnet 4.5 變體)opus-4-1或opus-4.1(所有 Opus 4.1 變體)opus-4-5或opus-4.5(所有 Opus 4.5 變體)
使用範例
- LiteLLM SDK
- LiteLLM Proxy
from litellm import completion
response = completion(
model="claude-sonnet-4-5-20250929",
messages=[{"role": "user", "content": "What is the capital of France?"}],
response_format={
"type": "json_schema",
"json_schema": {
"name": "capital_response",
"strict": True,
"schema": {
"type": "object",
"properties": {
"country": {"type": "string"},
"capital": {"type": "string"}
},
"required": ["country", "capital"],
"additionalProperties": False
}
}
}
)
print(response.choices[0].message.content)
# Output: {"country": "France", "capital": "Paris"}
- 設定 config.yaml
model_list:
- model_name: claude-sonnet-4-5
litellm_params:
model: anthropic/claude-sonnet-4-5-20250929
api_key: os.environ/ANTHROPIC_API_KEY
- 啟動 proxy
litellm --config /path/to/config.yaml
- 測試它!
curl http://0.0.0.0:4000/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $LITELLM_KEY" \
-d '{
"model": "claude-sonnet-4-5",
"messages": [{"role": "user", "content": "What is the capital of France?"}],
"response_format": {
"type": "json_schema",
"json_schema": {
"name": "capital_response",
"strict": true,
"schema": {
"type": "object",
"properties": {
"country": {"type": "string"},
"capital": {"type": "string"}
},
"required": ["country", "capital"],
"additionalProperties": false
}
}
}
}'
使用支援的模型進行結構化輸出時,LiteLLM 會自動:
- 將 OpenAI 的
response_format轉換為 Anthropic 的output_schema - 新增
anthropic-beta: structured-outputs-2025-11-13標頭 - 使用該結構建立工具並強制模型使用它
API 金鑰
import os
os.environ["ANTHROPIC_API_KEY"] = "your-api-key"
# os.environ["ANTHROPIC_API_BASE"] = "" # [OPTIONAL] or 'ANTHROPIC_BASE_URL'
# os.environ["LITELLM_ANTHROPIC_DISABLE_URL_SUFFIX"] = "true" # [OPTIONAL] Disable automatic URL suffix appending
Claude 模型也可透過 Microsoft Azure Foundry 使用。請使用 azure/ 前綴,而不是 anthropic/,並設定 Azure 驗證。詳情請參閱 Azure Anthropic 文件。
範例:
response = completion(
model="azure/claude-sonnet-4-5",
api_base="https://<resource-name>.services.ai.azure.com/anthropic",
api_key="your-azure-api-key",
messages=[{"role": "user", "content": "Hello!"}]
)
自訂 API Base
當使用 Anthropic 的自訂 API base(例如 proxy 或自訂端點)時,LiteLLM 會自動將適當的後綴(/v1/messages 或 /v1/complete)附加到您的 base URL。
如果您的自訂端點已包含完整路徑,或不遵循 Anthropic 的標準 URL 結構,您可以停用此自動附加後綴功能:
import os
os.environ["ANTHROPIC_API_BASE"] = "https://my-custom-endpoint.com/custom/path"
os.environ["LITELLM_ANTHROPIC_DISABLE_URL_SUFFIX"] = "true" # Prevents automatic suffix
不使用 LITELLM_ANTHROPIC_DISABLE_URL_SUFFIX:
- Base URL
https://my-proxy.com→https://my-proxy.com/v1/messages - Base URL
https://my-proxy.com/api→https://my-proxy.com/api/v1/messages
使用 LITELLM_ANTHROPIC_DISABLE_URL_SUFFIX=true:
- Base URL
https://my-proxy.com/custom/path→https://my-proxy.com/custom/path(不變)
Azure AI Foundry(替代方法)
若要完整支援 Azure(包含 Azure AD 驗證),請使用專用的 Azure Anthropic 提供者 並搭配 azure_ai/ 前綴。
或者,您也可以直接使用 anthropic/ 提供者搭配您的 Azure 端點,因為 Azure 是以 Anthropic 的原生 API 來公開 Claude。
from litellm import completion
response = completion(
model="anthropic/claude-sonnet-4-5",
api_base="https://<your-resource>.services.ai.azure.com/anthropic",
api_key="<your-azure-api-key>",
messages=[{"role": "user", "content": "Hello!"}],
)
print(response)
**尋找您的 Azure 端點:**前往 Azure AI Foundry → 您的部署 → Overview。您的 base URL 會是 https://<resource-name>.services.ai.azure.com/anthropic
使用方式
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-opus-4-20250514", messages=messages)
print(response)
使用方式 - 串流
在呼叫 completion 時,只要設定 stream=True 即可。
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-opus-4-20250514", messages=messages, stream=True)
for chunk in response:
print(chunk["choices"][0]["delta"]["content"]) # same as openai format
使用 LiteLLM Proxy 的使用方式
以下是如何使用 LiteLLM Proxy Server 呼叫 Anthropic
1. 將金鑰儲存在您的環境中
export ANTHROPIC_API_KEY="your-api-key"
2. 啟動 proxy
- config.yaml
- config - 預設所有 Anthropic 模型
- cli
model_list:
- model_name: claude-4 ### RECEIVED MODEL NAME ###
litellm_params: # all params accepted by litellm.completion() - https://docs.litellm.ai/docs/completion/input
model: claude-opus-4-20250514 ### MODEL NAME sent to `litellm.completion()` ###
api_key: "os.environ/ANTHROPIC_API_KEY" # does os.getenv("ANTHROPIC_API_KEY")
litellm --config /path/to/config.yaml
如果您想對 claude-3-haiku-20240307、claude-3-opus-20240229、claude-2.1 發出請求,而不必在 config.yaml 中定義它們,請使用此項
必要的環境變數
ANTHROPIC_API_KEY=sk-ant****
model_list:
- model_name: "*"
litellm_params:
model: "*"
litellm --config /path/to/config.yaml
此 config.yaml 的請求範例
請確保使用 anthropic/ 前綴,將請求路由至 Anthropic API
curl --location 'http://0.0.0.0:4000/chat/completions' \
--header 'Content-Type: application/json' \
--data ' {
"model": "anthropic/claude-3-haiku-20240307",
"messages": [
{
"role": "user",
"content": "what llm are you"
}
]
}
'
$ litellm --model claude-opus-4-20250514
# Server running on http://0.0.0.0:4000
3. 進行測試
- Curl 請求
- OpenAI v1.0.0+
- Langchain
curl --location 'http://0.0.0.0:4000/chat/completions' \
--header 'Content-Type: application/json' \
--data ' {
"model": "claude-3",
"messages": [
{
"role": "user",
"content": "what llm are you"
}
]
}
'
import openai
client = openai.OpenAI(
api_key="anything",
base_url="http://0.0.0.0:4000"
)
# request sent to model set on litellm proxy, `litellm --model`
response = client.chat.completions.create(model="claude-3", messages = [
{
"role": "user",
"content": "this is a test request, write a short poem"
}
])
print(response)
from langchain.chat_models import ChatOpenAI
from langchain.prompts.chat import (
ChatPromptTemplate,
HumanMessagePromptTemplate,
SystemMessagePromptTemplate,
)
from langchain.schema import HumanMessage, SystemMessage
chat = ChatOpenAI(
openai_api_base="http://0.0.0.0:4000", # set openai_api_base to the LiteLLM Proxy
model = "claude-3",
temperature=0.1
)
messages = [
SystemMessage(
content="You are a helpful assistant that im using to make a test request to."
),
HumanMessage(
content="test from litellm. tell me why it's amazing in 1 sentence"
),
]
response = chat(messages)
print(response)
支援的模型
Model Name 👉 人類可讀名稱。
Function Call 👉 在 LiteLLM 中如何呼叫該模型。
| 模型名稱 | 函式呼叫 |
|---|---|
| claude-opus-4-6 | completion('claude-opus-4-6-20260205', messages) |
| claude-sonnet-4-5 | completion('claude-sonnet-4-5-20250929', messages) |
| claude-opus-4-5 | completion('claude-opus-4-5-20251101', messages) |
| claude-opus-4-1 | completion('claude-opus-4-1-20250805', messages) |
| claude-opus-4 | completion('claude-opus-4-20250514', messages) |
| claude-sonnet-4 | completion('claude-sonnet-4-20250514', messages) |
| claude-3.7 | completion('claude-3-7-sonnet-20250219', messages) |
| claude-3-5-sonnet | completion('claude-3-5-sonnet-20240620', messages) |
| claude-3-haiku | completion('claude-3-haiku-20240307', messages) |
| claude-3-opus | completion('claude-3-opus-20240229', messages) |
| claude-3-5-sonnet-20240620 | completion('claude-3-5-sonnet-20240620', messages) |
| claude-3-sonnet | completion('claude-3-sonnet-20240229', messages) |
| claude-2.1 | completion('claude-2.1', messages) |
| claude-2 | completion('claude-2', messages) |
| claude-instant-1.2 | completion('claude-instant-1.2', messages) |
| claude-instant-1 | completion('claude-instant-1', messages) |
快取提示詞
使用 Anthropic Prompt Caching
以下是 LiteLLM 針對 Anthropic Context Caching 的一個範例原始請求樣貌:
POST Request Sent from LiteLLM:
curl -X POST \
https://api.anthropic.com/v1/messages \
-H 'accept: application/json' -H 'anthropic-version: 2023-06-01' -H 'content-type: application/json' -H 'x-api-key: sk-...' \
-d '{'model': 'claude-3-5-sonnet-20240620', [
{
"role": "user",
"content": [
{
"type": "text",
"text": "What are the key terms and conditions in this agreement?",
"cache_control": {
"type": "ephemeral"
}
}
]
},
{
"role": "assistant",
"content": [
{
"type": "text",
"text": "Certainly! The key terms and conditions are the following: the contract is 1 year long for $10/mo"
}
]
}
],
"temperature": 0.2,
"max_tokens": 10
}'
注意: Anthropic 不再需要 anthropic-beta: prompt-caching-2024-07-31 標頭。當您在訊息中使用 cache_control 時,提示快取現在會自動運作。
快取 - 大型上下文快取
此範例示範基本的 Prompt Caching 用法,將法律協議的完整文字快取為前綴,同時讓使用者指示保持不快取。
- LiteLLM SDK
- LiteLLM Proxy
response = await litellm.acompletion(
model="anthropic/claude-3-5-sonnet-20240620",
messages=[
{
"role": "system",
"content": [
{
"type": "text",
"text": "You are an AI assistant tasked with analyzing legal documents.",
},
{
"type": "text",
"text": "Here is the full text of a complex legal agreement",
"cache_control": {"type": "ephemeral"},
},
],
},
{
"role": "user",
"content": "what are the key terms and conditions in this agreement?",
},
]
)
LiteLLM Proxy 與 OpenAI 相容
這是一個使用 OpenAI Python SDK 向 LiteLLM Proxy 發送請求的範例
假設您在 litellm proxy config.yaml 上有一個 model=anthropic/claude-3-5-sonnet-20240620
import openai
client = openai.AsyncOpenAI(
api_key="anything", # litellm proxy api key
base_url="http://0.0.0.0:4000" # litellm proxy base url
)
response = await client.chat.completions.create(
model="anthropic/claude-3-5-sonnet-20240620",
messages=[
{
"role": "system",
"content": [
{
"type": "text",
"text": "You are an AI assistant tasked with analyzing legal documents.",
},
{
"type": "text",
"text": "Here is the full text of a complex legal agreement",
"cache_control": {"type": "ephemeral"},
},
],
},
{
"role": "user",
"content": "what are the key terms and conditions in this agreement?",
},
]
)
快取 - 工具定義
在此範例中,我們示範快取工具定義。
cache_control 參數放在最後一個 tool 上
- LiteLLM SDK
- LiteLLM Proxy
import litellm
response = await litellm.acompletion(
model="anthropic/claude-3-5-sonnet-20240620",
messages = [{"role": "user", "content": "What's the weather like in Boston today?"}]
tools = [
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
},
"required": ["location"],
},
"cache_control": {"type": "ephemeral"}
},
}
]
)
LiteLLM Proxy 與 OpenAI 相容
這是一個使用 OpenAI Python SDK 向 LiteLLM Proxy 發送請求的範例
假設您在 litellm proxy config.yaml 上有一個 model=anthropic/claude-3-5-sonnet-20240620
import openai
client = openai.AsyncOpenAI(
api_key="anything", # litellm proxy api key
base_url="http://0.0.0.0:4000" # litellm proxy base url
)
response = await client.chat.completions.create(
model="anthropic/claude-3-5-sonnet-20240620",
messages = [{"role": "user", "content": "What's the weather like in Boston today?"}]
tools = [
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
},
"required": ["location"],
},
"cache_control": {"type": "ephemeral"}
},
}
]
)
快取 - 延續多輪對話
在此範例中,我們示範如何在多輪對話中使用 Prompt Caching。
cache_control 參數放在 system 訊息上,以將其指定為靜態前綴的一部分。
對話歷史(先前的訊息)會包含在 messages 陣列中。最後一輪會標記 cache-control,以便在後續追問時繼續使用。倒數第二個使用者訊息會使用 cache_control 參數標記為可快取,讓此 checkpoint 可以讀取先前的快取。
- LiteLLM SDK
- LiteLLM Proxy
import litellm
response = await litellm.acompletion(
model="anthropic/claude-3-5-sonnet-20240620",
messages=[
# System Message
{
"role": "system",
"content": [
{
"type": "text",
"text": "Here is the full text of a complex legal agreement"
* 400,
"cache_control": {"type": "ephemeral"},
}
],
},
# marked for caching with the cache_control parameter, so that this checkpoint can read from the previous cache.
{
"role": "user",
"content": [
{
"type": "text",
"text": "What are the key terms and conditions in this agreement?",
"cache_control": {"type": "ephemeral"},
}
],
},
{
"role": "assistant",
"content": "Certainly! the key terms and conditions are the following: the contract is 1 year long for $10/mo",
},
# The final turn is marked with cache-control, for continuing in followups.
{
"role": "user",
"content": [
{
"type": "text",
"text": "What are the key terms and conditions in this agreement?",
"cache_control": {"type": "ephemeral"},
}
],
},
]
)
LiteLLM Proxy 與 OpenAI 相容
這是一個使用 OpenAI Python SDK 向 LiteLLM Proxy 發送請求的範例
假設您在 litellm proxy config.yaml 上有一個 model=anthropic/claude-3-5-sonnet-20240620
import openai
client = openai.AsyncOpenAI(
api_key="anything", # litellm proxy api key
base_url="http://0.0.0.0:4000" # litellm proxy base url
)
response = await client.chat.completions.create(
model="anthropic/claude-3-5-sonnet-20240620",
messages=[
# System Message
{
"role": "system",
"content": [
{
"type": "text",
"text": "Here is the full text of a complex legal agreement"
* 400,
"cache_control": {"type": "ephemeral"},
}
],
},
# marked for caching with the cache_control parameter, so that this checkpoint can read from the previous cache.
{
"role": "user",
"content": [
{
"type": "text",
"text": "What are the key terms and conditions in this agreement?",
"cache_control": {"type": "ephemeral"},
}
],
},
{
"role": "assistant",
"content": "Certainly! the key terms and conditions are the following: the contract is 1 year long for $10/mo",
},
# The final turn is marked with cache-control, for continuing in followups.
{
"role": "user",
"content": [
{
"type": "text",
"text": "What are the key terms and conditions in this agreement?",
"cache_control": {"type": "ephemeral"},
}
],
},
]
)
函式/工具呼叫
from litellm import completion
# set env
os.environ["ANTHROPIC_API_KEY"] = "your-api-key"
tools = [
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
},
"required": ["location"],
},
},
}
]
messages = [{"role": "user", "content": "What's the weather like in Boston today?"}]
response = completion(
model="anthropic/claude-3-opus-20240229",
messages=messages,
tools=tools,
tool_choice="auto",
)
# Add any assertions, here to check response args
print(response)
assert isinstance(response.choices[0].message.tool_calls[0].function.name, str)
assert isinstance(
response.choices[0].message.tool_calls[0].function.arguments, str
)
強制 Anthropic 工具使用
如果您希望 Claude 使用特定 tool 來回答使用者的問題
您可以像這樣在 tool_choice 欄位中指定該 tool:
response = completion(
model="anthropic/claude-3-opus-20240229",
messages=messages,
tools=tools,
tool_choice={"type": "tool", "name": "get_weather"},
)
停用工具呼叫
您可以將 tool_choice 設為 "none" 來停用 tool calling。
- SDK
- Proxy
from litellm import completion
response = completion(
model="anthropic/claude-3-opus-20240229",
messages=messages,
tools=tools,
tool_choice="none",
)
- 設定 config.yaml
model_list:
- model_name: anthropic-claude-model
litellm_params:
model: anthropic/claude-3-opus-20240229
api_key: os.environ/ANTHROPIC_API_KEY
- 啟動 proxy
litellm --config /path/to/config.yaml
- 測試它!
如果有 設定,請將 anything 替換為您的 LiteLLM Proxy 虛擬金鑰。
curl http://0.0.0.0:4000/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer anything" \
-d '{
"model": "anthropic-claude-model",
"messages": [{"role": "user", "content": "Who won the World Cup in 2022?"}],
"tools": [{"type": "mcp", "server_label": "deepwiki", "server_url": "https://mcp.deepwiki.com/mcp", "require_approval": "never"}],
"tool_choice": "none"
}'
MCP 工具呼叫
以下是如何在 Anthropic 中使用 MCP 工具呼叫:
- LiteLLM SDK
- LiteLLM Proxy
LiteLLM 支援以 OpenAI Responses API 格式,與 Anthropic 進行 MCP 工具呼叫。
- OpenAI 格式
- Anthropic 格式
import os
from litellm import completion
os.environ["ANTHROPIC_API_KEY"] = "sk-ant-..."
tools=[
{
"type": "mcp",
"server_label": "deepwiki",
"server_url": "https://mcp.deepwiki.com/mcp",
"require_approval": "never",
},
]
response = completion(
model="anthropic/claude-sonnet-4-20250514",
messages=[{"role": "user", "content": "Who won the World Cup in 2022?"}],
tools=tools
)
import os
from litellm import completion
os.environ["ANTHROPIC_API_KEY"] = "sk-ant-..."
tools = [
{
"type": "url",
"url": "https://mcp.deepwiki.com/mcp",
"name": "deepwiki-mcp",
}
]
response = completion(
model="anthropic/claude-sonnet-4-20250514",
messages=[{"role": "user", "content": "Who won the World Cup in 2022?"}],
tools=tools
)
print(response)
- 設定 config.yaml
model_list:
- model_name: claude-4-sonnet
litellm_params:
model: anthropic/claude-sonnet-4-20250514
api_key: os.environ/ANTHROPIC_API_KEY
- 啟動 proxy
litellm --config /path/to/config.yaml
- 測試它!
- OpenAI 格式
- Anthropic 格式
curl http://0.0.0.0:4000/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $LITELLM_KEY" \
-d '{
"model": "claude-4-sonnet",
"messages": [{"role": "user", "content": "Who won the World Cup in 2022?"}],
"tools": [{"type": "mcp", "server_label": "deepwiki", "server_url": "https://mcp.deepwiki.com/mcp", "require_approval": "never"}]
}'
curl http://0.0.0.0:4000/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $LITELLM_KEY" \
-d '{
"model": "claude-4-sonnet",
"messages": [{"role": "user", "content": "Who won the World Cup in 2022?"}],
"tools": [
{
"type": "url",
"url": "https://mcp.deepwiki.com/mcp",
"name": "deepwiki-mcp",
}
]
}'
平行函式呼叫
以下是如何將函式呼叫的結果傳回給 anthropic 模型:
from litellm import completion
import os
os.environ["ANTHROPIC_API_KEY"] = "sk-ant.."
litellm.set_verbose = True
### 1ST FUNCTION CALL ###
tools = [
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
},
"required": ["location"],
},
},
}
]
messages = [
{
"role": "user",
"content": "What's the weather like in Boston today in Fahrenheit?",
}
]
try:
# test without max tokens
response = completion(
model="anthropic/claude-3-opus-20240229",
messages=messages,
tools=tools,
tool_choice="auto",
)
# Add any assertions, here to check response args
print(response)
assert isinstance(response.choices[0].message.tool_calls[0].function.name, str)
assert isinstance(
response.choices[0].message.tool_calls[0].function.arguments, str
)
messages.append(
response.choices[0].message.model_dump()
) # Add assistant tool invokes
tool_result = (
'{"location": "Boston", "temperature": "72", "unit": "fahrenheit"}'
)
# Add user submitted tool results in the OpenAI format
messages.append(
{
"tool_call_id": response.choices[0].message.tool_calls[0].id,
"role": "tool",
"name": response.choices[0].message.tool_calls[0].function.name,
"content": tool_result,
}
)
### 2ND FUNCTION CALL ###
# In the second response, Claude should deduce answer from tool results
second_response = completion(
model="anthropic/claude-3-opus-20240229",
messages=messages,
tools=tools,
tool_choice="auto",
)
print(second_response)
except Exception as e:
print(f"An error occurred - {str(e)}")
感謝 @Shekhar Patnaik 提出這個需求!
上下文管理(Beta)
Anthropic 的 內容編輯 API 可讓您自動清除較舊的工具結果或思考區塊。LiteLLM 現在在您呼叫 Anthropic 模型時會轉送原生 context_management 負載,並自動附加所需的 context-management-2025-06-27 beta 標頭。
from litellm import completion
response = completion(
model="anthropic/claude-sonnet-4-20250514",
messages=[{"role": "user", "content": "Summarize the latest tool results"}],
context_management={
"edits": [
{
"type": "clear_tool_uses_20250919",
"trigger": {"type": "input_tokens", "value": 30000},
"keep": {"type": "tool_uses", "value": 3},
"clear_at_least": {"type": "input_tokens", "value": 5000},
"exclude_tools": ["web_search"],
}
]
},
)
Anthropic 託管工具(電腦、文字編輯器、網頁搜尋、記憶)
- 電腦
- 文字編輯器
- 網頁搜尋
- Memory
from litellm import completion
tools = [
{
"type": "computer_20241022",
"function": {
"name": "computer",
"parameters": {
"display_height_px": 100,
"display_width_px": 100,
"display_number": 1,
},
},
}
]
model = "claude-3-5-sonnet-20241022"
messages = [{"role": "user", "content": "Save a picture of a cat to my desktop."}]
resp = completion(
model=model,
messages=messages,
tools=tools,
# headers={"anthropic-beta": "computer-use-2024-10-22"},
)
print(resp)
- SDK
- PROXY
from litellm import completion
tools = [{
"type": "text_editor_20250124",
"name": "str_replace_editor"
}]
model = "claude-3-5-sonnet-20241022"
messages = [{"role": "user", "content": "There's a syntax error in my primes.py file. Can you help me fix it?"}]
resp = completion(
model=model,
messages=messages,
tools=tools,
)
print(resp)
- 設定 config.yaml
- model_name: claude-3-5-sonnet-latest
litellm_params:
model: anthropic/claude-3-5-sonnet-latest
api_key: os.environ/ANTHROPIC_API_KEY
- 啟動 proxy
litellm --config /path/to/config.yaml
- 測試它!
curl http://0.0.0.0:4000/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $LITELLM_KEY" \
-d '{
"model": "claude-3-5-sonnet-latest",
"messages": [{"role": "user", "content": "There's a syntax error in my primes.py file. Can you help me fix it?"}],
"tools": [{"type": "text_editor_20250124", "name": "str_replace_editor"}]
}'
自 v1.70.1+ 起提供
LiteLLM 會將 OpenAI 的 search_context_size 參數對應到 Anthropic 的 max_uses 參數。
| OpenAI | Anthropic |
|---|---|
| 低 | 1 |
| 中 | 5 |
| 高 | 10 |
- SDK
- PROXY
- OpenAI 格式
- Anthropic 格式
from litellm import completion
model = "claude-3-5-sonnet-20241022"
messages = [{"role": "user", "content": "What's the weather like today?"}]
resp = completion(
model=model,
messages=messages,
web_search_options={
"search_context_size": "medium",
"user_location": {
"type": "approximate",
"approximate": {
"city": "San Francisco",
},
}
}
)
print(resp)
from litellm import completion
tools = [{
"type": "web_search_20250305",
"name": "web_search",
"max_uses": 5
}]
model = "claude-3-5-sonnet-20241022"
messages = [{"role": "user", "content": "There's a syntax error in my primes.py file. Can you help me fix it?"}]
resp = completion(
model=model,
messages=messages,
tools=tools,
)
print(resp)
- 設定 config.yaml
- model_name: claude-3-5-sonnet-latest
litellm_params:
model: anthropic/claude-3-5-sonnet-latest
api_key: os.environ/ANTHROPIC_API_KEY
- 啟動 proxy
litellm --config /path/to/config.yaml
- 測試它!
- OpenAI 格式
- Anthropic 格式
curl http://0.0.0.0:4000/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $LITELLM_KEY" \
-d '{
"model": "claude-3-5-sonnet-latest",
"messages": [{"role": "user", "content": "What's the weather like today?"}],
"web_search_options": {
"search_context_size": "medium",
"user_location": {
"type": "approximate",
"approximate": {
"city": "San Francisco",
},
}
}
}'
curl http://0.0.0.0:4000/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $LITELLM_KEY" \
-d '{
"model": "claude-3-5-sonnet-latest",
"messages": [{"role": "user", "content": "What's the weather like today?"}],
"tools": [{
"type": "web_search_20250305",
"name": "web_search",
"max_uses": 5
}]
}'
Anthropic Memory 工具目前處於 beta 版。
- SDK
- Proxy
from litellm import completion
tools = [{
"type": "memory_20250818",
"name": "memory"
}]
model = "claude-sonnet-4-5-20250929"
messages = [{"role": "user", "content": "Please remember that my favorite color is blue."}]
response = completion(
model=model,
messages=messages,
tools=tools,
)
print(response)
- 設定 config.yaml
model_list:
- model_name: claude-memory-model
litellm_params:
model: anthropic/claude-sonnet-4-5-20250929
api_key: os.environ/ANTHROPIC_API_KEY
- 啟動 proxy
litellm --config /path/to/config.yaml
- 測試一下!
curl http://0.0.0.0:4000/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $LITELLM_KEY" \
-d '{
"model": "claude-memory-model",
"messages": [{"role": "user", "content": "Please remember that my favorite color is blue."}],
"tools": [{"type": "memory_20250818", "name": "memory"}]
}'
使用方式 - 視覺
from litellm import completion
# set env
os.environ["ANTHROPIC_API_KEY"] = "your-api-key"
def encode_image(image_path):
import base64
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode("utf-8")
image_path = "../proxy/cached_logo.jpg"
# Getting the base64 string
base64_image = encode_image(image_path)
resp = litellm.completion(
model="anthropic/claude-3-opus-20240229",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "Whats in this image?"},
{
"type": "image_url",
"image_url": {
"url": "data:image/jpeg;base64," + base64_image
},
},
],
}
],
)
print(f"\nResponse: {resp}")
使用方式 - 思考 / reasoning_content
LiteLLM 會將 OpenAI 的 reasoning_effort 轉譯為 Anthropic 的 thinking 參數。程式碼
| reasoning_effort | thinking |
|---|---|
| "low" | "budget_tokens": 1024 |
| "medium" | "budget_tokens": 2048 |
| "high" | "budget_tokens": 4096 |
reasoning_effort 會對應到 Anthropic 的 [adaptive thinking](https: //docs.claude.com/en/docs/build-with-claude/extended-thinking/adaptive-thinking),以及 Claude 4.6 和 4.7 模型上的 output_config.effort 參數(包含 claude-opus-4-6、claude-opus-4-7、claude-sonnet-4-6 等),不是 budget_tokens。特別是,LiteLLM 會在 OpenAI 相容的 /chat/completions 路由上,將下列內容注入底層 Anthropic 請求:
{
"thinking": {"type": "adaptive"},
"output_config": {"effort": "<low|medium|high|xhigh|max>"}
}
這表示,對於這些模型,reasoning_effort 的 "none" 以外的任何值,都會自動啟用 thinking,即使 OpenAI 相容的請求本文沒有獨立的 thinking 欄位。這是為了符合 Anthropic 自己建議的用法:在 4.6 模型上,budget_tokens 已被棄用;在 Opus 4.7 上則完全被拒絕,因為只有 adaptive 是受支援的 thinking 模式。
您可以透過完全省略 reasoning_effort,或將其設定為 "none" 來停用 thinking。在這種情況下,LiteLLM 不會送出 thinking 欄位。如果您希望在先前的模型上以固定預算明確控制 thinking,仍可直接傳遞原生的 thinking 參數:
from litellm import completion
# Disable thinking on Claude 4.6/4.7
resp = completion(
model="anthropic/claude-opus-4-7",
messages=[{"role": "user", "content": "What is the capital of France?"}],
reasoning_effort="none", # no thinking field sent
)
# Explicit budget (pre-4.6 models; deprecated on 4.6, rejected on Opus 4.7)
resp = completion(
model="anthropic/claude-sonnet-4-5-20250929",
messages=[{"role": "user", "content": "What is the capital of France?"}],
thinking={"type": "enabled", "budget_tokens": 1024},
)
Anthropic /v1/messages 直通路由不受此 reasoning_effort 對應影響。thinking 會原樣傳遞。
- SDK
- PROXY
from litellm import completion
resp = completion(
model="anthropic/claude-3-7-sonnet-20250219",
messages=[{"role": "user", "content": "What is the capital of France?"}],
reasoning_effort="low",
)
- 設定 config.yaml
- model_name: claude-3-7-sonnet-20250219
litellm_params:
model: anthropic/claude-3-7-sonnet-20250219
api_key: os.environ/ANTHROPIC_API_KEY
- 啟動 proxy
litellm --config /path/to/config.yaml
- 測試一下!
curl http://0.0.0.0:4000/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <YOUR-LITELLM-KEY>" \
-d '{
"model": "claude-3-7-sonnet-20250219",
"messages": [{"role": "user", "content": "What is the capital of France?"}],
"reasoning_effort": "low"
}'
預期回應
ModelResponse(
id='chatcmpl-c542d76d-f675-4e87-8e5f-05855f5d0f5e',
created=1740470510,
model='claude-3-7-sonnet-20250219',
object='chat.completion',
system_fingerprint=None,
choices=[
Choices(
finish_reason='stop',
index=0,
message=Message(
content="The capital of France is Paris.",
role='assistant',
tool_calls=None,
function_call=None,
provider_specific_fields={
'citations': None,
'thinking_blocks': [
{
'type': 'thinking',
'thinking': 'The capital of France is Paris. This is a very straightforward factual question.',
'signature': 'EuYBCkQYAiJAy6...'
}
]
}
),
thinking_blocks=[
{
'type': 'thinking',
'thinking': 'The capital of France is Paris. This is a very straightforward factual question.',
'signature': 'EuYBCkQYAiJAy6AGB...'
}
],
reasoning_content='The capital of France is Paris. This is a very straightforward factual question.'
)
],
usage=Usage(
completion_tokens=68,
prompt_tokens=42,
total_tokens=110,
completion_tokens_details=None,
prompt_tokens_details=PromptTokensDetailsWrapper(
audio_tokens=None,
cached_tokens=0,
text_tokens=None,
image_tokens=None
),
cache_creation_input_tokens=0,
cache_read_input_tokens=0
)
)
將 thinking 傳遞給 Anthropic 模型
您也可以將 thinking 參數傳給 Anthropic 模型。
您也可以將 thinking 參數傳給 Anthropic 模型。
- SDK
- PROXY
response = litellm.completion(
model="anthropic/claude-3-7-sonnet-20250219",
messages=[{"role": "user", "content": "What is the capital of France?"}],
thinking={"type": "enabled", "budget_tokens": 1024},
)
curl http://0.0.0.0:4000/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $LITELLM_KEY" \
-d '{
"model": "anthropic/claude-3-7-sonnet-20250219",
"messages": [{"role": "user", "content": "What is the capital of France?"}],
"thinking": {"type": "enabled", "budget_tokens": 1024}
}'
自適應思考(Claude Opus 4.6)
- SDK
- PROXY
response = litellm.completion(
model="anthropic/claude-opus-4-6",
messages=[{"role": "user", "content": "What is the optimal strategy for solving this problem?"}],
thinking={"type": "adaptive"},
)
curl http://0.0.0.0:4000/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $LITELLM_KEY" \
-d '{
"model": "anthropic/claude-opus-4-6",
"messages": [{"role": "user", "content": "What is the optimal strategy for solving this problem?"}],
"thinking": {"type": "adaptive"}
}'
啟用具有預算的思考
- SDK
- PROXY
response = litellm.completion(
model="anthropic/claude-opus-4-6",
messages=[{"role": "user", "content": "What is the capital of France?"}],
thinking={"type": "enabled", "budget_tokens": 5000},
)
curl http://0.0.0.0:4000/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $LITELLM_KEY" \
-d '{
"model": "anthropic/claude-opus-4-6",
"messages": [{"role": "user", "content": "What is the capital of France?"}],
"thinking": {"type": "enabled", "budget_tokens": 5000}
}'
將額外標頭傳遞給 Anthropic API
將 extra_headers: dict 傳遞給 litellm.completion
from litellm import completion
messages = [{"role": "user", "content": "What is Anthropic?"}]
response = completion(
model="claude-3-5-sonnet-20240620",
messages=messages,
extra_headers={"anthropic-beta": "max-tokens-3-5-sonnet-2024-07-15"}
)
使用方式 - 「Assistant 預填」
您可以透過在 messages 陣列中最後一個項目加入 assistant 角色訊息,來「替 Claude 輸入台詞」。
[!IMPORTANT] 傳回的完成內容將 不會 包含您的「預填」文字,因為它本身就是提示的一部分。請務必在 Claude 的完成內容前加上您的預填文字。
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": "How do you say 'Hello' in German? Return your answer as a JSON object, like this:\n\n{ \"Hello\": \"Hallo\" }"},
{"role": "assistant", "content": "{"},
]
response = completion(model="claude-2.1", messages=messages)
print(response)
傳送給 Claude 的範例提示詞
Human: How do you say 'Hello' in German? Return your answer as a JSON object, like this:
{ "Hello": "Hallo" }
Assistant: {
使用方式 - 「System」訊息
如果您使用的是 Anthropic 的 Claude 2.1,system 角色訊息會自動為您正確格式化。
import os
from litellm import completion
# set env - [OPTIONAL] replace with your anthropic key
os.environ["ANTHROPIC_API_KEY"] = "your-api-key"
messages = [
{"role": "system", "content": "You are a snarky assistant."},
{"role": "user", "content": "How do I boil water?"},
]
response = completion(model="claude-2.1", messages=messages)
傳送給 Claude 的範例提示詞
You are a snarky assistant.
Human: How do I boil water?
Assistant:
使用方式 - PDF
使用 file 內容類型與 file_data 欄位,將 base64 編碼的 PDF 檔案傳遞給 Anthropic 模型。
- SDK
- PROXY
使用 base64
from litellm import completion, supports_pdf_input
import base64
import requests
# URL of the file
url = "https://storage.googleapis.com/cloud-samples-data/generative-ai/pdf/2403.05530.pdf"
# Download the file
response = requests.get(url)
file_data = response.content
encoded_file = base64.b64encode(file_data).decode("utf-8")
## check if model supports pdf input - (2024/11/11) only claude-3-5-haiku-20241022 supports it
supports_pdf_input("anthropic/claude-3-5-haiku-20241022") # True
response = completion(
model="anthropic/claude-3-5-haiku-20241022",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "You are a very professional document summarization specialist. Please summarize the given document."},
{
"type": "file",
"file": {
"file_data": f"data:application/pdf;base64,{encoded_file}", # 👈 PDF
}
},
],
}
],
max_tokens=300,
)
print(response.choices[0])
- 將模型加入設定檔
- model_name: claude-3-5-haiku-20241022
litellm_params:
model: anthropic/claude-3-5-haiku-20241022
api_key: os.environ/ANTHROPIC_API_KEY
- 啟動 Proxy
litellm --config /path/to/config.yaml
- 測試它!
curl http://0.0.0.0:4000/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <YOUR-LITELLM-KEY>" \
-d '{
"model": "claude-3-5-haiku-20241022",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "You are a very professional document summarization specialist. Please summarize the given document"
},
{
"type": "file",
"file": {
"file_data": f"data:application/pdf;base64,{encoded_file}", # 👈 PDF
}
}
}
]
}
],
"max_tokens": 300
}'
[BETA] 引用 API
將 citations: {"enabled": true} 傳遞給 Anthropic,以取得您文件回應中的引文。
注意:此介面目前為 BETA。如果您對引文應如何回傳有任何回饋,請 在此告訴我們
- SDK
- PROXY
from litellm import completion
resp = completion(
model="claude-3-5-sonnet-20241022",
messages=[
{
"role": "user",
"content": [
{
"type": "document",
"source": {
"type": "text",
"media_type": "text/plain",
"data": "The grass is green. The sky is blue.",
},
"title": "My Document",
"context": "This is a trustworthy document.",
"citations": {"enabled": True},
},
{
"type": "text",
"text": "What color is the grass and sky?",
},
],
}
],
)
citations = resp.choices[0].message.provider_specific_fields["citations"]
assert citations is not None
- 設定 config.yaml
model_list:
- model_name: anthropic-claude
litellm_params:
model: anthropic/claude-3-5-sonnet-20241022
api_key: os.environ/ANTHROPIC_API_KEY
- 啟動 proxy
litellm --config /path/to/config.yaml
# RUNNING on http://0.0.0.0:4000
- 測試它!
curl -L -X POST 'http://0.0.0.0:4000/v1/chat/completions' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer sk-1234' \
-d '{
"model": "anthropic-claude",
"messages": [
{
"role": "user",
"content": [
{
"type": "document",
"source": {
"type": "text",
"media_type": "text/plain",
"data": "The grass is green. The sky is blue.",
},
"title": "My Document",
"context": "This is a trustworthy document.",
"citations": {"enabled": True},
},
{
"type": "text",
"text": "What color is the grass and sky?",
},
],
}
]
}'
檔案 API
上傳檔案一次,並在多個請求中以 file_id 參照它們——無須每次重新上傳內容。
從 Anthropic 取得的 file_id 僅適用於 Anthropic Claude 模型。您無法將其用於其他提供者(OpenAI、Bedrock 等)。
- 檔案大小上限: 500 MB | 總儲存空間: 每個 org 100 GB
- 價格: File API 操作免費。Messages 請求中使用的檔案內容會依 input tokens 計價。
依檔案類型支援的模型:
- 圖片: 所有 Claude 3+ 模型
- PDF: 所有 Claude 3.5+ 模型
- 其他檔案類型(用於程式碼執行):Claude 3.5 Haiku + 所有 Claude 3.7+ 模型
快速開始
import litellm
import os
os.environ["ANTHROPIC_API_KEY"] = "sk-ant-..."
# 1. Upload a file once
file = litellm.create_file(
file=open("document.pdf", "rb"),
purpose="messages",
custom_llm_provider="anthropic",
)
# 2. Use file_id in messages (no re-upload needed)
response = litellm.completion(
model="anthropic/claude-sonnet-4-5-20250929",
messages=[{
"role": "user",
"content": [
{"type": "text", "text": "Summarize this document"},
{"type": "file", "file": {"file_id": file.id, "format": "application/pdf"}}
]
}]
)
檔案操作
| 操作 | 函式 |
|---|---|
| 上傳 | litellm.create_file(file, purpose="messages", custom_llm_provider="anthropic") |
| 列出 | litellm.file_list(custom_llm_provider="anthropic") |
| 取回 | litellm.file_retrieve(file_id, custom_llm_provider="anthropic") |
| 刪除 | litellm.file_delete(file_id, custom_llm_provider="anthropic") |
| 下載 | litellm.file_content(file_id, custom_llm_provider="anthropic") |
下載僅適用於由 程式碼執行工具 建立的檔案,不適用於已上傳的檔案。
支援的格式
| 檔案類型 | 格式值 |
|---|---|
application/pdf | |
| 純文字 | text/plain |
| JPEG | image/jpeg |
| PNG | image/png |
| GIF | image/gif |
| WebP | image/webp |
使用圖片
# Upload image
image = litellm.create_file(
file=open("photo.jpg", "rb"),
purpose="messages",
custom_llm_provider="anthropic",
)
# Use in message
response = litellm.completion(
model="anthropic/claude-sonnet-4-5-20250929",
messages=[{
"role": "user",
"content": [
{"type": "text", "text": "What's in this image?"},
{"type": "file", "file": {"file_id": image.id, "format": "image/jpeg"}}
]
}]
)
使用方式 - 將 'user_id' 傳遞給 Anthropic
LiteLLM 會將 OpenAI 的 user 參數轉換為 Anthropic 的 metadata[user_id] 參數。
- SDK
- PROXY
response = completion(
model="claude-3-5-sonnet-20240620",
messages=messages,
user="user_123",
)
- 設定 config.yaml
model_list:
- model_name: claude-3-5-sonnet-20240620
litellm_params:
model: anthropic/claude-3-5-sonnet-20240620
api_key: os.environ/ANTHROPIC_API_KEY
- 啟動 Proxy
litellm --config /path/to/config.yaml
- 測試看看!
curl http://0.0.0.0:4000/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <YOUR-LITELLM-KEY>" \
-d '{
"model": "claude-3-5-sonnet-20240620",
"messages": [{"role": "user", "content": "What is Anthropic?"}],
"user": "user_123"
}'
使用方式 - Agent Skills
LiteLLM 支援透過 API 使用 Agent Skills
- SDK
- PROXY
response = completion(
model="claude-sonnet-4-5-20250929",
messages=messages,
tools= [
{
"type": "code_execution_20250825",
"name": "code_execution"
}
],
container= {
"skills": [
{
"type": "anthropic",
"skill_id": "pptx",
"version": "latest"
}
]
}
)
- 設定 config.yaml
model_list:
- model_name: claude-sonnet-4-5-20250929
litellm_params:
model: anthropic/claude-sonnet-4-5-20250929
api_key: os.environ/ANTHROPIC_API_KEY
- 啟動 Proxy
litellm --config /path/to/config.yaml
- 測試看看!
curl --location 'http://localhost:4000/chat/completions' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <YOUR-LITELLM-KEY>' \
--data '{
"model": "claude-sonnet-4-5-20250929",
"messages": [
{
"role": "user",
"content": "Hi"
}
],
"tools": [
{
"type": "code_execution_20250825",
"name": "code_execution"
}
],
"container": {
"skills": [
{
"type": "anthropic",
"skill_id": "pptx",
"version": "latest"
}
]
}
}'
容器及其「id」將會出現在串流/非串流回應中的「provider_specific_fields」內