Build an AI Text Summarizer with Python
Every project so far has been conversational. This part builds a genuinely different real AI task — summarization, condensing a real, existing piece of text rather than generating a response to an open question. The real use case: GreenDesk's support team, drowning in long, detailed real ticket threads.
The real, complete summarizer
# summarizer.py
from shared.ai_client import get_client
def summarize(text: str, max_sentences: int = 3) -> str:
client = get_client()
response = client.messages.create(
model="claude-sonnet-5",
max_tokens=512,
system=(
f"Summarize the following support ticket thread in at "
f"most {max_sentences} sentences. Focus specifically on: "
f"the customer's core issue, what's already been tried, "
f"and what real action is needed next. Do not include "
f"pleasantries or restate the customer's name."
),
messages=[{"role": "user", "content": text}],
)
return response.content[0].textA real, concrete example
real_ticket_thread = """
Customer: Our team can't see task assignments updating in real time
anymore, started yesterday afternoon.
Agent: Can you confirm your browser and whether you've tried a hard
refresh?
Customer: Chrome, latest version. Tried refreshing, still broken.
Agent: Checking on our end now.
Customer: Also noticed it's only happening on the Projects board, not
the Tasks list view.
"""
print(summarize(real_ticket_thread))Real, expected output:
"Customer reports task assignments not updating in real time on the
Projects board specifically (Tasks list view unaffected), started
yesterday afternoon. Hard refresh in Chrome didn't resolve it. Needs
backend investigation into real-time sync for the Projects board view."This is a genuinely different, useful output shape than any chatbot response so far — dense, specific, and structured around exactly the three things the system prompt asked for, not a conversational reply.
Why the system prompt's specificity matters more here than in earlier parts
Vague: "Summarize this"
Specific (used above): "Focus on the core issue, what's been tried,
and what action is needed next"This directly applies the AI Fundamentals series' own prompt engineering guidance to a genuinely concrete, real case — a vague "summarize this" instruction produces a real, generic, unpredictable summary shape every time; specifying exactly what real information a summary needs to preserve is what makes the output consistently useful for GreenDesk's actual support workflow, rather than varying wildly between tickets.
A real summarizer's system prompt is genuinely doing more design work than its code — the summarize() function itself is barely different from part 1's ask() helper. The real skill in this project is entirely in specifying what a good summary actually needs to contain for this specific, real use case, not in the Python code wrapping the API call.
A real, practical length-control technique
def summarize(text: str, target_length: str = "3 sentences") -> str:
system = (
f"Summarize the following text in exactly {target_length}. "
f"Do not exceed this length under any circumstances."
)
# ...Real LLMs treat a length instruction as a strong but not perfectly guaranteed constraint — "exactly 3 sentences" produces genuinely more reliable results than a vague "keep it short," but a real, production summarizer that needs a guaranteed hard length limit should still validate the actual output length in code and, if needed, truncate or retry, rather than trusting the prompt alone.
Summarizing real, longer documents: a genuine limit worth flagging now
A single real support ticket thread: comfortably fits in one request
A real, entire quarter's worth of tickets, or a long document:
genuinely risks exceeding the context window (AI Fundamentals'
own coverage) in a single requestThis part's summarizer works correctly for a real, single, reasonably-sized document — the next part covers exactly what to do when the real text to summarize is too large to fit in one request at all.
Next: handling documents that exceed the context window — the real chunking technique this summarizer needs for genuinely long, real input.