How to Intermediate · 4 min read

AI cheating detection in schools

Quick answer
Use AI models like gpt-4o-mini or claude-3-5-sonnet-20241022 to analyze student submissions for signs of AI-generated text by checking for unnatural phrasing, repetition, or inconsistencies. Combine AI detection with plagiarism tools and metadata analysis to effectively identify cheating in schools.

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>=1.0
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 example uses the gpt-4o-mini model to analyze a student's essay and detect AI-generated patterns by prompting the model to classify the text.

python
import os
from openai import OpenAI

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

student_essay = """
Artificial intelligence has revolutionized many industries, including education. However, it also poses challenges such as AI-generated cheating.
"""

prompt = f"Detect if the following text is AI-generated or human-written. Respond with 'AI-generated' or 'Human-written'.\n\nText:\n{student_essay}"

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

result = response.choices[0].message.content
print(f"Detection result: {result}")
output
Detection result: AI-generated

Common variations

You can use claude-3-5-sonnet-20241022 from Anthropic for detection by prompting similarly. For asynchronous calls, use async SDK methods. Streaming responses can help with large text inputs.

python
import os
import asyncio
from openai import OpenAI

async def detect_ai_text_async(text: str):
    client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
    prompt = f"Detect if the following text is AI-generated or human-written. Respond with 'AI-generated' or 'Human-written'.\n\nText:\n{text}"
    response = await client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[{"role": "user", "content": prompt}]
    )
    return response.choices[0].message.content

async def main():
    essay = "AI is transforming education rapidly."
    result = await detect_ai_text_async(essay)
    print(f"Async detection result: {result}")

asyncio.run(main())
output
Async detection result: Human-written

Troubleshooting

  • If you receive authentication errors, verify your OPENAI_API_KEY environment variable is set correctly.
  • If detection results are inconsistent, try refining the prompt or using multiple models for cross-validation.
  • For rate limits, implement exponential backoff or batch requests.

Key Takeaways

  • Use AI models like gpt-4o-mini to classify text as AI-generated or human-written.
  • Combine AI detection with plagiarism and metadata analysis for robust cheating detection.
  • Refine prompts and test multiple models to improve detection accuracy.
  • Handle API errors and rate limits with proper environment setup and retry logic.
Verified 2026-04 · gpt-4o-mini, claude-3-5-sonnet-20241022
Verify ↗