Agent SDK: dual result events cause false retry on exit code 1
Bug
The Claude Agent SDK (@anthropic-ai/claude-agent-sdk@0.2.50) emits two result events from a single query() call:
{type: "result", subtype: "success", result: "<actual response text>"}— correct response{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
resultevent should be emitted per query - Or the error result event should NOT have
type: "result"(use a different event type likeexit_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.
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗