Agent SDK: dual result events cause false retry on exit code 1

Resolved 💬 3 comments Opened Apr 16, 2026 by belangertrading Closed May 24, 2026

Bug

The Claude Agent SDK (@anthropic-ai/claude-agent-sdk@0.2.50) 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.

View original on GitHub ↗

This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗