Automatic code review
Have a model review each change before it merges.
- Give the review its own key.
- Write the review script.
- Run it on your branch.
- Run it on every pull request.
1. Give the review its own key
Create an API keyAPI keyThe key you create and send with every request that calls a model.More on API keys → named for the job, such as Code review, and cap it on API Keys (API keys shows how). A review runs on every push: its own key and cap keep that spend apart from your other apps, and a spent cap stops the review alone with a 402.
Put the key in OPENDUNES_API_KEY, in the terminal you run the script from.
2. Write the review script
The script reads what your branch changed since it left main, as a unified diff, and sends it to POST /v1/chat/completions with instructions for a reviewer. It prints the review, then the charge in DADAThe Algerian dinar, the one currency the docs show.More on Balance and charges →.
| Input | Holds |
|---|---|
BASE_REF | the branch your change goes into: origin/main |
INSTRUCTIONS | what the reviewer looks for, sent as the system message |
| the diff | git diff origin/main...HEAD: this branch's changes alone |
import os
import subprocess
from openai import OpenAI
API_KEY = os.environ["OPENDUNES_API_KEY"]
BASE_URL = "https://opendunes.com/v1"
MODEL = "anthropic/claude-sonnet-4.5"
BASE_REF = "origin/main"
INSTRUCTIONS = (
"You review a code change, given as a unified diff. "
"List bugs, security holes and missing tests, each with its "
"file and line, most serious first. Say nothing about what is fine."
)
diff = subprocess.run(
["git", "diff", f"{BASE_REF}...HEAD"],
capture_output=True,
text=True,
check=True,
).stdout
if not diff.strip():
print("No change to review.")
raise SystemExit
client = OpenAI(base_url=BASE_URL, api_key=API_KEY)
reply = client.chat.completions.create(
model=MODEL,
messages=[
{"role": "system", "content": INSTRUCTIONS},
{"role": "user", "content": diff},
],
)
print(reply.choices[0].message.content)
print(reply.usage.cost)The cURL tab builds the body with jq, which escapes the diff's quotes and line breaks for you.
3. Run it on your branch
Save the script as review.py at the root of your repository, fetch the base branch, and run it from your branch.
git fetch origin main
python review.pyThe review prints first, then its charge in DA. Illustrative, for a small change:
1. src/cart.py:42: total() adds the delivery fee twice when the
coupon is empty. Add it once, after the discount.
2. src/cart.py:57: no test covers an empty cart.
0.412871The whole diff is prompt tokens, sent again on every run. A branch that renames a folder or commits a generated file sends thousands of lines: review from a branch that holds your change alone, or leave generated files out with git diff origin/main...HEAD -- . ':!*.lock'.
4. Run it on every pull request
In your repository's settings on GitHub, add the review key as the Actions secret OPENDUNES_API_KEY. Then commit this workflow as .github/workflows/review.yml. On each pull request into main, it runs review.py and posts the review as a comment.
name: Code review
on:
pull_request:
branches: [main]
permissions:
contents: read
pull-requests: write
jobs:
review:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
with:
fetch-depth: 0
- uses: actions/setup-python@v7
with:
python-version: "3.13"
- run: pip install openai
- run: python review.py > review.md
env:
OPENDUNES_API_KEY: ${{ secrets.OPENDUNES_API_KEY }}
- run: gh pr comment "$PR" --body-file review.md
env:
GH_TOKEN: ${{ github.token }}
PR: ${{ github.event.pull_request.number }}fetch-depth: 0 fetches the history, so origin/main...HEAD finds where the branch left main.
Don't expect the review on a pull request opened from a fork. GitHub gives such a run no secrets: the script sends no key and gets a 401.
Review forks' changes from your own branch, with step 3, once you have read them.
Pick the reviewer
Any text model that reads code does the job: the model is the one value to change. A stronger model reads a large change more closely, and each review costs more.
Model IDModel IDThe name a request calls a model by, lab/model, copied from Models.More on Models → | Suits |
|---|---|
anthropic/claude-sonnet-4.5 | every pull request |
anthropic/claude-opus-5.5 | a change you want read line by line before a release |
{
"model": "anthropic/claude-opus-5.5"
}Each model's charge is on its page in Models. Every review's charge is on Activity, in the app, under the review key.
Errors
| Status | Means | Do |
|---|---|---|
| 401 | no key reached the script | set OPENDUNES_API_KEY in the terminal, or add the Actions secret |
| 402 | the balanceBalanceWhat you can spend, in DA.More on Balance and charges → is at zero or below, cannot pay the request's hold, or the review key's cap is spent | make a depositDepositMoney entering your balance, by CIB or Edahabia card.More on Balance and charges →, or raise the cap |
| 400 | the diff is longer than the model reads | review a smaller change, or pick a model that reads more |
Every other status: Errors and retries.
Next: Claude Code · Coding agents · Streaming
Last modified on