🎉 DevOps Interview Prep Bundle is live — 1000+ Q&A across 20 topicsGet it →
All Articles

Build an AI Dependency Upgrade Planner with Claude API

Renovate and Dependabot tell you a new version exists. Build a tool that reads the actual changelog and your codebase's usage patterns with Claude API to tell you whether the upgrade is safe, what specifically to test, and how to sequence a major version bump.

Shubham4 min read
Share:Tweet

Automated dependency bots correctly identify that a new version exists, but they don't read the changelog for you — a major version bump PR sits unreviewed for weeks because nobody wants to be the one who spends an hour reading release notes to figure out what actually changed. This tool does that reading and cross-references it against how your codebase actually uses the library.

Setup

bash
pip install anthropic requests

Changelog and Usage Analysis

python
import anthropic
import requests
import subprocess
 
client = anthropic.Anthropic()
 
 
def get_changelog_between_versions(package: str, current: str, target: str) -> str:
    """Pull the actual changelog content between versions — GitHub releases
    API works for most packages hosted there."""
    resp = requests.get(f"https://api.github.com/repos/{get_repo_for_package(package)}/releases")
    releases = resp.json()
    relevant = [r for r in releases if version_between(r["tag_name"], current, target)]
    return "\n\n".join(f"## {r['tag_name']}\n{r['body']}" for r in relevant)
 
 
def find_package_usage(package: str, repo_path: str) -> list[dict]:
    """Find every place in the codebase this package is actually imported and used —
    the specific API surface matters more than 'is it used at all.'"""
    result = subprocess.run(
        ["grep", "-rn", f"import.*{package}\\|from {package}", repo_path, "--include=*.py"],
        capture_output=True, text=True
    )
    return [{"file": line.split(":")[0], "line": line.split(":")[1], "code": line}
            for line in result.stdout.splitlines()]

Upgrade Risk Analysis

python
import json
 
ANALYZE_PROMPT = """Analyze this dependency upgrade for actual risk to our codebase.
 
Package: {package}
Current version: {current_version} -> Target version: {target_version}
 
Changelog between these versions:
{changelog}
 
How our codebase actually uses this package (imports and usage sites):
{usage_sites}
 
Determine:
1. Does the changelog mention any breaking changes that affect APIs
   we actually use (cross-reference against usage_sites, not just
   "there were breaking changes somewhere")
2. Are there deprecated APIs we're using that will be removed in a
   FUTURE version (worth fixing now even if this specific upgrade is safe)
3. What specifically should be tested after upgrading, based on which
   parts of our code touch the changed APIs
4. Recommended sequencing if this is a major bump with multiple breaking
   changes — should it be done in one PR or split into incremental steps
 
Respond with ONLY valid JSON:
{{"safe_to_auto_merge": true/false, "breaking_changes_affecting_us": [...],
  "deprecation_warnings": [...], "specific_test_recommendations": [...],
  "sequencing_advice": "..."}}"""
 
 
def analyze_upgrade(package: str, current: str, target: str, changelog: str, usage_sites: list) -> dict:
    response = client.messages.create(
        model="claude-sonnet-5",
        max_tokens=2000,
        messages=[{
            "role": "user",
            "content": ANALYZE_PROMPT.format(
                package=package, current_version=current, target_version=target,
                changelog=changelog[:6000], usage_sites=usage_sites,
            )
        }]
    )
    text = response.content[0].text.strip()
    if text.startswith("```"):
        text = text.split("```")[1].replace("json", "", 1).strip()
    return json.loads(text)

Example Output

Dependency Upgrade Analysis: sqlalchemy 1.4.51 -> 2.0.28

safe_to_auto_merge: false

Breaking changes affecting us (2):
- SQLAlchemy 2.0 removes the legacy Query.get() method in favor of
  Session.get(). Our codebase uses Query.get() in 14 locations
  (src/repositories/*.py). This WILL break on upgrade.
- The `autocommit` execution option was removed; we found 1 usage
  in src/db/legacy_migration_runner.py that relies on it.

Deprecation warnings (worth fixing regardless):
- 6 locations use the 1.x-style `session.query(Model).filter(...)`
  pattern that still works in 2.0 but is soft-deprecated in favor of
  `select(Model).where(...)` — not blocking, but worth migrating
  opportunistically since you're touching this code anyway.

Specific test recommendations:
- Run the full repository layer test suite (src/repositories/) given
  the Query.get() usage — these are the highest-risk call sites
- Manually verify src/db/legacy_migration_runner.py's autocommit
  behavior post-upgrade, no automated test currently covers this path

Sequencing advice: Do NOT do this as a single PR. First PR: migrate all
14 Query.get() call sites to Session.get() while still on 1.4.x (this
works on both versions). Second PR: bump to 2.0 once the codebase no
longer depends on the removed API. This avoids a single large PR
touching both dependency version and 14 call sites simultaneously.

Wiring Into Renovate/Dependabot PRs

yaml
# .github/workflows/dependency-analysis.yml
name: Dependency Upgrade Analysis
on:
  pull_request:
    types: [opened]
 
jobs:
  analyze:
    if: github.actor == 'renovate[bot]' || github.actor == 'dependabot[bot]'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: python analyze_upgrade.py --pr ${{ github.event.pull_request.number }}
        env:
          ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
      # Posts the analysis as a PR comment, turning a bare version-bump PR
      # into one with actual risk context attached

Why This Turns Bot-Generated PRs From Ignored to Reviewable

Renovate and Dependabot PRs get a bad reputation for being either auto-merged blindly (risky for major bumps) or left to rot unreviewed (defeats the purpose of automated dependency management) — both failure modes stem from the same root cause: nobody has time to manually read every changelog for every dependency bump. Attaching a specific, codebase-aware risk analysis to each PR is what makes "review this before merging" actually tractable instead of a chore everyone skips.


More AI DevOps tooling? Read our Renovate vs Dependabot dependency updates and Build AI feature flag risk analyzer with Claude API.

🔧

Today I Fixed

Short real fixes from production — posted daily

Browse fixes
Newsletter

Stay ahead of the curve

Get the latest DevOps, Kubernetes, AWS, and AI/ML guides delivered straight to your inbox. No spam — just practical engineering content.

Related Articles

Comments