{"slug": "building-an-ai-text-detector-from-scratch", "title": "Building an AI Text Detector From Scratch", "summary": "Sebastian Raschka, an AI researcher and author, published a tutorial on building an AI text detector from scratch, including dataset construction, model training, local deployment, and reinforcement learning from verifier feedback (RLVR). The project fine-tunes a DistilBERT classifier to output a 0-100 AI-generated probability score, similar to the approach behind Substack's AI detection feature, and uses it as a verifier to train a small language model to avoid detection. Raschka emphasizes the cat-and-mouse nature of AI detection and the risk of false positives.", "body_md": "# Building an AI Text Detector From Scratch\n\n### An End-to-End Project With Dataset Construction, Model Training, Local Deployment, and RLVR\n\nSubstack recently launched its AI detector feature in the UI, which is super interesting.\n\nSeparately, lots of people asked me about interesting local do-it-yourself LLM projects as demos to show what small language models (SLMs) are capable of.\n\nPutting one and one together, I thought it would be interesting to show how an AI detector can be implemented. I will also use it as a verifier to train a small language model to produce text that avoids detection. This is a small educational project for studying the limitations of AI detectors and exploring a verifier-based LLM application beyond regular reasoning models trained on math and code.\n\nSo, as mentioned above, the intended goal of this tutorial is to explain how AI detectors work by building (a simple) one.\n\nIn practice, such a detector can be used to filter out spammy content, but also to potentially improve your personal writing without turning it into AI-generated text. For example, if you wrote a lengthy article and want to improve spelling and grammar, it is tempting (and actually useful) to use a grammar checker to polish it and improve readability. There are different services for that, including general-purpose LLMs like ChatGPT. However, this also runs the risk that these tools turn your writing, even though it’s still your own writing, into something that is then overpolished and now sounds like AI and gets flagged as spammy content.\n\nFor example, with an AI checker, one could say, “Fix my grammar while ensuring that my text still scores 0% AI-generated.”\n\nAnyway, while we are building a fully functional checker here, the goal is to explain 1) how AI checkers (can) work and 2) use this as a case study for a more general topic on how to build a scorer or verifier that can be used with LLMs.\n\nDisclaimer: AI checkers are essentially a cat-and-mouse game. AI checkers may learn to detect a certain pattern that is indicative of AI-generated content. Then, the next LLM may incidentally or deliberately not exhibit that pattern and avoid detection. The AI checker then has to be updated to detect said LLM, and so forth. Plus, it’s also likely to encounter false positives (human written text flagged as AI-generated), but more on that later.\n\n## Project goals\n\nThere are several goals of this project. The overarching goal is, of course, to illustrate how AI detectors work and show an applied end-to-end LLM project including evaluation, training, and local deployment for real-world use.\n\nThe outcome of this is an AI-detector API that can be used by humans and agents, and a user-friendly UI.\n\n## Method overview\n\nHere, we are going to develop a method similar to Pangram models, which, as far as I know, are behind Substack AI detection feature.\n\nI wrote a short article about AI-text detection a while back in 2023: [What Are the Different Approaches for Detecting Content Generated by LLMs Such As ChatGPT? And How Do They Work and Differ?](https://sebastianraschka.com/blog/2023/detect-ai.html)\n\nIn essence, there are different ways to detect AI-written text, from supervised classifiers and perturbation-based probability tests to perplexity measures and watermarking.\n\nIn this tutorial, we will build a model that returns a 0-100 score. It’s essentially a classifier with an estimated probability score. The probability score will denote how likely a text is AI-generated according to the classifier. (Or, to be precise the score is the classifier’s estimated probability for the AI-generated class based on its training distribution. However, we shouldn’t interpreted it as a general probability that the text was written by AI.)\n\nFor this, we are going to fine-tune a DistilBERT classifier (similar to what I described in one of my early Substack articles, [Finetuning Large Language Models](https://magazine.sebastianraschka.com/p/finetuning-large-language-models)), but more details on that later when we get to that stage.", "url": "https://wpnews.pro/news/building-an-ai-text-detector-from-scratch", "canonical_source": "https://magazine.sebastianraschka.com/p/ai-detector-from-scratch", "published_at": "2026-08-15 11:54:24+00:00", "updated_at": "2026-08-15 12:12:23.758729+00:00", "lang": "en", "topics": ["artificial-intelligence", "machine-learning", "natural-language-processing", "ai-research", "ai-tools"], "entities": ["Sebastian Raschka", "DistilBERT", "Substack", "ChatGPT"], "alternates": {"html": "https://wpnews.pro/news/building-an-ai-text-detector-from-scratch", "markdown": "https://wpnews.pro/news/building-an-ai-text-detector-from-scratch.md", "text": "https://wpnews.pro/news/building-an-ai-text-detector-from-scratch.txt", "jsonld": "https://wpnews.pro/news/building-an-ai-text-detector-from-scratch.jsonld"}}