Claude Code 配 Opus 5.5 如何讀檔案、調工具並輸出 JSON?2026 strict 與 Structured Output 實操
2026 年 11 月前後,Structured Outputs 在 Opus 5.5 上 GA,Claude Code 也隨 Opus 5.5 強化了 Plan Mode(可編輯的 plan.md)與更穩的讀改循環。工程上仍是老紀律:讀檔案是 tool_use JSON,改倉庫是宿主執行,給 UI/CI 的摘要走 output_config.format——別把三種物件糊成一段 markdown。
閱讀前速覽
| 階段 | Claude Code / API | 宿主 JSON |
|---|---|---|
| 讀倉庫 | Read / Glob / Grep 的 tool_use.input | file_path、offset/limit 審計 |
| 改與跑 | Edit / Write / Bash;strict 校驗參數 | 權限門、cwd、allowed roots |
| 結構化交付 | output_config.format json_schema | deliverable_schema、risk、next_tool |
| Plan Mode | plan.md 人類可改,再執行 | plan_path、turn、plan_mode |
| 長任務 | 仍走 Messages + tool 循環 | 見 Compaction 專文,本篇不展開 |
事實截至 2026-11-05:Anthropic Structured Outputs / strict tool use、Claude Code Plan Mode with Opus 5.5。路徑與 toolu_read_01 為虛構示例。
先讀(Read),再 strict 調工具,最後用 json_schema 交交付物
Claude Code 在 Opus 5.5 上的典型 Coding 循環可以收成三層 JSON 契約:
- 觀察層:模型發
Read/Grep的tool_use,宿主把檔案片段或 ripgrep 命中寫進tool_result——不是讓模型「假裝看過檔案」。 - 執行層:對自建 harness 或審計副本,在 Messages 請求裡給工具加
strict: true,讓input必過input_schema;Claude Code 內建工具面仍由產品封裝,但形狀相同。 - 交付層:要給儀表盤、PR 模板或 CI gate 的物件,用
output_config.format.type: json_schema,與 tool 參數分開——Structured Output 管「最後說什麼」,strict 管「怎麼調函式」。
Claude Code 產品入門見 Coding Agent 入門;Opus 5.5 Messages 與 effort 見 Opus 5.5 工具 JSON。Compaction 與 checklist state 見 長任務 state,本篇只補「讀檔案 + 結構化交付」實操。
Read 塊、strict 工具表、Structured Output、宿主 turn
1. Read 的 tool_use。 Opus 5.5 仍用內容塊;Claude Code 裡最常見的第一步:
{
"type": "tool_use",
"id": "toolu_read_01",
"name": "Read",
"input": {
"file_path": "src/orders/lookup.ts",
"offset": 1,
"limit": 120
}
}
2. tool_result 寫回。 宿主加行號、截斷超長檔案;模型下一輪只看見這段字串:
{
"type": "tool_result",
"tool_use_id": "toolu_read_01",
"content": " 1|export function lookupOrder(id: string) {\n 2| // TODO: validate A-1042 pattern\n 3| return fetch(`/api/orders/${id}`);\n 4|}"
}
3. strict 工具表(Messages 請求)。 適合你對齊 Claude Code 工具面做離線 replay 或 CI 審計:
{
"model": "claude-opus-5-5",
"max_tokens": 8192,
"tools": [
{
"name": "Read",
"description": "Read a slice of a file. Paths must stay under the workspace root.",
"strict": true,
"input_schema": {
"type": "object",
"properties": {
"file_path": { "type": "string" },
"offset": { "type": "integer", "minimum": 1 },
"limit": { "type": "integer", "minimum": 1, "maximum": 500 }
},
"required": ["file_path"],
"additionalProperties": false
}
},
{
"name": "Grep",
"description": "Search the repo with ripgrep.",
"strict": true,
"input_schema": {
"type": "object",
"properties": {
"pattern": { "type": "string" },
"path": { "type": "string" },
"glob": { "type": "string" }
},
"required": ["pattern"],
"additionalProperties": false
}
}
],
"messages": [{
"role": "user",
"content": "Find where order A-1042 is validated before calling lookup."
}]
}
4. Structured Output 交付物。 與 tool 循環同請求或下一跳均可;欄位給 UI/門禁用:
{
"model": "claude-opus-5-5",
"max_tokens": 4096,
"tools": [{ "name": "Read", "strict": true, "input_schema": { "type": "object", "properties": { "file_path": { "type": "string" } }, "required": ["file_path"], "additionalProperties": false } }],
"output_config": {
"format": {
"type": "json_schema",
"schema": {
"type": "object",
"properties": {
"order_id": { "type": "string", "pattern": "^[A-Z]-\\d{4}$" },
"files_touched": { "type": "array", "items": { "type": "string" } },
"risk": { "type": "string", "enum": ["low", "medium", "high"] },
"next_tool": { "type": "string", "enum": ["Read", "Edit", "Bash", "none"] }
},
"required": ["order_id", "files_touched", "risk", "next_tool"],
"additionalProperties": false
}
}
},
"messages": []
}
5. Claude Code 宿主 turn(示意)。 Plan Mode 與 deliverable 版本號進 state,便於 diff 會話:
{
"session_id": "cc_sess_demo_01",
"model": "claude-opus-5-5",
"plan_mode": true,
"plan_path": ".claude/plan.md",
"turn": 3,
"last_blocks": ["tool_use:Read", "tool_result", "tool_use:Grep"],
"deliverable_schema": "task_summary_v1",
"awaiting": "tool_result"
}
三種 JSON 各管一攤
把「聊天 markdown」和「可機讀物件」分開,Review 會輕鬆很多:
| 物件 | 誰產生 | 典型欄位 |
|---|---|---|
| tool_use / tool_result | 模型提議 + 宿主執行 | name、input、tool_use_id |
| strict input_schema | API 約束取樣 | additionalProperties: false |
| output_config.format | 模型最終文字塊 | order_id、risk、next_tool |
| plan.md | Plan Mode 人類編輯 | 步驟列表,不是 tool 參數 |
Bash / Edit 走權限門;Read 通常更寬,但仍應限制 workspace root。見 權限模型。
五步在 staging 跑通 A-1042
用虛構訂單 A-1042 走一遍,再接到真實倉庫:
- 只開 Read + Grep。 讓模型定位校驗邏輯;禁止第一步就 Bash。
- 錄 tool_use / tool_result。 每條對齊 id;state 裡寫 last_blocks。
- 需要改檔案時再開 Edit。 參數裡 path 必須在 allowlist。
- 交付用 json_schema。 risk / next_tool 給人工或 CI;不要從 tool_result 複製 secrets。
- Plan Mode 先 plan.md。 人改步驟再 auto;把 plan_path 寫進 state 方便 diff。
五步都落在 JSON 上,Claude Code 會話才可 replay——否則只剩「它說改好了」。
審軌跡,不是審 prose
Opus 5.5 + Structured Output 減少 parse 失敗,但不代替權限審查:
- 用 格式化 展開 messages;用 Schema 校驗 校驗 strict schema 與 deliverable schema。
- 用 JSONPath 抽
$.content[?(@.type=='tool_use')].input.file_path等路徑。 - Plan 前後對 state JSON Diff;看 plan_mode 切換是否丟 deliverable_schema。
- Structured 塊裡的 order_id 應與 tool 鏈一致(A-1042),不要各說各話。
- JSONSort 在瀏覽器本地處理;不要上傳真實倉庫或金鑰。
能單獨校驗 deliverable JSON,才算接好「結構化輸出」——不是多一個 pretty print。
常見誤區
- 把 Structured Output 當 tool 參數。 format 管回覆文字;Edit 仍走 tool_use。
- 跳過 Read 直接 Edit。 Opus 5.5 更敢改,宿主更要強制「先讀後改」。
- strict 開一半。 只給 Read strict、Edit 不 strict,審計會斷檔。
- plan.md 與 deliverable schema 混寫。 plan 給人看;schema 給程式看。
- tool_result 塞未截斷日誌。 仍 JSON 字串;先格式化再截斷展示欄位。
- 重寫 Coding Agent 入門。 產品對比見專文;本篇只談 Opus 5.5 讀/工具/結構化三角。
FAQ
Claude Code 內建工具還要自己寫 strict 嗎?
終端產品已封裝執行與確認門。你要做離線 replay、合規審計或自建 harness 時,應在 Messages 層 mirroring 同一 input_schema 並開 strict。
Structured Output 和 strict tool 能同請求嗎?
可以。Anthropic 文件明確兩者互補:tool 參數與最終 json_schema 回覆分開約束。
Plan Mode 的 plan.md 是 JSON 嗎?
不是。它是 Markdown 計劃;可機讀的進度仍建議用 deliverable schema 或宿主 state JSON。
Read 的 limit 設多大?
取決於宿主策略。示例用 500 行上限防止一次 tool_result 撐爆 context;大檔案應分段 Read。
和 Gemini 2026 結構化輸出新聞怎麼對照?
同屬「Schema 約束取樣」熱點;Claude Code 棧仍看 Messages + strict + output_config。跨廠商欄位對照見 Opus vs Astra 文,本篇不展開。
結論
Claude Code 配 Opus 5.5 時,讀檔案 = tool_use JSON,可靠改庫 = 權限 + strict 工具,給系統用的結論 = output_config.format。Plan Mode 把「計劃」交給人,Structured Output 把「交付」交給 schema。
模型只填 JSON;讀盤、跑命令、校驗 schema 永遠是宿主的事。先讓 A-1042 的軌跡能 replay,再開 full auto。
延伸閱讀
- Claude Code 與 GPT Agent 工具鏈 Coding Agent 入門與 JSON 循環
- Claude Opus 5.5 與工具 JSON Messages、effort、tool_use
- Opus 5.5 長任務 JSON State Compaction、checklist
- Coding Agent 權限模型 Shell / Git / 檔案系統
- 全部文章 更多 JSON 與 AI 開發筆記
更新記錄: 首次發布