Zaiets.ClaudeClient 1.0.0

dotnet add package Zaiets.ClaudeClient --version 1.0.0
                    
NuGet\Install-Package Zaiets.ClaudeClient -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Zaiets.ClaudeClient" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Zaiets.ClaudeClient" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Zaiets.ClaudeClient" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Zaiets.ClaudeClient --version 1.0.0
                    
#r "nuget: Zaiets.ClaudeClient, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Zaiets.ClaudeClient@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Zaiets.ClaudeClient&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Zaiets.ClaudeClient&version=1.0.0
                    
Install as a Cake Tool

Zaiets.ClaudeClient

A lightweight, production-ready Claude API client for .NET 8. Supports chat completions, streaming, tool use, and vision — with full DI integration.

NuGet License: MIT


Installation

dotnet add package Zaiets.ClaudeClient

Quick Start

using Zaiets.ClaudeClient;

var client = new ClaudeClient(new ClaudeClientOptions
{
    ApiKey = "sk-ant-..."   // or set ANTHROPIC_API_KEY env var
});

var response = await client.CompleteAsync("What is the capital of France?");
Console.WriteLine(response.GetText());
// Output: Paris is the capital of France.

Features

Feature API
Single-turn chat client.CompleteAsync(...)
Multi-turn conversation MessageRequestBuilder
Real-time token streaming client.StreamTextAsync(...)
Full streaming events client.StreamEventsAsync(...)
Tool use (function calling) Tool.Create(...) + ToolOrchestrator
Vision (image input) ImageBlock.FromUrl(...) / ImageBlock.FromBytes(...)
ASP.NET Core DI services.AddClaudeClient(...)
Automatic retries Built-in, configurable

Usage Examples

Multi-turn Conversation

var request = new MessageRequestBuilder()
    .WithModel(ClaudeModels.Sonnet4)
    .WithSystem("You are a helpful assistant.")
    .WithMaxTokens(2048)
    .AddUserMessage("Hello! My name is Alice.")
    .AddAssistantMessage("Hello Alice! How can I help you today?")
    .AddUserMessage("What's my name?")
    .Build();

var response = await client.SendAsync(request);
Console.WriteLine(response.GetText());

Streaming

await foreach (var token in client.StreamTextAsync("Tell me a story about a robot."))
{
    Console.Write(token);
}

Vision

var imageBytes = await File.ReadAllBytesAsync("chart.png");

var request = new MessageRequestBuilder()
    .WithModel(ClaudeModels.Sonnet4)
    .AddUserMessage(Message.UserWithContent(
        new ContentBlockBuilder()
            .AddText("What does this chart show?")
            .AddImageBytes(imageBytes, "image/png")
            .Build()))
    .Build();

var response = await client.SendAsync(request);

Tool Use

var weatherTool = Tool.CreateSimple(
    "get_weather",
    "Returns the current weather for a city",
    ("city", "The city name", true),
    ("unit", "celsius or fahrenheit", false));

var orchestrator = new ToolOrchestrator(client);

var request = new MessageRequestBuilder()
    .WithModel(ClaudeModels.Sonnet4)
    .AddUserMessage("What's the weather in London?")
    .AddTool(weatherTool)
    .Build();

var response = await orchestrator.RunAsync(request,
    async (toolName, input, ct) =>
    {
        if (toolName == "get_weather")
        {
            var city = input.GetProperty("city").GetString();
            return $"It is 18°C and cloudy in {city}.";
        }
        throw new NotSupportedException($"Unknown tool: {toolName}");
    });

Console.WriteLine(response.GetText());

ASP.NET Core / Dependency Injection

// Program.cs
builder.Services.AddClaudeClient(opts =>
{
    opts.ApiKey = builder.Configuration["Anthropic:ApiKey"];
    opts.DefaultModel = ClaudeModels.Sonnet4;
    opts.MaxRetries = 3;
});

// In your controller or service
public class ChatService(ClaudeClient claude)
{
    public async Task<string> AskAsync(string question)
    {
        var response = await claude.CompleteAsync(question);
        return response.GetText();
    }
}

Configuration Reference

Property Default Description
ApiKey ANTHROPIC_API_KEY env var Your Anthropic API key
BaseUrl https://api.anthropic.com API base URL (override for proxies)
ApiVersion 2023-06-01 Anthropic API version header
Timeout 60 seconds Timeout for non-streaming requests
StreamingTimeout 10 minutes Timeout for streaming requests
DefaultModel claude-sonnet-4-5 Model used when not specified per request
DefaultMaxTokens 1024 Max tokens when not specified per request
MaxRetries 2 Retries on 429/529 responses
RetryDelay 500ms Initial retry delay (doubles each attempt)

Available Models

ClaudeModels.Opus4      // claude-opus-4-5       — Most capable
ClaudeModels.Sonnet4    // claude-sonnet-4-5     — Balanced (default)
ClaudeModels.Haiku4     // claude-haiku-4-5      — Fastest & cheapest
ClaudeModels.Sonnet35   // claude-3-5-sonnet-20241022
ClaudeModels.Haiku35    // claude-3-5-haiku-20241022

Error Handling

using Zaiets.ClaudeClient.Exceptions;

try
{
    var response = await client.CompleteAsync("Hello");
}
catch (ClaudeAuthenticationException)
{
    // Bad or missing API key
}
catch (ClaudeRateLimitException ex)
{
    // 429 — check ex.RetryAfterSeconds
}
catch (ClaudeOverloadedException)
{
    // 529 — Anthropic is overloaded
}
catch (ClaudeApiException ex)
{
    // Other API errors — ex.StatusCode, ex.ErrorType
}

License

MIT — Copyright (c) 2025 Vladyslav Zaiets
https://sarmkadan.com

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 96 5/3/2026