Anthropic.Client
1.0.0
dotnet add package Anthropic.Client --version 1.0.0
NuGet\Install-Package Anthropic.Client -Version 1.0.0
<PackageReference Include="Anthropic.Client" Version="1.0.0" />
<PackageVersion Include="Anthropic.Client" Version="1.0.0" />
<PackageReference Include="Anthropic.Client" />
paket add Anthropic.Client --version 1.0.0
#r "nuget: Anthropic.Client, 1.0.0"
#:package Anthropic.Client@1.0.0
#addin nuget:?package=Anthropic.Client&version=1.0.0
#tool nuget:?package=Anthropic.Client&version=1.0.0
Anthropic.Client
A strongly-typed, feature-rich .NET client library for Anthropic's Claude API, providing easy integration with Claude's powerful language models.
✨ Features
- 🎯 Strongly-typed models with comprehensive IntelliSense support
- 🔄 Async/await pattern with full cancellation token support
- 🛠️ Fluent builder pattern for easy request construction
- 🔁 Automatic retry logic with exponential backoff and jitter
- 📊 Streaming responses for real-time chat experiences
- 🧰 Tool support for function calling capabilities
- 💾 Prompt caching for improved performance
- 📦 Message batching for high-volume processing
- 🔍 Comprehensive logging with Microsoft.Extensions.Logging
- 🏗️ Extension methods for simplified usage
- ⚡ High performance with System.Text.Json serialization
- 🔧 Configurable options with validation
- 🌐 Multi-framework support (.NET Standard 2.1, .NET 7, .NET 8, .NET 9)
📦 Installation
Install the package via NuGet Package Manager:
dotnet add package Anthropic.Client
Or via Package Manager Console:
Install-Package Anthropic.Client
🚀 Quick Start
Basic Setup
using Anthropic.Client;
using Anthropic.Client.Models;
// Configure the client
var options = new AnthropicClientOptions
{
ApiKey = "your-api-key-here", // or set ANTHROPIC_API_KEY environment variable
EnableLogging = true,
MaxRetries = 3,
Timeout = TimeSpan.FromSeconds(100)
};
// Create the client
using var client = new AnthropicClient(options);
Simple Message Request
var request = new MessagesRequest
{
Model = AnthropicModels.Claude35Sonnet,
MaxTokens = 1000,
Messages = new List<Message>
{
new Message
{
Role = "user",
Content = "Hello, Claude!"
}
}
};
var response = await client.CreateMessagesAsync(request);
Console.WriteLine(response.Content[0].Text);
📚 Available Models
The library includes constants for all available Claude models:
// Claude 3.5 models (recommended)
AnthropicModels.Claude35Sonnet // "claude-3-5-sonnet-20241022"
AnthropicModels.Claude35Haiku // "claude-3-5-haiku-20241022"
// Claude 3 models
AnthropicModels.Claude3Opus // "claude-3-opus-20240229"
AnthropicModels.Claude3Sonnet // "claude-3-sonnet-20240229"
AnthropicModels.Claude3Haiku // "claude-3-haiku-20240307"
🏗️ Builder Pattern
Use the fluent builder pattern for clean, readable request construction:
using Anthropic.Client.Builders;
var request = MessagesRequestBuilder.Create()
.WithModel(AnthropicModels.Claude35Sonnet)
.WithMaxTokens(1000)
.WithTemperature(0.7)
.WithSystem("You are a helpful assistant.")
.AddUserMessage("Explain quantum computing")
.AddAssistantMessage("Quantum computing is...")
.AddUserMessage("Can you elaborate on quantum entanglement?")
.Build();
var response = await client.CreateMessagesAsync(request);
🔧 Extension Methods
Simplified usage with extension methods:
using Anthropic.Client.Extensions;
// Quick single message
var response = await client.SendMessageAsync(
"What is the capital of France?",
AnthropicModels.Claude35Sonnet
);
// With system prompt
var response = await client.SendMessageAsync(
"Write a haiku about programming",
AnthropicModels.Claude35Sonnet,
systemPrompt: "You are a creative poet"
);
🔄 Streaming Responses
For real-time chat experiences:
var request = new MessagesRequest
{
Model = AnthropicModels.Claude35Sonnet,
MaxTokens = 1000,
Messages = new List<Message>
{
new Message
{
Role = "user",
Content = "Tell me a story"
}
}
};
await foreach (var chunk in client.CreateStreamingMessagesAsync(request))
{
if (chunk.Type == "content_block_delta")
{
Console.Write(chunk.Delta?.Text);
}
}
🧰 Tool Support
Enable function calling capabilities:
var tools = new List<Tool>
{
new Tool
{
Name = "get_weather",
Description = "Get current weather for a location",
InputSchema = new ToolInputSchema
{
Type = "object",
Properties = new Dictionary<string, object>
{
["location"] = new { type = "string", description = "City name" }
},
Required = new[] {"location"}
}
}
};
var request = new MessagesRequest
{
Model = AnthropicModels.Claude35Sonnet,
MaxTokens = 1000,
Messages = new List<Message>
{
new Message
{
Role = "user",
Content = "What's the weather in New York?"
}
}
};
var response = await client.CreateMessagesWithToolsAsync(request, tools);
💾 Prompt Caching
Improve performance with prompt caching:
var request = new MessagesRequest
{
Model = AnthropicModels.Claude35Sonnet,
MaxTokens = 1000,
Messages = new List<Message>
{
new Message
{
Role = "user",
Content = "Large context that should be cached...",
CacheControl = new CacheControl
{
Type = "ephemeral"
}
}
}
};
var response = await client.CreateMessagesWithCachingAsync(request);
📦 Message Batching
Process multiple requests efficiently:
var requests = new List<MessagesRequest>
{
new MessagesRequest
{
Model = AnthropicModels.Claude35Sonnet,
MaxTokens = 100,
Messages = new List<Message>
{
new Message
{
Role = "user",
Content = "Translate 'Hello' to French"
}
}
},
new MessagesRequest
{
Model = AnthropicModels.Claude35Sonnet,
MaxTokens = 100,
Messages = new List<Message>
{
new Message
{
Role = "user",
Content = "Translate 'Hello' to Spanish"
}
}
}
};
var batch = await client.CreateMessageBatchAsync(requests);
Console.WriteLine($"Batch ID: {batch.Id}");
// Check batch status
var status = await client.GetMessageBatchAsync(batch.Id);
Console.WriteLine($"Status: {status.ProcessingStatus}");
⚙️ Configuration Options
var options = new AnthropicClientOptions
{
ApiKey = "your-api-key", // Required
BaseUrl = "https://api.anthropic.com", // Default API endpoint
Timeout = TimeSpan.FromSeconds(100), // Request timeout
MaxRetries = 3, // Retry attempts
EnableLogging = true, // Enable request/response logging
EnableTelemetry = true, // Enable telemetry collection
UserAgent = "MyApp/1.0" // Custom user agent
};
// Validate configuration
options.Validate();
📊 Logging Integration
The client integrates with Microsoft.Extensions.Logging:
using Microsoft.Extensions.Logging;
// Setup logging
using var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole().SetMinimumLevel(LogLevel.Information));
var logger = loggerFactory.CreateLogger<AnthropicClient>();
// Pass logger to client
using var client = new AnthropicClient(options, logger: logger);
🔐 Authentication
Set your API key using one of these methods:
Environment Variable (Recommended):
export ANTHROPIC_API_KEY="your-api-key-here"
Configuration Options:
var options = new AnthropicClientOptions { ApiKey = "your-api-key-here" };
appsettings.json (for ASP.NET Core):
{
"Anthropic":
{
"ApiKey": "your-api-key-here"
}
}
🔄 Dependency Injection
Register the client in your DI container:
// Program.cs (ASP.NET Core)
builder.Services.Configure<AnthropicClientOptions>(builder.Configuration.GetSection("Anthropic"));
builder.Services.AddHttpClient<AnthropicClient>();
builder.Services.AddScoped<AnthropicClient>();
🛡️ Error Handling
The client provides comprehensive error handling:
try
{
var response = await client.CreateMessagesAsync(request);
}
catch (AnthropicApiException ex)
{
Console.WriteLine($"API Error: {ex.Message}");
Console.WriteLine($"Status Code: {ex.StatusCode}");
Console.WriteLine($"Error Type: {ex.ErrorType}");
}
catch (HttpRequestException ex)
{
Console.WriteLine($"Network Error: {ex.Message}");
}
📈 Performance Tips
- Reuse the client instance - it's thread-safe and designed for reuse
- Use streaming for long responses to improve perceived performance
- Enable prompt caching for repeated large contexts
- Use message batching for high-volume scenarios
- Configure appropriate timeouts based on your use case
🧪 Testing
The library includes comprehensive test coverage and supports mocking:
// Example test setup
var mockHttpClient = new Mock<HttpClient>();
var client = new AnthropicClient(options, mockHttpClient.Object);
🔧 Advanced Usage
Custom HTTP Client
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("Custom-Header", "Value");
using var client = new AnthropicClient(options, httpClient);
Retry Configuration
var options = new AnthropicClientOptions
{
ApiKey = "your-api-key",
MaxRetries = 5, // Custom retry count
Timeout = TimeSpan.FromMinutes(2) // Extended timeout
};
📋 Requirements
- .NET Standard 2.1 or later
- .NET 7 or later
- .NET 8 or later
- .NET 9 or later
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
🔗 Links
📞 Support
For issues and questions:
- Create an issue on GitHub
- Check the Anthropic API documentation
Made with ❤️ by Anubhav Ranjan
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. 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 is compatible. 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. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- Microsoft.Extensions.Logging.Abstractions (>= 9.0.7)
- Polly (>= 8.6.2)
- System.Text.Json (>= 9.0.7)
-
net7.0
- Microsoft.Extensions.Logging.Abstractions (>= 9.0.7)
- Polly (>= 8.6.2)
- System.Text.Json (>= 9.0.7)
-
net8.0
- Microsoft.Extensions.Logging.Abstractions (>= 9.0.7)
- Polly (>= 8.6.2)
- System.Text.Json (>= 9.0.7)
-
net9.0
- Microsoft.Extensions.Logging.Abstractions (>= 9.0.7)
- Polly (>= 8.6.2)
- System.Text.Json (>= 9.0.7)
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 | 73 | 7/18/2025 |