速查卡 · 全本一頁
← 回 7 分頁總覽 · 此頁是 7 個分頁合併版,給想 A4 一次印全本 的人用。日常瀏覽建議用分頁版(左 sidebar 列表)。
列印:Cmd-P / Ctrl-P → A4 → 縮放 75-90%。
@media print自動隱藏 nav / sidebar / 編輯連結。
速查卡 · CLI / CLAUDE.md / Git
Claude Code CLI 核心指令
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 # 強制 exitCLAUDE.md 推薦結構
# 專案規則
## 角色
- 你是 X 工程師,負責 Y。
## 風格
- 繁中、簡潔、不廢話
- 改 code 前先說「我要 X」
## 規則
- 不 force push
- 不 commit 不 review 過的 secret
- 改 prod 前要 confirmation
## 不要
- 不要寫測試(這個專案沒測試框架)
- 不要重構(保持 minimal change)
## 工具
- 我們用 X 不用 YGit / Repo conventions
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, 不要去 webCommit 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 速查
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)
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.1SDK 速比較 (function calling)
# 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)
| Model | Input | Output |
|---|---|---|
| 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)。
章節練習推薦 model + 預估 cost
跑 AgentZ 章節練習時最划算的 model 配置(用 Haiku 走完幾乎不到 $5):
| 章 | 練習類型 | 推薦 model | 預估 cost / 跑 |
|---|---|---|---|
| Ch 1-3 | 概念 prompt 試 | Haiku 4.5 / Gemini Flash | < $0.01 |
| Ch 4-5 | Claude Code real task | Sonnet 4.6 (Claude Code default) | $0.05-0.15 |
| Ch 6 | MCP server hello world | Haiku 4.5 | < $0.01 |
| Ch 7 | Skill 觸發測試 | Sonnet 4.6 | $0.02-0.05 |
| Ch 8 | cost cap 測試 | Haiku 4.5(故意設低 cap) | < $0.05 |
| Ch 9 | function call loop | Haiku 4.5 | < $0.02 |
| Ch 10 | ReAct + Reflection | Sonnet 4.6(Reflection 需穩定) | $0.05-0.20 |
| Ch 11 | framework 4 對照 | Haiku 4.5 | $0.05-0.10 |
| Ch 12 | mini framework 自寫 | Sonnet 4.6 | $0.10-0.30 |
| Ch 13 | session memory + RAG | Haiku 4.5 + embedding | $0.05-0.15 |
| Ch 14 | multi-agent 3 架構 | Sonnet (supervisor) + Haiku (worker) | $0.10-0.40 |
| Ch 15 | V3 governance 完整 case | 自選(建 cost cap 練習) | $0.30-1.00 |
| Ch 16 | research agent + DOI 驗證 | Sonnet 4.6(防幻覺) | $0.20-0.50 |
| Ch 17 | SFT / GRPO 訓練 | GPU local(非 API) | GPU 電費 |
| Ch 18 | menubar / 個人助理 | 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 範式
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 範式
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
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
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下一頁 → MCP / Skills · Governance · CLI / Git · SDK · Pricing
速查卡 · MCP / Skills
MCP Server boilerplate (Python FastMCP)
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):
{
"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)
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、移動滑鼠、打字:
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 太擠時,把子任務外包:
# 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):
.claude/skills/<name>/SKILL.md(project)~/.claude/skills/<name>/SKILL.md(user)- Plugin marketplace 提供的(裝過後)
要 trigger 一個 skill:description 寫得清楚(< 150 字、含「何時用」),LLM 才會在 conversation 看到正確 signal 後自己叫起來。
Skill (SKILL.md) 標準格式
---
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)模型路由建議
| 任務類型 | 推薦 model |
|---|---|
| Routing / 分類 | Haiku 4.5 / Gemini Flash |
| 摘要 / 整理 | Haiku 4.5 / GPT-4o-mini |
| Coding 修 bug | Sonnet 4.6 / DeepSeek V3 |
| Coding 大重構 | Opus 4.7 |
| 數學 / 推理 | DeepSeek R1 / Opus 4.7 / o1 |
| Embedding | text-embedding-3-small (OpenAI) / cohere multilingual |
| Function calling | Haiku 4.5 (cost) / Sonnet 4.6 (穩) |
| Multi-agent supervisor | Sonnet 4.6 |
| Multi-agent worker | Haiku 4.5 |
| Reviewer / Critic | Sonnet 4.6 |
| Long context (>200K) | Sonnet 4.6 (1M) / Gemini 1.5 Pro (2M) |
Anti-hallucination 5 條 (Researcher 用)
- 每 citation 必經 DOI / arxiv ID verify
- 區分 [直接引用] vs [基於 X 推論] vs [推測]
- Reflection critique 必含 fact-check
- 多 source 對比 (≥ 2 source 才寫進報告)
- uncertainty 標記 (不確定日期就只標年份)
下一頁 → CLI / Git · SDK · Pricing · Patterns · MCP / Skills
速查卡 · 合規 + 國際標準
agent 上 production 前必確認的 4 個合規標準。不是法律建議——細節找律師確認。
4 大標準 1 分鐘版
| 標準 | 出品 | 性質 | 強制? |
|---|---|---|---|
| ISO/IEC 42001 | ISO + IEC | AI Management System 認證 | voluntary,但 2026 EU RFP 40% 詢問 |
| NIST AI RMF + GenAI Profile | NIST (美) | Risk Management 4 function 框架 | voluntary,美國公部門 procurement 必查 |
| EU AI Act | 歐盟 | 法律,high-risk system enforcement | 強制 2026-08-02 起(trilogue 可能延至 2027-12) |
| OpenTelemetry GenAI | CNCF | observability 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 functions:GOVERN / 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 強制
| 風險等級 | 範例 | 義務 | 罰款上限 |
|---|---|---|---|
| Prohibited | social scoring / subliminal manipulation | 禁止 | €35M 或 7% 全球年營業 |
| High-risk (Annex III 8 domain) | biometric / critical infra / education / employment / credit / law enforcement / migration / justice | audit trail + risk assessment + EU database 註冊 + 強制 human oversight | €15M 或 3% |
| Transparency | chatbot / 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.*命名規約。 - 核心 span:
invoke_agent {gen_ai.agent.name}/chat {model}/embeddings。 - 核心 metric:
gen_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):
pip install opentelemetry-instrumentation-anthropicfrom opentelemetry.instrumentation.anthropic import AnthropicInstrumentor
AnthropicInstrumentor().instrument()
# agent code 跑下去、span 自動 export 到 OTel collector何時該做哪個?決策樹
你在做的 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