claude-code - 💡(How to fix) Fix Agent SDK: dual result events cause false retry on exit code 1 [2 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
anthropics/claude-code#48971Fetched 2026-04-17 08:54:22
View on GitHub
Comments
2
Participants
2
Timeline
9
Reactions
0
Timeline (top)
labeled ×4commented ×2mentioned ×1referenced ×1

Error Message

  • Or the error result event should NOT have type: "result" (use a different event type like exit_error)
  • Or the error result should preserve the response text from the successful completion

Root Cause

  1. {type: "result", subtype: "success", result: "<actual response text>"} — correct response
  2. {type: "result", subtype: "error_during_execution", result: null} — because Claude Code exits with code 1

Fix Action

Workaround

Guard against overwriting a good result:

if (event.type === "result") {
  if (!resultText) resultText = event.result ?? null;
}

Code Example

import { query } from "@anthropic-ai/claude-agent-sdk";

let resultText = null;
for await (const event of query({ prompt: "Hello", options: { permissionMode: "bypassPermissions" } })) {
  if (event.type === "result") {
    console.log(event.subtype, event.result); // Logs: "success" "Hello!" then "error_during_execution" null
    resultText = event.result ?? null; // Second pass sets resultText = null
  }
}
console.log(resultText); // null — response lost

---

if (event.type === "result") {
  if (!resultText) resultText = event.result ?? null;
}
RAW_BUFFERClick to expand / collapse

Bug

The Claude Agent SDK (@anthropic-ai/[email protected]) emits two result events from a single query() call:

  1. {type: "result", subtype: "success", result: "<actual response text>"} — correct response
  2. {type: "result", subtype: "error_during_execution", result: null} — because Claude Code exits with code 1

If consumer code processes both events sequentially (as a natural for await loop does), the second event overwrites the first, losing the valid response.

Reproduction

import { query } from "@anthropic-ai/claude-agent-sdk";

let resultText = null;
for await (const event of query({ prompt: "Hello", options: { permissionMode: "bypassPermissions" } })) {
  if (event.type === "result") {
    console.log(event.subtype, event.result); // Logs: "success" "Hello!" then "error_during_execution" null
    resultText = event.result ?? null; // Second pass sets resultText = null
  }
}
console.log(resultText); // null — response lost

Expected Behavior

Either:

  • Only one result event should be emitted per query
  • Or the error result event should NOT have type: "result" (use a different event type like exit_error)
  • Or the error result should preserve the response text from the successful completion

Environment

  • @anthropic-ai/claude-agent-sdk: 0.2.50
  • Claude Code CLI: 2.1.109
  • Node.js: 22.22.2
  • OS: Ubuntu 24.04

Workaround

Guard against overwriting a good result:

if (event.type === "result") {
  if (!resultText) resultText = event.result ?? null;
}

Additional Context

The exit code 1 appears to come from Claude Code's internal JSON parser hitting corrupted data (stderr: "Unexpected non-whitespace character after JSON at position 2"). Setting NO_COLOR=1, TERM=dumb, FORCE_COLOR=0 in the environment does not prevent the exit code 1, but may reduce frequency.

This was discovered while building ClaudeClaw, a Telegram bot wrapper for the Agent SDK.

extent analysis

TL;DR

To prevent overwriting a valid response, guard against updating the result if it's already set.

Guidance

  • Verify that the issue is indeed caused by the second result event overwriting the first by adding logging or debugging statements to track the events and their order.
  • Implement the provided workaround to guard against overwriting a good result: only update resultText if it's currently null.
  • Consider filing an issue with the @anthropic-ai/claude-agent-sdk maintainers to report the unexpected behavior of emitting two result events.
  • Review the error handling in the consumer code to ensure it can properly handle the error_during_execution subtype and the potential loss of response text.

Example

if (event.type === "result") {
  if (!resultText) resultText = event.result ?? null;
}

Notes

The root cause of the issue seems to be related to Claude Code's internal JSON parser hitting corrupted data, but the provided workaround focuses on mitigating the effect of the second result event. Further investigation into the JSON parsing error may be necessary to fully resolve the issue.

Recommendation

Apply the workaround to guard against overwriting a good result, as it provides a reliable way to preserve the valid response text despite the unexpected behavior of the @anthropic-ai/claude-agent-sdk.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING