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
    指南

    串流輸入

    了解 Claude Agent SDK 的兩種輸入模式以及何時使用每種模式

    概述

    Claude Agent SDK 支援兩種不同的輸入模式來與代理程式互動:

    • 串流輸入模式(預設且推薦)- 持續的互動式會話
    • 單一訊息輸入 - 使用會話狀態和恢復的一次性查詢

    本指南說明每種模式的差異、優點和使用案例,幫助您為應用程式選擇正確的方法。

    串流輸入模式(推薦)

    串流輸入模式是使用 Claude Agent SDK 的首選方式。它提供對代理程式功能的完整存取,並實現豐富的互動體驗。

    它允許代理程式作為長期運行的程序運作,接收使用者輸入、處理中斷、顯示權限請求,並處理會話管理。

    運作方式

    %%{init: {"theme": "base", "themeVariables": {"edgeLabelBackground": "#F0F0EB", "lineColor": "#91918D", "primaryColor": "#F0F0EB", "primaryTextColor": "#191919", "primaryBorderColor": "#D9D8D5", "secondaryColor": "#F5E6D8", "tertiaryColor": "#CC785C", "noteBkgColor": "#FAF0E6", "noteBorderColor": "#91918D"}, "sequence": {"actorMargin": 50, "width": 150, "height": 65, "boxMargin": 10, "boxTextMargin": 5, "noteMargin": 10, "messageMargin": 35}}}%%
    sequenceDiagram
        participant App as Your Application
        participant Agent as Claude Agent
        participant Tools as Tools/Hooks
        participant FS as Environment/<br/>File System
        
        App->>Agent: Initialize with AsyncGenerator
        activate Agent
        
        App->>Agent: Yield Message 1
        Agent->>Tools: Execute tools
        Tools->>FS: Read files
        FS-->>Tools: File contents
        Tools->>FS: Write/Edit files
        FS-->>Tools: Success/Error
        Agent-->>App: Stream partial response
        Agent-->>App: Stream more content...
        Agent->>App: Complete Message 1
        
        App->>Agent: Yield Message 2 + Image
        Agent->>Tools: Process image & execute
        Tools->>FS: Access filesystem
        FS-->>Tools: Operation results
        Agent-->>App: Stream response 2
        
        App->>Agent: Queue Message 3
        App->>Agent: Interrupt/Cancel
        Agent->>App: Handle interruption
        
        Note over App,Agent: Session stays alive
        Note over Tools,FS: Persistent file system<br/>state maintained
        
        deactivate Agent

    優點

    圖片上傳

    直接將圖片附加到訊息中進行視覺分析和理解

    排隊訊息

    發送多個按順序處理的訊息,並具有中斷能力

    工具整合

    在會話期間完整存取所有工具和自訂 MCP 伺服器

    鉤子支援

    使用生命週期鉤子在各個點自訂行為

    即時回饋

    在回應生成時即時查看,而不僅僅是最終結果

    上下文持續性

    在多輪對話中自然地維護對話上下文

    實作範例

    import { query } from "@anthropic-ai/claude-agent-sdk";
    import { readFileSync } from "fs";
    
    async function* generateMessages() {
      // 第一個訊息
      yield {
        type: "user" as const,
        message: {
          role: "user" as const,
          content: "分析此程式碼庫的安全問題"
        }
      };
      
      // 等待條件或使用者輸入
      await new Promise(resolve => setTimeout(resolve, 2000));
      
      // 後續附加圖片
      yield {
        type: "user" as const,
        message: {
          role: "user" as const,
          content: [
            {
              type: "text",
              text: "檢視此架構圖"
            },
            {
              type: "image",
              source: {
                type: "base64",
                media_type: "image/png",
                data: readFileSync("diagram.png", "base64")
              }
            }
          ]
        }
      };
    }
    
    // 處理串流回應
    for await (const message of query({
      prompt: generateMessages(),
      options: {
        maxTurns: 10,
        allowedTools: ["Read", "Grep"]
      }
    })) {
      if (message.type === "result") {
        console.log(message.result);
      }
    }

    單一訊息輸入

    單一訊息輸入較為簡單但功能有限。

    何時使用單一訊息輸入

    在以下情況下使用單一訊息輸入:

    • 您需要一次性回應
    • 您不需要圖片附件、鉤子等功能
    • 您需要在無狀態環境中運作,例如 lambda 函數

    限制

    單一訊息輸入模式不支援:

    • 訊息中的直接圖片附件
    • 動態訊息排隊
    • 即時中斷
    • 鉤子整合
    • 自然的多輪對話

    實作範例

    import { query } from "@anthropic-ai/claude-agent-sdk";
    
    // 簡單的一次性查詢
    for await (const message of query({
      prompt: "解釋身份驗證流程",
      options: {
        maxTurns: 1,
        allowedTools: ["Read", "Grep"]
      }
    })) {
      if (message.type === "result") {
        console.log(message.result);
      }
    }
    
    // 透過會話管理繼續對話
    for await (const message of query({
      prompt: "現在解釋授權程序",
      options: {
        continue: true,
        maxTurns: 1
      }
    })) {
      if (message.type === "result") {
        console.log(message.result);
      }
    }
      © 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