AI-Assisted Code Review with LLMs: A Practical Guide

    This guide explains why traditional code review often misses semantic defects that appear only under specific input combinations, boundary data, or cross-module contract changes. It covers what large language models do well in code review, how to feed the right context in four layers, a reusable prompt template, a human-in-the-loop review workflow, and practical advice on choosing between free and paid tiers. The goal is to help developers use LLMs as a reliable review partner without shifting review responsibility away from people.

    1. The Problem Is Not Syntax, but the Illusion of “It Runs”

    Many defects never throw an exception. The code compiles, the service starts, and the main flow works. Change the input set, however, and the output drifts away from expectations. Three scenarios are typical.

    The first is rule combinations. A coupon works correctly on its own, and a full-reduction discount works correctly on its own, but the amount is calculated incorrectly when the two are stacked. The root cause is usually that priority and rounding timing are not clearly defined: apply the discount first or the reduction first, how to handle the remainder when an offer is split across multiple items. Different orders produce different results.

    The second is boundary data. Date logic behaves flawlessly on ordinary days but starts to drift at month-end, in February of a leap year, across year boundaries, and during time zone switches. Pagination parameters misbehave when the total is zero or divides evenly. Floating-point amounts accumulate precision errors after repeated additions.

    The third is cross-module contract drift. One place changes a field type from string to number, while another still parses the old format. Each file looks self-consistent on its own; the problem only surfaces when they are combined.

    1. # Defect example: stacking order and rounding timing are undefined
    2. def payable(amount, coupon=None, full_reduction=None):
    3. if coupon:
    4. amount -= coupon.value
    5. if full_reduction and amount >= full_reduction.threshold:
    6. amount -= full_reduction.value
    7. return round(amount, 2) # subtract first, round later; prone to remainders when splitting

    The conclusion is clear: the goal of code review is not to find syntax errors, but to find semantic errors under condition combinations. Compilers and static analysis tools are good at the former; the latter requires understanding business intent, which is exactly where large language models can help.

    2. What LLMs Do Well in Code Review

    When you hand code to a general-purpose LLM or an AI coding tool, the first thing it can cover is local semantic defects: misused variables, inverted conditions, missing null and exception branches, overlooked boundary conditions, and functions that return unexpected results for particular inputs. These problems are often “visible but inconspicuous,” and they tend to surface when the model restates the execution path line by line.

    Next is cross-file reasoning. When related files are provided together, the model can try to map call relationships and data contracts, identifying problems that require context to judge, such as a rename in one place while another still reads the old name. Its value lies in offering a fresh perspective: people who stare at the same code for a long time develop habits, while the model reorganizes the logic and often points out a branch that has never been covered.

    The capability boundary must be stated clearly. Pasting a few dozen lines into a chat window is not the same capability as letting a tool read the entire project and review changes alongside test cases. Files the model cannot see, the runtime environment, configuration, real data distribution, and business rules cannot be filled in by guessing. So when evaluating “can AI find the problem,” first confirm the context the model can actually access, rather than treating different usage modes as the same thing.

    3. The Key to Results: Feeding the Right Context

    The ceiling on output quality is set by the input information. Organize the material in four layers:

    • Code layer: relevant functions, callees, type definitions, and data models, not isolated snippets;
    • Contract layer: API inputs and outputs, field meanings, unit and precision conventions;
    • Rule layer: business rules such as discount stacking order, tax calculation basis, and state machine transition conditions;
    • Verification layer: existing unit tests, boundary cases, and production exception samples.

    A reusable prompt skeleton:

    1. Role: senior code reviewer
    2. Goal: find defects that return incorrect results under condition combinations and boundary data
    3. Context: <related files><type definitions><business rules><test cases>
    4. Requirements:
    5. 1. Report only concrete issues with a trigger condition, in the format location/trigger input/wrong result/fix suggestion;
    6. 2. Mark uncertain items as "needs confirmation"; do not speculate about business rules;
    7. 3. List suggested additional test cases at the end.

    Explicitly requiring a trigger condition and marking uncertainty significantly reduces vague statements and fabricated conclusions.

    4. A Practical Human-in-the-Loop Review Workflow

    1. Before the change: have the model generate a list of boundary cases from the requirement description, as input for writing code;
    2. During coding: use an AI coding assistant to complete boilerplate and explain unfamiliar modules, but verify critical logic yourself;
    3. Before committing: static analysis tools handle syntax and common defects, while the model handles semantic and combined-condition review;
    4. During review: treat the model output as a question list, and let people judge whether each item holds and whether to add tests;
    5. After release: feed real defect samples back into prompts and test cases to form an iteration loop.
    1. # Fix approach: define the order explicitly, use integer cents as a single unit, round once at the end
    2. def payable(cents: int, coupon_cents: int = 0, full_reduction_cents: int = 0) -> int:
    3. amount = cents
    4. if full_reduction_cents and amount >= THRESHOLD_CENTS:
    5. amount -= full_reduction_cents # full reduction first
    6. if coupon_cents:
    7. amount -= coupon_cents # then the coupon
    8. return max(amount, 0)

    5. Are Free Tiers Enough: How to Choose a Plan

    One point first: paying does not automatically mean fewer defects. It brings higher context capacity, longer file parsing, faster responses, and more stable availability at peak times. Choose in three steps:

    • Look at the task shape: occasional Q&A and single-file explanation are usually fine on a free tier; whole-repository context, long documents, and multi-round iteration require a higher tier;
    • Look at compliance requirements: for enterprise scenarios, prioritize options where data stays within your domain, is auditable, and can be deployed privately. General capabilities from platforms such as Baidu AI Cloud (model services, object storage, log services, monitoring and alerting) can be used to carry such a workflow;
    • Look at total cost: put the subscription fee and the loss from a single production amount-calculation error, plus rework effort, on the same sheet before deciding on a tier.

    The pragmatic advice is to first run the “context feeding + prompt + test case feedback” workflow on a free tier, confirm it reliably finds the problem types in your business, and only then pay for throughput and capacity.

    6. Conclusion

    AI does not replace review responsibility; it replaces the blind spot of “not seeing the problem.” Explain the business rules clearly, provide complete context, and treat conclusions as hypotheses to be verified, and an ordinary developer can make an LLM a reliable code review partner. As for a paid plan, it depends on how large a project you want the model to read, not on marketing claims.