This recipe wires up a small CLI script that reads a git diff, sends it to a model via the OpenDunes API, and streams a structured code review back to your terminal. Drop it into a pre-push hook or a CI step to get automated review on every diff.
- Read the diff from
git diff (or a file).
- Construct a system prompt that instructs the model to act as a code reviewer.
- Pass the diff as the user message.
- Stream the response so feedback appears as it's generated.
Save this as review.py and run it from any git repo:
"""
review.py — stream an AI code review for the current git diff.
Usage: python review.py [--staged] [base_ref]
"""
import os
import sys
import subprocess
from openai import OpenAI
SYSTEM_PROMPT = """You are an expert code reviewer.
Given a unified diff, produce a concise review that covers:
1. Bugs or logic errors
2. Security concerns
3. Performance issues
4. Style or readability improvements
Be specific: cite the file and line number. Skip praise for correct code.
Keep the review under 600 words unless the diff is very large."""
def get_diff(staged: bool, base_ref: str | None) -> str:
if staged:
result = subprocess.run(["git", "diff", "--staged"], capture_output=True, text=True)
elif base_ref:
result = subprocess.run(["git", "diff", base_ref, "HEAD"], capture_output=True, text=True)
else:
result = subprocess.run(["git", "diff"], capture_output=True, text=True)
result.check_returncode()
return result.stdout.strip()
def main():
staged = "--staged" in sys.argv
base_ref = next((a for a in sys.argv[1:] if not a.startswith("--")), None)
diff = get_diff(staged, base_ref)
if not diff:
print("No changes to review.")
return
client = OpenAI(
base_url="https://opendunes.com/api/v1",
api_key=os.environ["OPENDUNES_API_KEY"],
)
print("Reviewing diff…\n")
stream = client.chat.completions.create(
model="anthropic/claude-opus-4.8",
messages=[
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": f"```diff\n{diff}\n```"},
],
stream=True,
max_tokens=1024,
)
for chunk in stream:
piece = chunk.choices[0].delta.content or ""
print(piece, end="", flush=True)
print()
if __name__ == "__main__":
main()
python review.py
python review.py --staged
python review.py main
#!/usr/bin/env node
import OpenAI from "openai";
import { execSync } from "child_process";
const SYSTEM_PROMPT = `You are an expert code reviewer.
Given a unified diff, produce a concise review covering bugs, security issues,
performance problems, and readability. Cite file and line number. Be direct.`;
const diff = execSync("git diff HEAD~1 HEAD").toString().trim();
if (!diff) { console.log("No changes to review."); process.exit(0); }
const client = new OpenAI({
baseURL: "https://opendunes.com/api/v1",
apiKey: process.env.OPENDUNES_API_KEY,
});
console.log("Reviewing diff…\n");
const stream = await client.chat.completions.create({
model: "anthropic/claude-opus-4.8",
messages: [
{ role: "system", content: SYSTEM_PROMPT },
{ role: "user", content: "```diff\n" + diff + "\n```" },
],
stream: true,
max_tokens: 1024,
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
}
console.log();
set -e
echo "Running AI code review before push..."
python /path/to/review.py --staged
anthropic/claude-opus-4.8 gives thorough, high-signal reviews. For faster, cheaper feedback on large diffs, try google/gemini-2.5-pro (larger context window handles big diffs without truncation). Check current DA pricing before committing to a model for CI.
- Streaming — SSE format and mid-stream error handling.
- Models — compare context windows and DA pricing for code review tasks.
- Claude Code recipe — the intended Claude Code integration (not yet available).