How to beginner · 3 min read

Improve summary quality tips

Quick answer
To improve summary quality with AI, use clear and specific prompts that define the summary length and focus. Employ models like gpt-4o with system instructions to guide tone and detail, and provide context-rich input for better coherence.

PREREQUISITES

  • Python 3.8+
  • OpenAI API key (free tier works)
  • pip install openai>=1.0

Setup

Install the openai Python package and set your API key as an environment variable for secure access.

bash
pip install openai>=1.0
output
Collecting openai
  Downloading openai-1.x.x-py3-none-any.whl
Installing collected packages: openai
Successfully installed openai-1.x.x

Step by step

Use the gpt-4o model with a system prompt that instructs the AI to generate concise, focused summaries. Provide the full text in the user message and specify summary length or style.

python
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

system_prompt = (
    "You are a helpful assistant that summarizes text clearly and concisely. "
    "Focus on key points and avoid unnecessary details. "
    "Limit the summary to 3-4 sentences."
)

text_to_summarize = (
    "Artificial intelligence (AI) is transforming industries by automating tasks, "
    "enhancing decision-making, and enabling new capabilities. It leverages machine learning, "
    "natural language processing, and computer vision to analyze data and generate insights. "
    "Businesses use AI for customer service, predictive analytics, and process optimization."
)

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[
        {"role": "system", "content": system_prompt},
        {"role": "user", "content": f"Summarize the following text:\n{text_to_summarize}"}
    ]
)

summary = response.choices[0].message.content
print("Summary:", summary)
output
Summary: AI is revolutionizing industries by automating tasks and improving decision-making. It uses machine learning, natural language processing, and computer vision to analyze data. Businesses apply AI in customer service, predictive analytics, and optimizing processes.

Common variations

  • Use different models like gpt-4o-mini for faster, cheaper summaries with slightly less detail.
  • Adjust the system prompt to change tone (e.g., formal, casual) or summary length.
  • Implement async calls with asyncio for concurrent summarization tasks.
  • Use streaming to display summary tokens as they generate for better UX.
python
import os
import asyncio
from openai import OpenAI

async def async_summarize(text: str):
    client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
    system_prompt = (
        "You are a concise summarizer. Limit output to 2 sentences."
    )
    response = await client.chat.completions.acreate(
        model="gpt-4o-mini",
        messages=[
            {"role": "system", "content": system_prompt},
            {"role": "user", "content": f"Summarize:\n{text}"}
        ]
    )
    return response.choices[0].message.content

async def main():
    text = "OpenAI's API enables developers to integrate powerful AI models into their applications, " \
           "providing capabilities like summarization, translation, and code generation."
    summary = await async_summarize(text)
    print("Async summary:", summary)

asyncio.run(main())
output
Async summary: OpenAI's API allows developers to add AI features such as summarization, translation, and code generation to their apps.

Troubleshooting

  • If summaries are too vague, make your system prompt more specific about length and focus.
  • If output is too long, explicitly limit sentences or word count in the prompt.
  • For inconsistent style, set tone instructions clearly in the system prompt.
  • Check API key and environment variables if you get authentication errors.

Key Takeaways

  • Use clear system prompts to guide summary length, tone, and focus for better quality.
  • Provide complete and relevant context in user messages to improve coherence.
  • Choose models balancing cost and detail, like gpt-4o for quality or gpt-4o-mini for speed.
  • Leverage async and streaming APIs for efficient and interactive summarization workflows.
  • Refine prompts iteratively to fix vagueness, verbosity, or style inconsistencies.
Verified 2026-04 · gpt-4o, gpt-4o-mini
Verify ↗