Skip to content

速查卡 · 全本一頁

← 回 7 分頁總覽 · 此頁是 7 個分頁合併版,給想 A4 一次印全本 的人用。日常瀏覽建議用分頁版(左 sidebar 列表)。

列印:Cmd-P / Ctrl-P → A4 → 縮放 75-90%。@media print 自動隱藏 nav / sidebar / 編輯連結。


速查卡 · CLI / CLAUDE.md / Git

← 回速查卡總覽

Claude Code CLI 核心指令

bash
claude                          # 起 Claude Code
claude --plan                   # 開 plan mode 起手
/help                           # 看 builtin commands
/cost                           # 看當前 session 花費
/mcp                            # MCP server status
/skill                          # 看 / 切換 skill
/clear                          # 清 context
/resume                         # 從上次 session 接著跑
/exit                           # 離開
Esc                             # 中斷當前 task
Ctrl-C × 2                      # 強制 exit

CLAUDE.md 推薦結構

markdown
# 專案規則

## 角色
- 你是 X 工程師,負責 Y。

## 風格
- 繁中、簡潔、不廢話
- 改 code 前先說「我要 X」

## 規則
- 不 force push
- 不 commit 不 review 過的 secret
- 改 prod 前要 confirmation

## 不要
- 不要寫測試(這個專案沒測試框架)
- 不要重構(保持 minimal change)

## 工具
- 我們用 X 不用 Y

Git / Repo conventions

bash
git checkout -b <type>/<feature>     # type: feat / fix / docs / chore
# 寫 code
git add <specific files>             # 不要用 git add -A
git commit -m "feat: add ..."        # imperative mood
git push origin <branch>
gh pr create --title "..." --body "..."  # 用 gh, 不要去 web

Commit message 範例:

feat: add cost cap fail-closed pattern (Ch 8)

- DAILY_CAP env var
- pre-flight check today's total
- raise CostCapExceeded with explicit message

Closes #42

下一頁SDK 速查 · Pricing · Patterns · MCP / Skills · Governance


速查卡 · SDK

← 回速查卡總覽

Anthropic SDK 速查

python
import anthropic
client = anthropic.Anthropic()  # 自動讀 ANTHROPIC_API_KEY

# 基本 call
resp = client.messages.create(
    model="claude-haiku-4-5",  # or sonnet-4-6, opus-4-7
    max_tokens=1000,
    system="你是 X",
    messages=[
        {"role": "user", "content": "..."},
    ],
)
print(resp.content[0].text)
print(f"in={resp.usage.input_tokens} out={resp.usage.output_tokens}")

# Tool use
resp = client.messages.create(
    model="claude-haiku-4-5",
    max_tokens=1000,
    tools=[{
        "name": "get_weather",
        "description": "Get current weather for a city.",
        "input_schema": {
            "type": "object",
            "properties": {"city": {"type": "string"}},
            "required": ["city"],
        },
    }],
    messages=[{"role": "user", "content": "Tokyo 天氣"}],
)

# 處理 stop_reason
if resp.stop_reason == "tool_use":
    for block in resp.content:
        if block.type == "tool_use":
            result = my_tool(**block.input)  # 真跑 tool
            # 把 tool_result 加回 messages, 再 call 一次
elif resp.stop_reason == "end_turn":
    # 結束,最終 text 是 resp.content[0].text
    pass

# Streaming
with client.messages.stream(model=..., max_tokens=..., messages=[...]) as s:
    for delta in s.text_stream:
        print(delta, end="", flush=True)

Prompt Cache (省 90% cost)

python
resp = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1000,
    system=[
        {
            "type": "text",
            "text": "<長 system prompt 5K+ token>",
            "cache_control": {"type": "ephemeral"},  # cache 1 hr
        },
    ],
    messages=[...],
    extra_headers={"anthropic-beta": "prompt-caching-2024-07-31"},
)
# cache hit: input cost × 0.1

SDK 速比較 (function calling)

python
# Anthropic
resp.content                 # list of blocks
block.type == "tool_use"     # tool block
block.input                  # dict (parsed)
block.id                     # for tool_result reference

# OpenAI
resp.choices[0].message.tool_calls  # list
tc.function.name             # string
tc.function.arguments        # JSON string (要 json.loads)
tc.id                        # for tool_result reference

# Gemini (google-generativeai)
resp.candidates[0].content.parts
part.function_call.name
part.function_call.args      # proto.MapComposite

下一頁Pricing · Patterns · MCP / Skills · Governance · CLI / Git


速查卡 · Pricing

← 回速查卡總覽

Pricing (2026-05 snapshot, USD / 1M token)

ModelInputOutput
Haiku 4.5$1$5
Sonnet 4.6$3$15
Opus 4.7$15$75
GPT-4o-mini$0.15$0.60
GPT-4o$2.5$10
Gemini 2.5 Flash$0.30$2.50
Gemini 2.5 Pro$1.25-2.50$10-15
Groq Llama 3.3 70B$0.59$0.79
DeepSeek V3$0.27$1.10
DeepSeek R1$0.55$2.19
Mistral Large$2$6

Cache hit:input × 10% (Anthropic / OpenAI 都有)。 Cache write:input × 1.25x(首次寫的 overhead)。

完整 LLM 申請指南 →


章節練習推薦 model + 預估 cost

跑 AgentZ 章節練習時最划算的 model 配置(用 Haiku 走完幾乎不到 $5):

練習類型推薦 model預估 cost / 跑
Ch 1-3概念 prompt 試Haiku 4.5 / Gemini Flash< $0.01
Ch 4-5Claude Code real taskSonnet 4.6 (Claude Code default)$0.05-0.15
Ch 6MCP server hello worldHaiku 4.5< $0.01
Ch 7Skill 觸發測試Sonnet 4.6$0.02-0.05
Ch 8cost cap 測試Haiku 4.5(故意設低 cap)< $0.05
Ch 9function call loopHaiku 4.5< $0.02
Ch 10ReAct + ReflectionSonnet 4.6(Reflection 需穩定)$0.05-0.20
Ch 11framework 4 對照Haiku 4.5$0.05-0.10
Ch 12mini framework 自寫Sonnet 4.6$0.10-0.30
Ch 13session memory + RAGHaiku 4.5 + embedding$0.05-0.15
Ch 14multi-agent 3 架構Sonnet (supervisor) + Haiku (worker)$0.10-0.40
Ch 15V3 governance 完整 case自選(建 cost cap 練習)$0.30-1.00
Ch 16research agent + DOI 驗證Sonnet 4.6(防幻覺)$0.20-0.50
Ch 17SFT / GRPO 訓練GPU local(非 API)GPU 電費
Ch 18menubar / 個人助理Haiku 4.5$0.05-0.20 / 天

全本走完總估:< $10 USD(Haiku 為主、Sonnet 必要時切);省更多用 Groq Llama 3.3 70B / Gemini Flash 免費 tier。


下一頁Patterns · MCP / Skills · Governance · CLI / Git · SDK


速查卡 · Patterns

← 回速查卡總覽

ReAct loop 範式

python
def react_loop(goal, max_steps=5):
    history = []
    for _ in range(max_steps):
        thought = llm(prompt=f"Goal: {goal}\nHistory: {history}\nNext?")
        if "FINAL_ANSWER:" in thought:
            return thought.split("FINAL_ANSWER:")[-1]
        # parse: tool_name, args = parse_tool(thought)
        # result = run_tool(tool_name, args)
        history.append({"thought": thought, "result": result})
    return "max steps reached"

Plan-and-Solve 範式

python
plan = llm("給定 task X,列 5 步驟 plan")
for step in plan:
    result = react_loop(step, max_steps=3)
    if not ok(result):
        plan = revise(plan, result)  # 中途 revise
final = llm("整合 results 寫 answer")

Reflection wrap

python
draft = base_agent(task)
for _ in range(max_reflect_iters := 3):
    critique = critic_agent(draft)
    if critique.verdict == "pass":
        break
    draft = base_agent(task, critique=critique)

Cost cap pattern

python
import sqlite3, datetime
conn = sqlite3.connect("costs.db")
conn.execute("""CREATE TABLE IF NOT EXISTS calls (
    call_id TEXT, ts TEXT, model TEXT,
    input_tokens INT, output_tokens INT, cost_usd REAL
)""")

DAILY_CAP = 0.10  # USD

def safe_call(**kwargs):
    today_total = conn.execute(
        "SELECT COALESCE(SUM(cost_usd), 0) FROM calls WHERE date(ts)=?",
        (datetime.date.today().isoformat(),)
    ).fetchone()[0]
    if today_total >= DAILY_CAP:
        raise CostCapExceeded(f"${today_total} >= ${DAILY_CAP}")
    resp = client.messages.create(**kwargs)
    # log usage
    conn.execute("INSERT INTO calls VALUES (?, ?, ?, ?, ?, ?)", ...)
    conn.commit()
    return resp

完整 Ch 8 starter →


下一頁MCP / Skills · Governance · CLI / Git · SDK · Pricing


速查卡 · MCP / Skills

← 回速查卡總覽

MCP Server boilerplate (Python FastMCP)

python
from mcp.server.fastmcp import FastMCP
import sys

mcp = FastMCP("my-server")

@mcp.tool()
def my_tool(arg: str) -> dict:
    """What this does. When to use.

    Args:
        arg: description
    """
    # 注意:不能 print 到 stdout!
    print(f"debug: {arg}", file=sys.stderr)
    return {"result": "..."}

if __name__ == "__main__":
    mcp.run(transport="stdio")

Claude Code 設定 (~/.config/claude/claude.json):

json
{
  "mcpServers": {
    "my-server": {
      "command": "uv",
      "args": ["--directory", "/abs/path", "run", "server.py"]
    }
  }
}

MCP scope 三層

  • user~/.config/claude/claude.json(全域)
  • project → repo 內 .mcp.json(git commit、團隊共用)
  • local → repo 內 .claude/claude.json(個人 key,.gitignore
bash
claude mcp add my-server --scope project ./server.py
claude mcp list                     # 看現有 server
claude mcp remove my-server         # 拔

Computer Use(Anthropic 2024-10+)

讓 Claude 看 screenshot、移動滑鼠、打字:

python
resp = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=4096,
    tools=[
        {"type": "computer_20241022", "name": "computer",
         "display_width_px": 1024, "display_height_px": 768},
        {"type": "text_editor_20241022", "name": "str_replace_editor"},
        {"type": "bash_20241022", "name": "bash"},
    ],
    messages=[{"role": "user", "content": "打開瀏覽器搜尋 'AgentZ'"}],
)
# 處理 tool_use blocks 跑 pyautogui / Selenium / xdotool

必 sandbox(不然它真的會 click 你的 email):Docker / VM / 隔離 user。範例容器:ghcr.io/anthropics/anthropic-quickstarts/computer-use-demo


Subagent / Task delegation

主 agent context 太擠時,把子任務外包:

python
# Anthropic Agent SDK (TypeScript)
import { query } from "@anthropic-ai/claude-agent-sdk";

for await (const msg of query({
  prompt: "Survey codebase 30 files, identify 5 hotspots",
  options: {
    agents: {
      "code-surveyor": {
        description: "Read-only file surveyor",
        prompt: "You are a thorough but fast code reader...",
        tools: ["Read", "Grep", "Glob"],
        model: "haiku",
      },
    },
  },
})) {
  console.log(msg);
}

Claude Code 用 builtin Task tool;自寫 agent 用 sub-messages.create call。


Skill auto-load 順序

Claude Code 啟動時依序找 SKILL.md 並讀 frontmatter(only Level 1 always-loaded):

  1. .claude/skills/<name>/SKILL.md(project)
  2. ~/.claude/skills/<name>/SKILL.md(user)
  3. Plugin marketplace 提供的(裝過後)

要 trigger 一個 skill:description 寫得清楚(< 150 字、含「何時用」),LLM 才會在 conversation 看到正確 signal 後自己叫起來。


Skill (SKILL.md) 標準格式

markdown
---
name: my-skill
description: 一句話 < 150 字, 涵蓋「何時用 + 做什麼」.
---

# Skill 名稱

## When to invoke
User says X / Y / Z.

## What you do
1. Step
2. Step
3. ...

## Constraints
- Never commit/push/delete without approval
- Use existing format

## Example
User: ...
You: ...

放在 .claude/skills/my-skill/ (project) 或 ~/.claude/skills/my-skill/ (user)。


下一頁Governance · CLI / Git · SDK · Pricing · Patterns


速查卡 · Governance

← 回速查卡總覽

V3 Governance pattern (Production agent)

每次 LLM call → 必走 4 道閘門:

1. Cost cap check    (pre-flight, fail-closed)
2. Tool sandbox      (allowed list, deny-by-default)
3. Audit log         (SQLite: ts/run_id/event/payload/cost)
4. Replay record     (input + output + model + temperature)

完整 V3 case →


模型路由建議

任務類型推薦 model
Routing / 分類Haiku 4.5 / Gemini Flash
摘要 / 整理Haiku 4.5 / GPT-4o-mini
Coding 修 bugSonnet 4.6 / DeepSeek V3
Coding 大重構Opus 4.7
數學 / 推理DeepSeek R1 / Opus 4.7 / o1
Embeddingtext-embedding-3-small (OpenAI) / cohere multilingual
Function callingHaiku 4.5 (cost) / Sonnet 4.6 (穩)
Multi-agent supervisorSonnet 4.6
Multi-agent workerHaiku 4.5
Reviewer / CriticSonnet 4.6
Long context (>200K)Sonnet 4.6 (1M) / Gemini 1.5 Pro (2M)

Anti-hallucination 5 條 (Researcher 用)

  1. 每 citation 必經 DOI / arxiv ID verify
  2. 區分 [直接引用] vs [基於 X 推論] vs [推測]
  3. Reflection critique 必含 fact-check
  4. 多 source 對比 (≥ 2 source 才寫進報告)
  5. uncertainty 標記 (不確定日期就只標年份)

完整 Ch 16 →


下一頁CLI / Git · SDK · Pricing · Patterns · MCP / Skills


速查卡 · 合規 + 國際標準

← 回速查卡總覽

agent 上 production 前必確認的 4 個合規標準。不是法律建議——細節找律師確認。


4 大標準 1 分鐘版

標準出品性質強制?
ISO/IEC 42001ISO + IECAI Management System 認證voluntary,但 2026 EU RFP 40% 詢問
NIST AI RMF + GenAI ProfileNIST (美)Risk Management 4 function 框架voluntary,美國公部門 procurement 必查
EU AI Act歐盟法律,high-risk system enforcement強制 2026-08-02 起(trilogue 可能延至 2027-12)
OpenTelemetry GenAICNCFobservability span / metric 命名規約technical convention,不是合規

ISO/IEC 42001:2023 — AI Management System

  • 是什麼:類似 ISO 9001 (品管) 之於 AI——「我有負責任做 AI 的管理流程」的認證。
  • 時程:6-12 個月全程。Stage 1 audit (scope / inventory / policy / risk 文件) → Stage 2 audit (運作測試 + sample use case) → 年度 surveillance。
  • 誰拿了:AWS / Microsoft Azure 已取得;Anthropic / OpenAI 走 SOC 2 + 自家 RSP 為主。
  • 何時申請:賣 enterprise(特別是 EU / 金融 / 政府)→ 2026 後 RFP 會問。

NIST AI RMF + GenAI Profile

  • 是什麼:美國 NIST 2023 發布的 4-function 流程框架。
  • 4 functionsGOVERN / MAP / MEASURE / MANAGE
  • GenAI Profile (NIST-AI-600-1, 2024-07):補 200+ actions 對 LLM;涵蓋 12 risks (CBRN info / Confabulation / Data privacy / InfoSec / IP / Toxicity-bias / Value chain 等)。
  • 何時用:美國公部門 vendor、銀行 OCC 監管、保險。
  • 正在發展:CSA (Cloud Security Alliance) 2026 在發展 Agentic Profile v1,把 agent 場景 risk 對映到 4 function。

EU AI Act — 2026-08-02 強制

風險等級範例義務罰款上限
Prohibitedsocial scoring / subliminal manipulation禁止€35M 或 7% 全球年營業
High-risk (Annex III 8 domain)biometric / critical infra / education / employment / credit / law enforcement / migration / justiceaudit trail + risk assessment + EU database 註冊 + 強制 human oversight€15M 或 3%
Transparencychatbot / deepfake / emotion AI必標「AI-bot」/「AI generated」€17M 或 4%
GPAI (General Purpose AI)foundation model (Claude / GPT / Gemini)technical doc + copyright policy + summary of training data依規模

8 high-risk domain(記下來):biometric / critical infrastructure / education / employment / credit & insurance / law enforcement / migration & border / justice & democratic process。

實作影響:用 agent 做 resume scanner → 自動 promote 為 high-risk → 開 audit trail + risk assessment + EU 註冊 + 強制 human oversight。對 EU 用戶必加「[AI-bot]」標籤(transparency)。


OpenTelemetry GenAI Semantic Conventions

  • 是什麼:CNCF OTel 為 LLM / agent 標準化的 gen_ai.* 命名規約。
  • 核心 spaninvoke_agent {gen_ai.agent.name} / chat {model} / embeddings
  • 核心 metricgen_ai.client.token.usage (histogram by direction) / gen_ai.client.operation.duration
  • 2026 狀態:spec experimental,multi-agent convention 在 CNCF SIG 發展中。
  • 已實作:Datadog LLM Observability / Uptrace / OpenLLMetry SDK / Grafana Tempo / Honeycomb。

5 分鐘上手(Python):

bash
pip install opentelemetry-instrumentation-anthropic
python
from opentelemetry.instrumentation.anthropic import AnthropicInstrumentor
AnthropicInstrumentor().instrument()
# agent code 跑下去、span 自動 export 到 OTel collector

Ch 15 §5a 完整講解 →


何時該做哪個?決策樹

你在做的 agent 是?
├─ 內部 demo / POC → 都不用做,先把 functional 跑起來
├─ 賣給 EU 客戶 / 公部門 RFP → ISO 42001 認證準備
├─ 賣給美國 federal / 銀行 → NIST AI RMF 流程文件
├─ 涉 8 high-risk domain → EU AI Act 強制 audit + EU 註冊
└─ 任何 production agent → OpenTelemetry GenAI (observability 基本盤)

完整講解

  • 3 大標準對 V3 / agentz_mini 影響Ch 15 §5b 合規對照 — V3 audit / replay / cost cap / intervention 4 pillar 怎麼對映 ISO/NIST/EU AI Act
  • OpenTelemetry GenAI 5 分鐘上手Ch 15 §5a + Ch 8 §3
  • Multi-agent 新興安全Ch 14 §8c — ICE / Consensus Trap / Slopsquatting 在 multi-agent 場景下的防禦(合規 audit 要看的 risk)
  • 詞條深入名詞表 · Production(4 個合規詞 4 欄解釋)+ 名詞表 · Agent §5(3 個 multi-agent 安全詞)
  • EU AI Act 倒數 → 2026-08-02 deadline,Ch 15 §3

⚠️ 不是法律建議。production 導入合規前找律師 / 顧問。台灣國科會 AI 基本法草案 2025-11 已公布、跟 EU AI Act 對齊(Ch 15 §5b.3)。


下一頁CLI / Git · SDK · Pricing · Patterns · MCP / Skills · Governance


← 回 7 分頁總覽 · 首頁 · GitHub

MIT License — 章節內容跟 starter code 都可以 copy 進你自己的商業專案