How to Intermediate · 4 min read

How to review contracts with AI

Quick answer
Use large language models (LLMs) like gpt-4o-mini to analyze contracts by extracting key clauses, summarizing terms, and identifying risks. Automate contract review by sending contract text to the OpenAI API with prompts designed for legal analysis.

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 for secure access.

bash
pip install openai
output
Collecting openai
  Downloading openai-1.x.x-py3-none-any.whl (xx kB)
Installing collected packages: openai
Successfully installed openai-1.x.x

Step by step

Use the gpt-4o-mini model to review contract text by prompting it to extract key clauses and summarize risks.

python
import os
from openai import OpenAI

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

contract_text = '''\
This Agreement is made between Acme Corp and Beta LLC. The term is 2 years with automatic renewal. Confidentiality must be maintained. Termination requires 30 days notice.
'''

prompt = f"""
You are a legal assistant. Review the following contract and extract key clauses including term, confidentiality, and termination. Summarize any potential risks.

Contract Text:\n{contract_text}

Key clauses and summary:
"""

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": prompt}]
)

print(response.choices[0].message.content)
output
Key clauses:
- Term: 2 years with automatic renewal
- Confidentiality: Must be maintained
- Termination: Requires 30 days notice

Summary of risks:
- Automatic renewal may extend obligations unexpectedly.
- Termination notice period requires careful tracking to avoid unintended contract continuation.

Common variations

You can use asynchronous calls for scalability, stream responses for large contracts, or switch to other models like claude-3-5-haiku-20241022 for different legal language styles.

python
import asyncio
from openai import OpenAI

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

async def async_review_contract(text: str):
    prompt = f"Review this contract and extract key clauses:\n{text}\n"
    response = await client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[{"role": "user", "content": prompt}]
    )
    print(response.choices[0].message.content)

asyncio.run(async_review_contract(contract_text))
output
Key clauses:
- Term: 2 years with automatic renewal
- Confidentiality: Must be maintained
- Termination: Requires 30 days notice

Troubleshooting

  • If the model returns vague answers, refine your prompt to be more specific about clauses and risks.
  • Ensure your contract text is clean and well-formatted to avoid parsing errors.
  • If you hit token limits, split large contracts into sections and review them separately.

Key Takeaways

  • Use gpt-4o-mini with clear prompts to extract and summarize contract clauses effectively.
  • Set up environment variables and install the openai package for secure API access.
  • For large contracts, process in chunks or use streaming to handle token limits.
  • Async API calls improve throughput when reviewing multiple contracts.
  • Prompt specificity directly impacts the quality of contract analysis.
Verified 2026-04 · gpt-4o-mini, claude-3-5-haiku-20241022
Verify ↗