How to beginner · 3 min read

Document AI use cases explained

Quick answer
Document AI automates processing of documents using OCR, data extraction, classification, and summarization. Common use cases include invoice processing, contract analysis, and automated form filling using LLM APIs like gpt-4o.

PREREQUISITES

  • Python 3.8+
  • OpenAI API key (free tier works)
  • pip install openai>=1.0

Setup

Install the openai Python package and set your API key as an environment variable.

  • Run pip install openai
  • Set environment variable OPENAI_API_KEY with your API key
bash
pip install openai

Step by step

This example demonstrates extracting key information from a document text using gpt-4o chat completions.

python
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

document_text = """
Invoice #12345\nDate: 2026-04-01\nTotal: $1,250.00\nVendor: Acme Corp\n"""

messages = [
    {"role": "system", "content": "You are a document AI assistant extracting invoice details."},
    {"role": "user", "content": f"Extract invoice number, date, total amount, and vendor from the following text:\n{document_text}"}
]

response = client.chat.completions.create(
    model="gpt-4o",
    messages=messages
)

print("Extracted data:", response.choices[0].message.content)
output
Extracted data: Invoice Number: 12345
Date: 2026-04-01
Total Amount: $1,250.00
Vendor: Acme Corp

Common variations

You can use streaming for real-time extraction, switch to gpt-4o-mini for cost efficiency, or use Anthropic Claude models for similar tasks.

python
import os
from anthropic import Anthropic

client = Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])

document_text = "Invoice #12345\nDate: 2026-04-01\nTotal: $1,250.00\nVendor: Acme Corp"

response = client.messages.create(
    model="claude-3-5-sonnet-20241022",
    max_tokens=256,
    system="You are a document AI assistant extracting invoice details.",
    messages=[{"role": "user", "content": f"Extract invoice number, date, total amount, and vendor from the following text:\n{document_text}"}]
)

print("Extracted data:", response.content[0].text)
output
Extracted data: Invoice Number: 12345
Date: 2026-04-01
Total Amount: $1,250.00
Vendor: Acme Corp

Troubleshooting

  • If extraction results are incomplete, increase max_tokens or clarify instructions in the system prompt.
  • If you get authentication errors, verify your API key is set correctly in os.environ.
  • For noisy scanned documents, preprocess with OCR before sending text to the API.

Key Takeaways

  • Use gpt-4o or claude-3-5-sonnet-20241022 for accurate document data extraction.
  • Set clear system prompts to guide the AI for specific document AI tasks.
  • Preprocess scanned documents with OCR before AI processing for best results.
Verified 2026-04 · gpt-4o, gpt-4o-mini, claude-3-5-sonnet-20241022
Verify ↗