Harry Tormey
Harry TormeySan Francisco

How APX’s Sentry agent finds and fixes N+1 queries in a Django app

APX is an AI agents platform that monitors production infrastructure using your existing tooling to detect performance issues, and suggest targeted fixes.

Our AI agents integrate with GitHub, Datadog, Sentry, and Postgres to identify problems and either push fixes directly or deliver high-impact recommendations. They work like a team of engineers on-call 24/7 to reduce your observability costs, fix production errors, and eliminate database bottlenecks.

Previously, I showed how the APX Sentry agent caught a frontend bug where a function was called on a null object and automatically generated a pull request in GitHub with a one-line fix. Today we’ll tackle something more challenging: how our Sentry agent can detect and resolve an N+1 query in a Django application. 

What is APX’s Sentry Agent?

Our APX Sentry agent continuously watches your systems, detects issues as they arise, and suggests solutions before they impact users. Instead of waiting for engineers to sift through logs and alerts, APX pinpoints the root cause, explains why the issue is happening, and suggests a fix.

Why APX is different from other AI monitoring tools

Our APX Sentry agent continuously monitors your systems, detects issues as they arise, and suggests solutions before they affect users. While Sentry can be noisy, APX zeroes in on high-impact problems, explains their root causes, and shows you what to fix, and, what to ignore

APX analyzes telemetry from tools like Sentry, Datadog, and Postgres, proactively identifying common issues like application crashes, performance bottlenecks, and inefficient database queries. For simple, high-confidence fixes, it automates the solution by generating a pull request. But for complex, multi-file problems, APX takes a pragmatic approach, providing an enriched prompt instead of a full AI-generated pull request. The enriched prompt includes root cause analysis, recommended solutions, and everything you need to feed into your AI workflow (like Cursor).The enriched prompt includes:

  • The exact source of the problem (e.g., specific methods in spree/serializers.py)
  • A recommended fix that aligns with best practices
  • Contextual production data pulled from tools like Sentry, Datadog, and Postgres

We take a pragmatic approach to AI-driven monitoring. Instead of forcing AI-generated code that might not fit your coding standards, APX lets developers take control of implementation. The enriched prompt can be copied into tools like Cursor, Windsor, or GitHub Copilot, allowing teams to refine solutions before merging.

1. Detecting the issue

The APX Sentry Agent flagged a real performance bottleneck in Django application, a classic N+1 query problem in the /api/spree/products/endpoint which was slowing down product listings. I could see that database operations were consuming 95% of the request time. 

Instead of efficiently retrieving categories and images in bulk, the API was executing separate database queries for each product. The more products it fetched, the worse the problem became.

Normally, diagnosing something like this would mean digging through logs, manually tracing queries, and trying to reproduce the issue in staging. But the APX Sentry agent had not only pinpointed the affected files but provided an actionable breakdown of the issue in seconds.

2. Generating an enriched prompt

To solve the problem, the Sentry agent automatically generated an enriched prompt that explained exactly why this was happening and how to fix it. The problem boiled down to two key inefficiencies:

  • The ProductSerializer in spree/serializers.py fetched categories and images inefficiently, triggering a new database query for each product instead of handling them in bulk
  • The ProductViewSet in spree/views.py didn’t use Django’s prefetch_related, which meant the application was issuing redundant queries instead of optimizing its database access

And so the enriched prompt suggested two targeted fixes:

  1. Adding prefetch_related in ProductViewSet to fetch all related data in a single, efficient query
  2. Modifying the serializer methods to use the prefetched data, eliminating unnecessary queries entirely

APX’s enriched prompt pinpoints the exact file paths, the lines of code to update, and why the fix improves performance. I had full control over what the final implementation looked like in production.

3. Implementing the fix in Cursor

You could have APX auto-generate a pull request, and for simple, high-confidence issues, that’s often the fastest path. But in this case, I wanted more control over how the fix was applied.

Some teams prefer to review and adapt fixes to fit their code standards, ongoing initiatives, or testing requirements. That’s where enriched prompts shine. I copied the prompt into Cursor, which used APX’s detailed analysis to generate the optimized fix.

These changes completely eliminated the redundant queries, significantly improving API response times and reducing database load. The fix was clean, idiomatic, and merge-ready on the first try too. No unnecessary back-and-forth in code review.

This gave me the best of both worlds: APX's deep system insight paired with Cursor's flexible AI-assisted implementation.

APX balances automation with dev control

APX is redefining AI-driven monitoring by balancing automation with developer control. Instead of flooding teams with generic AI-generated code, our suite of agents deliver precise, high-quality insights that integrate seamlessly into your existing developer workflow. 

At StepChange, we built APX to fundamentally change how reliability is managed. Instead of engineers constantly reacting to outages, APX proactively detects, fixes, and optimizes your infrastructure autonomously. 

Instead of spending your time debugging and optimizing manually, let AI-powered agents do it for you. Try APX for free with a 30-day free trial or book a live demo to see how APX can eliminate firefighting and keep your systems running at peak performance. 

We work with companies of all sizes, from smaller teams to enterprise-level operations, and can tailor APX agents to your specific enterprise requirements. 

Read Next

Contact Us

Contact us and we'll get back to you shortly.

hello@stepchange.work

StepChange

StepChange Labs © 2025·Privacy&Terms