Use the Claude API to auto-generate pull request summaries from git diffs. Working Python code plus an instant no-code tool.
Claude's large context window and code understanding make it ideal for automated PR reviews and diff summaries. This guide shows the minimal Python implementation, plus links to a no-code tool for one-off reviews.
import subprocess
import anthropic
def summarize_diff(base="HEAD~1", head="HEAD"):
diff = subprocess.check_output(
["git", "diff", base, head], text=True
)
if not diff.strip():
return "No changes detected."
client = anthropic.Anthropic()
message = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=1024,
messages=[{
"role": "user",
"content": f"""You are a senior code reviewer. Summarize this git diff:
{diff}
Output:
1. One-sentence overall summary
2. Changed files (bullet list, what changed in each)
3. Potential bugs or risks
4. Suggestions for improvement"""
}]
)
return message.content[0].text
print(summarize_diff())
# Compare feature branch to main
diff = subprocess.check_output(
["git", "diff", "main...feature/my-branch"], text=True
)
# Or get staged changes only
diff = subprocess.check_output(
["git", "diff", "--cached"], text=True
)
# Or compare specific files
diff = subprocess.check_output(
["git", "diff", "HEAD~1", "HEAD", "--", "src/api.py"], text=True
)
import json
def structured_diff_review(diff_text):
client = anthropic.Anthropic()
message = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=2048,
messages=[{
"role": "user",
"content": f"""Review this git diff and respond ONLY with valid JSON:
{diff_text}
{{
"summary": "one sentence overall summary",
"files_changed": [{{"file": "path/to/file.py", "changes": "what changed"}}],
"risks": ["list of potential bugs or security issues"],
"suggestions": ["list of improvement suggestions"],
"severity": "low|medium|high"
}}"""
}]
)
return json.loads(message.content[0].text)
# Use in CI pipeline
review = structured_diff_review(diff)
if review["severity"] == "high":
print("⚠️ High-risk changes detected — requires senior review")
for risk in review["risks"]:
print(f" - {risk}")
def stream_diff_summary(diff_text):
client = anthropic.Anthropic()
with client.messages.stream(
model="claude-sonnet-4-6",
max_tokens=2048,
messages=[{"role": "user", "content": f"Review this diff:\n\n{diff_text}"}]
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)
print() # newline at end
# Good for very large PRs where you want to see output as it arrives
stream_diff_summary(diff)
Don't want to write code? The AI Diff Summarizer lets you paste any git diff and get an instant Claude-powered code review. No API key needed — ideal for one-off reviews or sharing with teammates.
| PR size | ~Tokens | Claude Sonnet cost |
|---|---|---|
| Small (<100 lines) | ~1K | ~$0.003 |
| Medium (100–500 lines) | ~4K | ~$0.012 |
| Large (500–5K lines) | ~35K | ~$0.10 |
| Massive (>5K lines) | ~150K+ | ~$0.45 — consider chunking |
Use the Claude API Cost Calculator to estimate costs for your specific diff sizes.