Skip to content

Latest commit

 

History

History

MyCodingAgent.NET

Folders and files

README.md

How Does a Coding Agent Work?

This repository contains demo code for the presentation "How Does a Coding Agent Work?" - a hands-on exploration of the core mechanics behind AI coding agents.

The code is presented together with the Coding Agent Explorer, my open-source .NET tool that shows what a coding agent such as Claude Code sends to the model: every API request, tool call and token count, live in a dashboard. Read more in the blog post Introducing the Coding Agent Explorer (.NET).

About the Presentation

Coding agents are increasingly used to automate software tasks, yet how they actually work is often treated as a black box. This talk breaks a coding agent down into its core mechanics: how it is structured, how it communicates with a language model, and how a text-only model can read and modify real source code.

We cover:

  • What a coding agent is, mechanically
  • How agents communicate with language models
  • How tools and MCP calls are executed
  • How a text-only model works with source code
  • How to build a basic coding agent in C#

Repository Structure

This repository contains two implementations of the same coding agent:

🎨 MyCodingAgent (Full Version)

The feature-rich version with Spectre.Console UI, showing:

  • Formatted request/response tables
  • Visual tool call tracking
  • Rich console output for debugging

⚡ MyCodingAgent-Minimalistic

The bare-bones version stripped down to under 200 lines of code, demonstrating:

  • How compact a functional coding agent can be
  • Core agentic loop pattern
  • Essential tool execution without visual flourishes

Both versions implement the exact same functionality:

  • Read, write, and list files in a sandboxed folder
  • Maintain conversation context with Claude
  • Execute tool calls in a loop until completion
  • Path security to prevent escaping the working directory

Quick Start

Prerequisites

  • .NET 10.0 SDK
  • Anthropic API key

Setup

  1. Clone the repository

    git clone https://github.com/tndataab/PublicBlogContent.git
    cd PublicBlogContent/MyCodingAgent.NET
  2. Configure API Key

    The agent reads the key from the ANTHROPIC_API_KEY environment variable.

    $env:ANTHROPIC_API_KEY = "your-api-key-here"   # PowerShell
    export ANTHROPIC_API_KEY="your-api-key-here"   # bash
  3. Run (from the same terminal)

    cd MyCodingAgent  # or MyCodingAgent-Minimalistic
    dotnet run

See the Requests in Coding Agent Explorer

To see every request the agent sends, run it through the Coding Agent Explorer. Start the Explorer, then point the agent at its proxy before dotnet run:

$env:ANTHROPIC_BASE_URL = "http://localhost:8888"   # PowerShell
export ANTHROPIC_BASE_URL="http://localhost:8888"   # bash

The Anthropic SDK picks up ANTHROPIC_BASE_URL automatically, so no code changes are needed. Open the dashboard at http://localhost:5000 to follow the requests live.

Try It Out

Example prompts to test the agent:

Create a simple fizz buzz application in C#, with program.cs and project file. Do use .NET 10.

Create a simple calculator class with add, subtract, multiply, and divide methods.

Read the fizzbuzz program and add comments explaining how it works.

How It Works

The Agentic Loop

At its core, a coding agent is just a simple loop:

while (true)
{
    var input = GetUserInput();
    var response = await agent.SendMessage(input);

    // LLM chains tools together (read file -> edit file -> test code)
    while (agent.WantsToUseTool(response))
        response = await agent.ProcessToolCalls(response);
}

The Three Tools

The agent exposes three simple tools to Claude:

  1. read_file - Read contents of a file
  2. write_file - Write content to a file (creates directories as needed)
  3. list_files - List all files and directories in a path

All file operations are sandboxed to a working folder: c:\MyAgent for the full version and c:\MyAgentMini for the minimalistic one. Change workFolder at the top of Program.cs to use another folder.

Message Flow

  1. User provides a prompt
  2. Agent sends prompt + conversation context + tool definitions to Claude
  3. Claude responds with text and/or tool calls
  4. Agent executes tools and sends results back
  5. Loop continues until Claude has no more tools to call
  6. User sees the final response

Path Security

The ResolvePath() method ensures all file paths stay within the working folder:

string? ResolvePath(string relative)
{
    var full = Path.GetFullPath(Path.Combine(workFolder, relative));
    return full.StartsWith(workFolder, StringComparison.OrdinalIgnoreCase) ? full : null;
}

Key Concepts Demonstrated

1. Tool Use Pattern

Claude's API supports function calling through the "tool use" pattern. The agent:

  • Defines tools with JSON schemas
  • Claude responds with ToolUseBlock objects
  • Agent executes tools and returns results as ToolResultBlockParam

2. Context Management

The agent maintains full conversation history in a List<MessageParam>, including:

  • User messages
  • Assistant responses (text + tool calls)
  • Tool results (sent as user messages)

3. Stateless API with Stateful Client

Claude's API is stateless - the agent must send the full conversation context on every request. This allows Claude to "remember" previous interactions.

4. Sandboxing

The agent restricts all file operations to a designated working folder, preventing Claude from accessing or modifying files outside the sandbox.

Code Comparison

Feature MyCodingAgent MyCodingAgent-Minimalistic
Lines of Code ~300 Under 200
Dependencies Anthropic SDK, Spectre.Console Anthropic SDK only
Output Rich formatted tables Plain console output
Debugging Request/response tracking Minimal output
Use Case Development & demos Understanding core concepts

Technical Details

  • Framework: .NET 10.0
  • Language Model: Claude Sonnet 4.5
  • API Client: Anthropic C# SDK
  • Max Tokens: 4096 (to limit costs and prevent runaway token usage)
  • Tool Execution: Synchronous, sequential

Further Resources

License

This demo code is provided for educational purposes as part of the presentation "How Does a Coding Agent Work?".


For more about my talks and training, visit nestenius.se.