Standard OCR fails at semantic extraction. I built a Python and Anthropic API architecture that forces Claude 4.7 to extract liability clauses into strict JSON.
If you are still paying paralegals to manually review 100-page Master Services Agreements to find the governing law and liability caps, your law firm is burning cash. This is exactly what business AI agents automate.
The Problem with Standard OCR
Standard OCR reads text. It does not understand semantics. If a contract says "Jurisdiction: Delaware" or "This agreement shall be governed by the laws of the State of Delaware", traditional regex parsers break.
You need an LLM. But if you just dump the text into ChatGPT, it hallucinates or gives you a conversational summary. I need strict JSON.
The Claude 4.7 Architecture
Anthropic's Claude 4.7 API lets you enforce output schemas. The exact Python plumbing to extract specific clauses deterministically.
import anthropic
import json
client = anthropic.Anthropic(api_key="sk-ant-...")
def extract_clauses(contract_text):
prompt = f"""
You are an expert legal AI. Extract the following from the contract:
1. Governing Law
2. Liability Cap
Return ONLY a valid JSON object. No conversational text.
{contract_text}
"""
response = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=1000,
temperature=0,
system="You output strictly in JSON.",
messages=[{"role": "user", "content": prompt}]
)
return json.loads(response.content[0].text)Key insight: Setting temperature=0 and enforcing a system prompt of "You output strictly in JSON" eliminates hallucination and gives you deterministic, database-ready output.
PDF text in. Database-ready JSON out. That is human capability multiplication in practice.
Want to deploy this in your practice? Download the Blueprint. For the full prompt engineering approach, see Best AI contract review prompts. Start with a workflow diagnostics.
The extraction pipeline
Raw PDF goes in. Structured JSON comes out. The steps:
- OCR the scan. If the contract is a PDF with text layer, skip this. If it is a scan, run OCR first. Claude 4.7 handles both, but pre-processing improves accuracy on degraded scans.
- Segment by clause. Do not send the whole contract and ask for "all clauses." Send section by section. A 40-page contract segmented into 15 clause groups extracts more reliably than one giant prompt.
- Define your schema. Every clause type gets a JSON schema: termination, indemnification, limitation of liability, confidentiality, assignment. The schema is the contract with the AI. Without it, you get prose back and have to parse it yourself.
- Extract with confidence scores. Ask Claude to return a confidence value per field. Low confidence means human review. That single field saves hours of manual checking.
- Reconcile at the document level. Cross-references between clauses need a second pass. Clause 7.2 says "as defined in Section 3.1." The reconcile step resolves those.
The schema that works
For a standard commercial contract, I extract these fields per clause: clause_type, text, parties_obligations, triggers, exceptions, and cross_references. The confidence score sits at the top level. For the full document processing architecture, see document processing agents.
Where it breaks
Handwritten annotations. Non-standard clause numbering. Amendments layered on top of the original. For those, the pipeline flags the page and routes to human review. The goal is 80% automated extraction with 100% human coverage of the uncertain 20%. For OCR-specific issues, see my Claude 4.7 OCR guide.
| Operational layer | Fragile default | Production architecture |
|---|---|---|
| Document ingestion | Manual reading and copy-paste into matter fields | Automated OCR and classification with strict JSON schema validation |
| Conflict & risk check | Ad-hoc search after the consult is booked | Pre-booking contact search and mandatory human gate on low confidence |
| Case file retrieval | Naive chunking that splits clauses mid-sentence | Section-aware chunking with citation anchors back to page and line |
| Compliance & custody | Prompts sent to consumer chat accounts | Zero-retention API endpoints with full per-document audit logs |
FAQ
How long does it take to deploy extract clauses from legal contracts using claude 4-7 api in a law firm?
A focused legal workflow covering intake, OCR extraction, or matter sync takes two to four weeks from mapping to production handover, with human approval gates on every client-facing step.
Does client data stay private when using AI for legal workflows?
Yes. Production legal pipelines use zero-retention API endpoints, scoped field permissions, and full audit logs so client documents never train public models.
Can this integrate with Clio Manage, MyCase, or Lawmatics?
Yes. The architecture connects directly to Clio Manage, MyCase, Lawmatics, and HubSpot via webhooks and REST APIs with search-before-create deduplication.