Duck
Tutorials9 min read

A Practical Guide to AI for Engineering: Automating Code Reviews

Samet Turan— Editor··9 min read

Learn how to use AI for engineering tasks by building an automated code review agent. This guide covers the tools, prompts, and debugging for real-world use.

The Core Problem: Why Manual Code Reviews Drain Your Time

If you’re a solo operator or running a small team, you know the pain of the pull request. Every feature, every bug fix, sits waiting for a second pair of eyes. This process is essential for quality, but it’s also a massive bottleneck. The time spent checking for style guide violations, missing docstrings, or basic formatting errors is time not spent on architectural decisions or solving actual user problems. This is the low-value, high-volume work that burns out your best people. It’s a perfect candidate for automation. This is where practical AI for engineering comes in, not as a magic bullet, but as a tireless assistant.

We’re not talking about sentient AI that understands business logic. We’re talking about a focused agent that acts as a first-pass filter. It catches the objective, rule-based issues so that human reviewers can focus on the subjective, high-level stuff—the things that actually require intelligence and experience. This guide shows you how to build that agent.

What Most Guides on AI for Engineering Get Wrong

Most tutorials on this topic are, frankly, useless. They show you a screenshot of pasting a code snippet into a generic ChatGPT window and marveling at the output. That isn’t a production system. It’s a party trick. It doesn’t scale, it has no context of your project’s specific rules, and it’s not repeatable.

🤖
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)

Real automation requires a system. It needs to be triggered automatically, operate with specific context, and produce structured output that can be used in a workflow. Just telling an LLM to “review my code” will give you a verbose, inconsistent, and often unhelpful wall of text. It might even hallucinate security flaws that send your team on a wild goose chase. The key is constraint. You need to build a pipeline that tells the AI exactly what to look for, how to report its findings, and what to ignore.

This is not about replacing developers.

It’s about building a specialized tool. Forget generic prompts. We’re building a focused AI agent framework that integrates directly into your development lifecycle, specifically within your pull request process.

The Stack: Building Your AI Code Review Agent

To build this properly, you need three components. I’ve built this exact system, and this stack is simple, cheap, and effective.

  • The Trigger: GitHub Actions. This is the connective tissue. You can configure a workflow to run automatically whenever a new pull request is opened. It’s native to the developer environment, so there’s no need for an external service like Zapier to poll for changes. My main gripe with setting this up via GitHub Actions is the secret management. It’s functional, but the UI for managing environment secrets feels clunky and disconnected from the workflow file itself. You have to navigate to a completely different settings page, which is just annoying.
  • The Logic: A Simple Python Script. The GitHub Action will execute a script. This script is responsible for grabbing the code changes (the ‘diff’), formatting them into a clear prompt for the AI, making the API call, and then parsing the response. Python is perfect for this with its `requests` library for API calls and solid JSON handling.
  • The Brain: OpenAI API. This is where the analysis happens. You’ll send the formatted code and your instructions to a model. Honestly, I think using anything less than GPT-4o for this task is a waste of API credits. The cheaper models just aren’t nuanced enough for code and tend to miss subtle issues or give generic advice. The API cost for this is surprisingly low. For a small team pushing a dozen commits a day, you’re looking at maybe $15-20/month on GPT-4o. That’s less than an hour of a junior developer’s time, which makes it a fantastic deal.

How Do You Actually Prompt for a Good Code Review?

This is the most critical part. A lazy prompt gets you lazy results. A precise, structured prompt gets you precise, structured results. You need to give the AI a role, clear context, a list of specific rules to enforce, and a strict output format. Don’t ask it to be creative; tell it to be a linter.

Here’s a prompt structure that I’ve found works reliably. It’s designed to be placed in your Python script and populated with the actual code diff.

You are an automated code review assistant. Your role is to perform a first-pass check on a code diff from a pull request. You are precise, objective, and only report on violations of the rules provided below. Do not offer stylistic opinions or suggestions outside of these rules. Do not comment on the logic or functionality of the code.Your task is to review the following code diff and identify any violations of our team's coding standards.## CONTEXTThe code is written in Python.The diff shows the changes proposed in a pull request. Lines starting with '+' are new lines. Lines starting with '-' are removed lines.## RULES TO ENFORCE1.  **Docstrings:** Every public function and class must have a Google-style docstring. The docstring must describe the function's purpose, arguments (`Args:`), and return value (`Returns:`).2.  **Type Hinting:** All function arguments and return values must have type hints.3.  **Variable Naming:** All variables and function names must be in `snake_case`. Class names must be in `PascalCase`.4.  **Print Statements:** No `print()` statements should be present in the committed code. Use a proper logger instead.## CODE DIFFdiff{code_diff_goes_here}## OUTPUT FORMATRespond with a JSON object ONLY. Do not include any other text, explanations, or conversational filler. The JSON object should have a single key, "issues", which is a list of objects. Each object in the list represents one violation and must have three keys: `line_number` (integer), `rule_violated` (string, e.g., "Docstrings"), and `comment` (string, a brief explanation of the issue).If there are no issues, return an empty list: `{"issues": []}`.

This prompt does several things right. It defines a narrow role, provides the specific rules, and—most importantly—demands a machine-readable JSON output. This structure is the foundation of a real operator playbook for AI automation.

A Concrete Example: Linting and Docstrings

Let’s see this in action. Imagine a developer submits a pull request with the following new Python function:

+ def processData(data, Retries=3):+     # a function to process some stuff+     print(f"Processing {data}")+     for i in range(Retries):+         if data.get("status") == "ok":+             return True+     return False

Our Python script would grab this diff, insert it into the prompt template, and send it to the OpenAI API. The expected JSON response would be:

{  "issues": [    {      "line_number": 1,      "rule_violated": "Variable Naming",      "comment": "Function name 'processData' should be in snake_case, like 'process_data'."    },    {      "line_number": 1,      "rule_violated": "Docstrings",      "comment": "Missing a Google-style docstring explaining the function, its arguments, and what it returns."    },    {      "line_number": 1,      "rule_violated": "Type Hinting",      "comment": "Missing type hints for the 'data' and 'Retries' arguments and the function's return value."    },    {      "line_number": 3,      "rule_violated": "Print Statements",      "comment": "A 'print()' statement was found. A logger should be used instead for production code."    }  ]}

This structured output is gold. Your GitHub Action can now parse this JSON and automatically post each issue as a comment on the corresponding line in the pull request. No human intervention needed. What I love about this system is the consistency. A human reviewer might miss a missing docstring on a Friday afternoon. The AI agent never does. It’s a perfect, tireless nitpicker for the stuff humans shouldn’t be wasting brainpower on.

How to Debug When This Breaks

This system will fail (and trust me, it will fail in weird ways the first few times you run it). The key is knowing what to look for. The failures usually fall into a few predictable categories.

  1. The AI Hallucinates a Rule: Sometimes, the model will get creative and flag something that isn’t in your rules. For instance, it might complain about line length even if you didn’t specify a rule for it. The fix is to tighten the prompt. Add a line like: “You must *only* check for the rules listed above. Do not report on any other potential issues.” Reinforcing the constraints is crucial.
  2. The Output is Broken JSON: The most common failure mode. The model might add a friendly intro like “Sure, here is the JSON you requested!” which breaks your parser. The fix is two-fold. First, be explicit in the prompt: “Respond with a JSON object ONLY. Do not include any other text…”. Second, add a simple example of the desired output in the prompt (this is called one-shot prompting). Showing it what you want is more effective than just telling it.
  3. The AI Misses an Obvious Violation: This happens with less capable models, which is why I recommend GPT-4o. If it’s still happening, your prompt might be too complex or the code diff too large. You can try breaking down large diffs into smaller chunks and making multiple API calls. Or, simplify your rule set. It’s better to have an agent that reliably catches 5 types of errors than one that unreliably catches 10.

Is This a Replacement for Human Reviewers?

Absolutely not. This entire system is designed to handle the first 10 minutes of a code review—the boring, repetitive part. It frees up human developers to focus on what matters: the business logic, the architecture, the potential edge cases, and the overall strategy.

An AI agent can’t tell you if a feature is a bad idea. It can’t ask clarifying questions about user requirements. It can’t judge if a particular abstraction is clever or just overly complex. That’s what your senior engineers are for. This tool lets them spend their expensive time on those high-value judgments instead of checking for missing commas and style guide pedantry.

If you implement this correctly, you’re not just automating a task; you’re creating a higher-quality feedback loop for your entire team. Junior developers get instant, objective feedback, and senior developers can dedicate their reviews to mentorship and substantive architectural guidance.

If you want the deep cut on this, deeper coverage of AI agent platforms.

You can build this entire workflow yourself following the steps above. It’s a powerful piece of no code AI (or rather, low-code) infrastructure that pays for itself almost immediately. If you’d rather skip the build and deploy a working version in an afternoon, we’ve packaged this exact workflow as a deployable AI automation 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.