DEV Community

Cover image for Built a C# AI Agent That Researches Errors and Suggests Fixes
Kaushal Kumar
Kaushal Kumar

Posted on

Built a C# AI Agent That Researches Errors and Suggests Fixes

We've all done this.

  • You hit an exception
  • You copy the stack trace
  • Open Google
  • Read StackOverflow
  • Open GitHub issues
  • Check Microsoft docs
  • Read random blogs
  • Open three more tabs

Thirty minutes later you're still debugging.

Modern AI tools help, but there is a problem: They often answer from memory.

Sometimes they produce fixes that sound right but were never actually validated against real developer discussions, GitHub issues, or official documentation.

I wanted an AI agent that behaves more like a senior engineer sitting next to me:

  • Understand the exception
  • Research external sources
  • Analyze community findings
  • Rank likely causes
  • Suggest fixes with evidence

So I built a .NET Error Research Agent using:

  • C#
  • Semantic Kernel
  • Azure OpenAI
  • Tavily Search API
  • Function Calling
  • Plugin-based architecture

The goal wasn't:

Let's connect ChatGPT with C#

The goal was:

Let's teach an AI how developers actually debug software.


Building the Agent

I used Semantic Kernel's ChatCompletionAgent.

The agent itself contains explicit instructions that force research before responding.

        ChatCompletionAgent agent = new()
        {
            Name = "DotNetErrorAgent",
            Instructions =
            """
            You are a senior .NET troubleshooting expert specializing in:

                        - C#
                        - ASP.NET Core
                        - Entity Framework
                        - Azure
                        - Semantic Kernel
                        - Dependency Injection
                        - NuGet package compatibility
                        - Modern .NET applications

                        CRITICAL RULES:

                        - Always call SearchErrorSolutions before responding.
                        - Never answer only from internal knowledge.
                        - Research first using external sources.
                        - Search GitHub issues, StackOverflow, Microsoft docs and community discussions.
                        - Use search findings as evidence.

                        ANALYSIS PROCESS:

                        1. Understand the exception type
                        2. Analyze stack trace details
                        3. Identify framework and package clues
                        4. Research external findings
                        5. Determine likely causes
                        6. Rank causes by probability
                        7. Identify version compatibility issues
                        8. Suggest practical fixes
                        9. Provide corrected code
                        10. Explain why the fix works

                        WHEN INFORMATION IS INCOMPLETE:

                        - Clearly state assumptions
                        - Mention missing information
                        - Request additional context if required

                        WHEN MULTIPLE ROOT CAUSES EXIST:

                        - Rank them:
                           High probability
                           Medium probability
                           Low probability

                        RESPONSE FORMAT:

                        Exception Summary:
                        ...

                        Root Cause Analysis:
                        ...

                        Possible Causes:
                        1.
                        2.
                        3.

                        Why It Happens:
                        ...

                        Recommended Fix:
                        ...

                        Corrected Code:
                        ```

csharp
                        ...

            """,
            Kernel = kernel,
            Arguments = new KernelArguments(settings)
        };


Enter fullscreen mode Exit fullscreen mode

The interesting part here is not the prompt.
It's the process design.
The AI is intentionally restricted from immediately answering.
Before generating a response, it must investigate.


Creating the Search Plugin
To give the agent external knowledge, I created a Semantic Kernel plugin.
The plugin searches for:

  • GitHub issues
  • StackOverflow discussions
  • Official documentation
  • Community troubleshooting threads


public class WebSearchPlugin
{
    private readonly HttpClient _httpClient;
    private readonly string _apiKey;

    public WebSearchPlugin(HttpClient httpClient, IConfiguration configuration)
    {
        _httpClient = httpClient;
        _apiKey = configuration["Tavily:ApiKey"]!;
    }

    [KernelFunction]
    [Description("Searches the web for software errors, fixes, GitHub issues and troubleshooting discussions")]
    public async Task<string> SearchErrorSolutions([Description("Full exception message including stack trace")] string error)
    {
        var body = new
        {
            query =
                    $"""
                        Find solutions for:

                        {error}

                        Include:GitHub issues,StackOverflow,official docs, root causes
                        """,
            max_results = 5,
            include_answer = "advanced",
            search_depth = "advanced"
        };

        var request = new HttpRequestMessage(HttpMethod.Post, "https://api.tavily.com/search");

        request.Content = new StringContent(JsonSerializer.Serialize(body), Encoding.UTF8, "application/json");

        request.Headers.Add("Authorization", $"Bearer {_apiKey}");

        var response = await _httpClient.SendAsync(request);

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



Enter fullscreen mode Exit fullscreen mode

The Agent Response Structure
I wanted outputs to feel like a senior engineer's analysis rather than random generated text.

So the agent follows a structured format:

  • Exception Summary
  • Root Cause Analysis
  • Possible Causes
    • High probability
    • Medium probability
    • Low probability
  • Why It Happens
  • Recommended Fix
  • Corrected Code

Example Flow
Developer input:



Paste your .NET exception:
System.InvalidOperationException: Unable to resolve service for type IUserService while activating UserController


Enter fullscreen mode Exit fullscreen mode

Behind the scenes:

  • Semantic Kernel receives exception
  • AI invokes plugin
  • Tavily searches external sources
  • Results return
  • AI analyzes findings
  • Root causes ranked
  • Fix generated

Output:

Instead of generic guessing, the answer is supported by actual findings

What I Learned
AI agents become valuable when they follow operational workflows rather than simply generating text.

The quality of an AI system depends less on model intelligence and more on process design.

That's the shift I'm starting to see:

Prompt engineering matters.

But workflow engineering matters more.

I'm curious:
How would you improve an AI debugging assistant?

Top comments (0)