0Pricing
AI Agents · Lesson

Document Classification and Routing

Categorizing documents by type and routing them to specialized agent handlers.

Why Document Classification Matters

A document intelligence agent may receive many different document types: invoices, contracts, reports, emails, receipts. Each type requires different extraction logic and business rules.

Document classification routes each document to the right handler before any further processing — acting as the agent's intake logic.

LLM-Based Classification

The simplest and most flexible classifier uses the LLM. Pass a sample of the document text and ask the model to identify the document type. Works well when document types are clearly distinct.

import openai
import os

client = openai.OpenAI(api_key=os.getenv('OPENAI_API_KEY'))

DOC_TYPES = ['invoice', 'contract', 'report', 'email', 'receipt', 'form', 'letter', 'other']

CLASSIFY_PROMPT = '''Classify this document into one of these types: {types}

Document excerpt (first 1000 characters):
{text}

Respond with ONLY the document type as a single word from the list above.'''

def classify_with_llm(text):
    response = client.chat.completions.create(
        model='gpt-4o-mini',  # cheap and fast for classification
        messages=[{
            'role': 'user',
            'content': CLASSIFY_PROMPT.format(
                types=', '.join(DOC_TYPES),
                text=text[:1000]
            )
        }],
        max_tokens=10,
        temperature=0
    )
    predicted = response.choices[0].message.content.strip().lower()
    return predicted if predicted in DOC_TYPES else 'other'

All lessons in this course

  1. PDF Parsing with PyMuPDF and pdfplumber
  2. OCR for Scanned Documents
  3. Multi-Document Q&A Agents
  4. Document Classification and Routing
← Back to AI Agents