Summarize Git Diffs with Claude AI

Use the Claude API to auto-generate pull request summaries from git diffs. Working Python code plus an instant no-code tool.

💥 50p impulse-buy: Power Prompts PDF (first 10 buyers) 30 battle-tested Claude Code prompts · 8-page PDF · paste into CLAUDE.md and never re-type a prompt again · 50p impulse-buy, no commitment

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.

Quick implementation

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())

Summarize any branch 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
)

Structured output for CI integration

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}")

Stream the review for large diffs

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)

No-code alternative

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.

Cost estimate

PR size~TokensClaude 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.

Frequently asked questions

How do I summarize a git diff with Claude?
Pipe the output of `git diff HEAD~1 HEAD` (or `git diff main...feature-branch`) into Claude's API as the user message. Prompt it to act as a senior code reviewer and summarize changes by file, impact, and risks. See the Python example below.
Can Claude review my whole pull request diff?
Yes. Claude Sonnet has a 200K token context window, which fits most PR diffs. For very large PRs (10K+ lines changed), split by file or directory and summarize each chunk.
What prompt works best for reviewing diffs?
Instruct Claude to output: (1) a one-sentence overall summary, (2) a bullet list of changed files with what changed, (3) potential bugs or risks, (4) suggestions. This structure is parseable and matches code-review conventions.
Is there a no-code tool for AI git diff summaries?
Yes — paste your diff into the AI Diff Summarizer at diff-summarizer.vercel.app for an instant Claude-powered review without writing any code.
How much does it cost to summarize a git diff with Claude?
A typical PR diff (500 lines changed, ~4K tokens) costs under $0.02 with Claude Sonnet. A large refactor (5K lines, ~35K tokens) costs ~$0.15. Use the Claude API Cost Calculator to estimate your exact workload.

Free tools

Cost Calculator → API Cookbook → Diff Summarizer → Skills Browser →

More examples

Claude API Python QuickstartClaude API Node.js / TypeScript QuickstartClaude API Streaming in PythonClaude API Streaming in Node.js / TypeScriptClaude API Tool Use in PythonClaude API Tool Use in Node.js / TypeScript