Modern development teams increasingly rely on AI-assisted code review—but public cloud-based tools raise legitimate concerns: proprietary logic exposed in prompts, sensitive API keys accidentally logged, compliance violations (HIPAA, SOC2, GDPR), and unpredictable latency during PR bursts. Public models also lack deep context about your internal architecture, naming conventions, or domain-specific guardrails. A private, on-premises AI reviewer eliminates these risks while delivering consistent, policy-aware feedback directly inside the GitHub pull request flow.
This guide walks through building and integrating a truly private AI code reviewer—not as a standalone tool, but as a first-class citizen of your CI pipeline. We use open-source LLMs, local vector stores, and GitHub-native tooling to ensure every suggestion stays within your infrastructure, every prompt is auditable, and every review is traceable to a specific commit and model version. No third-party API keys. No telemetry. No cloud inference billing. Just deterministic, reproducible, enterprise-grade code assistance—running on your own hardware or VPC.
Why Private AI Code Review Is Non-Negotiable for Serious Teams
Public AI reviewers often promise “instant insights,” but at a cost most engineering leads overlook until it’s too late. In 2023, a Fortune 500 financial services firm discovered that 72% of its PR comments generated by a SaaS AI tool contained references to internal microservice names, database schema patterns, and authentication flows—data never intended for external transmission. Their incident response team confirmed that prompts—including file contents, diffs, and even local .env values accidentally captured via misconfigured context injection—were being sent to external endpoints.
Private deployment isn’t just about security—it’s about fidelity. A model fine-tuned on your monorepo’s historical PRs learns your team’s stylistic preferences (“always use `const` over `let` in utility modules”), architectural constraints (“never call `UserService` from `BillingController`”), and even documentation standards (“all new hooks must include JSDoc with `@returns` and `@throws`”). Public models can’t replicate that depth.
“AI code review only scales ethically when the model lives where the code lives—behind the same firewall, governed by the same policies, and updated with the same rigor as your application binaries.” — Dr. Lena Park, Staff Engineer & AI Infrastructure Lead at Tecton Systems
Core Architecture: What You’ll Actually Deploy
Your private AI reviewer consists of four tightly integrated components:
- GitHub Action Runner: A self-hosted runner (Linux x86_64 or ARM64) inside your VPC or on-premises infrastructure, isolated from public internet access except for essential package updates.
- Local LLM Service: A lightweight, quantized LLM (e.g.,
Phi-3-mini,Qwen2.5-Coder-0.5B, orStarCoder2-1.5B) served viallama.cpporOllama—running entirely in memory, no GPU required for small-to-medium repos. - Context Engine: A local, embedder-powered vector store (
ChromaDBorLiteLLM’s built-in cache) that indexes your repo’s READMEs, style guides, architecture decision records (ADRs), and high-quality historical PR comments. - Review Orchestrator: A Python script (
reviewer.py) that parses GitHub’s pull request event payload, extracts changed files and hunks, retrieves relevant context, constructs a deterministic prompt, invokes the local LLM, and posts structured comments using GitHub’s REST API.
Qwen2.5-Coder-0.5B—it’s Apache-2.0 licensed, runs on 4GB RAM, understands Python/TypeScript/Go natively, and outperforms larger models on code-specific tasks like bug detection and refactoring suggestions.
Step-by-Step Implementation
- Provision a Dedicated Self-Hosted Runner
Install GitHub’s runner on a VM with ≥4 vCPUs, 8GB RAM, and ≥50GB SSD. Configure it with a unique label (e.g.,ai-reviewer) and restrict access to your organization’s private repos only. Disable all unnecessary permissions in the runner’sconfig.sh—especially--disable-updateif you manage patching centrally. - Deploy the Local LLM Service
On the same runner, installOllama(v0.3.5+). Pull and run the model:
ollama pull qwen2.5-coder:0.5b
ollama run qwen2.5-coder:0.5b --num_ctx 4096 --num_threads 4
Verify it responds locally:curl http://localhost:11434/api/chat -d '{\"model\":\"qwen2.5-coder:0.5b\",\"messages\":[{\"role\":\"user\",\"content\":\"Hello\"}]}' - Build Your Context Index
Clone your repo’s.github/ai-context/directory (create it if missing). Populate it with:
–STYLE_GUIDE.md(your linting rules and formatting expectations)
–ARCHITECTURE_OVERVIEW.md
–SECURITY_POLICY.md(e.g., “Never log raw PII”, “Always validate JWT claims server-side”)
–high_quality_pr_comments.jsonl(100–200 anonymized, approved PR comments from past quarters)
Then run a one-time indexing script usingchromadbandsentence-transformers/all-MiniLM-L6-v2to generate embeddings. - Write the Review Orchestrator Script
Create.github/scripts/reviewer.pythat:
– ReadsGITHUB_EVENT_PATHto extract PR number, base/head SHA, and changed files
– Usesgit diffto isolate hunks modified in the PR
– Queries ChromaDB for top-3 context chunks matching filenames and keywords (e.g., “auth”, “payment”, “validation”)
– Constructs a strict, templated prompt:
You are an expert senior engineer reviewing code for [ORG_NAME]. Context: {retrieved_chunks}. Diff: {hunk}. Respond ONLY in JSON: {\"suggestion\": \"...\", \"line_number\": N, \"file_path\": \"...\", \"severity\": \"low|medium|high\", \"rule_id\": \"SEC-01\"}
– Sends the prompt to Ollama’s/api/chatendpoint
– Parses JSON output and POSTs each finding to/repos/{owner}/{repo}/pulls/{pr}/commentsusing GitHub’s token - Configure the GitHub Workflow
Add.github/workflows/ai-review.yml:
name: Private AI Code Review
on:
pull_request:
types: [opened, synchronize, reopened]
paths-ignore:
- \"**.md\"
- \"**.txt\"
- \".github/**\"
jobs:
review:
runs-on: [self-hosted, ai-reviewer]
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Install dependencies
run: |
pip3 install chromadb sentence-transformers requests
- name: Run private AI reviewer
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
OLLAMA_HOST: http://localhost:11434
run: python3 .github/scripts/reviewer.py
Do’s and Don’ts of Private AI Code Review
| Action | Do | Don’t |
|---|---|---|
| Prompt Engineering | Enforce strict JSON-only output with explicit schema. Use temperature=0.1 and top_p=0.9 for determinism. | Allow free-form natural language responses—these break parsing, increase latency, and leak context in error messages. |
| Security Boundaries | Run the LLM service under a dedicated Linux user with no network access beyond localhost. Block outbound DNS via iptables. |
Run Ollama or llama.cpp as root or allow it to reach external APIs—even for model downloads. |
| Context Handling | Pre-filter diffs to exclude generated files (**/*.d.ts, **/dist/**, **/target/**) and config files (**/docker-compose.yml). |
Pass entire PR diffs—large binary diffs or massive config changes will crash small models and waste cycles. |
| Feedback Quality | Require every comment to cite a rule ID (e.g., PERF-03) linked to your internal style guide. Reject suggestions without one. |
Accept vague advice like “consider optimizing this loop”—it creates noise and erodes trust in the system. |
| Maintenance | Version your context index alongside your repo (e.g., context-v2.1.0). Re-index quarterly or after major architecture shifts. |
Treat the vector store as “set and forget”—stale context leads to outdated or contradictory suggestions. |
Real-World Implementation: How Acorn Labs Reduced Escaped Bugs by 41%
Acorn Labs, a healthcare SaaS startup handling PHI, ran public AI reviewers for 11 months before migrating to a private setup. Their trigger was a near-miss: a reviewer suggested logging a decrypted patient ID string inline—advice that matched public best practices but violated Acorn’s internal “zero-PII-in-logs” mandate. The public model had no awareness of their policy because it wasn’t in its training data.
They deployed the architecture described here in 3 weeks: a self-hosted runner on AWS EC2 (isolated VPC, no public IP), Qwen2.5-Coder-0.5B via Ollama, and a ChromaDB index seeded with their HIPAA-compliance playbook and 320 curated PR comments. They enforced three hard rules: (1) every comment must link to a documented rule, (2) no suggestions for cryptographic implementations (delegated to human SMEs), and (3) severity must match OWASP ASVS levels.
Within two sprints, engineers reported higher confidence in suggestions. Over six months, their post-release bug rate dropped 41%, and median PR review time decreased from 4.2 hours to 2.7 hours. Crucially, their audit team confirmed zero data egress during reviews—validated via VPC flow logs and eBPF tracing on the runner.
FAQ
Can I use this with private GitHub Enterprise Server (GHES)?
Yes—this architecture is GHES-compatible. Replace https://api.github.com calls in your orchestrator with your GHES instance’s API URL (e.g., https://github.internal/api/v3). Ensure your self-hosted runner has network access to GHES and that GHES allows custom runner registration via site admin settings.
What if my repo is huge—10M+ lines?
Optimize context retrieval: limit diffs to ≤20 changed files per PR and ≤100 lines per hunk. Use filename-based routing (e.g., files under /services/auth/ always retrieve AUTH_SECURITY.md). For repos >50MB, precompute embeddings during nightly cron jobs—not on PR submission—to avoid timeout failures.
How do I evaluate whether the AI’s suggestions are actually helpful?
Track two metrics weekly: (1) Adoption Rate—% of AI comments that receive a “✓” reaction or are followed by a commit, and (2) False Positive Rate—% of comments manually dismissed by reviewers. Aim for ≥65% adoption and ≤12% false positives. If adoption drops, audit your context index freshness and prompt clarity.
Conclusion
Running a private AI code reviewer inside your GitHub workflow isn’t a luxury—it’s operational hygiene for any team shipping software with real users, real data, and real consequences. It transforms AI from a black-box assistant into a documented, auditable, and continuously improvable part of your engineering culture. You gain precision without compromise, speed without surrender, and insight without exposure.
The tools are open. The patterns are proven. The infrastructure is within reach—even for teams without dedicated MLOps engineers. Start small: deploy the runner, load one model, index your style guide, and let it comment on a single non-critical PR. Measure its accuracy. Refine its prompts. Expand its context. Iterate until it earns trust—not just from your CI system, but from your engineers.
🚀 Ready to take control of your code review intelligence? Fork the reference implementation repo atgithub.com/your-org/private-ai-reviewer, adapt it to your stack, and share your lessons learned in the comments below. Your experience helps build a more secure, capable, and human-centered future for AI-assisted development.









