Day 4 of 5
⏱ ~60 minutes
Build AI Apps with JavaScript — Day 4

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.

Reading Files and Sending to Claude

Terminal
npm install multer pdf-parse
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
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 Exercise
Build 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.

Day 4 Summary

  • Text files are read with fs.readFile — content sent directly in the messages array.
  • Multer handles multipart file uploads in Express.
  • Always clean up temp files after processing.
  • Claude's 200K token context window handles most documents without chunking.
Finished this lesson?