How to use Claude for long document analysis
Quick answer
Use
claude-3-5-sonnet-20241022 with chunking to handle long documents beyond its context window. Split the document into manageable parts, send each chunk with relevant prompts, and aggregate responses for comprehensive analysis.PREREQUISITES
Python 3.8+Anthropic API keypip install anthropic>=0.20
Setup
Install the anthropic Python SDK and set your API key as an environment variable.
- Install SDK:
pip install anthropic - Set environment variable:
export ANTHROPIC_API_KEY='your_api_key'(Linux/macOS) orsetx ANTHROPIC_API_KEY "your_api_key"(Windows)
pip install anthropic output
Collecting anthropic Downloading anthropic-0.20.0-py3-none-any.whl (15 kB) Installing collected packages: anthropic Successfully installed anthropic-0.20.0
Step by step
Split your long document into chunks that fit within Claude's context window (~100k tokens max). Send each chunk with a prompt to analyze or summarize it. Combine chunk results for full document insights.
import os
import anthropic
client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
# Example: split document into chunks (simplified)
long_document = """Your very long document text here..."""
chunk_size = 3000 # tokens approx, adjust as needed
chunks = [long_document[i:i+chunk_size] for i in range(0, len(long_document), chunk_size)]
results = []
for i, chunk in enumerate(chunks):
prompt = f"Analyze this part of the document:\n\n{chunk}\n\nProvide a concise summary and key points."
response = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=1000,
system="You are a helpful assistant.",
messages=[{"role": "user", "content": prompt}]
)
results.append(response.content[0].text)
print(f"Chunk {i+1} summary:\n", response.content[0].text)
# Combine or further process results as needed
full_summary = "\n\n".join(results)
print("\nFull document summary:\n", full_summary) output
Chunk 1 summary: The first part of the document covers ... Chunk 2 summary: The next section discusses ... Full document summary: The document overall explains ...
Common variations
You can use asynchronous calls with the anthropic SDK for faster processing. Also, consider using claude-3-5-haiku-20241022 for shorter, poetic summaries or claude-3-5-sonnet-20241022 for detailed analysis. For very large documents, implement retrieval-augmented generation (RAG) by combining chunk summaries with vector search.
import asyncio
import anthropic
async def analyze_chunks_async(chunks):
client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
tasks = []
for chunk in chunks:
prompt = f"Summarize this chunk:\n\n{chunk}"
tasks.append(client.messages.acreate(
model="claude-3-5-sonnet-20241022",
max_tokens=1000,
system="You are a helpful assistant.",
messages=[{"role": "user", "content": prompt}]
))
responses = await asyncio.gather(*tasks)
return [resp.content[0].text for resp in responses]
# Usage example
# summaries = asyncio.run(analyze_chunks_async(chunks)) output
['Summary of chunk 1...', 'Summary of chunk 2...', ...]
Troubleshooting
- If you get
context window exceedederrors, reduce chunk size. - If summaries are incomplete, increase
max_tokens. - For rate limits, add retry logic with exponential backoff.
- Ensure your API key is correctly set in
ANTHROPIC_API_KEY.
Key Takeaways
- Chunk long documents to fit within Claude's ~100k token context window.
- Use
claude-3-5-sonnet-20241022for detailed analysis and summaries. - Aggregate chunk responses to build a full document understanding.
- Async calls speed up processing of multiple chunks.
- Adjust chunk size and max tokens to avoid errors and incomplete outputs.