<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>自動化工具 &#8211; 小人物看世界</title>
	<atom:link href="https://blog.che-ya.com/tag/%e8%87%aa%e5%8b%95%e5%8c%96%e5%b7%a5%e5%85%b7/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.che-ya.com</link>
	<description>軟體工程師的技術筆記</description>
	<lastBuildDate>Wed, 08 Apr 2026 08:07:42 +0000</lastBuildDate>
	<language>zh-TW</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://blog.che-ya.com/wp-content/uploads/2021/08/cropped-APP_icon-32x32.png</url>
	<title>自動化工具 &#8211; 小人物看世界</title>
	<link>https://blog.che-ya.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>實戰專案：用 Agent SDK 打造自動化工具</title>
		<link>https://blog.che-ya.com/agent-sdk-automation-tools/</link>
		
		<dc:creator><![CDATA[ㄚ槌]]></dc:creator>
		<pubDate>Tue, 05 May 2026 02:47:00 +0000</pubDate>
				<category><![CDATA[Claude Agent SDK]]></category>
		<category><![CDATA[Claude Code]]></category>
		<category><![CDATA[Agent SDK]]></category>
		<category><![CDATA[AI 編程工具]]></category>
		<category><![CDATA[Anthropic]]></category>
		<category><![CDATA[實戰專案]]></category>
		<category><![CDATA[自動化工具]]></category>
		<guid isPermaLink="false">https://blog.che-ya.com/?p=878</guid>

					<description><![CDATA[前兩篇文章中，我們分別學習了 Claude Agent SDK 的基礎建構方式，以及 Guardrails 與 ... <a title="實戰專案：用 Agent SDK 打造自動化工具" class="read-more" href="https://blog.che-ya.com/agent-sdk-automation-tools/" aria-label="Read more about 實戰專案：用 Agent SDK 打造自動化工具">閱讀全文</a>]]></description>
										<content:encoded><![CDATA[
<p>前兩篇文章中，我們分別學習了 <a href="https://blog.che-ya.com/claude-agent-sdk-getting-started/">Claude Agent SDK 的基礎建構方式</a>，以及 <a href="https://blog.che-ya.com/claude-agent-sdk-guardrails-handoffs/">Guardrails 與 Handoffs 的進階機制</a>。理論學完了，現在是時候動手實作了。本篇將帶你完成四個真實可執行的自動化工具專案，從程式碼品質檢查、研究報告產生，到批次檔案處理，讓你的 Agent SDK 技能真正派上用場。</p>



<h2 class="wp-block-heading">為什麼用 Agent SDK 打造自動化工具？</h2>



<p>傳統的自動化腳本需要你預先定義每一個步驟，遇到例外狀況就會卡住。用 Agent SDK 建構的自動化工具則不同：你只需要描述「要做什麼」，Claude 會自行判斷「要怎麼做」，並在過程中隨機應變。這讓 Agent 特別適合處理那些「邏輯複雜但可描述」的任務。</p>



<figure class="wp-block-table"><table><thead><tr><th>面向</th><th>傳統腳本</th><th>Agent SDK</th></tr></thead><tbody><tr><td>開發方式</td><td>逐步定義每個操作</td><td>用自然語言描述目標</td></tr><tr><td>例外處理</td><td>需手動撰寫所有分支</td><td>Claude 自動應對</td></tr><tr><td>維護成本</td><td>需求改變時大幅重寫</td><td>調整 Prompt 即可</td></tr><tr><td>工具整合</td><td>手動接 API 和 SDK</td><td>MCP Server 即插即用</td></tr><tr><td>適用場景</td><td>固定流程的重複任務</td><td>需要判斷力的複雜任務</td></tr></tbody></table></figure>



<h2 class="wp-block-heading">專案一：程式碼品質自動檢查 Agent</h2>



<p>這是最實用的入門專案：讓 Claude 自動讀取指定目錄的程式碼，找出潛在問題並產生報告。這類任務傳統做法需要整合多個 linting 工具，但 Agent SDK 讓你用幾十行程式碼就能完成。</p>



<h3 class="wp-block-heading">Python 版本</h3>



<pre class="wp-block-code"><code lang="python" class="language-python">import asyncio
from pathlib import Path
from claude_agent_sdk import query, ClaudeAgentOptions, ResultMessage


async def code_review_agent(target_dir: str = ".") -&gt; str:
    """程式碼品質檢查 Agent：掃描目錄並產生品質報告"""
    
    report_lines = []
    
    async for message in query(
        prompt=f"""請對 {target_dir} 目錄中的 Python 程式碼進行全面品質檢查。

        **檢查項目：**
        1. 找出所有可能導致 crash 的 bug（除以零、None 存取、索引越界等）
        2. 找出未使用的匯入和變數
        3. 找出缺少錯誤處理的危險操作（檔案 I/O、網路請求等）
        4. 找出命名不規範或難以理解的程式碼
        5. 找出重複程式碼（DRY 原則違反）

        **輸出格式：**
        - 以 Markdown 格式輸出報告
        - 每個問題列出：檔案路徑、行號、問題描述、建議修正方式
        - 最後給出整體評分（1-10 分）和優先修正建議
        """,
        options=ClaudeAgentOptions(
            allowed_tools=["Read", "Glob", "Grep", "Bash"],
            system_prompt="你是資深 Python 工程師，專注於程式碼品質與安全性。請用繁體中文輸出報告。",
        ),
    ):
        if isinstance(message, ResultMessage):
            report_lines.append(message.result or "")
    
    return "
".join(report_lines)


async def main():
    import sys
    target = sys.argv[1] if len(sys.argv) &gt; 1 else "."
    
    print(f"🔍 開始分析 {target} 目錄的程式碼品質...")
    report = await code_review_agent(target)
    
    # 儲存報告
    report_path = Path("code_review_report.md")
    report_path.write_text(report, encoding="utf-8")
    print(f"✅ 報告已儲存到 {report_path}")
    print(report)


asyncio.run(main())
</code></pre>



<h3 class="wp-block-heading">TypeScript 版本</h3>



<pre class="wp-block-code"><code lang="typescript" class="language-typescript">import { query } from "@anthropic-ai/claude-agent-sdk";
import { writeFile } from "fs/promises";

async function codeReviewAgent(targetDir: string = "."): Promise&lt;string&gt; {
  const reportLines: string[] = [];

  for await (const message of query({
    prompt: `請對 ${targetDir} 目錄中的 TypeScript/JavaScript 程式碼進行全面品質檢查。

    **檢查項目：**
    1. 找出型別安全問題（any 濫用、未處理的 null/undefined）
    2. 找出非同步處理問題（未 await 的 Promise、競態條件）
    3. 找出效能問題（不必要的重新渲染、記憶體洩漏風險）
    4. 找出安全性問題（XSS 風險、敏感資料外洩）
    5. 找出過時 API 使用和相依套件問題

    **輸出格式：**
    以 Markdown 格式輸出，包含每個問題的檔案路徑、行號與修正建議。`,
    options: {
      allowedTools: ["Read", "Glob", "Grep", "Bash"],
      systemPrompt: "你是資深 TypeScript 工程師，專注於程式碼品質與安全性。請用繁體中文輸出報告。"
    }
  })) {
    if ("result" in message &amp;&amp; message.result) {
      reportLines.push(message.result);
    }
  }

  return reportLines.join("\n");
}

async function main() {
  const target = process.argv[2] || ".";
  
  console.log(`🔍 開始分析 ${target} 目錄的程式碼品質...`);
  const report = await codeReviewAgent(target);
  
  await writeFile("code_review_report.md", report, "utf-8");
  console.log("✅ 報告已儲存到 code_review_report.md");
  console.log(report);
}

main().catch(console.error);
</code></pre>



<p>執行方式很簡單，在專案根目錄執行 <code>python agent.py ./src</code> 或 <code>npx tsx agent.ts ./src</code>，Claude 就會自動讀取所有 Python/TypeScript 檔案、分析問題，並輸出結構化的 Markdown 報告。</p>



<h2 class="wp-block-heading">專案二：自動化研究報告 Agent</h2>



<p>這個專案展示 Agent SDK 最強大的能力之一：Multi-agent 架構。主 Agent 負責規劃研究策略，並派遣多個子 Agent 並行搜尋不同面向的資訊，最後匯整成完整報告。</p>



<pre class="wp-block-code"><code lang="python" class="language-python">import asyncio
from claude_agent_sdk import query, ClaudeAgentOptions, AgentDefinition, ResultMessage


async def research_agent(topic: str, output_file: str = "research_report.md") -&gt; str:
    """多 Agent 研究報告產生器"""
    
    # 定義專門的研究子 Agent
    trend_researcher = AgentDefinition(
        description="搜尋最新趨勢與新聞。用於了解近期發展、市場動態和業界現況。",
        prompt="""你是趨勢分析師，專注於搜尋最新資訊。
        使用 WebSearch 搜尋過去 6 個月的最新動態、市場趨勢和重要新聞。
        整理成有條理的摘要，標注資訊來源和日期。""",
        tools=["WebSearch", "WebFetch"],
    )
    
    technical_researcher = AgentDefinition(
        description="搜尋技術細節與深度分析。用於了解技術原理、架構設計和實作方式。",
        prompt="""你是技術分析師，專注於搜尋深度技術資料。
        使用 WebSearch 和 WebFetch 搜尋官方文件、技術部落格和學術資料。
        整理核心概念、技術優缺點和使用場景。""",
        tools=["WebSearch", "WebFetch"],
    )
    
    comparison_researcher = AgentDefinition(
        description="搜尋競品比較與替代方案。用於比較不同工具、框架或方法的差異。",
        prompt="""你是競品分析師，專注於比較分析。
        使用 WebSearch 搜尋同類產品或方法的比較資料、使用者評價和選型建議。
        整理成清晰的比較表格和選型指南。""",
        tools=["WebSearch", "WebFetch"],
    )
    
    result_lines = []
    
    async for message in query(
        prompt=f"""請針對「{topic}」這個主題產生一份完整的研究報告。

        **研究策略：**
        1. 先用 trend_researcher 子 Agent 搜尋最新趨勢與發展
        2. 再用 technical_researcher 子 Agent 深入了解技術細節
        3. 最後用 comparison_researcher 子 Agent 分析競品和替代方案
        4. 匯整三個子 Agent 的研究成果，寫成完整的研究報告

        **報告格式（Markdown）：**
        - 執行摘要（300字以內）
        - 最新趨勢與市場動態
        - 技術深度分析
        - 競品比較與選型建議
        - 結論與行動建議
        
        請確保報告內容具體、有數據支撐，並附上參考來源。
        """,
        options=ClaudeAgentOptions(
            allowed_tools=["Agent", "Write"],
            agents={
                "trend_researcher": trend_researcher,
                "technical_researcher": technical_researcher,
                "comparison_researcher": comparison_researcher,
            },
            system_prompt="你是研究協調員，負責規劃研究策略並整合各子 Agent 的研究成果。請用繁體中文輸出。",
        ),
    ):
        if isinstance(message, ResultMessage):
            result_lines.append(message.result or "")
    
    report = "
".join(result_lines)
    
    # 儲存報告
    with open(output_file, "w", encoding="utf-8") as f:
        f.write(report)
    
    return report


async def main():
    import sys
    topic = sys.argv[1] if len(sys.argv) &gt; 1 else "Claude Agent SDK"
    
    print(f"🔬 開始研究：{topic}")
    report = await research_agent(topic)
    print("✅ 研究報告已產生！")
    print(report[:500] + "..." if len(report) &gt; 500 else report)


asyncio.run(main())
</code></pre>



<p>這個架構的優雅之處在於：主 Agent 像是研究主管，負責分派工作；三個子 Agent 像是各有專長的研究員，並行蒐集不同面向的資料。這種 Delegation 模式讓研究效率大幅提升。</p>



<h2 class="wp-block-heading">專案三：智慧檔案整理 Agent</h2>



<p>這個專案展示如何讓 Agent 處理日常的檔案管理任務。Agent 會分析指定目錄的檔案，根據內容和類型自動分類整理，並生成整理報告。</p>



<pre class="wp-block-code"><code lang="python" class="language-python">import asyncio
from claude_agent_sdk import query, ClaudeAgentOptions, ResultMessage


async def file_organizer_agent(source_dir: str, dry_run: bool = True) -&gt; str:
    """智慧檔案整理 Agent
    
    Args:
        source_dir: 要整理的目錄路徑
        dry_run: True 時只顯示計畫不執行，False 時實際移動檔案
    """
    
    mode_desc = "（模擬模式：只顯示計畫，不實際移動檔案）" if dry_run else "（執行模式：將實際移動檔案）"
    
    result_lines = []
    
    async for message in query(
        prompt=f"""請整理 {source_dir} 目錄中的檔案 {mode_desc}。

        **整理策略：**
        1. 先用 Glob 和 Bash 列出所有檔案，了解目錄結構
        2. 根據以下規則分類：
           - 圖片（jpg/png/gif/webp）→ images/ 子目錄
           - 文件（pdf/doc/docx/txt/md）→ documents/ 子目錄
           - 程式碼（py/js/ts/go/rs）→ code/ 子目錄
           - 影片（mp4/mov/avi）→ videos/ 子目錄
           - 壓縮檔（zip/tar/gz）→ archives/ 子目錄
           - 其他 → others/ 子目錄
        3. 若為模擬模式，輸出完整的整理計畫清單
        4. 若為執行模式，使用 Bash 執行 mkdir 和 mv 指令實際整理
        
        **輸出格式：**
        - 統計各類別檔案數量
        - 列出每個檔案的原始位置和目標位置
        - 標注任何需要人工確認的情況（如重複檔名）
        """,
        options=ClaudeAgentOptions(
            # dry_run 時只允許讀取工具，不允許寫入
            allowed_tools=["Glob", "Bash", "Read"] if dry_run else ["Glob", "Bash", "Read", "Write"],
            permission_mode="acceptEdits" if not dry_run else "default",
            system_prompt="你是專業的檔案管理助手。整理時務必謹慎，遇到不確定的情況要說明。",
        ),
    ):
        if isinstance(message, ResultMessage):
            result_lines.append(message.result or "")
    
    return "
".join(result_lines)


async def main():
    import sys
    
    source = sys.argv[1] if len(sys.argv) &gt; 1 else "./downloads"
    execute = "--execute" in sys.argv
    
    if execute:
        print("⚠️  執行模式：將實際移動檔案，請確認後繼續")
        confirm = input("確定要繼續嗎？(y/N) ")
        if confirm.lower() != "y":
            print("已取消")
            return
    else:
        print("📋 模擬模式：只顯示整理計畫")
    
    result = await file_organizer_agent(source, dry_run=not execute)
    print(result)


asyncio.run(main())
</code></pre>



<p>注意這個設計中的安全機制：預設使用 <code>dry_run=True</code> 模擬模式，只有在使用者明確傳入 <code>--execute</code> 參數且輸入確認後，才會實際移動檔案。這種「先預覽，後執行」的模式能有效防止意外操作。</p>



<h2 class="wp-block-heading">專案四：Git 提交訊息自動產生器</h2>



<p>這個小而實用的工具展示如何在開發工作流程中嵌入 Agent。每次準備 commit 時，Agent 自動分析 <code>git diff</code>，產生符合 Conventional Commits 規範的提交訊息。</p>



<pre class="wp-block-code"><code lang="typescript" class="language-typescript">import { query } from "@anthropic-ai/claude-agent-sdk";

interface CommitMessage {
  type: string;
  scope?: string;
  subject: string;
  body?: string;
  breaking?: boolean;
}

async function generateCommitMessage(): Promise&lt;CommitMessage&gt; {
  let result: CommitMessage | null = null;

  for await (const message of query({
    prompt: `請分析目前的 Git 變更並產生一個符合 Conventional Commits 規範的提交訊息。

    步驟：
    1. 執行 git diff --staged 查看暫存的變更
    2. 如果沒有暫存變更，執行 git diff 查看未暫存的變更
    3. 分析變更的性質（新功能、修復、重構等）
    4. 產生提交訊息

    Conventional Commits 格式：
    - feat: 新功能
    - fix: 錯誤修復
    - docs: 文件更新
    - style: 格式調整（不影響邏輯）
    - refactor: 重構
    - test: 測試相關
    - chore: 建構或輔助工具變更

    請以 JSON 格式輸出：
    {
      "type": "feat|fix|docs|style|refactor|test|chore",
      "scope": "選填，影響的模組",
      "subject": "簡短描述（英文，祈使語氣，不超過 50 字）",
      "body": "選填，詳細說明（中文或英文皆可）",
      "breaking": false
    }`,
    options: {
      allowedTools: ["Bash"],
      systemPrompt: "你是 Git 提交訊息專家，熟悉 Conventional Commits 規範。只輸出 JSON，不要其他文字。"
    }
  })) {
    if ("result" in message &amp;&amp; message.result) {
      try {
        // 嘗試解析 JSON 結果
        const jsonMatch = message.result.match(/{[sS]*}/);
        if (jsonMatch) {
          result = JSON.parse(jsonMatch[0]);
        }
      } catch (e) {
        console.error("解析 JSON 失敗:", e);
      }
    }
  }

  if (!result) {
    throw new Error("無法產生提交訊息");
  }

  return result;
}

async function main() {
  console.log("🔍 分析 Git 變更中...");
  
  const commit = await generateCommitMessage();
  
  // 組合提交訊息
  let fullMessage = `${commit.type}${commit.scope ? `(${commit.scope})` : ""}: ${commit.subject}`;
  
  if (commit.breaking) {
    fullMessage += "\n\nBREAKING CHANGE: 此版本包含不相容的變更";
  }
  
  if (commit.body) {
    fullMessage += `\n\n${commit.body}`;
  }
  
  console.log("\n📝 建議的提交訊息：");
  console.log("---");
  console.log(fullMessage);
  console.log("---");
  
  // 詢問是否直接使用
  const readline = await import("readline");
  const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
  
  rl.question("\n使用此訊息提交？(y/N) ", async (answer) =&gt; {
    if (answer.toLowerCase() === "y") {
      const { exec } = await import("child_process");
      const { promisify } = await import("util");
      const execAsync = promisify(exec);
      
      await execAsync(`git commit -m "${fullMessage.replace(/"/g, '\\"')}"`);
      console.log("✅ 提交成功！");
    } else {
      console.log("已取消，你可以手動使用上面的訊息。");
    }
    rl.close();
  });
}

main().catch(console.error);
</code></pre>



<p>這個工具可以整合進你的 Git hooks 中。在 <code>.git/hooks/prepare-commit-msg</code> 中呼叫這個腳本，每次 <code>git commit</code> 時就會自動分析變更並建議訊息，大幅降低撰寫提交訊息的心智負擔。</p>



<h2 class="wp-block-heading">Agent 設計模式：讓工具更可靠</h2>



<p>在開發以上四個專案的過程中，我們可以歸納出幾個讓 Agent 自動化工具更可靠的設計模式。</p>



<h3 class="wp-block-heading">模式一：漸進式授權（Progressive Permissions）</h3>



<p>先用唯讀工具分析，確認後再授予寫入權限。這是最重要的安全設計原則。</p>



<pre class="wp-block-code"><code lang="python" class="language-python">import asyncio
from claude_agent_sdk import query, ClaudeAgentOptions

async def safe_refactor(file_path: str):
    """安全重構：先分析，後修改"""
    
    # 第一階段：唯讀分析
    print("📋 第一階段：分析程式碼...")
    analysis = []
    async for msg in query(
        prompt=f"分析 {file_path} 中需要重構的地方，列出具體的修改建議",
        options=ClaudeAgentOptions(allowed_tools=["Read", "Grep"]),
    ):
        if hasattr(msg, "result") and msg.result:
            analysis.append(msg.result)
    
    plan = "
".join(analysis)
    print(f"
修改計畫：
{plan}
")
    
    # 確認後才進入修改階段
    confirm = input("確定要執行以上修改嗎？(y/N) ")
    if confirm.lower() != "y":
        print("已取消")
        return
    
    # 第二階段：執行修改
    print("
✏️  第二階段：執行修改...")
    async for msg in query(
        prompt=f"根據以下計畫修改 {file_path}：
{plan}",
        options=ClaudeAgentOptions(
            allowed_tools=["Read", "Edit"],
            permission_mode="acceptEdits",
        ),
    ):
        if hasattr(msg, "result"):
            print("✅ 重構完成！")
</code></pre>



<h3 class="wp-block-heading">模式二：結果驗證（Output Validation）</h3>



<p>不要盲目信任 Agent 的輸出，加入驗證步驟確保結果符合預期。</p>



<pre class="wp-block-code"><code lang="python" class="language-python">import asyncio
from claude_agent_sdk import query, ClaudeAgentOptions, ResultMessage


def validate_report(report: str) -&gt; tuple[bool, str]:
    """驗證報告是否包含必要章節"""
    required_sections = ["## 摘要", "## 分析", "## 建議"]
    missing = [s for s in required_sections if s not in report]
    
    if missing:
        return False, f"報告缺少以下章節：{', '.join(missing)}"
    
    if len(report) &lt; 500:
        return False, "報告內容太短，可能不完整"
    
    return True, "驗證通過"


async def generate_validated_report(topic: str, max_retries: int = 3) -&gt; str:
    """帶驗證和重試機制的報告產生器"""
    
    for attempt in range(1, max_retries + 1):
        print(f"🔄 嘗試第 {attempt} 次...")
        
        result_lines = []
        async for message in query(
            prompt=f"請針對「{topic}」產生一份包含摘要、分析和建議三個章節的報告",
            options=ClaudeAgentOptions(allowed_tools=["WebSearch", "WebFetch"]),
        ):
            if isinstance(message, ResultMessage) and message.result:
                result_lines.append(message.result)
        
        report = "
".join(result_lines)
        
        # 驗證輸出
        is_valid, reason = validate_report(report)
        if is_valid:
            print(f"✅ 第 {attempt} 次成功")
            return report
        else:
            print(f"⚠️  第 {attempt} 次失敗：{reason}")
    
    raise ValueError(f"經過 {max_retries} 次嘗試後仍無法產生有效報告")
</code></pre>



<h3 class="wp-block-heading">模式三：進度追蹤（Progress Tracking）</h3>



<p>對於耗時較長的 Agent 任務，即時顯示進度讓使用者知道 Agent 正在做什麼。</p>



<pre class="wp-block-code"><code lang="python" class="language-python">import asyncio
import time
from claude_agent_sdk import query, ClaudeAgentOptions
from claude_agent_sdk import AssistantMessage, ToolUseBlock, ResultMessage


async def track_progress_agent(prompt: str):
    """帶進度追蹤的 Agent 執行器"""
    
    start_time = time.time()
    tool_call_count = 0
    
    async for message in query(
        prompt=prompt,
        options=ClaudeAgentOptions(
            allowed_tools=["Read", "Glob", "Grep", "Bash", "WebSearch"],
        ),
    ):
        if isinstance(message, AssistantMessage):
            for block in message.content:
                if isinstance(block, ToolUseBlock):
                    tool_call_count += 1
                    elapsed = time.time() - start_time
                    print(f"⚙️  [{elapsed:.1f}s] 工具呼叫 #{tool_call_count}: {block.name}")
                    
                    # 顯示工具參數摘要
                    if block.name in ["Read", "Write", "Edit"] and "file_path" in block.input:
                        print(f"   📄 {block.input['file_path']}")
                    elif block.name == "Bash" and "command" in block.input:
                        cmd = block.input["command"][:60]
                        print(f"   💻 {cmd}...")
                    elif block.name == "WebSearch" and "query" in block.input:
                        print(f"   🔍 {block.input['query']}")
        
        elif isinstance(message, ResultMessage):
            elapsed = time.time() - start_time
            print(f"
✅ 完成！耗時 {elapsed:.1f}s，共執行 {tool_call_count} 次工具呼叫")
            if message.result:
                print("
📋 結果：")
                print(message.result)
</code></pre>



<h2 class="wp-block-heading">效能優化：讓 Agent 更快更省錢</h2>



<p>Agent SDK 的計費基於 token 用量。以下幾個技巧可以在不犧牲品質的前提下降低成本。</p>



<figure class="wp-block-table"><table><thead><tr><th>優化技巧</th><th>說明</th><th>預估節省</th></tr></thead><tbody><tr><td>精確的 allowed_tools</td><td>只授予任務所需工具，避免 Claude 嘗試不相關工具</td><td>10-20%</td></tr><tr><td>清晰的 system_prompt</td><td>明確定義角色和輸出格式，減少反覆修正</td><td>15-30%</td></tr><tr><td>分段執行</td><td>複雜任務拆分成多個小 Agent，每段 context 更乾淨</td><td>20-40%</td></tr><tr><td>Session 復用</td><td>多輪對話使用 resume session，避免重複讀取相同檔案</td><td>30-50%</td></tr><tr><td>結果快取</td><td>對相同輸入的 Agent 結果進行快取，避免重複執行</td><td>依情況</td></tr></tbody></table></figure>



<p>其中 Session 復用是最容易被忽略但效果最顯著的優化。當你需要對同一份程式碼做多次分析時，第一次讀取後可以保留 session_id，後續的 query 直接 resume，Claude 不需要重新讀取所有檔案。</p>



<pre class="wp-block-code"><code lang="python" class="language-python">import asyncio
from claude_agent_sdk import query, ClaudeAgentOptions, SystemMessage, ResultMessage


async def multi_pass_analysis(file_path: str):
    """多次分析同一檔案：使用 Session 復用節省 Token"""
    
    session_id = None
    
    # 第一輪：讀取並理解程式碼
    print("第一輪：讀取程式碼...")
    async for message in query(
        prompt=f"請讀取並理解 {file_path} 的結構和功能",
        options=ClaudeAgentOptions(allowed_tools=["Read"]),
    ):
        if isinstance(message, SystemMessage) and message.subtype == "init":
            session_id = message.data.get("session_id")
    
    print(f"Session ID: {session_id}")
    
    # 第二輪：在已有 context 的基礎上分析 bug（不需重讀檔案）
    print("
第二輪：分析潛在 Bug...")
    bugs = []
    async for message in query(
        prompt="根據你剛讀取的程式碼，找出所有可能的 Bug",
        options=ClaudeAgentOptions(
            resume=session_id,  # 復用 session，不需重讀檔案
        ),
    ):
        if isinstance(message, ResultMessage) and message.result:
            bugs.append(message.result)
    
    # 第三輪：繼續在相同 context 中找效能問題
    print("
第三輪：分析效能問題...")
    perf_issues = []
    async for message in query(
        prompt="根據你剛讀取的程式碼，找出效能瓶頸和優化機會",
        options=ClaudeAgentOptions(
            resume=session_id,  # 繼續復用同一個 session
        ),
    ):
        if isinstance(message, ResultMessage) and message.result:
            perf_issues.append(message.result)
    
    print("
=== 分析完成 ===")
    print("Bug 報告：", "
".join(bugs))
    print("
效能分析：", "
".join(perf_issues))


asyncio.run(multi_pass_analysis("./src/main.py"))
</code></pre>



<h2 class="wp-block-heading">錯誤處理與生產環境準備</h2>



<p>把 Agent 部署到生產環境前，需要加入完善的錯誤處理機制。以下是一個封裝好的 Agent 執行器，包含超時控制、重試邏輯和錯誤記錄。</p>



<pre class="wp-block-code"><code lang="python" class="language-python">import asyncio
import logging
from typing import AsyncIterator, TypeVar
from claude_agent_sdk import query, ClaudeAgentOptions, ResultMessage


# 設定日誌
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)


class AgentTimeoutError(Exception):
    pass


class AgentRunner:
    """生產環境用的 Agent 執行器"""
    
    def __init__(
        self,
        max_retries: int = 3,
        timeout_seconds: int = 300,
        retry_delay: float = 5.0,
    ):
        self.max_retries = max_retries
        self.timeout_seconds = timeout_seconds
        self.retry_delay = retry_delay
    
    async def run(
        self,
        prompt: str,
        options: ClaudeAgentOptions,
    ) -&gt; str:
        """執行 Agent，帶重試和超時機制"""
        
        last_error = None
        
        for attempt in range(1, self.max_retries + 1):
            try:
                logger.info(f"Agent 執行中（第 {attempt}/{self.max_retries} 次）")
                
                result = await asyncio.wait_for(
                    self._run_single(prompt, options),
                    timeout=self.timeout_seconds,
                )
                
                logger.info("Agent 執行成功")
                return result
                
            except asyncio.TimeoutError:
                last_error = AgentTimeoutError(
                    f"Agent 執行超時（{self.timeout_seconds}s）"
                )
                logger.warning(f"第 {attempt} 次超時，{self.retry_delay}s 後重試...")
                
            except Exception as e:
                last_error = e
                logger.error(f"第 {attempt} 次失敗：{e}")
                
                # 部分錯誤不值得重試
                if "API key" in str(e) or "authentication" in str(e).lower():
                    raise
            
            if attempt &lt; self.max_retries:
                await asyncio.sleep(self.retry_delay)
        
        raise last_error or RuntimeError("Agent 執行失敗")
    
    async def _run_single(self, prompt: str, options: ClaudeAgentOptions) -&gt; str:
        """單次執行 Agent"""
        result_parts = []
        async for message in query(prompt=prompt, options=options):
            if isinstance(message, ResultMessage) and message.result:
                result_parts.append(message.result)
        return "
".join(result_parts)


# 使用範例
async def main():
    runner = AgentRunner(
        max_retries=3,
        timeout_seconds=120,
        retry_delay=3.0,
    )
    
    try:
        result = await runner.run(
            prompt="分析目前目錄的 Python 檔案並找出所有 TODO 註解",
            options=ClaudeAgentOptions(
                allowed_tools=["Read", "Glob", "Grep"],
            ),
        )
        print(result)
    except AgentTimeoutError:
        print("❌ 任務超時，請縮小任務範圍後重試")
    except Exception as e:
        print(f"❌ 執行失敗：{e}")


asyncio.run(main())
</code></pre>



<h2 class="wp-block-heading">總結</h2>



<p>透過本篇的四個實戰專案，你已經掌握了用 Claude Agent SDK 打造自動化工具的核心技能。回顧一下我們學到的重點：</p>



<ul class="wp-block-list">
<li><strong>程式碼品質 Agent</strong>：展示如何用 Read、Glob、Grep 工具做唯讀分析，適合整合進 CI/CD 流程</li>



<li><strong>研究報告 Agent</strong>：展示 Multi-agent 架構如何讓複雜研究任務並行化，大幅提升效率</li>



<li><strong>檔案整理 Agent</strong>：展示漸進式授權模式，先模擬預覽後執行，保護資料安全</li>



<li><strong>Git 提交訊息產生器</strong>：展示如何把 Agent 嵌入日常開發工作流</li>
</ul>



<p>更重要的是幾個跨專案通用的設計原則：<strong>漸進式授權</strong>保護安全、<strong>結果驗證</strong>確保品質、<strong>進度追蹤</strong>提升體驗、<strong>Session 復用</strong>節省成本。這些原則在任何 Agent 自動化工具的開發中都同樣適用。</p>



<p>Agent SDK 最強大的地方在於：它讓你把「做什麼」和「怎麼做」解耦。你負責定義目標和規則，Claude 負責執行決策。這種分工讓你能快速建構出傳統自動化腳本需要數倍時間才能完成的工具。</p>



<p>在系列文章的下一篇，我們將進一步探討如何將這些 Agent 工具整合進企業級的工具鏈，包括 CI/CD 整合、權限管理和大規模部署策略。</p>
<div class="saboxplugin-wrap" itemtype="http://schema.org/Person" itemscope itemprop="author"><div class="saboxplugin-tab"><div class="saboxplugin-gravatar"><img alt='ㄚ槌' src='https://secure.gravatar.com/avatar/9914399915f96350f302945e8ddddee1a9b1995350182f513fd2e1fa816c100a?s=100&#038;d=mm&#038;r=g' srcset='https://secure.gravatar.com/avatar/9914399915f96350f302945e8ddddee1a9b1995350182f513fd2e1fa816c100a?s=200&#038;d=mm&#038;r=g 2x' class='avatar avatar-100 photo' height='100' width='100' itemprop="image"/></div><div class="saboxplugin-authorname"><a href="https://blog.che-ya.com/author/a3230230/" class="vcard author" rel="author"><span class="fn">ㄚ槌</span></a></div><div class="saboxplugin-desc"><div itemprop="description"></div></div><div class="saboxplugin-web "><a href="https://blog.che-ya.com" target="_self" >blog.che-ya.com</a></div><div class="clearfix"></div></div></div><p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fblog.che-ya.com%2Fagent-sdk-automation-tools%2F&amp;linkname=%E5%AF%A6%E6%88%B0%E5%B0%88%E6%A1%88%EF%BC%9A%E7%94%A8%20Agent%20SDK%20%E6%89%93%E9%80%A0%E8%87%AA%E5%8B%95%E5%8C%96%E5%B7%A5%E5%85%B7" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_line" href="https://www.addtoany.com/add_to/line?linkurl=https%3A%2F%2Fblog.che-ya.com%2Fagent-sdk-automation-tools%2F&amp;linkname=%E5%AF%A6%E6%88%B0%E5%B0%88%E6%A1%88%EF%BC%9A%E7%94%A8%20Agent%20SDK%20%E6%89%93%E9%80%A0%E8%87%AA%E5%8B%95%E5%8C%96%E5%B7%A5%E5%85%B7" title="Line" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_x" href="https://www.addtoany.com/add_to/x?linkurl=https%3A%2F%2Fblog.che-ya.com%2Fagent-sdk-automation-tools%2F&amp;linkname=%E5%AF%A6%E6%88%B0%E5%B0%88%E6%A1%88%EF%BC%9A%E7%94%A8%20Agent%20SDK%20%E6%89%93%E9%80%A0%E8%87%AA%E5%8B%95%E5%8C%96%E5%B7%A5%E5%85%B7" title="X" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fblog.che-ya.com%2Fagent-sdk-automation-tools%2F&#038;title=%E5%AF%A6%E6%88%B0%E5%B0%88%E6%A1%88%EF%BC%9A%E7%94%A8%20Agent%20SDK%20%E6%89%93%E9%80%A0%E8%87%AA%E5%8B%95%E5%8C%96%E5%B7%A5%E5%85%B7" data-a2a-url="https://blog.che-ya.com/agent-sdk-automation-tools/" data-a2a-title="實戰專案：用 Agent SDK 打造自動化工具"></a></p>]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
