DEV Community

shashank ms
shashank ms

Posted on

Building Text Summarization Apps with LLMs

We are going to build a command-line summarizer that turns articles, transcripts, and reports into structured takeaways: a title, a short paragraph, and three bullets. It is useful for analysts, product managers, and developers who need to process large volumes of text without managing infrastructure. We will run it on Oxlo.ai because flat per-request pricing keeps long-document chunking affordable, and the OpenAI-compatible SDK means no client library changes.

What you'll need

Step 1: Set up the Oxlo.ai client

Create a file named summarizer.py and initialize the client. We will make a quick call to confirm the endpoint is live.

from openai import OpenAI

client = OpenAI(base_url="https://api.oxlo.ai/v1", api_key="YOUR_OXLO_API_KEY")

response = client.chat.completions.create(
    model="llama-3.3-70b",
    messages=[
        {"role": "user", "content": "Summarize this in one sentence: The quick brown fox jumps over the lazy dog, which is a well-known pangram."},
    ],
)

print(response.choices[0].message.content)

Step 2: Write the system prompt

Define the output structure in a constant so it is easy to tweak without touching business logic. I ask for a title, a one-paragraph summary, and three bullets to keep the result scannable.

SYSTEM_PROMPT = """You are a precise summarization engine. Given a text, produce exactly:

1. A title of no more than 10 words.
2. A one-paragraph summary of no more than 75 words.
3. Three bullet points capturing the key takeaways.

Use plain language. Do not add preamble or markdown outside the requested sections."""

Step 3: Build the core summarizer function

Wrap the API call in a reusable summarize() helper. I default to llama-3.3-70b because it is a strong general-purpose model, but you can swap in qwen-3-32b or deepseek-v3.2 depending on whether you need multilingual support or want to experiment on the free tier.

from openai import OpenAI

client = OpenAI(base_url="https://api.oxlo.ai/v1", api_key="YOUR_OXLO_API_KEY")

SYSTEM_PROMPT = """You are a precise summarization engine. Given a text, produce exactly:

1. A title of no more than 10 words.
2. A one-paragraph summary of no more than 75 words.
3. Three bullet points capturing the key takeaways.

Use plain language. Do not add preamble or markdown outside the requested sections."""

def summarize(text: str, model: str = "llama-3.3-70b") -> str:
    response = client.chat.completions.create(
        model=model,
        messages=[
            {"role": "system", "content": SYSTEM_PROMPT},
            {"role": "user", "content": text},
        ],
        temperature=0.3,
        max_tokens=512,
    )
    return response.choices[0].message.content

Step 4: Chunk long documents and summarize each part

Real documents often exceed the context window. The standard fix is a map-reduce pipeline: split the text into overlapping chunks, summarize each independently, then merge the results. On Oxlo.ai, this is especially practical because the platform uses request-based pricing. Whether you send a short prompt or a very long one, the cost per request does not scale with token count, so chunking a long document does not inflate your bill the way token-based providers do. See https://oxlo.ai/pricing for current plan details.

def chunk_text(text: str, chunk_size: int = 3000, overlap: int = 200) -> list[str]:
    words = text.split()
    chunks = []
    start = 0
    while start < len(words):
        end = start + chunk_size
        chunks.append(" ".join(words[start:end]))
        start = end - overlap
    return chunks

def summarize_long(text: str, model: str = "llama-3.3-70b") -> list[str]:
    chunks = chunk_text(text)
    summaries = []
    for i, chunk in enumerate(chunks, 1):
        print(f"Processing chunk {i} of {len(chunks)}...")
        summaries.append(summarize(chunk, model=model))
    return summaries

Step 5: Consolidate partial summaries into a final output

After the map step we have multiple partial summaries. We feed them back into the model as a single context window to produce one coherent result. I use qwen-3-32b here because it handles long-context reasoning well, but any Oxlo.ai model works.

REDUCE_PROMPT = """You are a senior editor. Given the following partial summaries of a longer document, write a single, unified summary. Preserve the same structure: title, one-paragraph summary, and three key takeaways. Resolve any contradictions and remove redundancy."""

def consolidate(partials: list[str], model: str = "qwen-3-32b") -> str:
    combined = "\n\n---\n\n".join(partials)
    response = client.chat.completions.create(
        model=model,
        messages=[
            {"role": "system", "content": REDUCE_PROMPT},
            {"role": "user", "content": combined},
        ],
        temperature=0.3,
        max_tokens=1024,
    )
    return response.choices[0].message.content

Step 6: Add a CLI interface

Use argparse so you can pipe any .txt file through the tool. The script automatically skips chunking for inputs under 4,000 words.

import argparse

def main():
    parser = argparse.ArgumentParser(description="Summarize a text file with Oxlo.ai.")
    parser.add_argument("file", help="Path to the text file to summarize.")
    parser.add_argument("--model", default="llama-3.3-70b", help="Model for chunk summarization.")
    parser.add_argument("--reduce-model", default="qwen-3-32b", help="Model for final consolidation.")
    args = parser.parse_args()

    with open(args.file, "r", encoding="utf-8") as f:
        text = f.read()

    if len(text.split()) < 4000:
        print(summarize(text, model=args.model))
    else:
        partials = summarize_long(text, model=args.model)
        print(consolidate(partials, model=args.reduce_model))

if __name__ == "__main__":
    main()

Run it

Save a long article as article.txt, then run:

$ python summarizer.py article.txt --model llama-3.3-70b --reduce-model qwen-3-32b

Example output:

Processing chunk 1 of 3...
Processing chunk 2 of 3...
Processing chunk 3 of 3...

Title: Oxlo.ai Launches Flat-Rate Inference Platform

Summary: Oxlo.ai offers a developer-first inference platform that charges a flat rate per API request. This model removes the cost uncertainty typically tied to token count, making it ideal for long-context and agentic workloads.

Key Takeaways:
- Flat per-request pricing eliminates ballooning costs on long inputs.
- The platform hosts over 45 models with full OpenAI SDK compatibility.
- No cold starts and a simple base URL swap make migration trivial.

Next steps

Switch the output to JSON mode by setting response_format={"type": "json_object"} and updating the prompts so downstream tools can parse title, summary, and bullets programmatically. Alternatively, put this behind a FastAPI endpoint and host it on the Oxlo.ai free tier using deepseek-v3.2, which includes 60 requests per day and requires no upfront credit card.

Top comments (0)