DEV Community

Sam Chen
Sam Chen

Posted on

Build a Local AI Code Review Bot on MonkeyCode's Free Models and Free Server

Code review is the part of development that everyone agrees is important and nobody has time for. You can wait for a teammate to open your PR and respond sometime this week. Or you can let a local AI model give you a first pass in under a minute.

The catch used to be cost and data privacy. MonkeyCode, an open-source AI coding assistant, removes both by offering free models and a free server option you can run on your own machine. That combination means your code never leaves your laptop and your wallet stays shut.

Disclosure: This article was prepared as part of MonkeyCode's product outreach.

In this walkthrough, I'll show you how to build a tiny Python script that grabs a git diff, sends it to your local MonkeyCode server, and prints review comments. You'll end up with a reusable command that works on any repository.

Why run your own server?

Three reasons stand out.

  • Privacy: your code stays on your machine. No third-party API receives your diff.
  • Latency: the model runs locally, so you skip network round trips.
  • Cost: the free tier is exactly $0.

There are trade-offs, and I'll cover them at the end. But for personal projects, internal tools, and quick sanity checks, this setup is hard to beat.

What you need

  • Python 3.9 or newer
  • Git
  • MonkeyCode's server started locally (install instructions are in the project README)
  • An API key from your local server — usually just a random string you set once

Nothing else. No cloud account, no credit card, no external service.

Step 1: Start the free server

After you install MonkeyCode, launch the server with whatever command the README currently recommends. It will likely look something like this:

monkeycode-server --port 8000
Enter fullscreen mode Exit fullscreen mode

Keep that terminal open. The server now listens on localhost:8000 and exposes an OpenAI-compatible chat endpoint.

Step 2: Set environment variables

Point your script at the server with three variables.

export MONKEY_BASE="http://localhost:8000/v1"
export MONKEY_KEY="your-local-key"
export MONKEY_MODEL="" # check the server docs for the model identifier
Enter fullscreen mode Exit fullscreen mode

Some servers use an empty model name to select the default. Others expect a specific string. The documentation will tell you exactly what to put in MONKEY_MODEL.

Step 3: Write the reviewer script

Create a file named review.py with the following content. This script does three things: it reads the latest diff from git, sends it to your local server, and prints the model's feedback.

import os
import subprocess
import requests

def get_diff():
    result = subprocess.run(
        ["git", "diff", "HEAD~1", "--"],
        capture_output=True,
        text=True,
    )
    return result.stdout

def review(diff):
    base = os.getenv("MONKEY_BASE")
    key = os.getenv("MONKEY_KEY")
    model = os.getenv("MONKEY_MODEL")

    response = requests.post(
        f"{base}/chat/completions",
        headers={
            "Authorization": f"Bearer {key}",
            "Content-Type": "application/json",
        },
        json={
            "model": model,
            "messages": [
                {
                    "role": "system",
                    "content": (
                        "You are a senior code reviewer. Be concise and specific. "
                        "Focus on bugs, security issues, and readability problems. "
                        "Suggest concrete fixes. Ignore style nitpicks unless they affect correctness."
                    ),
                },
                {
                    "role": "user",
                    "content": f"Review this diff:\n\n{diff}",
                },
            ],
            "temperature": 0.2,
        },
    )
    response.raise_for_status()
    return response.json()["choices"][0]["message"]["content"]

if __name__ == "__main__":
    diff = get_diff()
    if diff.strip():
        print(review(diff))
    else:
        print("No diff found. Are you on a git repository with at least one commit?")
Enter fullscreen mode Exit fullscreen mode

The requests library is the only dependency. Install it once with pip install requests.

Step 4: Run it

From the root of any git repository, run:

python review.py
Enter fullscreen mode Exit fullscreen mode

You'll see something like this printed to your terminal:

- Line 14: You're comparing `count` to a string. Use `int(count)` or change the type earlier.
- Line 22: The `except` block swallows all exceptions. Catch `ValueError` explicitly.
- Suggestion: extract the retry logic into a helper to make it testable.
Enter fullscreen mode Exit fullscreen mode

Is this perfect? No. Is it a useful first pass before a human looks at the code? Absolutely.

Customizing the prompt

Your team's review style is unique. Adjust the system message to match it. For example, if you care about performance, add a line like:

"Flag any O(n²) patterns and suggest linear alternatives."
Enter fullscreen mode Exit fullscreen mode

If you're reviewing a security-sensitive change, add:

"Highlight any place where user input reaches a shell command or a database query."
Enter fullscreen mode Exit fullscreen mode

Tune the prompt until the output feels like a helpful colleague, not a nagging bot.

Turning it into a reusable command

You don't want to type python review.py every time. Add an alias to your shell config:

alias review-last='python ~/tools/review.py'
Enter fullscreen mode Exit fullscreen mode

Or wire it into a Git alias:

git config --global alias.ai-review '!python ~/tools/review.py'
Enter fullscreen mode Exit fullscreen mode

Now git ai-review works from anywhere in a repository.

You can also run it on a specific commit by changing the get_diff function. Replace HEAD~1 with a commit hash or a branch name like origin/main...HEAD to review only the changes in your working branch.

Decision table: should you use the free local server?

Scenario Recommendation
Personal project, public code Yes, great fit
Sensitive internal code Yes, because it runs locally
High-volume CI pipeline with SLAs No, use a paid hosted service
Team review process, non-critical Maybe, as an asynchronous first pass
Non-technical user No, you need to manage a server

This table is a starting point. Your mileage depends on your tolerance for occasional weird model output.

Limitations

Be honest about what this setup does not give you.

  • The free models have rate limits. They are fine for occasional diffs, not for thousands of requests per minute.
  • Output quality is below the best commercial models. You will see false positives and missed bugs.
  • There is no uptime guarantee. If the server crashes, you fix it yourself.
  • Resource usage is on you. A local model can consume significant CPU and memory.

Who should not use this

  • Teams that need enterprise-grade support or a formal SLA
  • Developers who cannot monitor a local process in their environment
  • Projects where a false negative means a regulatory violation

For those cases, a commercial API with a paid plan is the safer path. The free server is a learning tool, a privacy-friendly option, and a zero-cost starting point — not a replacement for every production need.

The takeaway

You do not need to wait for a human to catch obvious mistakes. With MonkeyCode's free models and free server, you can build a local AI reviewer in about ten minutes. You get privacy, zero cost, and a faster feedback loop.

Start on a side project. Run it on a small diff. Adjust the prompt until the comments feel useful. Then try it on your real work and see where it helps.

If you want a hands-on way to learn how local AI review feels, clone MonkeyCode's repo, start the free server, and run this script. Your future self — and your PR reviewers — will thank you.

Top comments (0)