AI 开发

Claude Code 配 Opus 5.5 如何读文件、调工具并输出 JSON?2026 strict 与 Structured Output 实操

阅读约 14 分钟

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 走一遍,再接到真实仓库:

  1. 只开 Read + Grep。 让模型定位校验逻辑;禁止第一步就 Bash。
  2. 录 tool_use / tool_result。 每条对齐 id;state 里写 last_blocks。
  3. 需要改文件时再开 Edit。 参数里 path 必须在 allowlist。
  4. 交付用 json_schema。 risk / next_tool 给人工或 CI;不要从 tool_result 复制 secrets。
  5. 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。

延伸阅读

更新记录: 首次发布