Day 04 Advanced Topics

Document Processing: Analyze Files with AI

Day 4 builds a document processor that reads files and uses Claude to analyze, summarize, and extract information from them.

~1 hour Hands-on Precision AI Academy

Today’s Objective

Day 4 builds a document processor that reads files and uses Claude to analyze, summarize, and extract information from them.

Document Processing: Analyze Files with AI is one of the most important topics in JavaScript + AI APIs. This lesson builds the foundation you need before moving to more advanced concepts — take time with each example and run the code yourself.

Reading Files and Sending to Claude

Terminal
TERMINAL
npm install multer pdf-parse
document-processor.js
DOCUMENT-PROCESSOR.JS
import Anthropic from '@anthropic-ai/sdk';
import fs from 'fs/promises';
import path from 'path';
import * as dotenv from 'dotenv';
dotenv.config();

const client = new Anthropic();

async function analyzeDocument(filePath, instruction) { const ext = path.extname(filePath).toLowerCase(); let content; if (ext === '.txt' || ext === '.md' || ext === '.csv') { content = await fs.readFile(filePath, 'utf-8'); } else { throw new Error(`Unsupported file type: ${ext}`); } // Truncate if too long (Claude handles up to 200K tokens) const maxChars = 180000; if (content.length > maxChars) { content = content.slice(0, maxChars) + '

[Content truncated...]'; } const response = await client.messages.create({ model: 'claude-opus-4-5', max_tokens: 2048, messages: [{ role: 'user', content: `Here is the document content:

${content}

---

${instruction}` }] }); return response.content[0].text;
}

// Example usage
const summary = await analyzeDocument('./report.txt', 'Summarize this document in 5 bullet points.');
console.log(summary);

const extraction = await analyzeDocument('./data.csv', 'List all unique values in the "category" column.');

File Upload API

Upload Route with Multer
UPLOAD ROUTE WITH MULTER
import multer from 'multer';
const upload = multer({ dest: 'uploads/', limits: { fileSize: 10 * 1024 * 1024 } });

app.post('/analyze', upload.single('document'), async (req, res) => { if (!req.file) return res.status(400).json({ error: 'No file uploaded' }); const { instruction = 'Summarize this document.' } = req.body; try { const content = await fs.readFile(req.file.path, 'utf-8'); const response = await client.messages.create({ model: 'claude-opus-4-5', max_tokens: 2048, messages: [{ role: 'user', content: `Document:

${content}

Task: ${instruction}` }] }); // Clean up temp file await fs.unlink(req.file.path); res.json({ result: response.content[0].text }); } catch (error) { res.status(500).json({ error: error.message }); }
});
Day 4 ExerciseBuild a Document Q&A Tool
  1. Create a text file with 500+ words of content (a report, article, or document you have).
  2. Run analyzeDocument() with different instructions: summarize, extract key points, list action items.
  3. Add the upload route to your Express server.
  4. Build a simple HTML form that uploads a file and shows the analysis result.

Supporting Resources

Go deeper with these references.

Anthropic
Claude API Reference Official documentation for the Messages API, tool use, and streaming.
npm
@anthropic-ai/sdk Official Node.js SDK for the Anthropic API with TypeScript support.
GitHub
Anthropic Cookbook Official Anthropic code examples for common JavaScript + Claude patterns.

Day 4 Checkpoint

Before moving on, make sure you can answer these without looking:

Continue To Day 5
Production Patterns: Error Handling, Rate Limits, and Caching