An AI code reviewer checks a Git change for bugs and security issues before you push or merge. Reviewers that only read the diff have to guess what the rest of the file looks like and whether a suspected bug actually fails at runtime, and their suggested fixes come back as text you copy in by hand.
The reviewer in this guide uses the OpenRouter Responses API:https://openrouter.ai/docs/api_reference/responses/overview to run your changed files in a Linux sandbox, confirm each bug by running a check, and return fixes as patches you can apply with a flag. Across the four models we tested on a five-bug change, a review takes one to four minutes and costs between $0.015 and $0.045.
The script combines three OpenRouter server tools:https://openrouter.ai/docs/guides/features/server-tools and one function tool on the Responses API:
You need Python 3.11 or newer, Git, and an OpenRouter API key from the quickstart:https://openrouter.ai/docs/quickstart exported in your shell:
The Files API, Shell tool, and Apply Patch tool run on the global endpoint ( https://openrouter.ai/api/v1 ); in-region endpoints return 403 . Your changed files and context files are uploaded to your OpenRouter workspace and sent to the model provider, so only run the script on code you are allowed to share and keep secrets out of those files.
Save the following script as review.py in the root of your repository:
Several choices in review.py come from issues we hit while building it:
Create a small repository where the existing tests pass even after a change introduces five bugs.
Save this five-bug change as buggy.diff :
Commit the initial files, apply buggy.diff , and run the existing tests:
The two existing tests still pass because they do not cover what the diff changed. (Our sandbox had Python 3.11.14 and SQLite 3.50.4; unittest works out of the box, while pytest is not installed.)
Now run the reviewer with the test file as context:
Here is the output from one of our anthropic/claude-haiku-5.5 runs:
The script exits 1 because three findings are HIGH . Four findings are confirmed by running code in the sandbox, and their commands and outputs are saved in .git/ai-review.json under evidence . The removed conn.close() is unconfirmed because CPython closes the local connection when the function returns. Pass --apply to copy the patched app/orders.py into your working tree.
Save this script as .git/hooks/pre-push and run chmod +x .git/hooks/pre-push :
When we committed review.py on main , committed the five-bug change on top, and ran git push origin main , the hook ran the review ( 5 findings, 1 patch, $0.0117, 96 s ), exited 1 , and Git printed error: failed to push some refs . The remote stayed at the previous commit.
Save this workflow as .github/workflows/ai-review.yml . We did not run this workflow in GitHub Actions; we ran the same git show and review.py commands locally.
Running git show "$BASE:review.py" uses the base branch’s copy of the script so a pull request cannot modify the reviewer that checks it, and python -I keeps the pull request’s files off Python’s import path. Fork pull requests skip the job, and timeout-minutes: 15 covers the one-to-four-minute review time.
Add OPENROUTER_API_KEY under Settings, Secrets and variables, Actions, and make review a required status check in your branch protection rule:https://docs.github.com/en/repositories/configuring-branches-and-merges-in-your-repository/managing-protected-branches/managing-a-branch-protection-rule. For a broader CI gating setup, see how to gate pull requests on LLM evals in CI:https://openrouter.ai/blog/tutorials/how-to-gate-pull-requests-on-llm-evals-in-ci/.
We ran three reviews per model on the sample above with default settings and --context tests/test_orders.py . “Bugs found” counts the five planted bugs across completed reviews. “Confirmed by running code” means the model ran a check in the sandbox that showed the bug. “Patch fixed all five” means we applied the accepted patches to a clean copy and our own tests passed for all five bugs.
Start with anthropic/claude-haiku-5.5 when you want the lowest cost per review ($0.0153), or anthropic/claude-sonnet-5.5 ( --model anthropic/claude-sonnet-5.5 ) when you want faster reviews (60 s) and patches that fixed all five bugs in every run ($0.0447). You can compare more models in the model catalog:https://openrouter.ai/models and test them on your own repository with Ori Eval:https://openrouter.ai/docs/guides/ori/eval.
Each review bill includes model tokens plus container:https://openrouter.ai/docs/guides/features/containers sandbox time at $0.0001 per second. The Files API has no separate charge (up to 100 MiB per file and 10 GiB per workspace, and workspace files do not expire).
Sandbox billing runs from the first shell command until the response completes, so model thinking time during the review also counts. As a result, a slower model pays more for the sandbox even when its tokens are cheap: sandbox time was $0.0074 to $0.0159 of each Haiku review, compared with $0.0047 to $0.0068 for the faster Sonnet runs.
A diff-only Chat Completions reviewer costs about $0.001 per review on Haiku 5.5. Running the code in a sandbox costs 10 to 40 times more, and in exchange the findings come with reproducer evidence and tested patches.
The script uploads your changed files, any context files you pass, and the Git diff to your OpenRouter workspace with the Files API, and the model provider receives their contents during the review. Uploaded files cannot be downloaded back and can only be used inside OpenRouter shell sandboxes. The script deletes every uploaded file in a finally block before it exits.
In our three-run tests on a five-bug change, a review averaged $0.0153 with Claude Haiku 5.5, $0.0167 with GPT-6 Luna, $0.0307 with DeepSeek V4.1 Flash, and $0.0447 with Claude Sonnet 5.5. That total includes model tokens and the Linux sandbox at $0.0001 per second from the first shell command until the response completes.
By default the sandbox has no outbound internet access, so the model cannot run pip install. It can still read code that imports third-party packages and propose patches, though any check that imports those packages will fail. If your checks need packages from PyPI, you can set network_policy on the shell tool to allowlist hosts such as pypi.org and files.pythonhosted.org.
No. When the model proposes a fix with the apply_patch tool, the script applies it to a temporary copy of your files and tells the model whether it applied cleanly. Your working tree is only updated if you pass the —apply flag.
Model usage data, product updates, and research reports. One email each week.
情报判断
Aioga 编辑摘要
OpenRouter 发布教程,介绍一个 233 行、仅用标准库的 review.py 脚本。该脚本通过 Responses API 组合 Files API、Shell 沙箱工具和 Apply Patch 工具,将变更文件上传到托管 Linux 沙箱,让模型实际运行检查确认 bug,并返回可用标志应用的 V4A 补丁。