来源:https://github.com/anthropics/courses/blob/master/real_world_prompting/04_call_summarizer.ipynb 爬取日期:2026-03-22
Lesson 4: Call transcript summarizer
In this lesson, we’re going to write a complex prompt for a common customer use-case: summarizing. Specifically, we’ll summarize long customer service call transcripts. Our goal is to summarize customer service calls for customer support metrics. We want summaries of complete customer service calls to evaluate the efficacy of our customer support team. This means we’ll exclude calls that have connection issues, language barriers, and other issues that hinder effective summarization.
Let’s imagine we work for Acme Corporation, a company that sells smart home devices. The company handles hundreds of customer service calls daily and needs a way to quickly turn these conversations into useful, structured data.
Some important considerations include:
- Calls can be short and sweet or long and complicated.
- Customers might be calling about anything from a simple Wi-Fi connection issue to a complex system malfunction.
- We need our summaries in a specific format so they’re easy to analyze later.
- We have to be careful not to include any personal customer information in our summaries.
To help us out, we’ll follow the best practices we described previously:
- Use a system prompt to set the stage.
- Structure the prompt for optimal performance.
- Give clear instructions and define your desired output.
- Use XML tags to organize information.
- Handle special cases and edge scenarios.
- Provide examples to guide the model.
Understanding the data
We’ll use a variety of simulated customer service call transcripts from Acme Corporation’s smart home device support team.
A short and simple transcript:
call1 = """
Agent: Thank you for calling Acme Smart Home Support. This is Alex. How can I help you?
Customer: Hi, I can't turn on my smart light bulb.
Agent: I see. Have you tried resetting the bulb?
Customer: Oh, no. How do I do that?
Agent: Just turn the power off for 5 seconds, then back on. It should reset.
Customer: Ok, I'll try that. Thanks!
Agent: You're welcome. Call us back if you need further assistance.
"""
A medium-length transcript with an eventual resolution:
call2 = """
Agent: Acme Smart Home Support, this is Jamie. How may I assist you today?
Customer: Hi Jamie, my Acme SmartTherm isn't maintaining the temperature I set. It's set to 72 but the house is much warmer.
Agent: I'm sorry to hear that. Let's troubleshoot. Is your SmartTherm connected to Wi-Fi?
Customer: Yes, the Wi-Fi symbol is showing on the display.
Agent: Great. Let's recalibrate your SmartTherm. Press and hold the menu button for 5 seconds.
Customer: Okay, done. A new menu came up.
Agent: Perfect. Navigate to "Calibration" and press select. Adjust the temperature to match your room thermometer.
Customer: Alright, I've set it to 79 degrees to match.
Agent: Great. Press select to confirm. It will recalibrate, which may take a few minutes. Check back in an hour to see if it's fixed.
Customer: Okay, I'll do that. Thank you for your help, Jamie.
Agent: You're welcome! Is there anything else I can assist you with today?
Customer: No, that's all. Thanks again.
Agent: Thank you for choosing Acme Smart Home. Have a great day!
"""
A longer call with no resolution:
call3 = """
Agent: Thank you for contacting Acme Smart Home Support. This is Sarah. How can I help you today?
Customer: Hi Sarah, I'm having trouble with my Acme SecureHome system. The alarm keeps going off randomly.
Agent: I'm sorry to hear that. Can you tell me when this started happening?
Customer: It started about two days ago. It's gone off three times now, always in the middle of the night.
Agent: I see. Are there any error messages on the control panel when this happens?
Customer: No, I didn't notice any. But I was pretty groggy each time.
Agent: Understood. Let's check a few things. First, can you confirm that all your doors and windows are closing properly?
Customer: Yes, I've checked all of them. They're fine.
Agent: Okay. Next, let's check the battery in your control panel. Can you tell me if the low battery indicator is on?
Customer: Give me a moment... No, the battery indicator looks normal.
Agent: Alright. It's possible that one of your sensors is malfunctioning. I'd like to run a diagnostic, but I'll need to transfer you to our technical team for that. Is that okay?
Customer: Yes, that's fine. I just want this fixed. It's really disruptive.
Agent: I completely understand. I'm going to transfer you now. They'll be able to run a full system diagnostic and hopefully resolve the issue for you.
Customer: Okay, thank you.
Agent: You're welcome. Thank you for your patience, and I hope you have a great rest of your day.
"""
These examples showcase the variety of calls and considerations we need to handle:
- Calls have wildly different lengths.
- Calls feature various support issues (simple fixes, device malfunctions, complex problems).
- Some calls end with a resolution and others remain unresolved cases.
- Some calls require follow-up.
A simple version of the prompt
prompt = """
Summarize the following customer service call transcript. Focus on the main issue, how it was resolved, and any required follow-up.
{transcript}
"""
This basic prompt has several limitations:
- It doesn’t specify the desired output format
- It doesn’t provide guidance on how to handle different scenarios
- It doesn’t set any constraints on length or content
- It doesn’t instruct Claude to omit personal information
Adding a system prompt
system = """
You are an expert customer service analyst, skilled at extracting key information from call transcripts and summarizing them in a structured format.
Your task is to analyze customer service call transcripts and generate concise, accurate summaries while maintaining a professional tone.
"""
Structuring our main prompt
We rely on these prompting tips:
- Put long documents (our transcripts) at the top.
- Add detailed instructions and output format requirements.
- Introduce XML tags for structuring the prompt and output.
- Give Claude space “to think out loud”.
The input data
prompt_pt1 = """
Analyze the following customer service call transcript and generate a JSON summary of the interaction:
<transcript>
[INSERT CALL TRANSCRIPT HERE]
</transcript>
"""
Instructions and output format
JSON output structure:
{
"summary": {
"customerIssue": "Brief description of the main problem or reason for the call",
"resolution": "How the issue was addressed or resolved, if applicable",
"followUpRequired": true/false,
"followUpDetails": "Description of any necessary follow-up actions, or null if none required"
},
"status": "COMPLETE",
"ambiguities": ["List of any unclear or vague points in the conversation, or an empty array if none"]
}
prompt_pt2 = """
Instructions:
1. Read the transcript carefully.
2. Analyze the transcript, focusing on the main issue, resolution, and any follow-up required.
3. Generate a JSON object summarizing the key aspects of the interaction according to the specified structure.
Important guidelines:
- Confidentiality: Omit all specific customer data like names, phone numbers, and email addresses.
- Character limit: Restrict each text field to a maximum of 100 characters.
- Maintain a professional tone in your summary.
Output format:
Generate a JSON object with the following structure:
<json>
{
"summary": {
"customerIssue": "Brief description of the main problem or reason for the call",
"resolution": "How the issue was addressed or resolved, if applicable",
"followUpRequired": true/false,
"followUpDetails": "Description of any necessary follow-up actions, or null if none required"
},
"status": "COMPLETE",
"ambiguities": ["List of any unclear or vague points in the conversation, or an empty array if none"]
}
</json>
"""
Using XML tags and giving Claude room to think
prompt_pt3 = """
Before generating the JSON, please analyze the transcript in <thinking> tags.
Include your identification of the main issue, resolution, follow-up requirements, and any ambiguities.
Then, provide your JSON output in <json> tags.
"""
By asking Claude to put its analysis within <thinking> tags, we’re prompting it to break down its thought process before formulating the final JSON output. This encourages a more thorough and structured approach to analyzing the transcript.
Edge cases
In the real world, we encounter transcripts that perhaps we don’t want to summarize:
- Calls with connection issues
- Calls with language barriers
- Calls with garbled transcripts
- Calls with irrational or upset customers
Edge case call examples:
wrong_number_call = """
Agent: Acme Smart Home Support, Lisa speaking. How can I help you?
Customer: Is this tech support?
Agent: Yes, this is technical support for Acme Smart Home devices. What can I help you with?
Customer: Sorry, wrong number.
Agent: No problem. Have a nice day.
"""
incomplete_call = """
Agent: Acme Smart Home Support, this is Sarah. How can I assist you today?
Customer: The thing isn't working.
Agent: I'm sorry to hear that. Could you please specify which device you're having trouble with?
Customer: You know, the usual one. Gotta go, bye.
Agent: Wait, I need more infor... [call disconnected]
"""
garbled_call = """
Agent: Thank you for calling Acme Smart Home Support. This is Alex. How may I assist you today?
Customer: [garbled voice]
Agent: Hello? Are you there?
"""
language_barrier_call = """
Agent: Acme Smart Home Support, Sarah speaking. How can I help you today?
Customer: [Speaking in Spanish]
Agent: I apologize, but I don't speak Spanish. Do you speak English?
Customer: [Continues Spanish]
Agent: One moment please, I'll try to get a translator on the line...
"""
Without special handling, these edge cases produce full “COMPLETE” summaries, which would skew our analysis.
Further prompt improvements
Handling insufficient data
We flag edge-case calls by asking the model to output:
{
"status": "INSUFFICIENT_DATA"
}
new_instructions_addition = """
Insufficient data criteria:
If either of these conditions are met:
a) The transcript has fewer than 5 total exchanges, or
b) The customer's issue is unclear
c) The call is garbled, incomplete, or is hindered by a language barrier
Then return ONLY the following JSON:
{
"status": "INSUFFICIENT_DATA"
}
"""
Adding examples
Three example input/output pairs covering:
- A complete interaction that does not require follow up
- A complete interaction that does require follow up and contains ambiguities
- A non-summarizable interaction that contains insufficient data
Our final prompt
system = """
You are an expert customer service analyst, skilled at extracting key information from call transcripts and summarizing them in a structured format.
Your task is to analyze customer service call transcripts and generate concise, accurate summaries while maintaining a professional tone.
"""
prompt = """
Analyze the following customer service call transcript and generate a JSON summary of the interaction:
<transcript>
[INSERT CALL TRANSCRIPT HERE]
</transcript>
<instructions>
1. Read the transcript carefully.
2. Analyze the transcript, focusing on the main issue, resolution, and any follow-up required.
3. Generate a JSON object summarizing the key aspects of the interaction according to the specified structure.
Important guidelines:
- Confidentiality: Omit all specific customer data like names, phone numbers, and email addresses.
- Character limit: Restrict each text field to a maximum of 100 characters.
- Maintain a professional tone in your summary.
Output format:
Generate a JSON object with the following structure:
<json>
{
"summary": {
"customerIssue": "Brief description of the main problem or reason for the call",
"resolution": "How the issue was addressed or resolved, if applicable",
"followUpRequired": true/false,
"followUpDetails": "Description of any necessary follow-up actions, or null if none required"
},
"status": "COMPLETE",
"ambiguities": ["List of any unclear or vague points in the conversation, or an empty array if none"]
}
</json>
Insufficient data criteria:
If any of these conditions are met:
a) The transcript has fewer than 5 total exchanges
b) The customer's issue is unclear
c) The call is garbled, incomplete, or is hindered by a language barrier
Then return ONLY the following JSON:
{
"status": "INSUFFICIENT_DATA"
}
Examples:
<examples>
1. Complete interaction:
<transcript>
[SmartTherm recalibration example - resolved, no follow-up]
</transcript>
<thinking>...</thinking>
<json>
{
"summary": {
"customerIssue": "SmartTherm not maintaining set temperature, showing higher than set 72 degrees",
"resolution": "Guided customer through SmartTherm recalibration process",
"followUpRequired": false,
"followUpDetails": null
},
"status": "COMPLETE",
"ambiguities": []
}
</json>
2. Interaction requiring follow-up:
<transcript>
[SmartCam Wi-Fi connection issue - escalated to technical team]
</transcript>
<thinking>...</thinking>
<json>
{
"summary": {
"customerIssue": "Unable to connect new SmartCam to Wi-Fi",
"resolution": "Initial troubleshooting unsuccessful, issue escalated to technical team",
"followUpRequired": true,
"followUpDetails": "Technical team to contact customer within 24 hours for further assistance"
},
"status": "COMPLETE",
"ambiguities": ["Specific error message customer is receiving not mentioned"]
}
</json>
3. Insufficient data:
<transcript>
[Brief, vague call about smart lock not working - customer leaves]
</transcript>
<thinking>
This transcript has fewer than 5 exchanges and the customer's issue is unclear.
</thinking>
<json>
{
"status": "INSUFFICIENT_DATA"
}
</json>
</examples>
</instructions>
Before generating the JSON, please analyze the transcript in <thinking> tags.
Include your identification of the main issue, resolution, follow-up requirements, and any ambiguities.
Then, provide your JSON output in <json> tags.
"""
Prompt structure overview:
Analyze the following customer service call transcript and generate a JSON summary of the interaction:
<transcript>
[INSERT CALL TRANSCRIPT HERE]
</transcript>
<instructions>
- General instructions and guidelines
- Output JSON format description
- Insufficient data (edge-case) criteria
<examples>
varied example inputs and outputs
</examples>
</instructions>
Before generating the JSON, please analyze the transcript in <thinking> tags.
Include your identification of the main issue, resolution, follow-up requirements, and any ambiguities.
Then, provide your JSON output in <json> tags.
Extracting JSON from the response:
import re
def summarize_call_with_final_prompt(transcript):
final_prompt = prompt.replace("[INSERT CALL TRANSCRIPT HERE]", transcript)
response = client.messages.create(
model="claude-3-sonnet-20240229",
system=system,
max_tokens=4096,
messages=[
{"role": "user", "content": final_prompt}
]
)
json_content = re.search(r'<json>(.*?)</json>', response.content[0].text, re.DOTALL)
if json_content:
print(json_content.group(1).strip())
else:
print("No JSON content found in the response.")
Final test results
Normal calls - Properly summarized with structured JSON:
{
"summary": {
"customerIssue": "Unable to turn on smart light bulb",
"resolution": "Agent guided customer to reset the bulb by cycling power off and on",
"followUpRequired": false,
"followUpDetails": null
},
"status": "COMPLETE",
"ambiguities": []
}
Ambiguous calls - Ambiguities correctly identified:
{
"summary": {
"customerIssue": "SmartLock not reliably locking automatically or through app, behavior is inconsistent",
"resolution": "Troubleshooting attempted but incomplete due to lack of model details, customer had to leave",
"followUpRequired": true,
"followUpDetails": "Customer to call back for further troubleshooting of SmartLock issue when available"
},
"status": "COMPLETE",
"ambiguities": [
"Unclear if related SmartTherm issue mentioned",
"SmartLock model not identified",
"Customer's contact number not confirmed"
]
}
Edge case calls - Correctly flagged as insufficient data:
{
"status": "INSUFFICIENT_DATA"
}
Summary
This lesson demonstrates comprehensive prompt engineering for a real-world customer service use case:
- Starting Simple: A basic prompt for transcript summarization
- Adding Structure: System prompts, XML tags, and JSON output formatting
- Improving Quality: Clear instructions, character limits, and confidentiality guidelines
- Handling Edge Cases: Adding logic to filter out non-summarizable calls
- Using Examples: Providing diverse in-context examples to guide the model
- Testing Iteratively: Testing at each stage to validate improvements
The final prompt successfully:
- Summarizes complete customer service calls into structured JSON
- Identifies main issues, resolutions, and follow-up requirements
- Detects ambiguities in transcripts
- Filters out edge-case calls that shouldn’t be summarized
- Maintains confidentiality by removing personal data
- Adheres to character limits for consistency