AI Agents  

Part 1 — Building an AI-Powered Code Review System in C# Using Git Diff and LLMs

AI Code Review Image


This first part focuses on the foundation: extracting Git diffs and sending them to an LLM for intelligent review.

Why Git Diff Is the Best Starting Point

A common mistake when building AI code reviewers is sending the entire repository to the LLM.

This creates multiple problems:

  • High token consumption

  • Slower responses

  • Increased cost

  • Poor context relevance

  • More hallucinations

Modern AI review systems instead focus on Git diffs.

Git diffs provide:

  • Changed lines

  • Added code

  • Removed code

  • Minimal contextual information

This keeps reviews focused and efficient.

Example:

git diff HEAD~1 HEAD

Or compare against the main branch:

git diff main...HEAD

This approach scales far better in enterprise environments.

High-Level Architecture

The architecture is intentionally simple.

Developer Changes Code
        ↓
Git Diff Extraction
        ↓
Prompt Builder
        ↓
LLM API
        ↓
AI Review Response
        ↓
VS Code / PR Comments / CLI

This model can integrate into:

  • VS Code extensions

  • Azure DevOps

  • GitHub Actions

  • Pull request Automation

  • Internal Developer Portals

Step 1 — Extract Git Diff in C#

The first step is retrieving code changes from Git.

using System.Diagnostics;

public static class GitHelper
{
    public static async Task<string> GetDiffAsync()
    {
        var process = new Process
        {
            StartInfo = new ProcessStartInfo
            {
                FileName = "git",
                Arguments = "diff HEAD~1 HEAD",
                RedirectStandardOutput = true,
                UseShellExecute = false
            }
        };

        process.Start();

        string diff =
            await process.StandardOutput.ReadToEndAsync();

        await process.WaitForExitAsync();

        return diff;
    }
}

This extracts the latest code changes.

Step 2 — Build a Review Prompt

Once the diff is collected, we generate a focused review prompt.

public static string BuildPrompt(string diff)
{
    return $@"
You are a senior C# code reviewer.

Review ONLY the changed code.

Focus on:
- Bugs
- Security vulnerabilities
- Null safety
- Performance issues
- Maintainability

Be concise and actionable.

Git Diff:
{diff}
";
}

The quality of the prompt directly impacts review quality.

Step 3 — Send Prompt to LLM

Now we send the prompt to the LLM.

using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;

public class AiReviewService
{
    private readonly HttpClient _httpClient;

    public AiReviewService(HttpClient httpClient)
    {
        _httpClient = httpClient;
    }

    public async Task<string> ReviewAsync(string prompt)
    {
        var request = new
        {
            model = "gpt-4.1",
            messages = new[]
            {
                new
                {
                    role = "user",
                    content = prompt
                }
            }
        };

        var json = JsonSerializer.Serialize(request);

        var content = new StringContent(
            json,
            Encoding.UTF8,
            "application/json");

        _httpClient.DefaultRequestHeaders.Authorization =
            new AuthenticationHeaderValue(
                "Bearer",
                "YOUR_API_KEY");

        var response = await _httpClient.PostAsync(
            "https://api.openai.com/v1/chat/completions",
            content);

        response.EnsureSuccessStatusCode();

        return await response.Content.ReadAsStringAsync();
    }
}

Step 4 — Display Review Results

The response can be shown in multiple ways:

IntegrationExample
VS Code ExtensionInline warnings
CLI ToolConsole feedback
GitHub PRReview comments
Azure DevOpsPull request review
DashboardCode quality metrics

Best Practices

Review Only Changed Code

Avoid full repository reviews unless absolutely necessary.

Add Minimal Context

Include:

  • Related Interfaces

  • Nearby Methods

  • Dependent DTOs

Avoid sending unrelated files.

Prefer Structured Output

JSON responses are easier to integrate into:

  • PR Annotations

  • IDE Diagnostics

  • Dashboards

Final Thoughts

Part 1 established the foundation of an AI-powered code review system:

Git Diff
   ↓
 Prompt
   ↓
 LLM
   ↓
 Review Feedback

This works well for simple review automation.

However, modern AI systems are moving beyond single prompts into intelligent AI Agents that can:

  • Use Tools

  • Invoke Skills

  • Generate Fixes

  • Validate Results

  • Orchestrate Workflows

In Part 2, we’ll build a true AI Agent architecture in C# using modular skills for:

  • Code Review

  • Security Analysis

  • Automated Fix Generation

  • Validation Workflows