LLM Guides

How to Detect AI Content Using Python

Jul 08, 2026

AI content detection is a weird little problem.

Everyone wants a clean yes-or-no answer: “Was this written by AI?” But real detection is messier. AI text can be edited by humans. Human text can look formulaic. Short text is hard to judge. Paraphrasing can fool many detectors. Some tools also create false positives, which is risky if the result affects students, writers, employees, or customers.

So in this guide, we’ll build a simple Python AI content detector and explain where it works, where it fails, and how to use it responsibly.

We’ll cover three levels:

  1. A quick Python detector with a Hugging Face model
  2. A simple feature-based detector using text statistics
  3. A safer production workflow with confidence scores, review, and LLMAPI routing

The goal is not to pretend AI detection is perfect. The goal is to build a practical starting point that helps flag suspicious text for review.

Quick answer

If you want the fastest version, use a transformer text-classification model through Hugging Face.

pip install transformers torch
from transformers import pipeline

detector = pipeline(
    "text-classification",
    model="roberta-base-openai-detector"
)

text = """
This article explores the benefits of artificial intelligence
in modern business workflows.
"""

result = detector(text)

print(result)

You’ll get output like:

[{'label': 'Real', 'score': 0.72}]

or:

[{'label': 'Fake', 'score': 0.91}]

That is enough for a demo. For real apps, treat the result as a signal, not proof.

Why we can write this guide

Our team tracks AI APIs, model behavior, developer tools, and AI workflow patterns across content, moderation, document processing, and LLM routing. For this guide, we reviewed current resources and research, including:

ResourceWhy it matters
Hugging Face Transformers pipelinesSimple Python path for text classification
Hugging Face text classification docsShows how classification models work
DetectGPT paperClassic zero-shot AI text detection method
Can AI-Generated Text be Reliably Detected?Shows how paraphrasing can weaken detectors
Detecting AI-Generated Text surveyReviews detection methods and limits
AI detection reliability studyDiscusses false positive risks
Why AI-Generated Text Detection FailsExplains cross-domain failure and dataset bias
OpenAI provenance noteDiscusses watermarking and false positives

We also focused on practical questions developers actually care about: how to test text, how to interpret scores, how to reduce false accusations, and how to connect detection into a larger AI workflow.

What AI content detection actually checks

Most AI detectors look for patterns that often appear in machine-generated text.

Common signals include:

SignalWhat it means
Predictable wordingAI text may use common phrasing patterns
Low burstinessSentence structure may feel too even
Low perplexityThe text may be very predictable to a language model
Repeated structureParagraphs may follow similar rhythm
Generic transitionsThe text may rely on safe, common connectors
Overly balanced toneThe text may avoid sharp opinions or specific voice
Model fingerprintsSome detectors learn patterns from specific generators

The problem is that humans can write this way too. A student, non-native speaker, corporate writer, or SEO writer can produce text that looks “AI-like.” A human-edited AI draft can also look human enough to pass.

That is why detection should be used for review, not automatic punishment.

Method 1: Use a pretrained detector

The fastest way to detect AI content in Python is to use a text classification model.

Install dependencies:

pip install transformers torch

Create detect_ai.py:

from transformers import pipeline

detector = pipeline(
    "text-classification",
    model="roberta-base-openai-detector"
)

text = """
Artificial intelligence is transforming how companies manage content,
support customers, and automate repetitive workflows.
"""

result = detector(text)

print(result)

Run it:

python detect_ai.py

Example output:

[{'label': 'Fake', 'score': 0.84}]

In this model, Fake usually means AI-generated and Real usually means human-written.

Make the output easier to read

Raw model labels can be confusing. Let’s wrap the detector in a cleaner function.

from transformers import pipeline

detector = pipeline(
    "text-classification",
    model="roberta-base-openai-detector"
)

def detect_ai_content(text):
    result = detector(text[:5000])[0]

    label = result["label"]
    score = result["score"]

    if label.lower() == "fake":
        prediction = "likely_ai"
    else:
        prediction = "likely_human"

    return {
        "prediction": prediction,
        "confidence": round(score, 4),
        "raw_label": label
    }

sample = """
This guide provides a comprehensive overview of the key benefits
of automation across modern business environments.
"""

print(detect_ai_content(sample))

Output:

{
  "prediction": "likely_ai",
  "confidence": 0.8421,
  "raw_label": "Fake"
}

This is easier to use in an app.

Method 2: Add a simple rule-based layer

A model score is useful, but you can also add simple text features.

These will not “prove” anything. They help explain why text may look suspicious.

import re
from statistics import mean

def text_features(text):
    sentences = re.split(r"[.!?]+", text)
    sentences = [s.strip() for s in sentences if s.strip()]

    words = re.findall(r"\b\w+\b", text.lower())

    avg_sentence_length = mean(
        len(re.findall(r"\b\w+\b", sentence))
        for sentence in sentences
    ) if sentences else 0

    unique_word_ratio = len(set(words)) / len(words) if words else 0

    repeated_phrases = 0
    trigrams = zip(words, words[1:], words[2:])
    seen = set()

    for trigram in trigrams:
        if trigram in seen:
            repeated_phrases += 1
        seen.add(trigram)

    return {
        "word_count": len(words),
        "sentence_count": len(sentences),
        "avg_sentence_length": round(avg_sentence_length, 2),
        "unique_word_ratio": round(unique_word_ratio, 3),
        "repeated_phrases": repeated_phrases
    }

text = """
AI tools can help businesses save time, improve workflows, and reduce repetitive work.
AI tools can also support teams with faster content creation and better analysis.
"""

print(text_features(text))

Example output:

{
  "word_count": 28,
  "sentence_count": 2,
  "avg_sentence_length": 14.0,
  "unique_word_ratio": 0.786,
  "repeated_phrases": 2
}

These features can help you build a basic review dashboard.

Method 3: Combine model score and features

Now we can combine the pretrained detector with our simple features.

from transformers import pipeline
import re
from statistics import mean

detector = pipeline(
    "text-classification",
    model="roberta-base-openai-detector"
)

def get_text_features(text):
    sentences = re.split(r"[.!?]+", text)
    sentences = [s.strip() for s in sentences if s.strip()]
    words = re.findall(r"\b\w+\b", text.lower())

    avg_sentence_length = mean(
        len(re.findall(r"\b\w+\b", sentence))
        for sentence in sentences
    ) if sentences else 0

    unique_word_ratio = len(set(words)) / len(words) if words else 0

    return {
        "word_count": len(words),
        "sentence_count": len(sentences),
        "avg_sentence_length": round(avg_sentence_length, 2),
        "unique_word_ratio": round(unique_word_ratio, 3)
    }

def detect_ai_content(text):
    model_result = detector(text[:5000])[0]
    features = get_text_features(text)

    label = model_result["label"].lower()
    confidence = model_result["score"]

    if label == "fake":
        prediction = "likely_ai"
    else:
        prediction = "likely_human"

    return {
        "prediction": prediction,
        "confidence": round(confidence, 4),
        "features": features,
        "warning": "Use this as a review signal, not final proof."
    }

sample = """
In today's rapidly evolving digital landscape, businesses must adopt
innovative tools to streamline operations and improve productivity.
"""

print(detect_ai_content(sample))

This gives a more useful result:

{
  "prediction": "likely_ai",
  "confidence": 0.8912,
  "features": {
    "word_count": 18,
    "sentence_count": 1,
    "avg_sentence_length": 18.0,
    "unique_word_ratio": 1.0
  },
  "warning": "Use this as a review signal, not final proof."
}

Add thresholds for review

A production app should avoid hard yes/no decisions.

Use thresholds:

ConfidenceSuggested action
0.90+Flag for review
0.70-0.89Soft warning
0.50-0.69Low confidence
Below 0.50Do not label strongly

Here is a simple function:

def review_decision(prediction, confidence):
    if confidence >= 0.90:
        return "flag_for_review"

    if confidence >= 0.70:
        return "soft_warning"

    return "low_confidence"

result = detect_ai_content(sample)

decision = review_decision(
    result["prediction"],
    result["confidence"]
)

print(decision)

This keeps the workflow safer. A detector can trigger human review without becoming judge, jury, and scary spreadsheet.

Build a small API with FastAPI

If you want to use the detector in an app, wrap it in an API.

Install:

pip install fastapi uvicorn transformers torch

Create app.py:

from fastapi import FastAPI
from pydantic import BaseModel
from transformers import pipeline

app = FastAPI()

detector = pipeline(
    "text-classification",
    model="roberta-base-openai-detector"
)

class TextRequest(BaseModel):
    text: str

def classify_text(text):
    result = detector(text[:5000])[0]

    label = result["label"].lower()
    confidence = round(result["score"], 4)

    prediction = "likely_ai" if label == "fake" else "likely_human"

    if confidence >= 0.90:
        action = "flag_for_review"
    elif confidence >= 0.70:
        action = "soft_warning"
    else:
        action = "low_confidence"

    return {
        "prediction": prediction,
        "confidence": confidence,
        "action": action
    }

@app.post("/detect")
def detect(request: TextRequest):
    return classify_text(request.text)

Run it:

uvicorn app:app --reload

Test with curl:

curl -X POST "http://127.0.0.1:8000/detect" \
  -H "Content-Type: application/json" \
  -d '{"text":"Artificial intelligence is transforming modern business workflows."}'

Example response:

{
  "prediction": "likely_ai",
  "confidence": 0.8735,
  "action": "soft_warning"
}

What about DetectGPT?

DetectGPT is a research method that detects machine-generated text by checking probability curvature. The idea is that AI-generated text tends to occupy certain regions of a model’s probability function.

The paper reported strong results on some generated news-style text, but DetectGPT is heavier than a simple classifier. It requires access to model probabilities and text perturbations.

MethodBest for
Hugging Face classifierQuick app prototype
Feature-based modelExplainable baseline
DetectGPT-style methodResearch and advanced detection
WatermarkingContent generated by systems that add watermarks
Human reviewHigh-risk decisions

For most developers, a pretrained classifier plus review thresholds is the faster starting point.

Why AI detection fails

AI detection has real limits.

A 2023 paper, Can AI-Generated Text be Reliably Detected?, found that paraphrasing can reduce detection rates. A 2026 paper, Why AI-Generated Text Detection Fails, found that detectors can perform well on benchmark data and then fail when the domain or generator changes.

That matters a lot.

A detector trained on essays may fail on emails. A detector trained on older AI models may fail on newer models. A detector that works on English blog posts may fail on short comments, technical docs, or non-native writing.

Common failure cases:

ProblemWhy it happens
False positivesHuman text can look predictable
False negativesAI text can be edited or paraphrased
Short textToo little signal
Newer modelsTraining data may be outdated
Different domainStyle changes across topics
Non-native writingDetectors may misread simple phrasing
Heavy editingHuman and AI signals mix
Prompt templatesReused structure can look machine-made

This is why AI detection should be framed as probability, not proof.

Better production workflow

For real apps, use a review-first workflow.

User submits text → Run AI detector → Calculate confidence → Check text length and domain → Add feature-based signals → Flag high-risk text for review → Show reviewer evidence → Store decision and feedback

A good detection system should return:

FieldExample
Predictionlikely_ai
Confidence0.91
Review actionflag_for_review
Text length743 words
Model usedroberta-base-openai-detector
ExplanationHigh model confidence, repeated structure
WarningDo not use as final proof

This makes the result more transparent and less reckless.

Where LLMAPI fits

AI content detection often sits inside a larger workflow.

For example:

Text upload → AI detection → Plagiarism check → PII redaction → Moderation → Human review → Final decision

LLMAPI can help when the app needs multiple AI steps after detection. For example, you can route:

TaskPossible route
AI detection explanationSmall/cheap model
Content risk summaryStronger reasoning model
Human review notesWriting-focused model
Policy classificationStructured-output model
Appeal analysisHigher-accuracy model

You can also use LLMAPI to track usage, manage provider fallback, and avoid building every model integration separately.

Privacy and safety notes

If you send user writing to a detector, you may be processing sensitive data.

Before shipping, check:

QuestionWhy it matters
Do we store submitted text?Privacy and retention
Do we send text to third parties?User consent and compliance
Can users appeal decisions?False positives happen
Do we log raw text?Logs can leak data
Is detection used automatically?High-risk decisions need review
Do we support non-native writers fairly?Bias risk
Can the model explain results?Reviewers need context

OpenAI has also discussed provenance and watermarking limits, noting that even low false-positive rates can create many total false positives at large scale. That is a good reminder for any detection tool used across many users.

Full example

Here is a copy-paste version with model score, features, and review decision.

import re
from statistics import mean
from transformers import pipeline

detector = pipeline(
    "text-classification",
    model="roberta-base-openai-detector"
)

def get_text_features(text):
    sentences = re.split(r"[.!?]+", text)
    sentences = [s.strip() for s in sentences if s.strip()]
    words = re.findall(r"\b\w+\b", text.lower())

    avg_sentence_length = mean(
        len(re.findall(r"\b\w+\b", sentence))
        for sentence in sentences
    ) if sentences else 0

    unique_word_ratio = len(set(words)) / len(words) if words else 0

    return {
        "word_count": len(words),
        "sentence_count": len(sentences),
        "avg_sentence_length": round(avg_sentence_length, 2),
        "unique_word_ratio": round(unique_word_ratio, 3)
    }

def review_decision(confidence):
    if confidence >= 0.90:
        return "flag_for_review"

    if confidence >= 0.70:
        return "soft_warning"

    return "low_confidence"

def detect_ai_content(text):
    model_result = detector(text[:5000])[0]

    raw_label = model_result["label"]
    confidence = round(model_result["score"], 4)
    prediction = "likely_ai" if raw_label.lower() == "fake" else "likely_human"

    return {
        "prediction": prediction,
        "confidence": confidence,
        "action": review_decision(confidence),
        "features": get_text_features(text),
        "raw_label": raw_label,
        "note": "Use this as a review signal, not final proof."
    }

if __name__ == "__main__":
    text = """
    Artificial intelligence is transforming modern business operations
    by streamlining workflows, improving productivity, and supporting
    faster decision-making across departments.
    """

    print(detect_ai_content(text))

Final thoughts

You can detect AI content in Python with a few lines of code, especially if you use a pretrained Hugging Face classifier. That is a good start for demos, internal tools, and review dashboards.

For production, add thresholds, explanations, review queues, privacy checks, and fallback logic. AI detectors can help flag suspicious text, but they should not be used as the only evidence in high-stakes decisions.

The safer setup is simple: detect, score, explain, review, then decide.

If AI detection is part of a bigger content workflow, connect it with LLMAPI so your app can route follow-up tasks, compare model costs, and manage several AI checks from one place.

Deploy in minutes