Structured Output with Tool Use: Worked Example — Prompt Engineering & Structured Output (Claude Certified Architect)
Structured Output with Tool Use: A Worked Example for Claude Certified Architect Foundation In this worked example, we demonstrate how to design...
Structured Output with Tool Use: A Worked Example for Claude Certified Architect Foundation
In this worked example, we demonstrate how to design precise prompts and enforce structured output using tool calling and JSON schemas within a realistic scenario. This approach is essential for ensuring reliable data extraction and review in production applications built with Claude.
Scenario: Extracting Customer Feedback Insights
Imagine you are designing a solution to extract structured insights from free-text customer feedback. The goal is to reliably identify key sentiment, product mentions, and suggested improvements, formatted as JSON for downstream analysis.
Step 1: Define Explicit Prompt Criteria
Begin by crafting a prompt that clearly specifies the expected output fields and their formats to reduce ambiguity and false positives.
- Example prompt snippet: "Extract the following fields: sentiment (positive, neutral, negative), products_mentioned (list of product names), and suggested_improvements (list of suggestions). Output the result as a JSON object."
Step 2: Incorporate Few-Shot Examples
Provide a few-shot demonstration of input-output pairs to improve output consistency and guide the model’s understanding of the JSON structure.
Worked Example: Few-Shot Prompt
Input: "I love the battery life of Product A but wish the screen was brighter."
Expected JSON Output:
{ "sentiment": "positive", "products_mentioned": ["Product A"], "suggested_improvements": ["screen brightness"] }
Step 3: Enforce Structured Output via Tool Calling and JSON Schemas
Use Claude’s tool calling capabilities to specify a JSON schema that validates the output format. This ensures the model’s response conforms to the expected structure, enabling automated parsing and reducing manual review.
- Define a JSON schema with required fields and data types.
- Invoke the tool call in the prompt to constrain output to this schema.
Step 4: Implement Validation, Retry, and Feedback Loops
After receiving the output, validate it against the JSON schema. If validation fails or key fields are missing, trigger a retry with an adjusted prompt or provide feedback to the model to improve extraction quality.
- Example: If products_mentioned is empty but the input mentions a product, prompt the model to re-extract focusing on product names.
Step 5: Design Efficient Batch Processing
For large volumes of feedback, design batch processing strategies that group inputs and process them in parallel while maintaining prompt clarity and output structure.
- Batch inputs with similar context to maximize model efficiency.
- Use consistent prompt templates with embedded JSON schema enforcement.
Step 6: Multi-Pass and Multi-Instance Review Architecture
Implement a multi-pass system where initial extraction is followed by a secondary review pass, potentially by a different model instance or human-in-the-loop, to verify and refine outputs.
- First pass: Automated extraction with tool-enforced JSON output.
- Second pass: Review flagged or low-confidence outputs for correction.
Summary
This worked example illustrates how to combine explicit prompt design, few-shot learning, tool calling with JSON schema enforcement, validation loops, batch processing, and multi-pass review to achieve reliable structured output in Claude-based applications. Mastering these techniques is key for Claude Certified Architect Foundation candidates aiming to build robust production solutions.
More in this topic
Ready to test your knowledge?
Put what you've learned into practice with a quick quiz and track your progress.
Test your knowledge →