Skip to content

多 Agent 設定

當你在同一台機器上運行多個 AI Agent 時,可以共用同一個 AgentGazer Proxy,同時分開追蹤各 Agent 的用量。

問題

預設情況下,所有經過 Proxy 的請求都會歸到單一 Agent(啟動時用 --agent-id 設定的)。這對單一 Agent 沒問題,但當你:

  • 運行多個 Agent(例如 coding assistant + research assistant)
  • 想看每個 Agent 的成本明細
  • 需要為每個 Agent 設定不同的告警規則

就會有問題。

解法 1:路徑式 Agent ID(推薦)

在 URL 路徑中包含 agent ID:

typescript
// Coding assistant
const openai = new OpenAI({
  baseURL: "http://localhost:18900/agents/coding-assistant/openai/v1",
  apiKey: "dummy",
});

// Research assistant
const anthropic = new Anthropic({
  baseURL: "http://localhost:18900/agents/research-assistant/anthropic",
  apiKey: "dummy",
});

這種方式:

  • 適用於任何 SDK(不需要自訂 header)
  • Agent ID 在 logs 和 URL 中可見
  • 首次請求時自動建立 Agent

解法 2:URL 路徑中的 Agent 名稱

在 URL 路徑中加上 agent 名稱來識別每個 Agent:

typescript
const openai = new OpenAI({
  baseURL: "http://localhost:18900/agents/coding-assistant/agentgazer",
  apiKey: "dummy",
});

每個 Agent 有自己的 URL 路徑。Proxy 會分開記錄各 Agent 的指標。

範例:兩個 Agent 共用一個 Proxy

Agent 1:Coding Assistant

typescript
import OpenAI from "openai";

const openai = new OpenAI({
  baseURL: "http://localhost:18900/agents/coding-assistant/agentgazer",
  apiKey: "dummy",
});

// 所有請求歸到 "coding-assistant"
await openai.chat.completions.create({
  model: "gpt-4o",
  messages: [{ role: "user", content: "Write a function..." }],
});

Agent 2:Research Assistant

typescript
import Anthropic from "@anthropic-ai/sdk";

const anthropic = new Anthropic({
  baseURL: "http://localhost:18900/agents/research-assistant/agentgazer",
  apiKey: "dummy",
});

// 所有請求歸到 "research-assistant"
await anthropic.messages.create({
  model: "claude-sonnet-4-20250514",
  max_tokens: 1024,
  messages: [{ role: "user", content: "Summarize this paper..." }],
});

啟動 Proxy

bash
agentgazer start

不需要特殊參數 — x-agent-id header 會處理 Agent 識別。

Dashboard 顯示

這樣設定後,AgentGazer Dashboard 會顯示:

  • Agents 頁面coding-assistantresearch-assistant 作為獨立的 Agent 列出
  • 各 Agent 統計:每個 Agent 有自己的成本、Token 用量、延遲指標
  • 告警:可以為每個 Agent 設定不同的告警規則(例如 coding assistant 預算較高)

用 curl 測試

bash
curl http://localhost:18900/agents/my-custom-agent/openai \
  -H "Content-Type: application/json" \
  -d '{"model":"gpt-4o","messages":[{"role":"user","content":"Hi"}]}'

Python 用法

python
import openai

client = openai.OpenAI(
    base_url="http://localhost:18900/agents/python-agent/agentgazer",
    api_key="dummy",
)

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Hello"}],
)

Agent ID 優先順序

當同時使用多種識別方式時,Proxy 依照以下優先順序:

優先順序方式範例
1(最高)x-agent-id headerx-agent-id: my-agent
2路徑前綴/agents/my-agent/openai/...
3(最低)預設值啟動時設定或 "default"

何時使用哪種方式

情境建議
單一 Agent不需要 agent ID — 使用預設值
多個 Agent,SDK 支援 header使用 x-agent-id header
多個 Agent,SDK 不支援 header使用路徑式 /agents/{id}/...
OpenClaw(不支援自訂 header)使用路徑式 /agents/{id}/...
測試不同的 prompt/設定兩種方式皆可比較

限制

  • 部分 SDK:如果你的 SDK 不支援 defaultHeaders,改用路徑式方式。