1. 專案定位
anthropic-cli(指令名稱 ant)是 Anthropic 官方為 Claude Developer Platform 提供的命令列工具。它直接對接 Claude API,讓開發者能從終端機發送 Messages、管理認證、操作 Beta 資源(Sessions / Agents / Environments / Vaults / Skills / Memory Stores),以及執行 self-hosted worker 輪詢工作。
核心價值:
- 官方維護:由 Anthropic 與 Stainless 聯合維護,API 規格變更時 CLI 同步更新
- 資源導向架構:
ant [resource] <command>結構直覺、與 API 文件一一對應 - 多認證模式:API Key / OAuth PKCE / OIDC Federation 三軌並行
- 跨平台:Go 單一 binary,goreleaser 產出 macOS / Linux / Windows 安裝包
- 自動補全:bash / zsh / fish 三種 shell 完整支援
適合誰?
- 需要從 CLI 快速測試 Claude API 的開發者
- 在 CI/CD 管線中整合 Claude API 呼叫的 DevOps 工程師
- 需要 self-hosted 環境管理的企業團隊
- 想用 OIDC Federation 做 keyless auth 的 K8s / GitHub Actions 使用者
2. 安裝指南
2.1 Homebrew(推薦,macOS / Linux)
1brew install anthropics/tap/ant
2ant --version
2.2 Go Install(需 Go 1.22+)
1go install 'github.com/anthropics/anthropic-cli/cmd/ant@latest'
2
3# 確認 Go bin 在 PATH 中
4export PATH="$PATH:$(go env GOPATH)/bin"
5ant --version
2.3 從原始碼建置
1git clone https://github.com/anthropics/anthropic-cli.git
2cd anthropic-cli
3./scripts/bootstrap # 安裝 Go 依賴
4./scripts/build # 產出 ./ant binary
5./ant --version
6
7# 或直接用 scripts/run(不產出 binary)
8./scripts/run messages create --help
2.4 環境變數設定
1# 必要:至少設定一種認證方式
2export ANTHROPIC_API_KEY="sk-ant-..."
3
4# 選用:OAuth token(ant auth login 會自動處理)
5export ANTHROPIC_AUTH_TOKEN="..."
6
7# 選用:Webhook 驗證
8export ANTHROPIC_WEBHOOK_SIGNING_KEY="..."
3. 核心架構解析
graph TD
subgraph "使用者端"
USER[使用者 Terminal]
end
subgraph "cmd/ant/main.go"
ENTRY[main.go 入口]
AUTOCOMPLETE[Shell 自動補全]
end
subgraph "pkg/cmd/ — 指令層"
CMD_ROOT["cmd.go
Command 根節點
全域 flags"]
CMD_AUTH["cmd_auth.go
OAuth PKCE 登入/登出
Token 管理"]
CMD_PROFILE["cmd_profile.go
多 Profile 管理"]
CMD_MSG["message.go
Messages API
count-tokens"]
CMD_BETA["beta*.go
Sessions / Agents
Environments / Vaults
Skills / Memory / Files"]
CMD_WORKER["worker.go
Self-Hosted Worker
poll / run"]
EXTRAS["extras.go
Federation flags
手寫擴充"]
CMDUTIL["cmdutil.go
Auth 決策邏輯
輸出格式化
Debug middleware"]
end
subgraph "internal/ — 內部工具"
APIFORM[apiform — 表單序列化]
APIQUERY[apiquery — 查詢參數]
REQFLAG[requestflag — CLI flag 綁定]
BPARAM[binaryparam — 檔案參數處理]
JSONVIEW[jsonview — JSON 視覺化]
DEBUGMW[debugmiddleware — HTTP 偵錯]
SHELL[autocomplete — Shell 補全腳本]
end
subgraph "外部依賴"
SDK["anthropic-sdk-go v1.46.0"]
URFAVE["urfave/cli/v3"]
BUBBLE["charmbracelet/bubbletea"]
GJSON["tidwall/gjson"]
end
USER --> ENTRY
ENTRY --> CMD_ROOT
ENTRY --> AUTOCOMPLETE
CMD_ROOT --> CMD_AUTH
CMD_ROOT --> CMD_PROFILE
CMD_ROOT --> CMD_MSG
CMD_ROOT --> CMD_BETA
CMD_ROOT --> CMD_WORKER
CMD_ROOT --> EXTRAS
CMD_MSG --> CMDUTIL
CMD_BETA --> CMDUTIL
CMD_WORKER --> CMDUTIL
CMDUTIL --> SDK
CMD_ROOT --> URFAVE
CMDUTIL --> GJSON
CMD_AUTH --> BUBBLE
CMDUTIL --> APIFORM
CMDUTIL --> APIQUERY
CMD_ROOT --> REQFLAG
CMD_MSG --> BPARAM
CMDUTIL --> JSONVIEW
CMDUTIL --> DEBUGMW
AUTOCOMPLETE --> SHELL
3.1 程式碼產出模式
本專案有一個獨特的「codegen + 手寫」混合架構:
- Codegen 產出:
pkg/cmd/下大部分beta*.go、message.go、completion.go等檔案由 Stainless 從 OpenAPI spec 自動產出。檔案開頭都有// File generated from our OpenAPI spec by Stainless.標記 - 手寫擴充:
extras.go(Federation flags)、cmd_auth.go(OAuth PKCE)、cmd_profile.go(Profile 管理)、worker.go(Self-Hosted Worker)、suggest.go(Jaro-Winkler 拼字建議)是手寫代碼
3.2 認證決策流程
flowchart TD
START[收到 API 請求] --> CHECK_FLAG{"--api-key 或
ANTHROPIC_API_KEY?"}
CHECK_FLAG -->|有| USE_APIKEY[使用 API Key]
CHECK_FLAG -->|無| CHECK_AUTH{"--auth-token 或
ANTHROPIC_AUTH_TOKEN?"}
CHECK_AUTH -->|有| USE_AUTH[使用 Auth Token]
CHECK_AUTH -->|無| CHECK_FED{"--identity-token
或 Federation 設定?"}
CHECK_FED -->|有| USE_FED[走 OIDC Federation
自動 mint token]
CHECK_FED -->|無| CHECK_PROFILE{Profile 有
saved credential?}
CHECK_PROFILE -->|有| USE_PROFILE[使用 Profile 憑證
自動 refresh]
CHECK_PROFILE -->|無| ERROR[錯誤:無可用憑證]
3.3 指令結構速覽
| 資源 | 子指令 | 說明 |
|---|---|---|
messages | create, count-tokens | Messages API 核心 |
completions | create | Legacy Text Completions |
message-batches | create, list, retrieve, cancel, results | 批次處理 |
models | list, retrieve | 模型清單與資訊 |
auth | login, logout, status | OAuth PKCE 認證 |
profile | activate, list, get, set | 多帳號管理 |
beta:sessions | create, retrieve, update, list, delete | 多輪 Session |
beta:agents | create, retrieve, update, list, delete | Agent 管理 |
beta:environments | create, retrieve, update, list, delete | 環境管理 |
beta:vaults | list, credentials | Vault 密鑰儲存 |
beta:skills | CRUD + versions | Skill 管理 |
beta:memory-stores | CRUD + memories | 記憶管理 |
beta:files | upload, retrieve, list, delete | 檔案操作 |
beta:worker | poll, run | Self-Hosted Worker |
4. 常用操作詳解
4.1 發送訊息
1# 基本用法
2ant messages create \
3 --model claude-sonnet-4-5-20250929 \
4 --max-tokens 1024 \
5 --message '{"content": [{"text": "Hello Claude", "type": "text"}], "role": "user"}'
6
7# 帶 system prompt
8ant messages create \
9 --model claude-sonnet-4-5-20250929 \
10 --max-tokens 2048 \
11 --system "You are a helpful coding assistant." \
12 --message '{"content": [{"text": "Write a Python hello world", "type": "text"}], "role": "user"}'
13
14# Streaming 模式
15ant messages create \
16 --model claude-sonnet-4-5-20250929 \
17 --max-tokens 1024 \
18 --stream \
19 --message '{"content": [{"text": "Explain quantum computing", "type": "text"}], "role": "user"}'
20
21# 計算 token 數
22ant messages count-tokens \
23 --model claude-sonnet-4-5-20250929 \
24 --message '{"content": [{"text": "Count my tokens", "type": "text"}], "role": "user"}'
4.2 傳送檔案
1# 自動偵測格式(binary→base64 / text→string)
2ant messages create \
3 --model claude-sonnet-4-5-20250929 \
4 --max-tokens 1024 \
5 --message '{"content": [{"type": "image", "source": {"data": "@photo.jpg"}}], "role": "user"}'
6
7# 強制 base64 編碼
8ant messages create --arg @data://binary-file.dat
9
10# 強制字串編碼
11ant messages create --arg @file://text-file.txt
4.3 認證管理
1# OAuth 登入(開瀏覽器走 PKCE 流程)
2ant auth login
3
4# 登入到指定 profile
5ant auth login --profile work
6
7# 查看認證狀態
8ant auth status
9
10# 登出
11ant auth logout
4.4 Profile 管理
1# 列出所有 profile
2ant profile list
3
4# 切換 active profile
5ant profile activate work
6
7# 查看 profile 設定
8ant profile get
9
10# 設定 profile 欄位
11ant profile set api_key sk-ant-...
12ant profile set base_url https://custom-api.example.com
4.5 輸出格式與轉換
1# JSON 格式輸出
2ant messages create ... --format json
3
4# YAML 格式
5ant messages create ... --format yaml
6
7# GJSON 轉換——只取 content 欄位
8ant messages create ... --transform "content"
9
10# 只看 error 內容
11ant messages create ... --format-error json --transform-error "error.message"
12
13# 互動式瀏覽(explore 模式)
14ant messages create ... --format explore
4.6 Self-Hosted Worker
1# 設定環境變數
2export ANTHROPIC_ENVIRONMENT_ID="env_..."
3export ANTHROPIC_ENVIRONMENT_KEY="envkey_..."
4
5# 啟動長輪詢 worker
6ant beta:worker poll \
7 --environment-id "$ANTHROPIC_ENVIRONMENT_ID" \
8 --environment-key "$ANTHROPIC_ENVIRONMENT_KEY"
9
10# 執行單一工作項目
11ant beta:worker run \
12 --environment-id "$ANTHROPIC_ENVIRONMENT_ID" \
13 --environment-key "$ANTHROPIC_ENVIRONMENT_KEY" \
14 --work-id "work_..."
4.7 OIDC Federation(Keyless Auth)
1# GitHub Actions 用法
2ant messages create \
3 --identity-token "$ACTIONS_ID_TOKEN_REQUEST_TOKEN" \
4 --federation-rule "fdrl_..." \
5 --organization-id "org-uuid" \
6 --model claude-sonnet-4-5-20250929 \
7 --max-tokens 1024 \
8 --message '...'
9
10# K8s Projected SA Token
11ant messages create \
12 --identity-token-file /var/run/secrets/tokens/anthropic \
13 --federation-rule "fdrl_..." \
14 --model claude-sonnet-4-5-20250929 \
15 --max-tokens 1024 \
16 --message '...'
5. 應用場景
5.1 CI/CD 管線整合
在 GitHub Actions 中使用 ant 做自動化程式碼審查:
1jobs:
2 review:
3 runs-on: ubuntu-latest
4 steps:
5 - uses: actions/checkout@v4
6 - name: Install ant
7 run: go install 'github.com/anthropics/anthropic-cli/cmd/ant@latest'
8 - name: Review PR diff
9 env:
10 ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
11 run: |
12 git diff origin/main...HEAD | ant messages create \
13 --model claude-sonnet-4-5-20250929 \
14 --max-tokens 4096 \
15 --system "Review this code diff. List issues and suggestions." \
16 --message "{\"content\": [{\"text\": \"$(cat)\", \"type\": \"text\"}], \"role\": \"user\"}"
5.2 批次處理大量請求
1# 建立 batch
2ant message-batches create \
3 --requests '[{"custom_id": "req1", "params": {"model": "claude-sonnet-4-5-20250929", "max_tokens": 1024, "messages": [{"role": "user", "content": "Hello"}]}}]'
4
5# 查詢狀態
6ant message-batches retrieve --id "batch_..."
7
8# 取得結果
9ant message-batches results --id "batch_..."
5.3 開發者日常快速測試
1# 快速問答(配合 alias)
2alias ask='ant messages create --model claude-sonnet-4-5-20250929 --max-tokens 2048 --message'
3
4ask '{"content": [{"text": "What is the capital of France?", "type": "text"}], "role": "user"}' \
5 --transform "content.0.text"
6. 資安掃描報告
掃描範圍
cmd/ant/— 主程式入口pkg/cmd/— 所有指令實作(含認證、API 呼叫、Worker)internal/— 內部工具模組scripts/— 建置 / 測試 / mock 腳本
掃描結果
| 風險等級 | 類別 | 位置 | 說明 |
|---|---|---|---|
| 🟢 低 | exec 使用 | pkg/cmd/worker.go:191 | exec.CommandContext 執行使用者指定 script — 但僅在 self-hosted worker 情境,需先認證 |
| 🟢 低 | exec 使用 | pkg/cmd/cmd_auth.go:1507 | exec.Cmd 開啟瀏覽器 — 標準 OAuth 流程 |
| 🟢 低 | exec 使用 | pkg/cmd/cmdutil.go:348,352 | exec.LookPath + exec.Command 啟動 pager — 標準 CLI 行為 |
| 🟢 低 | HTTP 連線 | pkg/cmd/cmd_auth.go:1425 | localhost callback listener — OAuth PKCE 標準模式 |
| 🟢 低 | 檔案寫入 | pkg/cmd/cmdutil.go:426,458 | 下載回應寫入檔案 / 建立暫存檔 — 用於 --outfile 功能 |
| 🟢 低 | 憑證處理 | pkg/cmd/cmd.go:83-91 | API Key / Auth Token / Webhook Key 從 env 讀取 — 標準做法 |
| 🟢 低 | curl 使用 | scripts/ | mock server 健康檢查 + artifact 上傳 — 僅開發/CI 用 |
| 🟢 低 | 硬編碼 URL | pkg/cmd/cmd_auth.go:31-32 | platform.claude.com / api.anthropic.com — 官方端點 |
整體評估
🟢 低風險 — 本專案為 Anthropic 官方維護、MIT 授權,程式碼品質高。無發現硬編碼密鑰、不安全的 eval/exec 模式、或敏感資料洩漏。所有 exec 呼叫都在合理的使用情境下(開瀏覽器、pager、worker script),且 worker 需認證後才能觸發。憑證全部走環境變數或 profile 檔案,不在程式碼中明文出現。
建議:
- 使用時確保
ANTHROPIC_API_KEY不寫入版本控制 - Worker 的
--environment-key應透過 secrets manager 注入 - Federation 模式下定期輪換 OIDC token
7. FAQ
Q1: ant 和 Claude Code CLI 是同一個東西嗎?
不是。ant 是 Claude Developer Platform 的 CLI,直接對接 Messages API / Batches / Sessions 等 HTTP API。Claude Code 是 Anthropic 的 AI 編程助手 CLI(claude 指令),用於互動式程式碼編寫。兩者互補但功能不重疊。
Q2: 我已經有 Python SDK / TypeScript SDK,還需要 ant 嗎?
如果你的需求是快速測試 API、在 shell script / CI 中呼叫 Claude、或管理多個 profile 的認證,ant 比寫程式更快。如果是在應用程式中整合,SDK 更合適。
Q3: 為什麼 README 說需要 Go 1.22,但 go.mod 寫 Go 1.25?
go.mod 的版本是建置需求。Issue #52 正在追蹤此文件不一致。建議直接用 Go 1.25+ 以確保相容。
Q4: --format explore 是什麼?
互動式 JSON 瀏覽模式,使用 charmbracelet/bubbletea 實作的 TUI。可以用方向鍵展開 / 收合 JSON 節點,適合探索大型回應。
Q5: 如何在沒有 API Key 的情況下使用?
三種方式:(1) ant auth login 走 OAuth PKCE,不需要 API Key;(2) OIDC Federation,適合 CI/CD;(3) Profile 中有 saved token 就能直接用。
8. 進階技巧
8.1 Debug 模式
1# 顯示完整 HTTP 請求 / 回應
2ant messages create ... --debug
會印出包含 headers、body 的完整 HTTP 往返,適合排除 API 錯誤。
8.2 自訂 Base URL
1# 指向代理或自建服務
2ant messages create ... --base-url https://my-proxy.example.com
3
4# 或用環境變數
5export ANTHROPIC_BASE_URL=https://my-proxy.example.com
8.3 GJSON 進階轉換
1# 取得第一個 content block 的 text
2ant messages create ... --transform "content.0.text"
3
4# 取得所有 content block 的 type
5ant messages create ... --transform "content.#.type"
6
7# 條件篩選
8ant messages create ... --transform 'content.#(type=="text").text'
8.4 Shell Completion 手動安裝
1# Bash
2ant @completion bash > /etc/bash_completion.d/ant
3
4# Zsh
5ant @completion zsh > "${fpath[1]}/_ant"
6
7# Fish
8ant @completion fish > ~/.config/fish/completions/ant.fish
8.5 連結本地 SDK 開發
1# 連結到本地 SDK 目錄
2./scripts/link ../anthropic-go
3
4# 連結到指定版本
5./scripts/link github.com/anthropics/anthropic-sdk-go@v1.46.0
6
7# 取消連結
8./scripts/unlink
9. 整合進其他工作流
9.1 與 jq 搭配
1# 只取回覆文字
2ant messages create ... --format json | jq -r '.content[0].text'
3
4# 取 usage 統計
5ant messages create ... --format json | jq '.usage'
9.2 與 pipe 搭配
1# 將檔案內容送入 Claude
2cat code.py | ant messages create \
3 --model claude-sonnet-4-5-20250929 \
4 --max-tokens 4096 \
5 --system "Review this code" \
6 --message "{\"content\": [{\"text\": \"$(cat)\", \"type\": \"text\"}], \"role\": \"user\"}"
9.3 與 Makefile 整合
1.PHONY: review
2review:
3 @git diff --staged | ant messages create \
4 --model claude-sonnet-4-5-20250929 \
5 --max-tokens 4096 \
6 --format json \
7 --transform "content.0.text" \
8 --system "Review this diff. Be concise." \
9 --message '{"content": [{"text": "$(shell git diff --staged)", "type": "text"}], "role": "user"}'
10. 重點摘要 Checklist
- 安裝:
brew install anthropics/tap/ant或go install - 認證:設定
ANTHROPIC_API_KEY或ant auth login - 指令結構:
ant [resource] <command> [flags] - 核心指令:
ant messages create(發訊息)、ant messages count-tokens(計 token) - 輸出格式:
--format json/yaml/pretty/explore+--transformGJSON - 檔案傳入:
@myfile.ext自動偵測、@data://base64、@file://text - 多帳號:
ant profile list/activate/get/set - Debug:
--debug印出完整 HTTP - Federation:
--identity-token+--federation-rule做 keyless auth - Worker:
ant beta:worker poll跑 self-hosted 環境 - Shell 補全:
ant @completion bash/zsh/fish - 資安:🟢 低風險,官方維護,MIT 授權
11. 進一步閱讀
- 官方文件 — ant 完整指令參考
- Claude API 文件 — Messages API 規格
- anthropic-sdk-go — ant 底層 Go SDK
- urfave/cli v3 — CLI 框架文件
- tidwall/gjson SYNTAX —
--transform查詢語法 - goreleaser — 跨平台建置工具
- OIDC Federation 說明 — Keyless 認證設定
Comments