Loading...
    • 開發者指南
    • API 參考
    • MCP
    • 資源
    • 發行說明
    Search...
    ⌘K
    第一步
    Claude 簡介快速開始
    模型與定價
    模型概覽選擇模型Claude 4.5 的新功能遷移至 Claude 4.5模型棄用定價
    使用 Claude 構建
    功能概覽使用 Messages API上下文窗口提示詞最佳實踐
    功能
    提示詞快取上下文編輯擴展思考努力串流消息批次處理引用多語言支援Token 計數嵌入視覺PDF 支援Files API搜尋結果結構化輸出Google Sheets 附加元件
    工具
    概覽如何實現工具使用Token 高效工具使用細粒度工具串流Bash 工具程式碼執行工具程式化工具呼叫電腦使用工具文字編輯器工具網頁擷取工具網頁搜尋工具記憶體工具工具搜尋工具
    Agent 技能
    概覽快速開始最佳實踐在 API 中使用技能
    Agent SDK
    概覽TypeScript SDKPython SDK遷移指南
    指南
    串流輸入處理權限工作階段管理SDK 中的結構化輸出託管 Agent SDK修改系統提示詞SDK 中的 MCP自訂工具SDK 中的子 AgentSDK 中的斜線命令SDK 中的 Agent 技能追蹤成本和使用量待辦事項清單SDK 中的外掛程式
    API 中的 MCP
    MCP 連接器遠端 MCP 伺服器
    第三方平台上的 Claude
    Amazon BedrockMicrosoft FoundryVertex AI
    提示詞工程
    概覽提示詞產生器使用提示詞範本提示詞改進器清晰直接使用範例(多次提示)讓 Claude 思考(CoT)使用 XML 標籤給 Claude 一個角色(系統提示詞)預填 Claude 的回應鏈接複雜提示詞長上下文提示擴展思考提示
    測試與評估
    定義成功標準開發測試案例使用評估工具降低延遲
    加強防護欄
    減少幻覺提高輸出一致性緩解越獄串流拒絕減少提示詞洩露保持 Claude 的角色
    管理和監控
    Admin API 概覽使用量和成本 APIClaude Code Analytics API
    Console
    Log in
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...

    Solutions

    • AI agents
    • Code modernization
    • Coding
    • Customer support
    • Education
    • Financial services
    • Government
    • Life sciences

    Partners

    • Amazon Bedrock
    • Google Cloud's Vertex AI

    Learn

    • Blog
    • Catalog
    • Courses
    • Use cases
    • Connectors
    • Customer stories
    • Engineering at Anthropic
    • Events
    • Powered by Claude
    • Service partners
    • Startups program

    Company

    • Anthropic
    • Careers
    • Economic Futures
    • Research
    • News
    • Responsible Scaling Policy
    • Security and compliance
    • Transparency

    Learn

    • Blog
    • Catalog
    • Courses
    • Use cases
    • Connectors
    • Customer stories
    • Engineering at Anthropic
    • Events
    • Powered by Claude
    • Service partners
    • Startups program

    Help and security

    • Availability
    • Status
    • Support
    • Discord

    Terms and policies

    • Privacy policy
    • Responsible disclosure policy
    • Terms of service: Commercial
    • Terms of service: Consumer
    • Usage policy
    工具

    細粒度工具串流

    細粒度工具串流

    工具使用現在支援參數值的細粒度串流。這允許開發者在不進行緩衝/JSON驗證的情況下串流工具使用參數,減少開始接收大型參數的延遲。

    細粒度工具串流是一個測試版功能。請確保在生產環境中使用之前評估您的回應。

    請使用此表單提供關於模型回應品質、API本身或文檔品質的反饋——我們迫不及待想聽到您的意見!

    使用細粒度工具串流時,您可能會收到無效或部分的JSON輸入。請確保在您的程式碼中考慮這些邊緣情況。

    如何使用細粒度工具串流

    要使用此測試版功能,只需在工具使用請求中添加測試版標頭fine-grained-tool-streaming-2025-05-14並開啟串流。

    以下是如何在API中使用細粒度工具串流的範例:

    curl https://api.anthropic.com/v1/messages \
      -H "content-type: application/json" \
      -H "x-api-key: $ANTHROPIC_API_KEY" \
      -H "anthropic-version: 2023-06-01" \
      -H "anthropic-beta: fine-grained-tool-streaming-2025-05-14" \
      -d '{
        "model": "claude-sonnet-4-5",
        "max_tokens": 65536,
        "tools": [
          {
            "name": "make_file",
            "description": "Write text to a file",
            "input_schema": {
              "type": "object",
              "properties": {
                "filename": {
                  "type": "string",
                  "description": "The filename to write text to"
                },
                "lines_of_text": {
                  "type": "array",
                  "description": "An array of lines of text to write to the file"
                }
              },
              "required": ["filename", "lines_of_text"]
            }
          }
        ],
        "messages": [
          {
            "role": "user",
            "content": "Can you write a long poem and make a file called poem.txt?"
          }
        ],
        "stream": true
      }' | jq '.usage'

    在此範例中,細粒度工具串流使Claude能夠將長詩的行串流到工具呼叫make_file中,而無需緩衝來驗證lines_of_text參數是否為有效的JSON。這意味著您可以在參數到達時看到參數串流,而無需等待整個參數緩衝和驗證。

    使用細粒度工具串流時,工具使用區塊開始串流得更快,通常更長且包含更少的斷詞。這是由於分塊行為的差異。

    範例:

    沒有細粒度串流(15秒延遲):

    區塊1:'{"'
    區塊2:'query": "Ty'
    區塊3:'peScri'
    區塊4:'pt 5.0 5.1 '
    區塊5:'5.2 5'
    區塊6:'.3'
    區塊8:' new f'
    區塊9:'eatur'
    ...

    使用細粒度串流(3秒延遲):

    區塊1:'{"query": "TypeScript 5.0 5.1 5.2 5.3'
    區塊2:' new features comparison'

    由於細粒度串流在不進行緩衝或JSON驗證的情況下發送參數,因此無法保證結果串流將以有效的JSON字串完成。 特別是,如果達到停止原因max_tokens,串流可能會在參數中途結束並可能不完整。您通常必須編寫特定的支援來處理達到max_tokens的情況。

    處理工具回應中的無效JSON

    使用細粒度工具串流時,您可能會從模型收到無效或不完整的JSON。如果您需要在錯誤回應區塊中將此無效JSON傳回給模型,您可以將其包裝在JSON物件中以確保正確處理(使用合理的鍵)。例如:

    {
      "INVALID_JSON": "<your invalid json string>"
    }

    這種方法幫助模型理解內容是無效的JSON,同時保留原始格式錯誤的資料以供除錯目的。

    包裝無效JSON時,請確保正確轉義無效JSON字串中的任何引號或特殊字元,以維持包裝物件中的有效JSON結構。

    • 處理工具回應中的無效JSON