How to Intermediate · 4 min read

AI for contract risk identification

Quick answer
Use large language models (LLMs) like gpt-4o or claude-3-5-sonnet-20241022 to analyze contract text and identify risk clauses by prompting them to extract and classify risky terms. This approach automates contract review by leveraging natural language understanding and can be implemented via API calls with Python.

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. This example uses OpenAI's gpt-4o model, but you can substitute with other LLMs like Anthropic's claude-3-5-sonnet-20241022.

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

This Python script sends a contract text to the gpt-4o model, asking it to identify and summarize potential risk clauses. It prints the extracted risks clearly.

python
import os
from openai import OpenAI

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

contract_text = '''\
This Agreement includes confidentiality obligations, indemnification clauses, and termination conditions.
The Supplier shall not be liable for indirect damages.
Payment terms require net 30 days.
'''  

prompt = f"""
You are a legal assistant. Identify and list potential risk factors in the following contract text. Provide a concise summary of each risk.

Contract text:\n{contract_text}
"""

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

print("Contract risk identification result:\n", response.choices[0].message.content)
output
Contract risk identification result:
- Confidentiality obligations may impose strict limits on information sharing.
- Indemnification clauses could expose the party to financial liability.
- Termination conditions might allow early contract exit with penalties.
- Supplier's exclusion of liability for indirect damages limits recourse.
- Payment terms of net 30 days may affect cash flow.

Common variations

You can use asynchronous calls with asyncio for higher throughput or switch to Anthropic's claude-3-5-sonnet-20241022 model by changing the client and model name. Streaming responses are also supported for real-time output.

python
import os
import asyncio
from openai import OpenAI

async def identify_risks_async(text: str):
    client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
    prompt = f"Identify risks in this contract:\n{text}"
    response = await client.chat.completions.create(
        model="gpt-4o",
        messages=[{"role": "user", "content": prompt}]
    )
    print("Async risk identification result:\n", response.choices[0].message.content)

contract_text = '''\
This Agreement includes confidentiality obligations, indemnification clauses, and termination conditions.
The Supplier shall not be liable for indirect damages.
Payment terms require net 30 days.
'''

asyncio.run(identify_risks_async(contract_text))
output
Async risk identification result:
- Confidentiality obligations may impose strict limits on information sharing.
- Indemnification clauses could expose the party to financial liability.
- Termination conditions might allow early contract exit with penalties.
- Supplier's exclusion of liability for indirect damages limits recourse.
- Payment terms of net 30 days may affect cash flow.

Troubleshooting

  • If you get authentication errors, verify your OPENAI_API_KEY environment variable is set correctly.
  • If the model returns irrelevant answers, refine your prompt to be more specific about risk identification.
  • For rate limits, implement exponential backoff retries or upgrade your API plan.

Key Takeaways

  • Use LLMs like gpt-4o to automate contract risk identification via natural language prompts.
  • Set up your environment with the official openai Python SDK and secure your API key in environment variables.
  • Customize prompts to extract specific risk clauses and summaries for clearer contract analysis.
  • Async and streaming API calls improve performance for large-scale contract reviews.
  • Troubleshoot common issues by checking API keys, refining prompts, and handling rate limits.
Verified 2026-04 · gpt-4o, claude-3-5-sonnet-20241022
Verify ↗