We are going to build a document summarization agent that ingests long-form text and emits a structured brief with a title, one-paragraph summary, and bullet-point takeaways. This is useful for research analysts, product managers, and developers who need to distill reports, meeting transcripts, or technical documentation without reading every page. Because Oxlo.ai charges a flat rate per request rather than per token, running this on long transcripts or whitepapers stays predictable and cheap.
What you'll need
- Python 3.10 or newer.
- The OpenAI SDK:
pip install openai. - An Oxlo.ai API key from https://portal.oxlo.ai.
Step 1: Initialize the Oxlo.ai client
Import the SDK and point it at Oxlo.ai's OpenAI-compatible endpoint. Replace the API key with your own from the portal.
from openai import OpenAI
client = OpenAI(
base_url="https://api.oxlo.ai/v1",
api_key="YOUR_OXLO_API_KEY"
)
Step 2: Write the system prompt
The system prompt locks the model into a consistent JSON schema so we can parse the result reliably. I keep it strict: factual, no markdown wrappers, and only the three requested keys.
SYSTEM_PROMPT = """You are a precise document summarization agent.
When given a text, output a JSON object with exactly these keys:
- title: a concise, informative title for the document.
- summary: a one-paragraph summary of the main points.
- key_points: an array of 3 to 5 bullet-worthy takeaways.
Be factual. Do not hallucinate details not present in the text.
Output only valid JSON with no markdown formatting."""
Step 3: Build the summarization function
This helper takes a raw string, sends it to Oxlo.ai with the system prompt, and parses the returned JSON. I use Llama 3.3 70B here because it is a strong general-purpose model for this kind of extraction. If you later need to summarize entire books in one shot, swap the model ID to deepseek-v4-flash to take advantage of its 1M context window at the same flat request cost.
import json
def summarize_document(text: str) -> dict:
"""Send text to Oxlo.ai and return structured summary fields."""
response = client.chat.completions.create(
model="llama-3.3-70b",
messages=[
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": text},
],
)
raw = response.choices[0].message.content
return json.loads(raw)
Step 4: Process a document
Feed the function a real block of text. In production this could come from a PDF parser, a transcript API, or a web scraper. For now, we will use a hardcoded industry report excerpt.
DOCUMENT = """
Artificial intelligence adoption in enterprise software has accelerated sharply over the past eighteen months. Organizations are moving from pilot programs to production deployments for customer support automation, code generation, and document processing. Key challenges remain around data privacy, hallucination rates, and integration with legacy systems. A 2024 industry survey found that 62 percent of enterprises now mandate human-in-the-loop review for any externally facing AI output. Meanwhile, inference costs have become a primary budgeting concern, particularly for long-context applications such as contract analysis and meeting summarization. New pricing models, including flat per-request structures, are gaining traction as finance teams seek predictable spend. The report concludes that the next phase of growth will depend on tool-use reliability and multi-step agentic workflows rather than raw model scale alone.
"""
result = summarize_document(DOCUMENT)
print(json.dumps(result, indent=2))
Run it
Here is the complete script. Save it as summarize.py, set your API key, and run python summarize.py.
from openai import OpenAI
import json
client = OpenAI(
base_url="https://api.oxlo.ai/v1",
api_key="YOUR_OXLO_API_KEY"
)
SYSTEM_PROMPT = """You are a precise document summarization agent.
When given a text, output a JSON object with exactly these keys:
- title: a concise, informative title for the document.
- summary: a one-paragraph summary of the main points.
- key_points: an array of 3 to 5 bullet-worthy takeaways.
Be factual. Do not hallucinate details not present in the text.
Output only valid JSON with no markdown formatting."""
def summarize_document(text: str) -> dict:
response = client.chat.completions.create(
model="llama-3.3-70b",
messages=[
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": text},
],
)
return json.loads(response.choices[0].message.content)
if __name__ == "__main__":
DOCUMENT = """
Artificial intelligence adoption in enterprise software has accelerated sharply over the past eighteen months. Organizations are moving from pilot programs to production deployments for customer support automation, code generation, and document processing. Key challenges remain around data privacy, hallucination rates, and integration with legacy systems. A 2024 industry survey found that 62 percent of enterprises now mandate human-in-the-loop review for any externally facing AI output. Meanwhile, inference costs have become a primary budgeting concern, particularly for long-context applications such as contract analysis and meeting summarization. New pricing models, including flat per-request structures, are gaining traction as finance teams seek predictable spend. The report concludes that the next phase of growth will depend on tool-use reliability and multi-step agentic workflows rather than raw model scale alone.
"""
output = summarize_document(DOCUMENT)
print(json.dumps(output, indent=2))
You should see output similar to this:
{
"title": "Enterprise AI Adoption Accelerates Amid Cost and Integration Challenges",
"summary": "Enterprise AI adoption has shifted from pilots to production over the past eighteen months, with widespread use in support, coding, and document processing. Despite this growth, organizations face hurdles around privacy, hallucinations, and legacy integration, leading most to require human review of AI outputs. Rising inference costs for long-context tasks are driving interest in predictable pricing models, and future growth is expected to hinge on reliable tool use and agentic workflows rather than simply larger models.",
"key_points": [
"Enterprise AI has moved from pilot programs to production deployments.",
"Data privacy, hallucinations, and legacy integration remain significant barriers.",
"62 percent of enterprises require human review for external-facing AI outputs.",
"Inference costs for long-context work are a major budget concern.",
"Future progress will likely come from agentic reliability, not just model scale."
]
}
Wrap-up and next steps
The agent works for single-shot summaries, but real-world documents often exceed context limits. One upgrade is recursive summarization: split long texts into chunks, summarize each chunk, then pass the chunk summaries back through the agent for a unified brief. Because Oxlo.ai uses request-based pricing, those extra passes do not inflate your bill based on total token volume.
Another practical next step is to wrap this function in a lightweight API with FastAPI and connect it to an S3 trigger or email inbox so every uploaded report or transcript is automatically summarized and stored.
Top comments (0)