Files API 讓您可以上傳和管理檔案,以便與 Claude API 搭配使用,而無需在每次請求時重新上傳內容。這在使用程式碼執行工具時特別有用,可用於提供輸入(例如資料集和文件),然後下載輸出(例如圖表)。除了本指南之外,您還可以直接探索 API 參考文件。
在 Messages 請求中參照 file_id 的功能,在所有支援該檔案類型的模型上均可使用。圖片在所有目前的 Claude 模型上均受支援。關於 PDF 和程式碼執行工具支援的其他檔案類型,請參閱連結頁面以了解模型支援情況。
Files API 提供「建立一次、多次使用」的檔案處理方式:
file_idfile_id 參照檔案,而無需重新上傳內容上傳檔案以便在未來的 API 呼叫中參照:
uploaded = client.beta.files.upload(
file=("document.pdf", open("/path/to/document.pdf", "rb"), "application/pdf"),
)
file_id = uploaded.id
print(file_id)上傳檔案的回應包含:
{
"id": "file_011CNha8iCJcU1wXNR6q4V8w",
"type": "file",
"filename": "document.pdf",
"mime_type": "application/pdf",
"size_bytes": 1024000,
"created_at": "2025-01-01T00:00:00Z",
"downloadable": false
}對於您上傳的檔案,downloadable 為 false。只有由技能或程式碼執行工具建立的檔案才能下載。請參閱下載檔案。
上傳後,透過將上傳回應中的 id 作為 file_id 傳遞來參照檔案:
response = client.beta.messages.create(
model="claude-opus-5",
max_tokens=1024,
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "Please summarize this document for me."},
{
"type": "document",
"source": {
"type": "file",
"file_id": file_id,
},
},
],
}
],
betas=["files-api-2025-04-14"],
)
print(response)Files API 支援對應於不同內容區塊類型的不同檔案類型:
| 檔案類型 | MIME 類型 | 內容區塊類型 | 使用案例 |
|---|---|---|---|
application/pdf | document | 文字分析、文件處理 | |
| 純文字 | text/plain | document | 文字分析、處理 |
| 圖片 | image/jpeg、image/png、image/gif、image/webp | image | 圖片分析、視覺任務 |
| 資料集、其他 | 各異 | container_upload | 分析資料、建立視覺化圖表 |
對於 PDF 和文字檔案,請使用 document 內容區塊:
{
"type": "document",
"source": {
"type": "file",
"file_id": "file_011CNha8iCJcU1wXNR6q4V8w"
},
"title": "Document Title", // Optional
"context": "Context about the document", // Optional
"citations": { "enabled": true } // Optional, enables citations
}對於圖片,請使用 image 內容區塊:
{
"type": "image",
"source": {
"type": "file",
"file_id": "file_011CPMxVD3fHLUhvTqtsQA5w"
}
}若要將檔案傳送至程式碼執行工具,請使用 container_upload 內容區塊:
{
"type": "container_upload",
"file_id": "file_011CNha8iCJcU1wXNR6q4V8w"
}對於 document 區塊不支援的檔案類型(例如 .docx 和 .xlsx),請將檔案轉換為純文字,並直接在訊息中包含內容。已經是純文字的檔案(例如 .csv 和 .md 檔案)可以用這種方式讀取,或透過 Files API 以明確的 text/plain 內容類型上傳。若要分析資料集而非將其作為文字讀取,請使用 container_upload 區塊為程式碼執行工具上傳它們。
以下範例讀取文字檔案並將其內容作為純文字傳送:
client = anthropic.Anthropic()
# 讀取文字檔案
with open("document.txt") as f:
text_content = f.read()
response = client.messages.create(
model="claude-opus-5",
max_tokens=1024,
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": f"Here's the document content:\n\n{text_content}\n\nPlease summarize this document.",
}
],
}
],
)
for block in response.content:
if block.type == "text":
print(block.text)擷取您已上傳檔案的清單。此端點採用分頁:每個請求最多回傳 limit 個檔案(預設為 20 個),而 before_id 和 after_id 參數可擷取相鄰的頁面。請參閱 List Files API 參考文件。SDK 會回傳第一頁並提供自動分頁輔助工具。CLI 範例使用 --max-items 限制總數:
client = anthropic.Anthropic()
files = client.beta.files.list()
print(files)擷取特定檔案的相關資訊:
file = client.beta.files.retrieve_metadata(file_id)
print(file)從您的工作區移除檔案:
client.beta.files.delete(file_id)下載由技能或程式碼執行工具建立的檔案。您上傳的檔案無法下載。所產生檔案的 file_id 會出現在建立該檔案的 Messages 回應的 bash_code_execution_tool_result 內容區塊中:
file_content = client.beta.files.download(file_id)
file_content.write_to_file("downloaded_file.txt")DELETE /v1/files/{file_id} 端點將其刪除使用 Files API 時的常見錯誤包括:
file_id 不存在,或您沒有存取權限"downloadable": false,無法下載。只有由技能或程式碼執行工具建立的檔案才能下載/v1/messages 請求中使用 500 MB 的純文字檔案)<、>、:、"、|、?、*、\、/,或 Unicode 字元 0-31){
"type": "error",
"error": {
"type": "not_found_error",
"message": "File `file_011CNha8iCJcU1wXNR6q4V8w` not found."
},
"request_id": "req_011CQFYcrRp7mCHLDsAYT8Qt"
}Files API 操作是免費的:
在 Messages 請求中使用的檔案內容會以輸入 token 計價。
在 beta 期間:
使用 Claude 處理 PDF。從您的文件中擷取文字、分析圖表,並理解視覺內容。
在沙箱容器中執行 Python 和 bash 程式碼,以分析資料、產生檔案並反覆改進解決方案。
處理和分析視覺輸入,並從圖片產生文字和程式碼。
| Supported platforms |
|
|---|
Was this page helpful?