A Practical Long-Context Document Summary Pipeline
Why Long Documents Need a Deliberate Strategy
Summarizing a 200-page contract, research archive, support-ticket export, or technical specification is not simply a matter of sending more text to a model. Even when a long-context model can accept the entire document, input cost, retrieval quality, and output focus still matter. A model may miss a buried exception, overemphasize repeated sections, or produce a generic summary if the prompt does not define the audience and required evidence.
A practical approach is hierarchical summarization: split the source into meaningful chunks, summarize each chunk with structured instructions, then ask the model to synthesize those intermediate summaries. This reduces cost, makes failures easier to inspect, and works whether the document fits in one context window or not.
Set Up an OpenAI-Compatible Client
59API is a useful low-cost option for this workflow because it provides pay-as-you-go access to native official-quality Claude models, including Opus, Sonnet, Haiku, and Fable, plus GPT models. Its OpenAI SDK compatibility means an existing application can generally switch by changing the base URL and API key rather than rewriting its integration.
Install the Python SDK and configure your environment:
pip install openai
export API_KEY="your_59api_key"
export SUMMARY_MODEL="your_preferred_long_context_model"
Then create the client. Use the model identifier currently available in your 59API account; keeping it in an environment variable makes it easy to test a faster model for chunk summaries and a stronger model for final synthesis.
from openai import OpenAI
import os
client = OpenAI(
api_key=os.environ["API_KEY"],
base_url="https://api.59api.com/v1"
)
model = os.environ["SUMMARY_MODEL"]
Chunk by Structure, Not Just Character Count
First extract clean text from PDF, DOCX, HTML, or another source. Remove recurring page headers, navigation, and duplicate footers before sending anything to the model. Next, split at headings or paragraph boundaries. A fixed character limit is acceptable as a fallback, but preserve overlap between chunks so that a section beginning near a boundary retains its context.
The example below creates approximately 8,000-character chunks with a 600-character overlap. In production, token-aware splitting is better because models bill and limit input by tokens, not characters.
def chunk_text(text, size=8000, overlap=600):
chunks = []
start = 0
while start < len(text):
end = min(start + size, len(text))
chunks.append(text[start:end])
start += size - overlap
return chunks
- Contracts: split by article, clause, and schedule.
- Research papers: preserve abstract, methodology, findings, and limitations as separate units.
- Technical documentation: split by feature, API endpoint, or configuration section.
Create Evidence-Focused Chunk Summaries
Ask for facts that will be useful during synthesis, not a vague recap. The prompt should explicitly retain numbers, dates, obligations, assumptions, risks, and source references. Including the chunk number helps trace a final claim back to its origin.
def summarize_chunk(chunk, index):
prompt = f"""Summarize chunk {index} for a decision-maker.
Return: key points, concrete facts and numbers, risks or exceptions,
open questions, and important quoted terms. Do not invent information.
Label every point with Chunk {index}.
TEXT:
{chunk}"""
response = client.chat.completions.create(
model=model,
messages=[{"role": "user", "content": prompt}],
temperature=0.1
)
return response.choices[0].message.content
Use a low temperature for compliance, legal, financial, and technical summaries, where faithful extraction matters more than creative phrasing. Save each chunk result with its source offset and document ID. That audit trail lets reviewers verify assertions without rerunning the whole job.
Synthesize Without Losing Critical Exceptions
Combine the chunk summaries and make the final prompt audience-specific. For example, executives may need decisions and risks, while engineers need requirements, dependencies, and unresolved implementation details.
chunk_summaries = [summarize_chunk(c, i + 1) for i, c in enumerate(chunk_text(document))]
evidence = "\n\n".join(chunk_summaries)
final_prompt = f"""Create a concise executive summary from the evidence below.
Include: purpose, major findings, decisions required, risks, and open questions.
When a point has a source label, retain it. Flag contradictions instead of resolving them.
EVIDENCE:
{evidence}"""
For especially large files, add a third pass: group chunk summaries into section summaries before producing the final answer. This keeps synthesis inputs focused and helps control spend. 59API is particularly attractive when iterating on prompts or processing many documents because its low-cost relay pricing supports experimentation while retaining access to capable native models. If you are building this pipeline, consider signing up for 59API and testing the same workload across a fast model and a premium model to find the best quality-to-cost balance.
Pronto para começar?
Conecte Claude e GPT em minutos pelos menores preços, sem cortes. Cadastre-se e obtenha sua chave API.
Cadastro grátis