Code beginner · 3 min read

How to summarize text with AI in Python

Direct answer
Use the OpenAI Python SDK to call client.chat.completions.create with a summarization prompt and extract the summary from response.choices[0].message.content.

Setup

Install
bash
pip install openai
Env vars
OPENAI_API_KEY
Imports
python
import os
from openai import OpenAI

Examples

inSummarize: OpenAI develops powerful AI models that help developers build intelligent applications.
outOpenAI creates advanced AI models to enable intelligent app development.
inSummarize: The Python programming language is widely used for AI due to its simplicity and rich ecosystem.
outPython is popular for AI because of its ease of use and extensive libraries.
inSummarize: This text is very short.
outThe text is brief.

Integration steps

  1. Install the OpenAI Python SDK and set the OPENAI_API_KEY environment variable.
  2. Import OpenAI and initialize the client with the API key from os.environ.
  3. Create a chat completion request with a model like gpt-4o and a message prompting summarization.
  4. Send the request and receive the response object.
  5. Extract the summary text from response.choices[0].message.content.
  6. Print or use the summary as needed.

Full code

python
import os
from openai import OpenAI

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

text_to_summarize = (
    "OpenAI develops powerful AI models that help developers build intelligent applications."
)

messages = [
    {"role": "user", "content": f"Summarize the following text concisely:\n\n{text_to_summarize}"}
]

response = client.chat.completions.create(
    model="gpt-4o",
    messages=messages
)

summary = response.choices[0].message.content
print("Summary:", summary)
output
Summary: OpenAI creates advanced AI models to enable intelligent app development.

API trace

Request
json
{"model": "gpt-4o", "messages": [{"role": "user", "content": "Summarize the following text concisely:\n\nOpenAI develops powerful AI models that help developers build intelligent applications."}]}
Response
json
{"choices": [{"message": {"content": "OpenAI creates advanced AI models to enable intelligent app development."}}], "usage": {"prompt_tokens": 30, "completion_tokens": 15, "total_tokens": 45}}
Extractresponse.choices[0].message.content

Variants

Streaming Summarization

Use streaming to display the summary as it is generated for better user experience with longer texts.

python
import os
from openai import OpenAI

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

text_to_summarize = (
    "OpenAI develops powerful AI models that help developers build intelligent applications."
)

messages = [
    {"role": "user", "content": f"Summarize the following text concisely:\n\n{text_to_summarize}"}
]

stream = client.chat.completions.create(
    model="gpt-4o",
    messages=messages,
    stream=True
)

print("Summary:", end=" ")
for chunk in stream:
    delta = chunk.choices[0].delta.content or ""
    print(delta, end="", flush=True)
print()
Async Summarization

Use async when integrating summarization into asynchronous applications or web servers.

python
import os
import asyncio
from openai import OpenAI

async def summarize_text(text: str):
    client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
    messages = [{"role": "user", "content": f"Summarize the following text concisely:\n\n{text}"}]
    response = await client.chat.completions.acreate(model="gpt-4o", messages=messages)
    return response.choices[0].message.content

async def main():
    text = "OpenAI develops powerful AI models that help developers build intelligent applications."
    summary = await summarize_text(text)
    print("Summary:", summary)

asyncio.run(main())
Using Claude Model for Summarization

Use Anthropic Claude models if you prefer Claude's style or have an Anthropic API key.

python
import os
from anthropic import Anthropic

client = Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])

text_to_summarize = (
    "OpenAI develops powerful AI models that help developers build intelligent applications."
)

prompt = f"Summarize the following text concisely:\n\n{text_to_summarize}"

response = client.messages.create(
    model="claude-3-5-sonnet-20241022",
    max_tokens=200,
    system="You are a helpful assistant.",
    messages=[{"role": "user", "content": prompt}]
)

summary = response.content[0].text
print("Summary:", summary)

Performance

Latency~800ms for <code>gpt-4o</code> non-streaming summarization
Cost~$0.002 for a 500-token summarization call on <code>gpt-4o</code>
Rate limitsTier 1: 500 requests per minute / 30,000 tokens per minute
  • Keep input text concise to reduce prompt tokens.
  • Use shorter model variants like <code>gpt-4o-mini</code> for cheaper summarization.
  • Avoid unnecessary system or assistant messages to save tokens.
ApproachLatencyCost/callBest for
Standard gpt-4o call~800ms~$0.002High-quality, general summarization
Streaming gpt-4o~800ms initial + streaming~$0.002Long texts with real-time output
Async gpt-4o~800ms~$0.002Integration in async apps
Anthropic claude-3-5-sonnet-20241022~900msCheck Anthropic pricingClaude-style summaries

Quick tip

Frame your prompt clearly with instructions like 'Summarize the following text concisely' to get focused summaries.

Common mistake

Beginners often forget to extract the summary from <code>response.choices[0].message.content</code> and instead print the entire response object.

Verified 2026-04 · gpt-4o, gpt-4o-mini, claude-3-5-sonnet-20241022
Verify ↗