Running Code Review with Local AI (No Cloud, No Waiting)
Your pull request sits in queue waiting for review. It's 3 AM. Your coworker's asleep. You need feedback now.
This is where most people reach for ChatGPT and hope nobody finds their proprietary code in a screenshot. But there's a better way: run AI code review locally, offline, with models that actually understand code structure.
The Problem with Cloud Code Review
Every time you paste code to ChatGPT or Claude, you're:
- Uploading proprietary logic (even if you don't think you are)
- Waiting for API rate limits
- Building muscle memory to just... ask a chatbot instead of thinking through the problem
- Getting generic feedback ("add error handling", "consider logging")
Local models don't have these issues. They're slower, sure. But they're yours.
Which Local Model to Use
Ollama is the easiest entry point. Download, run one command, done. For code review specifically:
ollama pull mistral:7b-instruct-q4
This pulls Mistral 7B (quantized), which is ~4GB. It's not bleeding-edge, but it understands code semantics well enough for real feedback.
For something heavier, Llama 2 13B is the sweet spot:
ollama pull llama2:13b-chat
Trades more VRAM for noticeably better code understanding. If you have a GPU, use it. CPU-only? Stick with 7B.
The Setup (5 Minutes)
- Install Ollama from ollama.ai (Mac, Linux, Windows)
-
Pull a model:
ollama pull mistral:7b-instruct-q4 -
Start the server: Just running Ollama keeps it running on
localhost:11434 - Create a simple script:
import requests
import json
def review_code(code_snippet, language="python"):
prompt = f"""You are a strict code reviewer. Analyze this {language} code:
{code_snippet}
Provide:
1. Real bugs or logic errors (be specific)
2. Performance issues (not "could be faster" - real bottlenecks)
3. One thing they did well
Keep it short. No pleasantries."""
response = requests.post(
"http://localhost:11434/api/generate",
json={"model": "mistral:7b-instruct-q4", "prompt": prompt},
stream=True
)
full_response = ""
for line in response.iter_lines():
data = json.loads(line)
full_response += data.get("response", "")
return full_response
# Test it
with open("your_code.py") as f:
code = f.read()
print(review_code(code))
Save this as review.py. Run it. That's your code review bot.
Real Example: Catching the Bug I Almost Shipped
Here's a function I wrote last week:
def fetch_user_data(user_ids):
results = []
for uid in user_ids:
try:
data = api.get_user(uid)
results.append(data)
except APIError:
continue # Skip failed requests
return results
Looks fine. Runs. Ships.
Local Mistral caught it: "You're silently dropping errors. Caller has no way to know which IDs failed. Use a dict with success/failure flags, or re-raise after collecting failures."
That's the difference between "your code works" and "your code is reliable." Cloud AI would probably say "consider error handling" and move on.
Integration Tips
With Git hooks:
#!/bin/bash
# .git/hooks/pre-commit
git diff --cached > /tmp/staged_changes.txt
python review.py < /tmp/staged_changes.txt
Won't commit if review flags something. Annoying? Yes. Educational? Absolutely.
With CI/CD:
Toss this in your pipeline as a non-blocking check. It won't fail the build, but you'll see the feedback in logs.
Real use case: Our team added this to our PR template. No enforcement—just available when someone wants a second opinion at 3 AM.
Why This Actually Works
Local models are:
- Trained on code. Mistral and Llama have billions of GitHub tokens in their training data.
- Surprisingly good at spotting patterns your eyes miss (unused variables, off-by-one in loops, missing bounds checks).
- Terrible at architecture advice. Don't ask it to redesign your auth system. DO ask it to spot the null pointer you missed.
- Confidential. Runs on your machine. Stays on your machine.
The Catch
Speed. A 7B model on CPU takes 30 seconds to review a 50-line function. GPU? 3-5 seconds. If you're reviewing 100 PRs a day, this isn't your bottleneck solver—use it for the complex ones.
Also: local models hallucinate. They'll sometimes flag something as a bug that isn't. That's why they're a second opinion, not a replacement for human review.
Quick Wins You Can Grab Today
- Immediately: Install Ollama, pull Mistral, run the script above on your last five functions. See what it finds.
- This week: Add it to your pre-commit hook. Let it run in the background.
- This month: Measure how many real bugs it catches vs. false positives. Adjust your model if needed.
Most teams treat code review as "someone else's job." This makes it your tool. You get faster feedback, the junior dev learns more, and nothing leaves your machine.
Want to stay sharp on dev tools and productivity? Check out LearnAI Weekly—real tips from people actually using this stuff, not AI hype.
Top comments (1)
The strongest part of this is the constraint you put on the tool: use Ollama/Mistral locally to catch specific review smells, not to redesign the system. The
fetch_user_dataexample is exactly the kind of bug a tired reviewer misses, because swallowingAPIErrorturns a partial outage into clean-looking data. For a founder or engineering lead, I'd keep the pre-commit or CI version non-blocking until the team has measured false positives across real PRs; trust in review automation is earned by precision, not volume.