跳至主要內容

Anthropic

LiteLLM 支援所有 anthropic 模型。

  • claude-opus-4-6 (claude-opus-4-6-20260205)
  • claude-sonnet-4-6
  • claude-sonnet-4-5-20250929
  • claude-opus-4-5-20251101
  • claude-opus-4-1-20250805
  • claude-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-2
  • claude-2.1
  • claude-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",
資訊

注意:

  • 當未傳入 max_tokens 時,Anthropic API 會使請求失敗。因此,當未傳入 max_tokens 時,litellm 會傳入 max_tokens=4096
  • response_format 已完整支援 Claude Sonnet 4.5 與 Opus 4.1 模型(請參閱 結構化輸出 章節)
  • reasoning_effort 會自動對應為 Claude 4.6 與 Opus 4.5 模型的 output_config={"effort": ...}(請參閱 Effort 參數

結構化輸出

LiteLLM 支援 Anthropic 的 結構化輸出功能,適用於 Claude Sonnet 4.5 與 Opus 4.1 模型。當您對這些模型使用 response_format 時,LiteLLM 會自動:

  • 加入必要的 structured-outputs-2025-11-13 beta 標頭
  • 將 OpenAI 的 response_format 轉換為 Anthropic 的 output_format 格式

支援的模型

  • sonnet-4-5sonnet-4.5(所有 Sonnet 4.5 變體)
  • opus-4-1opus-4.1(所有 Opus 4.1 變體)
    • opus-4-5opus-4.5(所有 Opus 4.5 變體)

使用範例

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"}
資訊

使用支援的模型進行結構化輸出時,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
Azure Foundry 支援

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.comhttps://my-proxy.com/v1/messages
  • Base URL https://my-proxy.com/apihttps://my-proxy.com/api/v1/messages

使用 LITELLM_ANTHROPIC_DISABLE_URL_SUFFIX=true

  • Base URL https://my-proxy.com/custom/pathhttps://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

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

3. 進行測試

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"
}
]
}
'

支援的模型

Model Name 👉 人類可讀名稱。
Function Call 👉 在 LiteLLM 中如何呼叫該模型。

模型名稱函式呼叫
claude-opus-4-6completion('claude-opus-4-6-20260205', messages)
claude-sonnet-4-5completion('claude-sonnet-4-5-20250929', messages)
claude-opus-4-5completion('claude-opus-4-5-20251101', messages)
claude-opus-4-1completion('claude-opus-4-1-20250805', messages)
claude-opus-4completion('claude-opus-4-20250514', messages)
claude-sonnet-4completion('claude-sonnet-4-20250514', messages)
claude-3.7completion('claude-3-7-sonnet-20250219', messages)
claude-3-5-sonnetcompletion('claude-3-5-sonnet-20240620', messages)
claude-3-haikucompletion('claude-3-haiku-20240307', messages)
claude-3-opuscompletion('claude-3-opus-20240229', messages)
claude-3-5-sonnet-20240620completion('claude-3-5-sonnet-20240620', messages)
claude-3-sonnetcompletion('claude-3-sonnet-20240229', messages)
claude-2.1completion('claude-2.1', messages)
claude-2completion('claude-2', messages)
claude-instant-1.2completion('claude-instant-1.2', messages)
claude-instant-1completion('claude-instant-1', messages)

快取提示詞

使用 Anthropic Prompt Caching

相關的 Anthropic API 文件

備註

以下是 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 用法,將法律協議的完整文字快取為前綴,同時讓使用者指示保持不快取。

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?",
},
]
)

快取 - 工具定義

在此範例中,我們示範快取工具定義。

cache_control 參數放在最後一個 tool 上

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"}
},
}
]
)

快取 - 延續多輪對話

在此範例中,我們示範如何在多輪對話中使用 Prompt Caching。

cache_control 參數放在 system 訊息上,以將其指定為靜態前綴的一部分。

對話歷史(先前的訊息)會包含在 messages 陣列中。最後一輪會標記 cache-control,以便在後續追問時繼續使用。倒數第二個使用者訊息會使用 cache_control 參數標記為可快取,讓此 checkpoint 可以讀取先前的快取。

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"},
}
],
},
]
)

函式/工具呼叫

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。

from litellm import completion

response = completion(
model="anthropic/claude-3-opus-20240229",
messages=messages,
tools=tools,
tool_choice="none",
)

MCP 工具呼叫

以下是如何在 Anthropic 中使用 MCP 工具呼叫:

LiteLLM 支援以 OpenAI Responses API 格式,與 Anthropic 進行 MCP 工具呼叫。

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
)

平行函式呼叫

以下是如何將函式呼叫的結果傳回給 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 託管工具(電腦、文字編輯器、網頁搜尋、記憶)

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)

使用方式 - 視覺

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_effortthinking
"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-6claude-opus-4-7claude-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 會原樣傳遞。

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",
)

預期回應

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 模型。

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},
)

自適應思考(Claude Opus 4.6)

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"},
)

啟用具有預算的思考

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},
)

將額外標頭傳遞給 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 模型。

使用 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])

[BETA] 引用 API

citations: {"enabled": true} 傳遞給 Anthropic,以取得您文件回應中的引文。

注意:此介面目前為 BETA。如果您對引文應如何回傳有任何回饋,請 在此告訴我們

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

檔案 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")
備註

下載僅適用於由 程式碼執行工具 建立的檔案,不適用於已上傳的檔案。

支援的格式

檔案類型格式值
PDFapplication/pdf
純文字text/plain
JPEGimage/jpeg
PNGimage/png
GIFimage/gif
WebPimage/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] 參數。

response = completion(
model="claude-3-5-sonnet-20240620",
messages=messages,
user="user_123",
)

使用方式 - Agent Skills

LiteLLM 支援透過 API 使用 Agent Skills

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"
}
]
}
)

容器及其「id」將會出現在串流/非串流回應中的「provider_specific_fields」內