在 Codex、Claude Code 等程式設計 Agent 中使用 GPT Image 2
讓 Codex、Claude Code 或其他程式設計 Agent 透過你的 API Key 用 GPT Image 2 生成影像,並附上一段在每次付費請求前都會先徵求你同意的可重複使用提示詞。
以 Markdown 閱讀Codex 或 Claude Code 這類程式設計 Agent 可以直接呼叫 API 來生成 GPT Image 2 影像。把 API Key 放進環境變數,把下面的提示詞交給 Agent,它就會建立請求、先給你看、經你同意後提交、輪詢任務,並把影像存進你的專案。不需要任何外掛:只要 Agent 能執行 shell 指令或一段簡短腳本,就能呼叫 API。
SeedRouter 不提供 MCP 伺服器,也不提供打包好的 skill。本指南中的提示詞就是全部的整合。你可以把它存成你所用 Agent 裡的可重複使用指令。
Agent 開始前需要準備什麼?
三樣東西:
- 環境變數中的 API Key。 在 API Key 頁面建立一把,並在 Agent 執行的 shell 中 export。Agent 從那裡讀取;它絕不應該要求你貼上 Key。
- 網路存取。 Agent 會呼叫
https://api.seedrouter.ai,所以它的沙箱必須允許這個任務發出對外請求。 - 額度。 請求從你的餘額扣款,所以第一次執行前請先儲值。
export SEEDROUTER_API_KEY="your-key"應該給 Agent 什麼提示詞?
在任務開頭貼上這段,再填入方括號中的目標。它列出了 API 接受的確切欄位,讓 Agent 不會自己編造參數,並要求 Agent 在任何扣費之前停下來等你同意:
Use the SeedRouter API to generate a gpt-image-2 image for me.
Security: read SEEDROUTER_API_KEY from my local environment. Never ask me to paste it and never expose it in code, prompts, logs, or output.
Goal:
- Use case: [product / social / concept art / UI mockup]
- Subject and style: [subject, composition, lighting, style]
- Size: [auto | 1024x1024 | 1536x1024 | 1024x1536 | WIDTHxHEIGHT]
- Quality: [auto | low | medium | high]
- Number of images: [1-10]
- Acceptance criteria: [e.g. no text in the image, consistent product, clean background]
Request fields this endpoint accepts, and nothing else:
model (required, "gpt-image-2"), prompt (required, up to 32000 chars),
n (1-10, default 1), size (default auto; a custom WIDTHxHEIGHT must have
both sides divisible by 16, neither edge over 3840, total pixels between
655360 and 8294400, and keep a ratio between
1:3 and 3:1), quality (auto|low|medium|high, default auto),
background (auto|opaque|transparent, default auto; transparent requires
output_format png), output_format (png|jpeg, default png),
output_compression (0-100, default 100, jpeg only),
moderation (auto|low, default auto), user (your own identifier).
For editing use /v1/images/generations with images: [{image_url: "https://..."}]
(up to 16) and optional mask: {image_url: "https://..."}. Mask requires images.
Inputs must be URLs, not base64 or multipart files. stream and partial_images
are not supported because delivery is asynchronous.
Before any paid request, show me the model id, the exact request body and the
estimated cost, then wait for my explicit approval.
After approval:
1. POST https://api.seedrouter.ai/v1/images/generations with the body above and
an Authorization: Bearer $SEEDROUTER_API_KEY header.
2. Save the task id from the response "id" field. The response is
{"id": "...", "status": "processing"} — the image is not in it.
3. Poll https://api.seedrouter.ai/v1/tasks/{task_id} with the same header every
5-10 seconds until status is "completed" or "failed". Do not retry forever;
if you stop waiting, preserve the task id and report that it is still pending.
A polling timeout is not a failed task. Never submit a duplicate just to check status.
4. On success, download every URL in output.data[].url, return the local paths,
the task id, and the parameters used. The response carries no cost field.
5. On failure, keep the task id, explain the reason and what to change, and do
not retry without my approval. A task that ends failed is not charged.同意這一步最重要。會自行重試的 Agent 可能把同一個付費請求提交好幾次。即使你已經信任這套設定,也請保留這條指令。
如何在 Codex 中使用?
在你的專案中開啟 Codex,貼上已填好目標的提示詞。要重複使用,就把提示詞加進專案的 AGENTS.md(Codex 為該儲存庫讀取的指令檔),放在「Generating images」這類標題下。之後只要說「幫定價頁做一張主視覺圖」就夠了;Codex 會照著保存的步驟做。
如果 Codex 在沒有網路存取的沙箱中執行,它就連不到 API。請先為該工作階段開啟網路存取,再要求它生成影像。
如何在 Claude Code 中使用?
同一段提示詞在 Claude Code 中也能用。要重複使用,就把它存進專案的 CLAUDE.md,或存成一個 skill:一個含有 SKILL.md 檔案的資料夾,其指令內容就是這段提示詞。之後你要求生成影像時,Claude Code 就會載入它。請確認啟動 Claude Code 的終端機中已經 export 了 SEEDROUTER_API_KEY。
一次順利的 Agent 執行是什麼樣子?
- 你描述想要的影像,以及它要放在哪裡。
- Agent 顯示模型 ID、確切的請求體和預估費用,然後等待。
- 你表示同意。Agent 只提交一次,並回報任務 ID。
- 它輪詢到任務完成,把影像下載到專案中,並告訴你檔案路徑。
如果執行中途停下,任務 ID 依然有效。請要求 Agent 接續輪詢那個 ID,而不是重新生成;第二次提交就是第二次扣費。結果為失敗的任務不收費。
常見問題
SeedRouter 有提供 GPT Image 2 的 MCP 伺服器或 skill 嗎?
沒有。API 本身就是整合,上面的提示詞已經給了 Agent 呼叫它所需的一切。如果想重複使用,把提示詞存成你自己 Agent 裡的 skill 或指令檔即可。
Agent 可以編輯我專案中既有的影像嗎?
可以,前提是這些影像能透過公開的 HTTPS URL 存取。API 以 URL 接收參考圖和遮罩,不接受本機檔案,所以請先上傳,或告訴 Agent 它們放在哪裡。
Agent 應該使用哪個模型?
gpt-image-2 每交付一張影像收取一個固定單價;gpt-image-2-official 按每次算繪回報的 token 付費。要切換,就替換提示詞中的模型。目前價格見模型頁。
讓人始終參與決策
透過環境變數給 Agent Key,透過提示詞給它欄位,透過同意步驟把最終決定權留給自己。這個組合能讓 Codex 或 Claude Code 在你的專案裡產出影像,而不會有意料之外的扣費。如果想擴充提示詞,GPT Image 2 API 參考文件列出了所有欄位。



