How to beginner · 3 min read

How to detect spam with AI

Quick answer
Use a large language model like gpt-4o via the OpenAI Python SDK to classify text as spam or not by prompting it with examples or instructions. Send the message content in a chat.completions.create call and parse the model's response to detect spam.

PREREQUISITES

  • Python 3.8+
  • OpenAI API key (free tier works)
  • pip install openai>=1.0

Setup

Install the openai Python package and set your OpenAI API key as an environment variable.

  • Install SDK: pip install openai
  • Set API key in your shell: export OPENAI_API_KEY='your_api_key' (Linux/macOS) or setx OPENAI_API_KEY "your_api_key" (Windows)
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 example uses the gpt-4o model to classify a message as spam or not. It sends a prompt instructing the model to respond with "Spam" or "Not spam". The code prints the classification result.

python
import os
from openai import OpenAI

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

message_to_check = "Congratulations! You've won a free iPhone. Click here to claim now!"

prompt = (
    "Classify the following message as 'Spam' or 'Not spam'.\n"
    f"Message: {message_to_check}\n"
    "Answer:"  
)

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

result = response.choices[0].message.content.strip()
print(f"Message classification: {result}")
output
Message classification: Spam

Common variations

You can use asynchronous calls with asyncio and the OpenAI SDK's async client. Also, you can customize the prompt for more nuanced spam detection or use other models like gpt-4o-mini for faster, cheaper inference.

python
import os
import asyncio
from openai import OpenAI

async def detect_spam_async(message: str):
    client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
    prompt = (
        "Classify the following message as 'Spam' or 'Not spam'.\n"
        f"Message: {message}\n"
        "Answer:"
    )
    response = await client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[{"role": "user", "content": prompt}]
    )
    return response.choices[0].message.content.strip()

async def main():
    message = "Get cheap meds now, limited offer!"
    classification = await detect_spam_async(message)
    print(f"Async classification: {classification}")

if __name__ == "__main__":
    asyncio.run(main())
output
Async classification: Spam

Troubleshooting

  • If you get an authentication error, verify your OPENAI_API_KEY environment variable is set correctly.
  • If the model returns unclear answers, refine your prompt to explicitly ask for 'Spam' or 'Not spam' only.
  • For rate limits, consider using a smaller model like gpt-4o-mini or add retries with exponential backoff.

Key Takeaways

  • Use the OpenAI Python SDK with gpt-4o to classify spam via prompt engineering.
  • Set clear instructions in the prompt to get reliable spam/not spam classification.
  • Async calls and smaller models like gpt-4o-mini help optimize cost and latency.
  • Always secure your API key via environment variables to avoid leaks.
  • Refine prompts and handle API errors to improve robustness in production.
Verified 2026-04 · gpt-4o, gpt-4o-mini
Verify ↗