How to Create a News Summarization Pipeline with GPT

Jacob Partington

Jacob Partington

·

19 minuten lesen

How to Create a News Summarization Pipeline with GPT

How to Create a News Summarization Pipeline with GPT

Anyone can call client.responses.create once and get a summary back. Doing it for 1,000 articles a day, without hallucinating, without lighting your OpenAI bill on fire, and without writing a summary the model invented from a headline it never read — that's a pipeline. This guide walks an AI developer through the production decisions generic LangChain tutorials skip: which chain to use, what it actually costs at news volume, how to ground summaries against hallucination, when to cache the prompt, and when to skip GPT entirely because the API already shipped you an extractive summary.

A GPT news summarization pipeline is a production system that ingests news articles (raw or via API), routes each article through one of four LangChain summarization strategies (stuff, map_reduce, refine, or a custom LCEL chain) based on token count, post-processes the output through a factuality grounding check, and persists the result with metadata for downstream use such as digests, alerts, or audio briefings. Unlike generic large-document summarization tutorials that ship one chain and move on, news summarization has to handle short articles in bulk plus occasional long features, must protect against entity-level hallucination because wrong facts in news are reputation incidents, and must compete on cost against a baseline that's free — namely, the extractive summary your news API already returns.

What You'll Build

A six-piece production pipeline:

  1. A baseline GPT call that summarizes one article from APITube
  2. A chain-type router that picks stuff / map_reduce / refine / custom LCEL by article length
  3. A monthly cost calculator that compares three OpenAI model tiers at news volume
  4. An entity-grounding check that flags hallucinated facts before publish
  5. Prompt caching that cuts input cost ~70% on a static system prompt
  6. An end-to-end runner: APITube → dedupe → chain → ground → store

Disclosure

We're APITube. Our /v1/news/everything endpoint already ships an extractive summary (key sentences with per-sentence sentiment) per article — for many use cases that's the right answer and you skip GPT entirely. We still wrote this guide because abstractive GPT summarization earns its cost for digests, audio briefings, and multi-article synthesis. We'll show the build-vs-buy line.

Prerequisites

  • Python 3.10+
  • openai>=1.40, langchain>=0.3, tiktoken, requests
  • An OpenAI API key with billing enabled
  • An APITube key (free tier: 30 req/30 min — enough for prototyping)
  • Familiarity with chat completions and basic LangChain primitives

Install:

pip install openai langchain langchain-openai tiktoken requests
export OPENAI_API_KEY=sk-...
export APITUBE_KEY=...

Step 1 — Baseline: Summarize One Article from APITube

Pull an article, summarize, count tokens. This is the call every other section optimizes around.

import os, requests
from openai import OpenAI
import tiktoken

openai_client = OpenAI()
enc = tiktoken.encoding_for_model("gpt-4o-mini")

def get_article(query):
    res = requests.get(
        "https://api.apitube.io/v1/news/everything",
        params={"title": query, "language.code": "en", "per_page": 1},
        headers={"X-API-Key": os.environ["APITUBE_KEY"]},
    )
    res.raise_for_status()
    return res.json()["results"][0]

SYSTEM = (
    "You are a news summarizer. Produce a 3-sentence summary that includes only "
    "facts present in the article. Do not infer beyond the text."
)

def summarize(article, model="gpt-4o-mini"):
    body = article.get("body") or article["description"]
    res = openai_client.chat.completions.create(
        model=model,
        messages=[
            {"role": "system", "content": SYSTEM},
            {"role": "user", "content": body},
        ],
        max_tokens=200,
    )
    return res.choices[0].message.content, len(enc.encode(body))

article = get_article("election")
summary, in_tokens = summarize(article)
print(in_tokens, "input tokens —", summary)

APITube's response shape is { status, results[], has_next_pages, page, request_id }. Each result has body (full text), description (short blurb), summary (extractive — we'll come back to this), keywords, is_duplicate, and words_count. Cache body if you'll re-summarize.

How Do You Summarize News Articles with GPT?

To summarize news articles with GPT, send the article body to a chat-completions endpoint with a tightly scoped system prompt that constrains the model to facts in the source ("only use facts present in the article; do not infer"), set a low max_tokens to bound output, and post-process for entity grounding to catch hallucinated names, numbers, or dates. For more than ~12K tokens of input, switch from a single call (stuff chain) to map_reduce or a custom LCEL chain.

Step 2 — Choose a Chain Type by Token Count

LangChain ships four chain strategies and most tutorials default to one. The right choice depends on input length, latency budget, and whether you want incremental output.

ChainMax input (tokens)Calls per articleUse when
stuff≤12,000 (fits modern context)1One article at a time, or a small bundle. Simple, cheap, lowest latency.
map_reduce12,000 – ~300,0001 per chunk + 1 reduceMulti-article batch, long features, parallelizable, no streaming requirement.
refinestreaming/incremental1 per chunk, sequentialContinuous ingest of related articles (e.g., a developing story), want progressively-better summary.
Custom LCEL (`promptmodelparser`)any

Most news pipelines should default to stuff. Modern chat models accept hundreds of thousands of input tokens, and a single news article rarely exceeds 4,000 tokens. map_reduce only earns its complexity when you summarize a multi-article digest in one call.

from langchain_openai import ChatOpenAI
from langchain.chains.summarize import load_summarize_chain
from langchain.docstore.document import Document
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser

def pick_chain(token_count, llm):
    if token_count <= 12_000:
        prompt = ChatPromptTemplate.from_messages([("system", SYSTEM), ("user", "{text}")])
        return prompt | llm | StrOutputParser()
    return load_summarize_chain(llm, chain_type="map_reduce")

llm = ChatOpenAI(model="gpt-4o-mini", max_tokens=200)
chain = pick_chain(in_tokens, llm)
result = chain.invoke({"text": article["body"]})

What Is the Best LangChain Chain Type for Summarization?

The best LangChain chain type for summarization depends on input length: use stuff for inputs up to ~12,000 tokens (one article or small bundle), map_reduce for 12K–500K tokens (multi-article digests), refine for incremental/streaming ingest of a developing story, and a custom LCEL chain (prompt | model | parser) when you need fine control over retries, structured output, or per-stage observability.

Step 3 — Cost Math at News Volume

Pricing as of 2026 — verify on openai.com before committing, because OpenAI updates the price list frequently. Numbers below are indicative orders of magnitude.

Assume an average article is 800 input tokens and the summary is 150 output tokens. For 1,000 articles per day across three model tiers:

TierModel classApprox input $/1MApprox output $/1MDaily input costDaily output costMonthly bill (1K/day)Monthly bill (10K/day)
CheapGPT-4o-mini class$0.15$0.60$0.12$0.09~$6~$60
MidGPT-4o class$2.50$10.00$2.00$1.50~$105~$1,050
PremiumGPT-4.1 / o-class$5–15$15–60$4.00–12.00$2.25–9.00~$190–630~$1,900–6,300

Formula:

monthly_cost = articles_per_day × 30 × (
    avg_input_tokens / 1_000_000  × input_price +
    avg_output_tokens / 1_000_000 × output_price
)

Three takeaways. First, the cheap tier is nearly free at 1,000 articles/day — start there and only escalate when quality demands it. Second, output tokens dominate cost on premium tiers; tighten max_tokens to 150–200 for headline-style summaries. Third, cost scales linearly with article volume but multiplicatively with model tier — a 17× cost increase from cheap to mid is rarely worth the quality bump for short news summaries; reserve premium for multi-article synthesis.

How Much Does It Cost to Summarize News with GPT?

Summarizing news with GPT costs approximately $6/month at 1,000 articles/day on a cheap-tier model (GPT-4o-mini class), $105/month on a mid-tier (GPT-4o class), and $190–630/month on a premium tier — at average inputs of 800 tokens and outputs of 150 tokens per article. Verify current pricing on openai.com because OpenAI updates rates regularly.

Step 4 — Hallucination Grounding for News

News summarization fails differently than generic doc summarization: a wrong company name, a misattributed quote, or an invented dollar amount becomes a public mistake the moment your summary ships. The cheap defense is entity grounding — extract entities (PERSON, ORG, MONEY, DATE, GPE) from the input and from the output, and flag any output entity that doesn't appear in the input.

import spacy

nlp = spacy.load("en_core_web_sm")
GROUND_LABELS = {"PERSON", "ORG", "MONEY", "DATE", "GPE", "PERCENT"}

def entities(text):
    return {(e.text, e.label_) for e in nlp(text).ents if e.label_ in GROUND_LABELS}

def grounding_check(input_text, summary_text):
    in_ents = entities(input_text)
    out_ents = entities(summary_text)
    in_strings = {e[0].lower() for e in in_ents}
    hallucinated = [e for e in out_ents if e[0].lower() not in in_strings]
    return hallucinated

bad = grounding_check(article["body"], result)
if bad:
    print("Hallucination flag:", bad)
    # retry with stricter prompt or escalate to human review

This catches the loud failures — invented people, made-up dollar figures — but not the quiet ones (paraphrased numbers, swapped pronouns). For higher-stakes pipelines, layer an NLI model (facebook/bart-large-mnli style) that scores each summary sentence against the input as entailment / contradiction / neutral. A flag rate above ~5% on your own corpus is a signal to switch models or tighten the prompt; below 1% is publishable with a sample-based human review.

How Do You Prevent Hallucinations in News Summaries?

Prevent hallucinations in news summaries with three layers: a constrained system prompt that forbids inference beyond the source, an entity grounding check that flags any PERSON/ORG/MONEY/DATE in the output absent from the input, and an NLI-based factuality classifier that scores each summary sentence against the article as entailment or contradiction. Reject or human-review any summary with a contradiction flag.

Step 5 — Prompt Caching for Production Cost Reduction

Static system prompts repeated across thousands of summarization calls qualify for prompt caching on modern OpenAI deployments. The mechanism: when the same prefix tokens hit the API repeatedly, cached tokens bill at a fraction of the normal input rate. The structural rule is simple — put everything static (system prompt, format examples, output schema) at the start of the request, put the dynamic article last.

For a typical news summarizer where the system prompt is ~500 tokens and the article body is ~800 tokens, the prompt is ~38% static. Push the static portion higher with examples and you can hit 70–80% static. With a 90% cache hit rate on the static prefix, total input cost drops by roughly:

savings = static_fraction × hit_rate × cache_discount
        = 0.80 × 0.90 × 0.50
        = 36%   on total input cost

Combined with the cheap-tier model, this is the difference between $6/month and $4/month at 1,000 articles/day — small in isolation, material at 100K articles/day where the same ratio means $400/month versus $250/month. Verify cache discount and eligibility on the current OpenAI docs.

Step 6 — End-to-End Pipeline

Tie it together. Pull from APITube, drop duplicates via is_duplicate, route by token count, ground, store.

from sqlite3 import connect
db = connect("summaries.db")
db.execute("CREATE TABLE IF NOT EXISTS summaries (article_id TEXT PRIMARY KEY, summary TEXT, flagged INT)")

def fetch_articles(query, limit=20):
    res = requests.get(
        "https://api.apitube.io/v1/news/everything",
        params={"title": query, "language.code": "en", "per_page": limit},
        headers={"X-API-Key": os.environ["APITUBE_KEY"]},
    )
    res.raise_for_status()
    return res.json()["results"]

def run(query):
    for a in fetch_articles(query):
        if a.get("is_duplicate"):
            continue
        body = a.get("body") or a["description"]
        in_tokens = len(enc.encode(body))
        chain = pick_chain(in_tokens, llm)
        summary = chain.invoke({"text": body})
        flagged = bool(grounding_check(body, summary))
        db.execute(
            "INSERT OR REPLACE INTO summaries VALUES (?, ?, ?)",
            (a["id"], summary, int(flagged)),
        )
    db.commit()

run("election")

At 1,000 articles/day this runs comfortably on a single worker. At 10K/day, parallelize with asyncio.gather and batch the OpenAI calls; at 100K/day, switch to the OpenAI Batch API for the 50% discount on non-time-sensitive summaries.

Can ChatGPT Summarize Multiple News Articles at Once?

ChatGPT can summarize multiple news articles in a single call by concatenating them into one input with clear delimiters and using a map_reduce chain that summarizes each article in the map step and synthesizes a unified digest in the reduce step. Modern context windows accept dozens of articles per call, but cost scales with total input tokens — for routine batch summarization, looping single-article stuff calls is usually cheaper and easier to debug.

Build vs Buy: GPT Pipeline vs Prebuilt Extractive Summary

APITube's /v1/news/everything already returns an extractive summary per article — key sentences pulled directly from the body, with per-sentence sentiment. You pay for it once via the API call. Honest comparison:

ApproachCost per articleOutput styleHallucination riskBest for
Extractive (APITube summary)included with article fetchVerbatim source sentencesNone (extractive can't invent facts)Search snippets, mobile feeds, alert push notifications, RSS-style digests
Hybrid (APITube summary + GPT polish)~$0.0001/article (cheap-tier polish)Smoothed, still groundedLow (constrain GPT to rephrase only)Email digests, executive briefings where reading flow matters
Full abstractive (GPT from body)~$0.0002–0.02/articleTrue synthesis, multi-sentence reasoningMedium-high (requires Step 4 grounding)Audio briefings, multi-article story summaries, cross-source synthesis

Pick extractive for any volume where reading-flow quality is not the differentiator. Move to hybrid when you need a uniform voice across thousands of articles. Reserve full abstractive for use cases where the model genuinely synthesizes — multi-article digests, audio briefings, "explain to me what happened today" interfaces — because that's the only place GPT earns its cost over the API's prebuilt summary.

Try It Free

Validate the entire pipeline on APITube's free tier — 30 requests per 30 minutes is enough to summarize a couple hundred articles end to end and decide whether you need GPT at all. Every result already includes the extractive summary, so you can compare it against your GPT output side by side. Try APITube free at apitube.io.

Resources

APITube - News API

Verwandte Artikel

So reduzieren Sie die Kosten für die News-API: 8 Strategien (sparen Sie 70–90 %)</h4><h2></h2>
Developer Guides

So reduzieren Sie die Kosten für die News-API: 8 Strategien (sparen Sie 70–90 %)</h4><h2></h2>

Ein Audit-zuerst-Leitfaden zum Senken der Ausgaben für Nachrichten-APIs um 70-90%: Fingerprinting Ihrer Aufrufe, Tiering der Cache-TTL nach Nachrichtengeschwindigkeit, Ersetzen von totem Polling durch Webhooks und Massenexport von Backfills. Kostenrechnung pro 100.000 Einheiten Code.

10 News-API-Filtermuster, die Sie kennen sollten (2026)</h4><h2></h2>
Developer Guides

10 News-API-Filtermuster, die Sie kennen sollten (2026)</h4><h2></h2>

10 ausführbare Nachrichten-API-Filtermuster – Titelsuche, Daten, Entitäten, Sentiment, Quellen und zusammengesetzte Abfragen – mit Curl-, Python- und JSON-Beispielen sowie der Falle, die Ihren ersten Tag verschwendet.

News API Quick Start: Ihre erste Anfrage in 5 Minuten</h4><h2></h2>
Developer Guides

News API Quick Start: Ihre erste Anfrage in 5 Minuten</h4><h2></h2>

Rufen Sie in 5 Minuten einen echten News-API-Endpunkt auf — Curl, JavaScript und Python nebeneinander, mit einer kommentierten JSON-Antwort und Korrekturen für die 401/429-Fehler, auf die Sie tatsächlich stoßen werden.

Telegram Nachrichten-Bot in Python (2026): aiogram APScheduler
Developer Guides

Telegram Nachrichten-Bot in Python (2026): aiogram APScheduler

Erstellen Sie einen Telegram-Nachrichtenbot in Python mit aiogram 3.27, APScheduler und APITube. Async, sentiment-gefiltert, Kanal-Auto-Post — vollständiger Code Docker.

Wir verwenden Cookies

Indem Sie auf "Akzeptieren" klicken, stimmen Sie der Speicherung von Cookies auf Ihrem Gerät zu Funktions- und Analysezwecken zu.