Task statement 6.3 · Prompt and context engineering
6.3 · Output Handling
Shelfwise, the course example, ends every answer with one line, such as "12 in stock at Leeds (feed 09:00)", which staff may repeat to a customer. Output handling decides what the service checks before that line, or any structured reply, is trusted.
share
The tempting belief is that output which parses can be used. Structured outputs make JSON parse, but a reply cut off at its token limit still breaks, and a schema-valid record can hold a count nobody looked up. A fluent line can also state a figure nobody computed or point to a document nobody sent.
KnowledgeStructured output patterns
Structured output patterns are the three ways to get a reply your code can read. JSON outputs send a JSON schema in output_config.format. Constrained decoding, which allows only tokens that keep the reply valid, makes the text block match it. This suits a reply that is itself data, such as an extraction. Strict tool use, strict: true on a tool (2.4), guarantees a tool's name and input instead, so it suits data passed into a function. A prompted format (6.2) guarantees nothing but keeps the reply as text. Citations need that, because citations with output_config.format return a 400 error. Schema design belongs to 2.5.
More precise than the guide. The structured-outputs page calls JSON outputs "always valid", then lists exceptions (as of 2026-10-05): a reply stopped by max_tokens may be incomplete, a refusal may not match the schema, and an enum value may differ in capitalisation. Exam answer: structured outputs fix the shape of a finished reply only, so read stop_reason before parsing and compare enum values ignoring case.
KnowledgeResponse validation
Response validation is the code that decides whether a reply may be used, and it runs in a fixed order. It reads stop_reason first: for a final answer, end_turn means Claude finished, while max_tokens, model_context_window_exceeded and refusal mean the content is cut short or is not an answer. Next comes the schema. Last come the rules no schema expresses: ranges, since structured outputs don't support minimum and maximum; consistency between fields, such as an end date after its start; and references that must exist, such as a catalogue code.
When a check fails, a validation-retry loop helps only if the right value is in what Claude was sent:
- Write each failure as a sentence Claude can act on: "end_date 2026-03-01 is before start_date 2026-03-04".
- If the input holds the value, resend it with the rejected reply and those sentences, and validate the new reply.
- If it doesn't, keep the field null and flag the record, because a retry only invites an invented value.
- Cap the attempts, and send whatever still fails to a person.
- Don't resend a refused request to the same model: read
stop_details, then fall back to another model, change the prompt, or show a fixed message. Aftermax_tokens, raise the limit.
Interactive · where each kind of bad output is caught
Choose a kind of output to see which check catches it.
Cut off at the limit: stop_reason is max_tokens, so the JSON may be unfinished. Don't parse it; raise max_tokens, or ask for less in each request.
Refusal: stop_reason is refusal, and the content may not match the schema. Read stop_details, then fall back to another model, change the prompt, or show a fixed message, rather than resending the request to the same model.
Sentence before the JSON: a prompted format with no schema behind it. Your code catches the parser's error, keeps the text for the log, and retries or shows a fixed message.
Enum in another case: structured outputs can return "Open Ticket" where the schema says "Open ticket", with no error. Compare enum values ignoring case, and avoid values that differ only in case.
End date before start: every field has the right type, so the schema passes. A cross-field rule in your code catches it, and because the input holds the right dates, one retry that quotes the failed check usually fixes it.
Count with no lookup: the line parses and breaks no rule, but no lookup in this turn holds that count. The grounding check catches it, and Shelfwise shows a fixed message instead.
Clean reply: it ended with end_turn, parses, matches the schema, passes the rules and agrees with its sources, so your code uses it.
KnowledgeDefensive parsing
Defensive parsing means reading Claude's output without assuming its shape. A prompted format may arrive with a sentence before the JSON, a code fence around it, or a field missing or extra, so use a real JSON parser, catch its error, default the optional fields and reject what still doesn't fit. Never call eval on the text, which RFC 8259 calls an unacceptable security risk, because the text could contain code. A tool's input arrives already parsed, so read it as given. In a stream (2.3), tool input comes as input_json_delta fragments, so join them and parse, guarded, when the block closes. Every parser needs a failure path that ends in a retry, a fixed message or a person, never a silent default.
KnowledgeScepticism toward confident output
Scepticism toward confident output, the guide's "skepticism toward confident output", means treating a fluent answer as a claim to check. Claude can state a wrong figure or cite a source that doesn't exist as smoothly as a right one, and the docs say their techniques reduce hallucinations without removing them. So give Claude fixed words for "I don't know" (6.2), limit it to the documents you send, and have it quote a source for each claim and withdraw any claim it cannot support. With citations, the API extracts each cited_text from your document, so every pointer is valid. Take figures from code, yours or the code execution tool, and have a person review high-stakes output.
SkillWorked example: a count with no lookup behind it
In the session incident (2.5), Leeds staff asking about 4 mm drill bits at 15:10 got "12 in stock", another chat's 09:40 count, with no lookup in that turn, shown like any other answer. After the session fix, the team added a check on each finished line. Replayed in today's answer format, the 15:10 reply fails it:
# 1 parse: the line must match one of two patterns, or it fails <n> in stock at <branch> (feed HH:MM) | No record at <branch> (feed HH:MM) # 2 ground: a stock_lookup result from this turn must hold that count and feed_time "12 in stock at Leeds (feed 09:00)" stock_lookup results this turn: none widget: "Stock check failed, check the shelf"
The widget streams the line greyed as "checking", then marks it checked or replaces it when the message ends. "No record" passes only after a same-turn lookup found nothing, not after an error.
stop_reason instead of parsing. Doesn't fit → retry once with the failed check named if the input holds the answer, otherwise null and a person. Not shown true → compare it with the lookup, source or computed figure.- JSON outputs and strict tool use fix the shape of a finished reply, not its truth.
- A retry pays only when the input holds the right value and the retry quotes the check that failed.
- Figures come from code, claims from a quote or citation, and high-stakes output goes to a person.