
August 17, 2026
11 min read
By Kokil Thapa | Last reviewed: September 2026
You want to add AI code review to your CI pipeline because manual reviews bottleneck your team. You are also right to worry about hallucinated feedback blocking merges. In practice, AI reviewers work best as non-blocking advisory steps. They surface security risks, style violations, and logic gaps before a human opens the merge request. This guide covers the GitLab CI configuration, prompt patterns, and safety guardrails I use on production Laravel and PHP projects. It also shows GitHub Actions equivalents for teams on that stack.
git diff, sends it to an LLM API with a structured system prompt, and posts the response as a merge request comment. Keep human approval required. Treat AI output as advisory, never as an automatic gate.Before wiring up any model, understand where this fits in your workflow. For teams already following Laravel API best practices, AI review catches deviations static analysis misses. Missing form request validation and N+1 queries are common examples. If you want help wiring this into an existing stack, my CI/CD pipeline setup services include AI review integration. The principle is simple: AI augments senior engineers. It does not replace their judgment.
How do you configure GitLab CI for AI code review?
The most reliable approach uses a dedicated CI job on merge requests only. It extracts changed files, calls an LLM endpoint, and posts feedback as a comment. This keeps the main branch clean. Review happens at the right moment in the workflow.
Extracting the diff safely
Never send entire files to the LLM. You waste tokens and expose unrelated code. Use git diff against the merge request base to get only changed lines. Pair this with secrets scanning in CI with gitleaks so API keys never reach the model.
# .gitlab-ci.yml excerpt
stages:
- test
- review
ai-code-review:
stage: review
image: alpine/git:2.45
variables:
GIT_DEPTH: 0
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
allow_failure: true
script:
- git fetch origin $CI_MERGE_REQUEST_TARGET_BRANCH_NAME
- git diff --unified=3 origin/$CI_MERGE_REQUEST_TARGET_BRANCH_NAME...HEAD > /tmp/mr.diff
- apk add --no-cache curl jq
- |
DIFF_CONTENT=$(cat /tmp/mr.diff | jq -Rs .)
RESPONSE=$(curl -s -X POST "$LLM_API_URL/v1/chat/completions" \
-H "Authorization: Bearer $LLM_API_KEY" \
-H "Content-Type: application/json" \
-d "{
\"model\": \"gpt-4o-mini\",
\"messages\": [
{\"role\": \"system\", \"content\": \"$SYSTEM_PROMPT\"},
{\"role\": \"user\", \"content\": \"Review this diff:\n\" + $DIFF_CONTENT}
],
\"max_tokens\": 1500
}")
COMMENT=$(echo "$RESPONSE" | jq -r '.choices[0].message.content')
curl -s -X POST "$CI_API_V4_URL/projects/$CI_PROJECT_ID/merge_requests/$CI_MERGE_REQUEST_IID/notes" \
-H "PRIVATE-TOKEN: $GITLAB_TOKEN" \
-H "Content-Type: application/json" \
-d "{\"body\": \"## AI Code Review\n\n$COMMENT\"}"
Three settings matter here. allow_failure: true keeps the pipeline green if the API times out. GIT_DEPTH: 0 ensures the diff has full history. The rules block limits the job to merge requests only.
Posting comments via the GitLab API
Store GITLAB_TOKEN as a masked CI variable with api scope. Use a project access token or bot account. Never commit tokens to the repo. For deeper GitLab CI patterns, see the GitLab CI pipeline for Laravel guide.
On sister sites I maintain with Deployer 7 and GitLab CI — including Notary Kathmandu — this job runs after PHPUnit and PHPStan. Static analysis catches typed errors. AI review catches architectural and security context that rules miss.
What prompt should you use for AI code review in CI?
A vague prompt produces vague feedback. Structure the system prompt with explicit output format, severity levels, and stack context. I base mine on patterns from prompt engineering for production systems.
You are a senior code reviewer for a Laravel 13 / PHP 8.3 application.
Review ONLY the provided git diff. Do not invent files or lines not shown.
Output format (Markdown):
## Summary
One sentence on overall change quality.
## Findings
For each issue:
- [CRITICAL|HIGH|MEDIUM|LOW] file:line — description — suggested fix
## Positive Notes
List good patterns you see.
Rules:
- Flag SQL injection, missing auth checks, and exposed secrets as CRITICAL.
- Ignore formatting already handled by Pint or ESLint.
- If the diff is too large to review, say so and list the top 5 files by risk.
- Never approve or reject the merge request.
Keep the prompt in a CI variable or a small file in the repo. Version it alongside your code. When Laravel upgrades change conventions, update the prompt in the same merge request.
Redacting sensitive data before the API call
Even with gitleaks in an earlier stage, add a redaction pass before the LLM call. Strip patterns matching API keys, JWTs, and database URLs. Read protecting PII and secrets in LLM apps for a fuller checklist. A regex tester helps you validate redaction patterns locally before pushing to CI.
Which LLM and API should you choose for pipeline code review?
Model choice is a cost-versus-quality trade-off. You do not need the largest model for routine diff review. Smaller models handle style and obvious bug patterns well.
| Model tier | Best for | Typical cost per MR | Latency |
|---|---|---|---|
| Small (gpt-4o-mini, Claude Haiku) | Style, naming, simple logic bugs | Rs 5–15 (~USD 0.04–0.10) | 5–15 seconds |
| Medium (gpt-4o, Claude Sonnet) | Security review, complex refactors | Rs 30–80 (~USD 0.22–0.60) | 15–45 seconds |
| Self-hosted (Ollama on runner) | Privacy-sensitive code, no egress | GPU runner cost only | 30–120 seconds |
For most Laravel teams, start with a small model on every MR. Escalate to a medium model only when the diff touches auth, payments, or file uploads. Track spend with the patterns in AI rate limits and cost optimization.
The OpenAI Chat Completions API and GitLab CI YAML reference are the two docs I keep open while wiring this up. For Laravel-specific API integration patterns, see OpenAI API integration in Laravel.
How do you prevent AI review from blocking merges?
This is the most common failure mode. Teams set AI review as a required job. The model hallucinates a CRITICAL issue. Developers learn to ignore all AI feedback. Within a month, the job is disabled.
Use this guardrail checklist on every setup:
- Set
allow_failure: trueon the AI review job. - Never add the AI job to protected branch required checks.
- Cap diff size — skip review when changes exceed 500 lines or 20 files.
- Run PHPStan, Pint, and PHPUnit first. AI review supplements static tools like PHPStan level 9 analysis.
- Log API failures separately. Alert on repeated timeouts, not on review content.
- Require at least one human approver on every merge request.
Combine AI review with existing quality gates. SonarQube security gates and code coverage gates handle measurable thresholds. AI handles contextual questions like "does this payment callback verify the signature?"
How do you integrate AI review with GitHub Actions?
Not every team uses GitLab. The same pattern works in GitHub Actions with a different comment API. Compare platforms in GitHub Actions vs GitLab CI.
# .github/workflows/ai-review.yml
name: AI Code Review
on:
pull_request:
types: [opened, synchronize]
jobs:
ai-review:
runs-on: ubuntu-latest
continue-on-error: true
permissions:
pull-requests: write
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Generate diff
run: |
git diff origin/${{ github.base_ref }}...HEAD > /tmp/pr.diff
- name: Call LLM and post comment
env:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
DIFF=$(cat /tmp/pr.diff | jq -Rs .)
REVIEW=$(curl -s https://api.openai.com/v1/chat/completions \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "Content-Type: application/json" \
-d "{\"model\":\"gpt-4o-mini\",\"messages\":[{\"role\":\"system\",\"content\":\"$SYSTEM_PROMPT\"},{\"role\":\"user\",\"content\":(\"Review:\\n\" + $DIFF)}]}")
BODY=$(echo "$REVIEW" | jq -r '.choices[0].message.content')
gh pr comment ${{ github.event.pull_request.number }} --body "## AI Review\n\n$BODY"
For a deeper GitLab-native bot approach, read building an AI code review bot for GitLab. That pattern adds inline comments on specific lines rather than a single summary note.
How do you measure whether AI review is worth keeping?
Track adoption, not vanity metrics. Count how many AI-flagged issues get fixed before merge. Count how many get dismissed with a reason. If dismissal rate exceeds 80%, your prompt or model tier needs adjustment.
Pair AI review with dependency vulnerability scanning and your existing test suite. On a production Laravel application, I have seen AI catch missing authorisation checks on new API routes. PHPStan did not flag them because the controller method existed and returned the correct type.
Validate JSON payloads from the API with a JSON formatter during local testing. Malformed responses should fail the job gracefully, not post garbage comments to the merge request.
If you need a full automation audit — CI, deploy, and AI integration — see AI integration and automation services or testing and optimization services. For custom pipeline work on greenfield apps, custom software development covers the full stack.
After merge, your deploy stage should stay independent. AI review never gates zero-downtime Deployer releases. The GitHub Actions encrypted secrets guide covers token storage if you use Actions instead of GitLab.
Key Takeaways
- Run AI review as a non-blocking merge request job with
allow_failure: trueorcontinue-on-error: true. - Send only
git diffoutput — never full files — and redact secrets before any API call. - Use a structured system prompt with severity levels so developers can triage feedback quickly.
- Start with a small, cheap model; escalate to larger models only for auth, payment, or upload changes.
- Keep PHPStan, tests, and dependency scans as hard gates; let AI handle contextual security questions.
- Track fix rate and dismissal rate monthly to tune prompts instead of disabling the job.
People Also Ask
Can AI code review replace human reviewers?
No. AI review catches patterns and flags risks early. It cannot judge product intent, business trade-offs, or team conventions that live in tribal knowledge. Keep at least one human approver on every merge request.
Is it safe to send code to cloud LLM APIs?
It depends on your data classification. Redact secrets and PII before any API call. For regulated or client-confidential code, use a self-hosted model on a private runner. Never send production credentials or customer data in diffs.
What file types work best with AI CI review?
Text-based source files work well: PHP, JavaScript, Python, YAML, and SQL migrations. Generated lock files, minified assets, and binary files should be excluded via .gitattributes or diff filters.
How long should an AI review CI job take?
Target under 60 seconds for small diffs with a fast model. Set a CI job timeout of 3–5 minutes. If review routinely exceeds that, reduce diff scope or switch to a faster model tier.
Ship AI review without slowing your team down
When you add AI code review to your CI pipeline the right way, developers get a second pair of eyes before standup. They keep full control over what merges. Start with one project, one non-blocking job, and a structured prompt. Expand after you measure real fix rates. Need help wiring this into GitLab CI on a Laravel or PHP stack? Contact us for a pipeline audit, or browse the Adventure Third Pole Trek portfolio for an example of production CI/CD in action.
Frequently Asked Questions
0 Comments
Leave a comment
Your email is not published. Comments appear once they have been read. Sign in to have your details filled in.

