How to detect AI-generated content
gpt-4o or claude-3-5-sonnet-20241022 using prompt engineering or dedicated detection APIs. You can also use statistical or embedding-based methods to identify AI writing patterns.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.
- Run
pip install openai - Set environment variable
OPENAI_API_KEYwith your OpenAI API key
pip install openai Step by step
Use the OpenAI gpt-4o model to classify if a text is AI-generated by prompting it directly. The example below sends a prompt asking the model to detect AI-generated content and returns the model's assessment.
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
text_to_check = "This is a sample text to test if it was generated by AI or a human."
prompt = f"Determine if the following text is AI-generated or human-written. Respond with 'AI-generated' or 'Human-written' only.\n\nText:\n{text_to_check}"
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": prompt}]
)
result = response.choices[0].message.content
print(f"Detection result: {result}") Detection result: AI-generated
Common variations
You can use the Anthropic claude-3-5-sonnet-20241022 model similarly by sending a system prompt and user message to detect AI-generated content. Alternatively, use async calls or stream responses for large-scale detection.
import os
import anthropic
client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
text_to_check = "This is a sample text to test if it was generated by AI or a human."
system_prompt = "You are an AI content detector. Respond with 'AI-generated' or 'Human-written' only."
messages = [{"role": "user", "content": f"Detect if this text is AI-generated:\n{text_to_check}"}]
response = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=50,
system=system_prompt,
messages=messages
)
print(f"Detection result: {response.content[0].text}") Detection result: AI-generated
Troubleshooting
If the detection result is ambiguous or inconsistent, try refining the prompt for clarity or increasing max_tokens. For API errors, verify your API key and network connectivity. Detection accuracy varies by text length and style.
Key Takeaways
- Use
gpt-4oorclaude-3-5-sonnet-20241022models with clear prompts to detect AI-generated content. - Set environment variables for API keys and install official SDKs for reliable integration.
- Prompt engineering is critical for accurate classification; keep prompts explicit and concise.
- Detection accuracy improves with longer text samples and context.
- Check API keys and network if you encounter errors or unexpected responses.