Generate Synthetic Test Data for Your Prompt Template
Imagine you have a prompt roughly along these lines:
"""Here's some things I want you to analyze:
{{thing1}}
{{thing2}}
These things are [description of things]. Please read them carefully and [do some task]."""
Here we'd call thing1 and thing2 the "variables" -- and you want your prompt to behave well for many different possible values of thing1 and thing2.
How can you test this prompt template? Maybe you have some real-life values you can substitute in. But maybe you don't, or maybe you aren't allowed to test on the ones you do have for privacy reasons. No worries -- Claude can make them up! This cookbook demonstrates how to generate synthetic test data for your prompts using Claude & the Claude API. It includes functions for extracting variables from templates, constructing example blocks, generating test cases, and iteratively refining the results. The benefits of this are twofold:
-
Prompt Evaluation You can use these test cases to see how Claude will perform on realistic examples.
-
Prompt Improvement with Multishot Examples Giving Claude examples is perhaps the best way to improve its performance. This notebook can help you generate realistic inputs which is half the battle in getting ideal input/output pairs.
Let's start by defining some helper functions that we'll use throughout this notebook.
Prompt Template for Generating the Data
The general idea of these prompt templates is to take a user-submitted prompt template with variables, and construct some values for the variables to fill the template.
There are actually two prompt templates below; one is formatted assuming that the user has already provided example variable values, and one does not assume that.
What they have in common is that both templates start by giving Claude context about the situation, and directing Claude to carefully think through the specs of each variable individually as well as the user-provided prompt template as a whole before outputting the test cases.
Next, another quick helper function for filling in the appropriate prompt template and calling Claude.
Now we can start to put the pieces together. To start, enter your prompt template here.
Identified variables: - DOCUMENTS - QUESTION
Next, if you have any "golden examples" of inputs and ideal outputs, you can enter those. The code is commented out for now.
Next, we can get the test case generation prompt template filled out with this information, and get a test case!
Now, let's take a look at both the test case and the planning that Claude used to generate it.
~~~~~~~~~~~ Generated test case: ~~~~~~~~~~~ DOCUMENTS: **Acme Corporation Customer Policies (Last updated: January 2025)** **1. Returns and Refunds** - Most items may be returned within 30 days of delivery for a full refund to the original payment method, provided they are unused and in their original packaging. - Items marked "Final Sale" or "Clearance" cannot be returned or exchanged, but are still covered by our warranty if defective (see Section 3). - Customers are responsible for return shipping costs unless the item arrived damaged or defective, or we sent the wrong item. In those cases, Acme will provide a prepaid return label. - Refunds are processed within 5-7 business days after we receive the returned item. Your bank may take an additional 3-5 business days to post the credit. - Original shipping fees are non-refundable unless the return is due to an Acme error. - Opened software, digital downloads, and gift cards are not eligible for return. **2. Shipping** - Standard shipping (5-8 business days) is free on orders over $50. Orders under $50 have a flat $5.99 shipping fee. - Expedited shipping (2-3 business days) costs $14.99. Overnight shipping costs $29.99 and must be ordered before 2:00 PM Eastern Time. - Acme currently ships to the United States and Canada only. International orders are not available at this time. - Once your order ships, you will receive a confirmation email with a tracking number. Orders typically ship within 1-2 business days of being placed. - Acme is not responsible for delays caused by carriers, weather, or incorrect shipping addresses provided by the customer. **3. Warranty** - All Acme-branded products include a 1-year limited warranty covering defects in materials and workmanship, starting from the date of delivery. - The warranty does not cover damage from misuse, accidents, unauthorized modifications, or normal wear and tear. - To make a warranty claim, contact Customer Support with your order number and a description or photo of the issue. Approved claims will be repaired or replaced at Acme's discretion. - Extended warranties (2 additional years) can be purchased within 30 days of the original purchase for 15% of the item's price. **4. Order Changes and Cancellations** - Orders can be modified or canceled within 1 hour of being placed by visiting the "My Orders" page in your account. - After 1 hour, orders enter processing and can no longer be changed. If the order has not yet shipped, you may contact Customer Support to request a cancellation, but we cannot guarantee it will be successful. - Once an order has shipped, it cannot be canceled. You may return it according to our Returns policy. **5. Contact Information** - Customer Support is available Monday-Friday, 9:00 AM to 8:00 PM Eastern Time, and Saturday, 10:00 AM to 4:00 PM Eastern Time. - Email: support@acmecorp.example | Phone: 1-800-555-0142 | Live chat available on acmecorp.example during business hours. - Please have your order number ready when contacting us. Response time for emails is typically within 24 hours. QUESTION: Hi, I bought a Roadrunner Blender from you guys about 3 weeks ago during the clearance sale and now the motor makes a burning smell whenever I turn it on. I'd like to send it back for a refund. Also, who pays for the shipping if I send it back? Thanks ~~~~~~~~~~~ Planning: ~~~~~~~~~~~ <planning> 1. **Summary of the prompt template:** The template sets up a customer support chatbot for Acme Corporation. The bot gets an FAQ or policy document set and a customer's question. It should answer using details from those policies. The creator wants accurate, policy-grounded answers that don't invent information. The bot should handle both questions the policies cover and questions they only partly cover or don't cover. 2. **Variables:** - **DOCUMENTS:** In production, the company or a developer would supply this, probably pulled from a knowledge base, CMS, or database of policy pages and inserted by a retrieval system or a static config. A realistic value is a set of FAQ entries or policy sections, such as returns, shipping, warranty, and account or payment policies. It should be fairly long (several hundred words), with headings or Q&A format, concrete details (timeframes, fees, conditions, contact info), and a neutral, professional tone. Including a few edge-case details (exceptions, conditions) makes the test case more useful. Acme Corporation is a generic company, so I'll make it a consumer products seller, in the spirit of the cartoon company. - **QUESTION:** In production, a human customer would type this into a chat widget or support form. It should be casual, perhaps with a bit of frustration or missing context, and one to a few sentences long. A good test question needs information from more than one policy section and has a wrinkle, such as an item bought on sale or a return outside the window. That checks whether the bot reads the policies carefully instead of just matching keywords. I'll write a question about returning a product that has an exception in the policy, and ask a related follow-up the documents only partly cover. </planning>
From here, there are a few ways we can go. We could generate more test cases, or we could edit Claude's planning logic. Let's edit Claude's planning logic a little bit. Maybe we know that ACME's documentation uses numbered lines. Some other realistic changes could be:
- Have Claude tell itself to make the documents longer and more detailed.
- Have Claude tell itself to make the customer support query more or less formal.
Let's reset our examples, but pass this edited planning text back to Claude. get_test_data appends it to the prompt and tells Claude to follow that plan instead of writing a new one.
Now let's see the new results.
~~~~~~~~~~~ Generated test case: ~~~~~~~~~~~ DOCUMENTS: ACME CORPORATION CUSTOMER POLICIES (Last updated: January 2024) SECTION 1: RETURNS AND REFUNDS Q: What is Acme's standard return policy? A: Most items purchased from Acme.com may be returned within 30 days of the delivery date for a full refund, provided they are unused, in their original packaging, and accompanied by proof of purchase. Refunds are issued to the original form of payment within 5-7 business days after we receive and inspect the returned item. Q: Are there items that cannot be returned? A: Yes. The following items are final sale and cannot be returned or exchanged: items marked "Clearance" or "Final Sale" at the time of purchase, custom or personalized products, perishable goods, and any product containing explosives (including the Acme Rocket Skates and Acme Dehydrated Boulders when opened). Gift cards are also non-refundable. Q: Who pays for return shipping? A: Customers are responsible for return shipping costs unless the item arrived damaged or defective, or we sent the wrong item. In those cases, Acme will email you a prepaid return label. A flat $7.95 restocking fee applies to returns of items over 50 lbs that are not defective. Q: Can I exchange an item instead of returning it? A: Yes. Exchanges for a different size, color, or model can be requested within 30 days of delivery. If the new item costs more, you will be charged the difference. If it costs less, the difference will be refunded to your original payment method. SECTION 2: SHIPPING Q: What shipping options are available? A: Standard Shipping (5-8 business days) is $5.99 on orders under $50 and free on orders of $50 or more. Expedited Shipping (2-3 business days) is $14.99. Overnight Shipping is $29.99 and must be ordered before 12:00 PM Pacific Time. We currently ship to the United States and Canada only. Q: How can I track my order? A: Once your order ships, you will receive an email with a tracking number and a link to the carrier's website. You can also view tracking information by logging in to your Acme account and selecting "Order History." Q: What if my package arrives damaged? A: Contact Customer Support within 14 days of delivery with your order number and photos of the damaged item and packaging. We will send a replacement or issue a full refund, at your choice. SECTION 3: WARRANTY Q: Does Acme offer a warranty? A: All Acme products come with a 90-day limited warranty covering defects in materials and workmanship. The warranty does not cover damage caused by misuse, unauthorized modifications, or normal wear and tear. It also does not cover damage resulting from use near cliffs, canyons, or desert highways. To make a warranty claim, contact Customer Support with your order number and a description of the problem. Q: Can I extend my warranty? A: Yes. Acme Protect extends your warranty to 2 years and is available for purchase within 30 days of your original order. Pricing varies by product. SECTION 4: ACCOUNT AND PAYMENT Q: What payment methods does Acme accept? A: We accept Visa, Mastercard, American Express, Discover, PayPal, and Acme Gift Cards. We do not accept checks or money orders. Q: How do I cancel an order? A: Orders can be canceled within 2 hours of placement by logging in to your account and selecting "Cancel Order." After that window, the order may have already entered processing, and you will need to wait for delivery and submit a return. SECTION 5: CONTACT INFORMATION Customer Support is available Monday-Friday, 8:00 AM-6:00 PM Pacific Time. Email: support@acme-corp.example Phone: 1-800-555-0142 Live chat: available on Acme.com during business hours. QUESTION: hey, i bought a pair of Acme Rocket Skates about 3 weeks ago and they're way too fast for me lol. i already took them out of the box and tried them once. can i still return them? also i think they were on clearance when i bought them. if i can't return them can i at least get my money back some other way? and who pays for shipping if i send them back ~~~~~~~~~~~ Planning: ~~~~~~~~~~~ <planning> 1. **Summary of the prompt template:** The template sets up a customer support chatbot for Acme Corporation. The bot gets an FAQ or policy document set and a customer's question. It should answer using details from those policies. The creator wants accurate, policy-grounded answers that don't invent information. The bot should handle both questions the policies cover and questions they only partly cover or don't cover. 2. **Variables:** - **DOCUMENTS:** In production, the company or a developer would supply this, probably pulled from a knowledge base, CMS, or database of policy pages and inserted by a retrieval system or a static config. A realistic value is a set of FAQ entries or policy sections, such as returns, shipping, warranty, and account or payment policies. It should be fairly long (several hundred words), with headings or Q&A format, concrete details (timeframes, fees, conditions, contact info), and a neutral, professional tone. Including a few edge-case details (exceptions, conditions) makes the test case more useful. Acme Corporation is a generic company, so I'll make it a consumer products seller, in the spirit of the cartoon company. - **QUESTION:** In production, a human customer would type this into a chat widget or support form. It should be casual, perhaps with a bit of frustration or missing context, and one to a few sentences long. A good test question needs information from more than one policy section and has a wrinkle, such as an item bought on sale or a return outside the window. That checks whether the bot reads the policies carefully instead of just matching keywords. I'll write a question about returning a product that has an exception in the policy, and ask a related follow-up the documents only partly cover. </planning>
Great, it did the numbered Q and A!
Let's make another example. This one will use the example we already have, so hopefully it will be interestingly different.
~~~~~~~~~~~ Generated test case: ~~~~~~~~~~~ DOCUMENTS: ACME CORPORATION CUSTOMER POLICIES (Last updated: March 2024) SECTION 1: ORDERS AND PAYMENT Q: What payment methods does Acme accept? A: We accept Visa, Mastercard, American Express, Discover, PayPal, and Acme Gift Cards. We do not accept personal checks, money orders, or cryptocurrency. Sales tax is calculated at checkout based on your shipping address. Q: Can I change or cancel my order after placing it? A: Orders can be changed or canceled within 1 hour of placement by logging in to your Acme account, opening "Order History," and selecting "Edit Order" or "Cancel Order." After 1 hour, orders enter our fulfillment process and can no longer be modified. If you no longer want the item, you may refuse delivery or return it once it arrives, subject to our return policy. Q: Can I use more than one promo code on an order? A: No. Only one promotional code may be applied per order. Promo codes cannot be applied to gift cards, Acme Protect plans, or items marked "Clearance." SECTION 2: SHIPPING AND DELIVERY Q: What are the shipping options and costs? A: Standard Shipping (5-8 business days) costs $6.49 for orders under $75 and is free for orders of $75 or more. Two-Day Shipping is $15.99. Overnight Shipping is $31.99 and must be ordered by 11:00 AM Pacific Time on a business day. Orders placed after the cutoff or on weekends ship the next business day. Q: Do you ship internationally? A: We currently ship to all 50 U.S. states, Puerto Rico, and Canada. We do not ship to P.O. boxes for orders containing oversized items. International shipping outside these regions is not available at this time. Q: My tracking says "Delivered" but I don't have my package. What should I do? A: First, check around your property and with neighbors, as carriers sometimes leave packages in unexpected places. Wait 24 hours, as tracking updates can occasionally be early. If the package has still not arrived, contact Customer Support within 7 days of the "Delivered" date and we will open an investigation with the carrier. Q: What should I do if my item arrives damaged or incorrect? A: Contact Customer Support within 10 days of delivery with your order number and photos of the item and its packaging. We will send a replacement at no cost or issue a full refund, whichever you prefer. We will email you a prepaid return label if a return is needed. SECTION 3: RETURNS AND REFUNDS Q: What is the standard return policy? A: Most items may be returned within 45 days of the delivery date for a full refund, provided they are unused, in their original packaging, and accompanied by proof of purchase. Refunds are issued to the original form of payment within 7-10 business days after we receive and inspect the item. Q: Which items are not eligible for return? A: The following are final sale: items marked "Clearance" or "Final Sale" at the time of purchase, custom-made or engraved products, perishable items, opened software or digital downloads, and gift cards. Items that have been used or damaged by the customer are also ineligible for refund. Q: Who pays for return shipping? A: Customers pay return shipping unless the item was defective, damaged in transit, or sent in error. In those cases, Acme provides a prepaid return label. Items over 40 lbs that are returned for reasons other than a defect are subject to a $9.95 restocking fee, which is deducted from the refund. Q: Can I get store credit instead of a refund? A: Yes. If you are within the 45-day return window, you may choose to receive Acme store credit instead of a refund to your original payment method. Store credit is issued as an Acme Gift Card by email within 3 business days of our receiving the return. Store credit does not expire. SECTION 4: WARRANTY AND REPAIRS Q: What warranty comes with Acme products? A: All Acme products include a 1-year limited warranty against defects in materials and workmanship, starting from the delivery date. The warranty does not cover accidental damage, misuse, unauthorized repairs or modifications, commercial use, or normal wear and tear. To start a claim, contact Customer Support with your order number, a description of the problem, and photos or video if possible. Q: Can I extend my warranty? A: Yes. Acme Protect extends your coverage to 3 years and also covers accidental damage from handling. It must be purchased within 30 days of your original order. Pricing depends on the product and is shown at checkout. Q: Do you offer repairs outside of warranty? A: For many products, yes. Contact Customer Support with your order number to request a repair quote. Repair costs and turnaround times vary by product and are not guaranteed to be less than the cost of a replacement. SECTION 5: CONTACT INFORMATION Customer Support hours: Monday-Saturday, 7:00 AM-7:00 PM Pacific Time. Closed on Sundays and major U.S. holidays. Email: help@acme-corp.example (responses within 1 business day) Phone: 1-800-555-0187 Live chat: available on Acme.com during support hours. QUESTION: Hi, I ordered a pair of Acme Anvil Boots (the heavy ones, they're like 45 lbs I think??) about 5 weeks ago and I've worn them around the house a few times. They're not defective, I just realized I don't like how they feel. I know there's a return window but I'm not sure if I'm still in it. Also if I can return them, would I have to pay for shipping, and is there anything else that'd be taken out of my refund? Last thing, could I get a gift card instead of the money back? ~~~~~~~~~~~ Planning: ~~~~~~~~~~~ <planning> 1. **Summary of the prompt template:** The template sets up a customer support chatbot for Acme Corporation. The bot gets an FAQ or policy document set and a customer's question. It should answer using details from those policies. The creator wants accurate, policy-grounded answers that don't invent information. The bot should handle both questions the policies cover and questions they only partly cover or don't cover. 2. **Variables:** - **DOCUMENTS:** In production, the company or a developer would supply this, probably pulled from a knowledge base, CMS, or database of policy pages and inserted by a retrieval system or a static config. A realistic value is a set of FAQ entries or policy sections, such as returns, shipping, warranty, and account or payment policies. It should be fairly long (several hundred words), with headings or Q&A format, concrete details (timeframes, fees, conditions, contact info), and a neutral, professional tone. Including a few edge-case details (exceptions, conditions) makes the test case more useful. Acme Corporation is a generic company, so I'll make it a consumer products seller, in the spirit of the cartoon company. - **QUESTION:** In production, a human customer would type this into a chat widget or support form. It should be casual, perhaps with a bit of frustration or missing context, and one to a few sentences long. A good test question needs information from more than one policy section and has a wrinkle, such as an item bought on sale or a return outside the window. That checks whether the bot reads the policies carefully instead of just matching keywords. I'll write a question about returning a product that has an exception in the policy, and ask a related follow-up the documents only partly cover. </planning>
Still about ACME corporation, but the question is different and so is the knowledge base.
From here, the world is your oyster -- you can generate more test cases by running the code in a loop, edit the planning more, evaluate Claude on these test cases, and put the test cases you make along with golden answers into your prompt as multishot examples.
To get golden answers, you can either write them yourself from scratch, or have Claude write an answer and then edit it to taste. With the advent of prompt caching, there's never been a better time to add tons of examples to your prompt to improve performance.
