The Cost of AI in CI

Integrating AI code review into Continuous Integration (CI) pipelines can seem like a smart move. However, a busy week of pull requests can lead to unexpectedly high bills when every commit, every push, sends full diffs to powerful, frontier AI models. This approach is often overkill, especially for minor changes like formatting adjustments or README updates. A more cost-effective strategy involves a tiered system: a lightweight, local model acts as a gatekeeper, determining which changes warrant the attention of a more expensive, sophisticated model, with prompt caching further optimizing resource usage.

The core idea is to avoid overwhelming costly models with trivial tasks. Instead, a cheaper, local model first assesses the diff. Its primary function is a classification task: is anything in this change sufficiently risky or complex to justify escalation? This triage step significantly reduces the volume of data sent to advanced models, thereby cutting costs without sacrificing critical code quality checks.

Diagram illustrating a two-tier AI code review process in CI

Tier One: Local Triage with Classification

The first tier of this AI review system is designed for speed and efficiency. It operates on a small, local model. On a standard developer machine, such a model can process changes quickly without significant overhead. The task is purely classification: does the change represent a potential risk or require deeper analysis? This is akin to a bouncer at a club, deciding who gets past the velvet rope. The local model is trained to identify patterns indicative of potential issues, such as significant logic changes, security vulnerabilities, or complex refactors. It doesn't need to understand the nuance of every line of code; it just needs to flag deviations that warrant further inspection.

The surprising detail here is not the efficiency of local models for classification, but how little data is actually needed to train them for this specific task. Instead of feeding them entire codebases, they can be fine-tuned on examples of risky vs. non-risky diffs. This makes them incredibly fast and cheap to run. The output of this tier is a simple binary decision: escalate or ignore. For ignored changes, the CI pipeline proceeds without further AI intervention. For escalated changes, the process moves to the second tier.

Tier Two: Caching and Frontier Models for Critical Diffs

When the first tier flags a diff for escalation, it is then passed to a more powerful, potentially cloud-based AI model. This is where prompt caching becomes crucial. Instead of sending the entire diff and context every time, the system checks if a similar diff has been reviewed recently. If a cached response exists for a near-identical change, that response is reused. This drastically reduces redundant API calls and computational load for the expensive model. The cache acts like a cheat sheet for the AI, remembering past decisions on common code patterns or bug fixes.

The context sent to the frontier model is carefully curated. It includes the diff itself, relevant surrounding code snippets, and the specific prompt that triggered the escalation. The prompt is designed to elicit a detailed review, focusing on aspects like potential bugs, security flaws, performance bottlenecks, or adherence to best practices. The combination of targeted prompts and prompt caching ensures that the most powerful AI capabilities are deployed only when necessary and with maximum efficiency. This strategy allows teams to leverage advanced AI for meaningful code quality assurance without incurring prohibitive costs.

Implementation and Future Considerations

Implementing this two-tier system requires a robust CI pipeline capable of orchestrating calls to local models and external APIs. Tools like Ollama can facilitate running small, local LLMs directly on CI runners or developer machines. For prompt caching, a simple key-value store or a dedicated caching service can be employed, with cache keys generated based on the diff content and the specific prompt. The effectiveness of the local triage model hinges on its training data; a well-curated dataset of risky vs. non-risky code changes is paramount.

What nobody has addressed yet is the potential for a