How to build a document analysis tool with Claude
Quick answer
Use the
anthropic Python SDK with the claude-3-5-sonnet-20241022 model to analyze documents by sending extracted text as user messages. Process responses to extract insights or summaries from documents efficiently.PREREQUISITES
Python 3.8+Anthropic API keypip install anthropic>=0.20
Setup
Install the anthropic SDK and set your API key as an environment variable for secure access.
pip install anthropic>=0.20 Step by step
This example shows how to load a document, send its content to Claude for analysis, and print the response.
import os
import anthropic
# Initialize the Anthropic client
client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
# Sample document text to analyze
document_text = """
Anthropic's Claude models are designed for safe and helpful AI interactions. This document explains how to use Claude for document analysis.
"""
# Create a prompt for document analysis
system_prompt = "You are a helpful assistant that analyzes documents and extracts key insights."
# Send the document text as a user message
response = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=512,
system=system_prompt,
messages=[{"role": "user", "content": document_text}]
)
# Extract and print the analysis
print("Document analysis result:\n", response.content[0].text) output
Document analysis result: Anthropic's Claude models provide safe, helpful AI interactions. This document explains usage for document analysis, highlighting Claude's design for extracting key insights effectively.
Common variations
You can adapt the tool by using different Claude models like claude-3-opus-20240229 for faster responses or increase max_tokens for longer documents. Async calls and streaming are also supported by the SDK for real-time processing.
import asyncio
import os
import anthropic
async def async_document_analysis(text: str):
client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
response = await client.messages.acreate(
model="claude-3-opus-20240229",
max_tokens=512,
system="You are a helpful assistant that analyzes documents.",
messages=[{"role": "user", "content": text}]
)
print("Async analysis result:\n", response.content[0].text)
# Run async example
asyncio.run(async_document_analysis("Sample document text for async analysis.")) output
Async analysis result: Sample document text for async analysis processed with helpful insights.
Troubleshooting
- If you get authentication errors, verify your
ANTHROPIC_API_KEYenvironment variable is set correctly. - For rate limit errors, implement exponential backoff retries.
- If responses are cut off, increase
max_tokensor split large documents into smaller chunks.
Key Takeaways
- Use the Anthropic Python SDK with the latest Claude models for document analysis.
- Send document text as user messages with a clear system prompt to guide analysis.
- Adjust
max_tokensand model choice based on document size and speed needs. - Async and streaming calls enable scalable, real-time document processing.
- Handle API errors by checking keys, respecting rate limits, and chunking large inputs.