Duck
Tutorials8 min read

How to Build an AI Checker for Code That Actually Works

Samet Turan— Editor··8 min read

Stop shipping buggy code. Learn to build a custom AI checker for code using GPT-4o and a simple script to automate your pull request reviews and catch errors.

Why Your Manual Code Reviews Are Leaking Bugs

Manual code reviews are a bottleneck. They’re slow, they depend on your senior dev’s availability, and they are wildly inconsistent. One day you’ll catch a subtle null pointer exception; the next, you’ll miss a glaring security flaw because you were distracted. For solo operators or small teams, the problem is worse. Who reviews your code? You, six hours later, when the context is gone? This is how bugs get into production.

You can fix this. By hooking a large language model into your Git workflow, you can build an automated ai checker for code that acts as a tireless, consistent junior developer. It won’t replace a human, but it will catch an entire class of errors before they ever waste a human’s time. This article shows you how to build a practical version of this system yourself.

The Core Components of an AI Code Checking System

This isn’t black magic. The entire system is just a few components wired together. Think of it as a simple assembly line. First, you need a trigger. For most of us, that’s a new pull request being opened in a repository. Tools like GitHub and GitLab offer webhooks for this exact purpose. When a PR is created, it sends a notification to a specific URL.

🤖
Recommended Reading

AI Side Hustles

12 Ways to Earn with AI

Practical setups for building real income streams with AI tools. No coding needed. 12 tested models with real numbers.


Get the Guide → $14

★★★★★ (89)

Second, a script listens for that trigger. This script’s job is to fetch the *changes* in the pull request. This is key. You don’t want the whole codebase, just the ‘diff’—the specific lines that were added, removed, or modified. Third, the script bundles that diff into a carefully constructed prompt and sends it to an LLM API. Your choices are typically an OpenAI model like GPT-4o or an Anthropic model like Claude 3 Sonnet. Finally, the script takes the model’s response, formats it, and posts it back to the pull request as a review comment. Trigger, fetch, analyze, report. That’s the entire loop.

What Most Guides Get Wrong About AI Code Review

Here’s where nearly every online tutorial on this topic fails. They tell you to just pipe the contents of a file into an LLM. cat my_file.js | llm. This is a terrible approach, and it’s a clear sign the author has never run this in production on a real project. It’s lazy, expensive, and ineffective.

Why? First, context windows. Even with massive 200K token windows, feeding entire files is wasteful and will eventually fail on larger codebases. Second, cost. You pay per token. Sending unchanged code is literally burning money. Third, and most importantly, the model loses the intent of the change. Reviewing code isn’t about checking syntax; it’s about understanding *why* a change was made. A diff provides that context implicitly. The AI can see what was there before and what’s there now, which is a much stronger signal for a useful review.

The correct method is to only send the diff. You use your version control system to generate a patch file for the pull request and feed that to the model. This is more efficient, cheaper, and yields drastically better results. Honestly, any guide that doesn’t start with the diff is wasting your time.

A Practical Build: Using GitHub Actions and OpenAI

Let’s build a working version. We’ll use GitHub Actions because it’s integrated directly into the place the code lives. No separate server needed. The action will trigger on any new pull request.

Your workflow file (e.g., .github/workflows/ai-review.yml) would look something like this:

name: AI Code Review
on:
pull_request:
types: [opened, synchronize]

jobs:
review:
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
uses: actions/checkout@v3

- name: AI Code Review
uses: some-awesome-ai-review-action@v1
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
openai_api_key: ${{ secrets.OPENAI_API_KEY }}
model: "gpt-4o"

Of course, you’d need to find or build the action (`some-awesome-ai-review-action@v1`), but the core logic inside that action is a script that performs the steps we discussed. My gripe here is that finding a pre-made action you can trust is hard, and debugging the YAML syntax for custom scripts inside Actions is a special kind of pain. The documentation is a maze.

The most important part is the prompt you send to the model. A bad prompt gives you generic, useless feedback. A good prompt gets you specific, actionable advice. Here is a starting point for a system prompt:

You are an expert senior software engineer performing a code review. Your tone is helpful and collaborative, not demanding. You are reviewing a pull request. The user will provide the code diff. Focus on the following:
1. **Logic Errors:** Look for off-by-one errors, incorrect assumptions, or potential race conditions.
2. **Security Vulnerabilities:** Check for common issues like SQL injection, XSS, or insecure handling of secrets.
3. **Best Practices & Idiomatic Code:** Suggest improvements that align with the language's conventions. Do not be overly pedantic about style.
4. **Clarity and Maintainability:** Is the code easy to understand? Are variable names clear? Could a complex function be simplified?

Format your response as a GitHub markdown comment. If you have no suggestions, simply respond with "LGTM!". Otherwise, provide specific, line-by-line feedback.

This level of instruction is the minimum viable prompt. You can, and should, refine it with examples specific to your own coding standards. When it comes to cost, using GPT-4o for this is a bargain. It’s around $5 per million input tokens. A typical PR review might cost you five to ten cents. This is a steal compared to dedicated SaaS tools that charge $199/mo for what is often just a fancy wrapper around the same API.

Is the AI’s Feedback Actually Any Good?

Yes and no. Let’s be realistic. An ai checker for code is not going to tell you that your entire architectural approach is wrong. It can’t understand the business context behind a feature. It is not a replacement for a senior engineer’s judgment.

But that’s not its job.

Its job is to be a tireless assistant that catches the dumb mistakes. And it’s incredibly good at that. I had it catch a subtle off-by-one error in a pagination function that two human reviewers (including me) had already approved. That one catch saved us from shipping a bug to customers and easily paid for the month’s API bill. That’s what I love about it—it’s a safety net for human fallibility.

It excels at finding things like: unused variables, inefficient loops, potential null dereferences, and security anti-patterns. It reduces the cognitive load on the human reviewer, allowing them to focus on the bigger picture—the *why* of the code, not just the *what*.

How to Debug When This Breaks

This system will break. When it does, it’s usually one of a few things. First, check your secrets and permissions. 90% of the time, a GitHub Action fails because the `OPENAI_API_KEY` is wrong or the `GITHUB_TOKEN` doesn’t have permissions to write comments to pull requests. Check the Action’s logs for authentication errors.

Second, the LLM gives you garbage output. It might be a malformed JSON object (if you requested one) or just a lazy, unhelpful comment. This is almost always a prompt problem. Your prompt isn’t specific enough. Try adding few-shot examples directly in the prompt, showing it exactly what a good review comment looks like. For example, add a user message with a sample diff and an assistant message with the ideal markdown review. This guides the model far better than just instructions alone.

Another common failure is the script choking on a large diff or a binary file. Your script needs error handling and should probably filter out files you don’t want to review, like `package-lock.json` or image assets. Don’t just blindly send every changed file. Finally, you might get rate-limited by the API provider. If you suddenly open 20 PRs at once, you’ll hit the limit. Your script should include a simple exponential backoff and retry mechanism to handle this gracefully.

Beyond Basic Checks: Building a Specialized Agent

Once you have a basic reviewer working, you can make it much smarter. This is where you move from a simple script to a proper AI agent framework. Instead of just sending the diff, you can use a technique called Retrieval-Augmented Generation (RAG). You could, for instance, give the agent access to your team’s official coding style guide. The agent would then check the pull request for compliance with your specific internal standards.

You can also connect it to your project’s documentation. The agent could check if a change to a component is reflected in its corresponding `README.md` file. This requires more complex tools like LangChain or LlamaIndex to manage the data, but it transforms the checker from a generic critic into a true project-aware assistant. This is the kind of high-value AI automation blueprint that separates operators from hobbyists.

This isn’t a no-code solution. Building this level of sophistication requires writing real code and understanding how these frameworks operate. But the payoff is an automation asset that is uniquely valuable to your business.

You can build this entire system following the steps above. It will take some trial and error, especially with the prompt engineering and GitHub Actions configuration.

Adjacent reading: deeper coverage of AI agent platforms.

If you’d rather skip the build and deploy a working version in an afternoon, we’ve packaged this workflow as a blueprint at deepusecase.com/vault.

— The Colophon

One AI tool. Tested. Reviewed.
In your inbox every Sunday.

~3 minute read. Real outcomes from operators, not marketers.

Free. One email per Sunday. Unsubscribe in one click.