Automated code review is usually a paid feature. This article shows how to build a lightweight PR reviewer using MonkeyCode's free server and free token allowance. The result is a GitHub Action that comments on diffs with suggestions, without requiring a local GPU or a paid API key.
MonkeyCode is an open-source coding assistant that provides an OpenAI-compatible endpoint. Its free server option means a CI runner can call the model without provisioning hardware. The free allowance of 10 million tokens is enough for a small repository's pull request traffic. Disclosure: This article was prepared as part of MonkeyCode's product outreach. The exact server URL, model name, and token policy change over time; check the official documentation before relying on them.
What the workflow does
The workflow triggers on every new pull request and every update to an existing one. It checks out the code, computes the diff against the base branch, sends that diff to the model, and posts the response as a PR comment. The entire pipeline runs on GitHub-hosted runners, so no self-hosted infrastructure is needed.
The workflow file
Create .github/workflows/ai-review.yml in the repository:
name: AI PR Review
on:
pull_request:
types: [opened, synchronize]
jobs:
review:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: actions/setup-python@v5
with:
python-version: '3.12'
- run: pip install openai
- name: Generate review
env:
MONKEYCODE_API_KEY: ${{ secrets.MONKEYCODE_API_KEY }}
MONKEYCODE_BASE_URL: ${{ secrets.MONKEYCODE_BASE_URL }}
MODEL_NAME: ${{ vars.MODEL_NAME }}
run: |
python - <<'EOF'
import os, subprocess, openai
client = openai.OpenAI(
api_key=os.environ["MONKEYCODE_API_KEY"],
base_url=os.environ["MONKEYCODE_BASE_URL"],
)
diff = subprocess.check_output(
["git", "diff", "origin/main...HEAD"]
).decode(errors="replace")
prompt = (
"You are a senior code reviewer. Review this diff for bugs, "
"style issues, and security problems. Be concise. Use bullet points. "
"If the diff is clean, say 'No issues found.'\n\n"
f"{diff}"
)
response = client.chat.completions.create(
model=os.environ.get("MODEL_NAME", "monkeycode-default"),
messages=[{"role": "user", "content": prompt}],
max_tokens=500,
)
comment = response.choices[0].message.content
with open("/tmp/ai-review.md", "w") as f:
f.write(comment)
EOF
- name: Post comment
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh pr comment ${{ github.event.pull_request.number }} --body-file /tmp/ai-review.md
The workflow assumes the base branch is main. For other base branches, change the git diff command accordingly.
Setting the secrets
The workflow reads three values from the environment. MONKEYCODE_API_KEY and MONKEYCODE_BASE_URL are stored as repository secrets. MODEL_NAME is optional and can be stored as a repository variable. The default value in the script is a placeholder; replace it with the actual model name from the MonkeyCode documentation.
Handling duplicate comments
The workflow above comments on every push. That creates noise. A simple fix is to check for an existing comment before posting. The following step replaces the final one:
- name: Post comment (skip duplicates)
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
EXISTING=$(gh pr view ${{ github.event.pull_request.number }} --json comments --jq '.comments[].body' | grep -c "AI Review" || true)
if [ "$EXISTING" -eq "0" ]; then
gh pr comment ${{ github.event.pull_request.number }} --body-file /tmp/ai-review.md
fi
This checks whether any existing comment contains the marker string "AI Review". The prompt can be adjusted to include that marker in the output.
Keeping the diff small
The free token allowance disappears quickly if every review sends a large diff. A practical limit is 500 changed lines. The script can enforce that with a simple check:
lines = diff.count("\n")
if lines > 500:
comment = "Diff too large for free-tier review. Skipping."
else:
# call the model
Large diffs produce vague feedback anyway. A focused review of a small change is more useful than a rushed review of a thousand lines.
Limitations and who should skip this
This workflow is not a replacement for human review. The model can miss context, hallucinate APIs, or produce false positives. The free server may have rate limits and latency spikes. The token allowance is shared across all uses of the account, so heavy use elsewhere reduces what is available for CI.
Teams working with proprietary code should not send diffs to a managed server. Teams that need a guaranteed SLA or a fine-tuned model on their own codebase should look at self-hosted options. Teams that already use a commercial review tool may find this redundant.
A practical starting point
The workflow above is a complete, runnable starting point. It takes about fifteen minutes to configure: create the workflow file, add the secrets, and push a test PR. The first review will show whether the model's style matches the project's expectations. Adjust the prompt until the output is useful.
For a small open-source project, this setup provides a second pair of eyes at zero marginal cost. That is the real value of a free server and a free token allowance. The tradeoff is trust: the code leaves the repository. For public repositories, that tradeoff is usually acceptable. For private ones, measure the risk before enabling the workflow.
MonkeyCode provides free models that can run this workflow.
Top comments (0)