Loading...
    • 開發者指南
    • API 參考
    • MCP
    • 資源
    • 發行說明
    Search...
    ⌘K

    第一步

    Claude 介紹快速入門

    模型與定價

    模型概覽選擇模型Claude 4.5 的新功能遷移到 Claude 4.5模型棄用定價

    使用 Claude 建構

    功能概覽使用 Messages API上下文視窗提示詞最佳實踐

    功能

    提示詞快取上下文編輯延伸思考串流訊息批次處理引用多語言支援Token 計數嵌入向量視覺PDF 支援Files API搜尋結果Google Sheets 附加元件

    工具

    概述如何實現工具使用代幣高效工具使用細粒度工具串流Bash 工具代碼執行工具電腦使用工具文字編輯工具網頁擷取工具網路搜尋工具記憶工具

    代理技能

    概述在 API 中開始使用 Agent Skills技能編寫最佳實踐使用 Agent Skills 與 API

    Agent SDK

    概述Agent SDK 參考 - TypeScriptPython SDK

    指南

    串流輸入處理權限會話管理託管 Agent SDK修改系統提示SDK 中的 MCP自訂工具SDK 中的子代理SDK 中的斜線命令SDK 中的代理技能追蹤成本和使用量待辦事項清單SDK 中的外掛程式

    API 中的 MCP

    MCP 連接器遠端 MCP 伺服器

    Claude 在第三方平台上

    Amazon BedrockVertex AI

    提示工程

    概述提示詞生成器使用提示模板提示詞改進器保持清晰和直接使用範例(多樣提示)讓 Claude 思考(思維鏈)使用 XML 標籤給 Claude 分配角色(系統提示詞)預填 Claude 的回應串接複雜提示長文本技巧延伸思考技巧

    測試與評估

    定義成功標準開發測試案例使用評估工具降低延遲

    加強防護措施

    減少幻覺提高輸出一致性防範越獄handle-streaming-refusals減少提示詞洩漏保持 Claude 的角色特性

    管理和監控

    Admin API 概述使用量和成本 APIClaude Code 分析 API
    Console
    工具

    細粒度工具串流

    細粒度工具串流

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

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

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

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

    如何使用細粒度工具串流

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

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

    Shell
    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'
    Python
    import anthropic
    
    client = anthropic.Anthropic()
    
    response = client.beta.messages.stream(
        max_tokens=65536,
        model="claude-sonnet-4-5",
        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?"
        }],
        betas=["fine-grained-tool-streaming-2025-05-14"]
    )
    
    print(response.usage)
    TypeScript
    import Anthropic from '@anthropic-ai/sdk';
    
    const anthropic = new Anthropic();
    
    const message = await anthropic.beta.messages.stream({
      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?" 
      }],
      betas: ["fine-grained-tool-streaming-2025-05-14"]
    });
    
    console.log(message.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
    © 2025 ANTHROPIC PBC

    Products

    • Claude
    • Claude Code
    • Max plan
    • Team plan
    • Enterprise plan
    • Download app
    • Pricing
    • Log in

    Features

    • Claude and Slack
    • Claude in Excel

    Models

    • Opus
    • Sonnet
    • Haiku

    Solutions

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

    Claude Developer Platform

    • Overview
    • Developer docs
    • Pricing
    • Amazon Bedrock
    • Google Cloud’s Vertex AI
    • Console login

    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

    Help and security

    • Availability
    • Status
    • Support center

    Terms and policies

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

    Products

    • Claude
    • Claude Code
    • Max plan
    • Team plan
    • Enterprise plan
    • Download app
    • Pricing
    • Log in

    Features

    • Claude and Slack
    • Claude in Excel

    Models

    • Opus
    • Sonnet
    • Haiku

    Solutions

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

    Claude Developer Platform

    • Overview
    • Developer docs
    • Pricing
    • Amazon Bedrock
    • Google Cloud’s Vertex AI
    • Console login

    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

    Help and security

    • Availability
    • Status
    • Support center

    Terms and policies

    • Privacy policy
    • Responsible disclosure policy
    • Terms of service: Commercial
    • Terms of service: Consumer
    • Usage policy
    © 2025 ANTHROPIC PBC