Claude Architect · 课时

可重试元数据与部分结果

errorCategory、isRetryable、attempted_query、partials。

第 3 / 4 课13 个步骤

可重试元数据与部分结果 是 CoddyKit 上的免费 Claude Architect 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Claude Architect 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Claude Architect 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Why Error Shape Matters

When a tool or MCP server fails, the model has to decide what to do next. A generic status like "Operation failed" gives it nothing to reason about, so the only safe move is to abort or guess.

A structured error turns a dead end into a decision: should we retry, route around the failure, or escalate to a human? In this lesson you'll learn the four metadata fields that make that possible: errorCategory, isRetryable, attempted_query, and partial_results.

The isError Flag

Every structured MCP error starts with one boolean: isError: true. This is the unambiguous signal that the tool result is a failure, not data.

Without it, the model may treat an error message as a legitimate answer and happily summarize the failure as if it were a result. The flag is the gate that activates all the recovery logic that follows.

tool_result = {
    "isError": True,
    "errorCategory": "transient",
    "isRetryable": True,
    "message": "Upstream timeout contacting orders DB",
    "attempted_query": "SELECT * FROM orders WHERE id='A-2291'",
    "partial_results": []
}

errorCategory: Four Buckets

errorCategory classifies why the call failed so the model can route intelligently. The four standard categories are:

  • transient — a temporary fault (timeout, rate limit). Likely worth retrying.
  • validation — the input was malformed. Fix the request, don't blindly retry.
  • business — a domain rule blocked it (e.g. order already shipped).
  • permission — the caller isn't authorized. Retrying won't help; escalate or re-auth.

The category drives the strategy; it doesn't make the decision alone.

isRetryable: The Action Hint

isRetryable is the explicit yes/no on whether retrying could possibly succeed. It works with the category but encodes a sharper signal.

A transient timeout is usually isRetryable: true. A validation error is isRetryable: false — retrying the same bad input just fails again. Crucially, this lets the subagent recover transient faults locally instead of bubbling every hiccup up to the coordinator.

if result.get("isError"):
    if result["isRetryable"] and attempt < max_attempts:
        attempt += 1
        continue          # recover locally in the subagent
    else:
        escalate(result)  # non-recoverable: pass it up with context

Don't Confuse Failure with Empty

A subtle but exam-critical distinction: an access FAILURE is not the same as a valid EMPTY result.

  • isError: true + transient → the query couldn't run. Consider a retry.
  • isError: false + empty list → the query ran fine and there are genuinely no matches. Retrying is pointless and wasteful.

Generic errors blur this line. Structured metadata keeps "I couldn't look" cleanly separated from "I looked, nothing's there."

attempted_query: Make Retry Possible

attempted_query records exactly what the tool tried to do — the SQL, the API call, the search string. This serves two jobs:

  • It lets the model retry with feedback: send the original intent plus the error so a corrected query can be formed.
  • It feeds provenance — you keep a claim-to-source trail of what was actually asked.

Remember: retry-with-feedback fixes format/structural mistakes. If the information is simply absent from the source, no amount of re-querying helps.

{
    "isError": True,
    "errorCategory": "validation",
    "isRetryable": True,
    "message": "Unknown column 'order_no'; did you mean 'order_id'?",
    "attempted_query": "SELECT * FROM orders WHERE order_no='A-2291'",
    "partial_results": []
}

partial_results: Don't Throw Away Good Data

When a multi-step or multi-source operation fails halfway, the work done before the failure is still valuable. partial_results carries it forward.

Imagine a research subagent that queried five sources and the fifth timed out. Returning the four successful results plus the error means the coordinator can keep going — instead of discarding everything because one leg failed. Never abort the whole workflow on a single failure.

{
    "isError": True,
    "errorCategory": "transient",
    "isRetryable": True,
    "message": "Source 5 (vendor API) timed out after 4 of 5 sources",
    "attempted_query": "fetch pricing from [s1..s5]",
    "partial_results": [
        {"source": "s1", "price": 19.0},
        {"source": "s2", "price": 21.5},
        {"source": "s3", "price": 18.9},
        {"source": "s4", "price": 20.0}
    ]
}

Recover Locally, Escalate with Context

The metadata enables a clean two-tier strategy in hub-and-spoke systems:

  • Recover transient faults locally inside the subagent — retry the isRetryable ones quietly.
  • Escalate non-recoverable failures up to the coordinator, carrying the full structured context: failure type, attempted query, and any partial results.

The coordinator handles errors and routes. But it can only route well if the subagent hands it a structured signal instead of a bare exception or silence.

Designing the Error Schema

If you define the error as structured output, apply the schema rules carefully. Mark a field required only if it is always present. partial_results is often empty or absent on a hard failure — so don't force it as required, or the model may fabricate entries to satisfy the schema.

For errorCategory, use an enum with an "other" value plus a free-text detail field. That keeps classification clean today and extensible for failure modes you haven't met yet.

error_schema = {
    "type": "object",
    "properties": {
        "isError": {"type": "boolean"},
        "errorCategory": {
            "enum": ["transient", "validation",
                     "business", "permission", "other"]
        },
        "categoryDetail": {"type": "string"},
        "isRetryable": {"type": "boolean"},
        "attempted_query": {"type": "string"},
        "partial_results": {"type": "array"}
    },
    "required": ["isError", "errorCategory", "isRetryable"]
}

Hooks for the Failures That Cost Money

Metadata guides the model probabilistically (~90%). When a failure has financial, legal, or safety consequences, that isn't enough.

Use a PostToolUse hook to intercept the tool result before the model sees it, and enforce policy deterministically (100%). For example: if errorCategory is permission on a refund tool, block any retry and force escalation — don't leave it to the prompt to behave.

# PostToolUse hook: deterministic guard on structured errors
def post_tool_use(result):
    if result.get("isError") and \
       result["errorCategory"] == "permission":
        return block_and_escalate(
            reason=result["message"],
            attempted=result["attempted_query"])
    return result

Anti-Pattern: Silent Suppression

The worst thing you can do with a failure is hide it. Two failure modes to avoid:

  • Silent suppression — swallowing the error and returning an empty or made-up result. Now the model can't tell a real "no matches" from a broken query.
  • Aborting the whole workflow on one failed leg — throwing away every partial result.

Structured errors are the cure for both: they surface the failure and preserve what succeeded.

Quick Check: Routing a Partial Failure

Apply what you've learned to a real multi-agent scenario.

Recap: The Recovery Toolkit

Structured errors turn failures into routable decisions:

  • isError — the gate that activates recovery logic.
  • errorCategory — transient / validation / business / permission (+ "other") sets the strategy.
  • isRetryable — the explicit retry hint; recover transient faults locally.
  • attempted_query — enables retry-with-feedback and provenance (won't help if info is truly absent).
  • partial_results — carry forward good data; never abort the whole workflow on one failure.

Mark only always-present fields as required, guard money/legal/safety failures with deterministic hooks, and never suppress errors silently. That's architect-grade error handling.

免费开始

用 AI 导师学习 Python — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
26
课程
104

常见问题解答

「可重试元数据与部分结果」课时是免费的吗?

是的 — 「可重试元数据与部分结果」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Claude Architect 课程的其余内容,请升级到 CoddyKit PRO。 Claude Architect 课程共包含 4 节课。

「可重试元数据与部分结果」这节课中我会学到什么?

errorCategory、isRetryable、attempted_query、partials。 你通过在浏览器中直接运行的动手代码来练习 Claude Architect,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Claude Architect 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Claude Architect 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。

「可重试元数据与部分结果」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Claude Architect 课中编写并运行代码吗?

能。每节 Claude Architect 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. isError 标志
  2. 错误类别
  3. 可重试元数据与部分结果
  4. 反模式:通用错误消息
← 返回 Claude Architect