﻿# Turn a Claude Agent SDK result into a typed application response

Parse Claude Code programmatic output correctly by separating result envelopes, structured_output and streaming events from the text a user sees.

Author: Retrivium editorial team
Updated: 2026-11-03
Canonical URL: https://retrivium.ai/journal/learn-claude-sdk-response/
Language: en

A programmatic Claude Code call can return plain text, a JSON result envelope or a stream of JSON events. These representations serve different consumers. The headless documentation explains the CLI forms and points to Agent SDK message interfaces for applications that need native callbacks and more direct programmatic control.

## Choose the output format for the consumer

A terminal user may want plain text. A batch job may need the JSON envelope with result text and session metadata. A live UI may need streamed events. The documented `--output-format` option selects the CLI representation. For a hypothetical symbol-extraction job, choose structured consumption rather than scraping a Markdown list whose punctuation can change between otherwise valid responses.

## Separate the envelope from generated data

With JSON output, the envelope contains execution metadata as well as the model result. When a JSON Schema is requested, the documented generated value appears in `structured_output`. It is not the same field as plain result text. The symbol-extraction consumer should locate the intended field, validate its shape and preserve the session reference separately from the extracted symbol list.

## Handle absent structured output

The documentation states that a run can finish without `structured_output`, for example when the request is declined. Test for the field before using it. A missing field should produce an explicit incomplete or unsupported outcome in the application, not an empty successful extraction by accident. Also distinguish a malformed schema rejected before work from a task that ran but did not return the required artifact.

## Keep stream updates distinct from final results

The stream-json mode emits newline-delimited event objects. Partial text events can update the display, while the final result carries the completed response and metadata. Do not treat the first parseable line as the whole task. A consumer should associate events with the active operation and finalize its symbol list only after receiving the relevant completed result or handling a documented failure.

## Validate the domain result as well

A correctly shaped list can still contain a symbol that does not exist in the inspected file. Compare the extraction with the source or an independent parser when correctness matters to the workflow. Transport parsing and domain validation answer different questions. This layered consumer can explain whether a failure came from execution, output shape or incorrect extracted content instead of reporting every problem as invalid JSON.

## Documentation and further study

- [Run Claude Code programmatically](https://code.claude.com/docs/en/headless).

- [How Claude Code works](https://code.claude.com/docs/en/how-claude-code-works).

- [Extend Claude Code](https://code.claude.com/docs/en/features-overview).

Related engineering guides: [Reproducible inference stacks: preserving a GPU pilot’s result](/journal/software-stack-reproducibility/); [Choose a local AI stack: desktop chat, shared UI or coding agent](/journal/local-ai-app-stack-selection/).
