Domain 4 · Prompt Engineering & Structured Output
This domain covers writing prompts that give precise, consistent results, forcing structured output with schemas, validating and retrying extractions, and choosing between real-time and batch processing.
Key terms: Prompt: the instructions you give Claude. Few-shot example: a worked example placed inside the prompt. JSON schema: a description of the exact shape of the data you want back. Extraction: pulling structured data out of documents. Batch: many requests processed together, later.
4.1 Design prompts with explicit criteria to improve precision and reduce false positives
What it is: A prompt works best when it says exactly what to report and what to ignore. Vague instructions like "be careful" or "only report if you are confident" do not make Claude more accurate; named categories do. Fewer false alarms means developers keep trusting the results.
Severity levels: Name them explicitly, for example CRITICAL, HIGH and MEDIUM, and define each one with a short code example instead of describing it in words.
The exam tests: why specific, categorical criteria beat general instructions. Video: Ep 14.
Full notes
Rule: Explicit criteria beat vague instructions. "Flag a comment only when its claim contradicts what the code does" works better than "check that comments are accurate".
Rule: General instructions such as "be conservative" or "only report high-confidence findings" do not improve precision the way specific categories do. Define what to report (bugs, security) and what to skip (minor style, local patterns).
Rule: A category with many false positives undermines trust in the accurate ones. Disable it temporarily while you improve its prompt. The course uses a 60% or higher false positive rate as an example.
Rule: Define each severity level with a concrete code example, so classification stays consistent. Do not describe severity only in words.
Deep dive
- Ep 14 at 04:02: why "be conservative" fails.
- Ep 14 at 08:41: false positives and trust.
- Ep 14 at 23:21: explicit severity levels.
- Ep 14 at 25:34: disabling noisy categories.
4.2 Apply few-shot prompting to improve output consistency and quality
What it is: A few-shot prompt includes a small number of worked examples, so Claude can see what a good answer looks like. It is the best fix when instructions alone give inconsistent output, especially for borderline cases.
The exam tests: what few-shot examples do that instructions cannot. Video: Ep 15.
Full notes
Rule: Few-shot examples are the most effective way to get consistently formatted, actionable output when detailed instructions alone give inconsistent results. Instructions describe what you want. Examples show what it looks like.
Rule: Use 2 to 4 targeted examples for ambiguous cases. Show the reasoning for why one action was chosen over the plausible alternatives. Reasoning-driven examples beat output-only ones.
Rule: Include examples of the exact output format you want, such as location, issue, severity and suggested fix.
Rule: Show acceptable code patterns next to genuine issues. This cuts false positives and lets the model generalize to patterns you did not list.
Rule: For extraction, examples of varied document structures (inline citations versus bibliographies, methodology sections versus details embedded in text) reduce hallucination and empty or null results.
Deep dive
- Ep 15 at 01:40: instructions describe what you want but do not show what it looks like.
- Ep 15 at 08:17: reasoning-driven examples: input, reasoning, output.
- Ep 15 at 13:55: the rule of two to four examples.
- Ep 15 at 21:35: ambiguous decision boundaries.
4.3 Enforce structured output using tool use and JSON schemas
What it is: Structured output means getting data in a fixed shape, like JSON, that other code can use. The reliable way is to define a tool with a JSON schema and have Claude fill it in. That guarantees the shape of the data, but not that the values are correct.
The exam tests: how tool use with a schema guarantees structure, and what it does not guarantee. Video: Ep 16.
Full notes
Rule: tool_use with a JSON schema is the most reliable way to get schema-compliant output. It removes JSON syntax errors.
Rule: A valid schema does not prevent semantic errors, such as line items that do not add up to the total or a value in the wrong field. Structurally correct is not the same as logically correct.
Rule: tool_choice set to "any" guarantees a tool call when several extraction schemas exist and the document type is unknown. A forced tool, {"type": "tool", "name": "extract_metadata"}, makes one specific extraction run first.
Rule: Make fields optional (nullable) when the source may not contain the information. A required field pushes the model to invent a value.
Rule: Add an "unclear" enum value for ambiguous cases, and "other" plus a detail string for categories that can grow.
Rule: Put format normalization rules in the prompt alongside the strict schema, to handle inconsistent source formatting.
Deep dive
- Ep 16 at 03:01: tool use.
- Ep 16 at 13:19:
tool_choicefor extraction. - Ep 16 at 19:21: syntax errors versus semantic errors.
4.4 Implement validation, retry and feedback loops for extraction quality
What it is: Even a well-formed extraction can contain mistakes, such as totals that do not add up. A validation loop checks the result and, if something is wrong, asks Claude again with the specific error. This works for format mistakes, but not when the information is missing from the source.
The exam tests: how to correct a failed extraction, and when retrying is pointless. Video: Ep 16.
Retry only when the information is in the source
Full notes
Rule: Retry with error feedback. The follow-up request includes the original document, the failed extraction and the specific validation error.
Rule: Retries fix format and structure problems. They cannot fix a field whose information is absent from the source, such as data that lives in an external document you did not provide.
Rule: Semantic validation errors (values that do not add up, a value in the wrong field) are separate from schema syntax errors, which tool use already removes.
Rule: Build self-correction checks: extract calculated_total next to stated_total to flag a mismatch, and add a conflict_detected boolean for inconsistent source data.
Rule: A detected_pattern field in each finding lets you analyze which constructs trigger findings, and spot false positive patterns when developers dismiss them.
Deep dive
- Ep 16 at 12:02: schema validation.
- Ep 16 at 23:16: business logic and cross-field validation.
- Ep 16 at 23:54: the self-correcting retry loop.
4.5 Design efficient batch processing strategies
What it is: The Message Batches API processes many requests together at half the cost, but results can take up to 24 hours, so nobody can wait on them. It suits overnight or weekly jobs, not checks that block a developer. Each request carries a custom ID so you can match answers back to questions.
The exam tests: which workloads suit the Message Batches API, and how to run it reliably. Video: Ep 17.
Use Batch only when nobody waits and no turns are needed
| Standard (Messages API) | Batch | |
|---|---|---|
| Cost | Full price | 50% less |
| Response | Real time | Up to 24 hours, no guaranteed latency |
| Tool calling | Multi-turn supported | Not within a single request |
Full notes
Rule: The Message Batches API gives 50% cost savings, a processing window of up to 24 hours, and no guaranteed latency SLA.
Rule: Use it for non-blocking, latency-tolerant work such as overnight reports, weekly audits and nightly test generation. Do not use it for blocking work such as a pre-merge check.
Rule: Use custom_id to match each response to its request. When some fail, resubmit only the failed documents, found by custom_id, with a fix such as chunking documents that exceeded the context limit.
Rule: Work out submission frequency from the SLA. Example: submitting every 4 hours plus a 24-hour processing window is 28 hours at worst, which fits inside a 30-hour SLA.
Rule: Refine the prompt on a small sample before you process a large volume, to raise first-pass success and avoid costly resubmission.
Deep dive
- Ep 17 at 07:40: the
custom_idpattern. - Ep 17 at 08:21: the 24-hour window.
- Ep 17 at 19:31: the blocking-workflow anti-pattern.
4.6 Design multi-instance and multi-pass review architectures
What it is: Claude is less likely to find faults in work it just produced, because it remembers its own reasoning. A better review uses a fresh, independent instance. For large changes, review each file on its own, then check how the files fit together.
The exam tests: why a model reviews its own work poorly, and how to structure larger reviews. Video: Ep 17.
Multi-pass review: depth, then breadth
Depth comes from parallel, independent per-file instances.
Full notes
Rule: A model keeps the reasoning from when it generated the work, so it is less likely to question its own decisions in the same session.
Rule: An independent instance, with no access to the generator's reasoning, catches subtle issues better than self-review instructions or extended thinking.
Rule: Split a large review into per-file passes for local issues (depth) plus a separate cross-file pass for data flow and integration (breadth). This avoids attention dilution and contradictory findings.
Rule: In a verification pass, have the model report its confidence with each finding, so you can route reviews by calibrated confidence.
Deep dive
- Ep 17 at 04:31: why self-review is weak.
- Ep 17 at 22:58: independent review instances.
- Ep 17 at 25:27: the multi-pass review architecture.