Skip to main content
What is News API blog banner

To summarize news articles with an LLM API in Python, request recent articles from the NewsData.io latest endpoint, send each title and description to a chat completions endpoint with a short instruction, and print the reply. This tutorial builds that script with DeepSeek V4 Pro on AI/ML API in 51 lines.

The script needs only the requests library. Each step below comes with a screenshot of the code and a screenshot of a real run, so you can repeat every action on your own machine.

How the pipeline works

The script makes two kinds of HTTP calls. A GET request to https://newsdata.io/api/1/latest returns a JSON object with a results list. Each item in the list is one article with the fields title, description, link, source_id and pubDate. A POST request to https://api.aimlapi.com/v1/chat/completions sends the article text to the model and returns the summary in choices[0].message.content.

The script filters out articles that have no description, because there is nothing to summarize in them. It then sends one request per article, so a failed request affects one summary and not the whole batch.

The four stages of the script.

What you need before you start

You need four things.

  • Python 3 with the requests library.
  • A NewsData.io API key. Register on the NewsData.io sign up page and copy the key from your dashboard.
  • An AI/ML API key. Create an account on the AI/ML API website and generate a key on the API keys page.
  • A small balance on AI/ML API. Billing is per token. In the test run for this article, one summary used 154 input tokens and 99 output tokens, which costs about $0.0005 at the DeepSeek V4 Pro prices of $1.17 and $3.51 per 1M tokens.

According to the NewsData.io documentation, a free account returns up to 10 articles per request and covers the past 48 hours. That is enough for this tutorial. The script requests 5 articles, so the whole run costs about $0.003 on the AI/ML API side.

Install the library and store both keys in environment variables. The script reads the keys from the environment, so they never end up in your source code or in a Git repository.

On Windows PowerShell, set a variable with $env:NEWSDATA_KEY=”your_newsdata_key” instead of the export command.

Step 1. Set up the keys and the prompt

Create a file named news_summary.py. The first block imports the libraries, reads the keys, and defines the two URLs, the model ID and the system prompt.

The system prompt does three jobs. It limits the model to the facts in the text, it fixes the length at 2 sentences, and it forbids opinions and background. A fixed length makes the output easy to compare across articles. A limit to the given facts keeps the model from adding details that the source does not contain.

Step 2. Fetch the latest articles

The fetch_articles function sends the key, the search query, the language and the number of articles as URL parameters. The q parameter searches for keywords in the article, and language set to en keeps the results in English. The call to raise_for_status stops the script on any HTTP error, such as a wrong key. The last line keeps only the articles that have a description.

Step 3. Summarize one article

The summarize function joins the title and the description into one text and sends it as the user message, next to the system prompt. The request body follows the OpenAI chat format, so the same code works with other chat models on AI/ML API after you change the model ID. The function returns the text of the first choice.

The max_tokens value is 400. DeepSeek V4 Pro is a reasoning model, and its reasoning tokens are counted inside the output tokens. In the test run, 68 of the 99 output tokens were reasoning tokens. A very small max_tokens value can leave too little room for the answer, so keep some margin.

Step 4. Print the results and run the script

The main function fetches 5 articles about artificial intelligence, summarizes each one and prints the title, the source, the summary and the link.

Run the script.

The output below comes from a real run of this script. The summary of each article appears between its title and its link.

Output of news_summary.py. The model returned two sentences for each article.

Each summary has exactly 2 sentences, as the system prompt requires. The first sentence often restates the title, because a description is only a few lines long. The summary becomes more useful when you pass the full text of the article instead of the description. In that case, change the first line of the text variable in the summarize function and keep the rest of the code.

Which model to use

The script uses the DeepSeek V4 Pro API with the model ID deepseek/deepseek-v4-pro. According to the model page, the context window is 1,000,000 tokens, the maximum output is 384,000 tokens, and the model was released on April 24, 2026. The page lists streaming, structured output, tools and reasoning among the supported parameters.

ItemValue
Model IDdeepseek/deepseek-v4-pro
Context window1,000,000 tokens
Maximum output384,000 tokens
Input price$1.17 per 1M tokens
Output price$3.51 per 1M tokens
Cost of one summary in the test runAbout $0.0005

Verdict for this tutorial. DeepSeek V4 Pro at about $0.0005 per summarized article. That figure comes from one request with 154 input tokens and 99 output tokens, so measure your own articles before you plan a budget.

To try another model, replace the value of MODEL with an ID from the AI/ML API Models catalog. Parameters differ between models, so check the model page if a request returns an error.

Common errors and how to handle them

  • NewsData.io returns 401. The response body reads {“status”:”error”,”results”:{“message”:”The provided API key is not valid.”,”code”:”Unauthorized”}}. Check that NEWSDATA_KEY is set in the same terminal session that runs the script.
  • AI/ML API returns 401. The message says that the request requires a valid API key and points to the API keys page. Check AIMLAPI_KEY in the same way.
  • The script prints nothing. The query returned no articles with a description. Use a broader query or remove the language parameter.
  • A summary looks cut off. Raise the max_tokens value, because reasoning tokens use part of it.

Do not remove the timeout argument from the requests calls. Without it, a stalled connection blocks the script forever.

Summary

  • Get articles from the NewsData.io latest endpoint and keep those with a description.
  • Send the title and the description to the chat completions endpoint with a system prompt that fixes the length.
  • One summary used 154 input and 99 output tokens, about $0.0005 with DeepSeek V4 Pro.
  • Keep both API keys in environment variables.

FAQ

What is a news summarization API?

A news summarization API turns news text into a short summary. In this tutorial, two services work together. A news API such as NewsData.io supplies the articles, and a language model behind a chat completions endpoint writes the summary. You can swap either part without changing the other.

How to summarize news articles with Python?

Fetch articles with a GET request to a news API, then send the title and text of each article to a chat completions endpoint with a prompt that asks for a summary of a fixed length. Read the answer from choices[0].message.content. The script in this article does this in 51 lines.

How much does it cost to summarize news with an LLM API?

Billing is per token. In the test run for this article, one summary used 154 input tokens and 99 output tokens. At the DeepSeek V4 Pro prices of $1.17 and $3.51 per 1M tokens, that is about $0.0005 per article. Longer articles use more input tokens.

How to get a NewsData.io API key?

Register on the NewsData.io sign up page and copy the API key from your dashboard. The key goes into the apikey parameter of every request. According to the NewsData.io documentation, a free account returns up to 10 articles per request and covers the past 48 hours.

How to get an AI/ML API key?

Create an account on AI/ML API and generate a key on the API keys page. Send it in the Authorization header as a Bearer token. Store it in an environment variable such as AIMLAPI_KEY and never write it into source code.

Can I use a different model for news summaries?

Yes. The request follows the chat completions format, so you change only the model ID in the MODEL constant. Pick an ID from the AI/ML API model catalog. Parameters such as max_tokens can behave differently between models, so check the model page after the switch.

Leave a Reply