Universal.Anthropic.Client
3.0.0.1
dotnet add package Universal.Anthropic.Client --version 3.0.0.1
NuGet\Install-Package Universal.Anthropic.Client -Version 3.0.0.1
<PackageReference Include="Universal.Anthropic.Client" Version="3.0.0.1" />
<PackageVersion Include="Universal.Anthropic.Client" Version="3.0.0.1" />
<PackageReference Include="Universal.Anthropic.Client" />
paket add Universal.Anthropic.Client --version 3.0.0.1
#r "nuget: Universal.Anthropic.Client, 3.0.0.1"
#:package Universal.Anthropic.Client@3.0.0.1
#addin nuget:?package=Universal.Anthropic.Client&version=3.0.0.1
#tool nuget:?package=Universal.Anthropic.Client&version=3.0.0.1
Universal.Anthropic.Client
Universal.Anthropic.Client is a C# library for interacting with the Anthropic API. It provides a simple and efficient way to create messages and handle streaming responses from Anthropic's AI models.
Features
- Easy-to-use client for Anthropic API
- Support for creating messages
- Streaming support for real-time responses
- Token counting for messages before creation
- Extended thinking support
- Structured JSON output
- List available models
- Customizable API version
- Built-in error handling and deserialization
Usage
Initializing the Client
var client = new AnthropicClient("YOUR_API_KEY");
Creating a Message
var request = new MessageRequest
{
Model = Models.ClaudeSonnet46,
Messages = new List<Message>
{
new Message(Roles.User, "Hello, how are you?")
},
MaxTokens = 8192
};
var response = await client.CreateMessageAsync(request);
Console.WriteLine(response.Content[0].Text);
Streaming a Message
var streamingRequest = new MessageRequest
{
Model = Models.ClaudeSonnet46,
Messages = new List<Message>
{
new Message(Roles.User, "Tell me a story about a brave knight.")
},
Stream = true,
MaxTokens = 8192
};
var streamingResponse = client.CreateMessageStreamingResponse(streamingRequest);
streamingResponse.Updated += (sender, args) =>
{
Console.WriteLine("Response updated: " + streamingResponse.Value.Content);
};
await streamingResponse.Task; // Wait for completion
Counting Message Tokens
The Token Count API allows you to count the number of tokens in a message, including tools, images, and documents, without actually creating the message. This is useful for estimating costs, validating message size limits, or optimizing your requests:
var countRequest = new CountMessageTokensRequest
{
Model = Models.ClaudeOpus46,
Messages = new List<Message>
{
new Message(Roles.User, "What is the square root of 841, and how did you determine it?")
},
System = "You are an assistant for Red Marble AI. Show your detailed reasoning process when solving problems.",
Thinking = new Thinking
{
Type = ThinkingTypes.Enabled,
BudgetTokens = 2048
}
};
var tokenCountResponse = await client.CountMessageTokensAsync(countRequest);
Console.WriteLine($"Input tokens: {tokenCountResponse.InputTokens}");
if (tokenCountResponse.InputTokens > maxAllowedTokens)
{
Console.WriteLine("Message exceeds token limit, please reduce content.");
}
else
{
var messageRequest = new MessageRequest
{
Model = countRequest.Model,
Messages = countRequest.Messages,
System = countRequest.System,
Thinking = countRequest.Thinking,
MaxTokens = 8192
};
var response = await client.CreateMessageAsync(messageRequest);
}
The CountMessageTokensResponse includes:
InputTokens: The number of input tokens in your request- Additional token information depending on the request structure
This method supports all the same parameters as CreateMessageAsync, including messages with text and image content, system messages, tools and tool schemas, thinking configuration, and all supported models.
Listing Available Models
The library provides a way to retrieve all available models from the Anthropic API:
var modelsResponse = await client.ListModelsAsync();
foreach (var model in modelsResponse.Data)
{
Console.WriteLine($"ID: {model.Id}");
Console.WriteLine($"Name: {model.DisplayName}");
Console.WriteLine($"Created: {model.CreatedAt}");
Console.WriteLine();
}
This functionality allows you to programmatically determine which models are available for use with the API. Models are returned with the most recently released ones listed first.
Using Tools
The Anthropic API supports the use of tools, allowing the AI to perform specific actions or retrieve information. Here's an example of how to use a tool for weather checking:
var weatherTool = new CustomTool
{
Name = "get_weather",
Description = "Get the current weather in a given location",
InputSchema = new JsonSchema
{
Type = "object",
Properties = new Dictionary<string, JsonSchema>()
{
["location"] = new JsonSchema()
{
Type = "string",
Description = "The city and state, e.g. San Francisco, CA"
}
},
Required = new[] { "location" }
}
};
var request = new MessageRequest()
{
Model = Models.ClaudeSonnet46,
Messages = new List<Message>
{
new Message(Roles.User, "What's the weather like in San Francisco?")
},
System = "When asked about weather, always use the get_weather tool.",
MaxTokens = 8192,
Tools = new List<Tool> { weatherTool },
ToolChoice = new AutoToolChoice()
};
var response = await client.CreateMessageAsync(request);
var toolUseBlock = response.Content.FirstOrDefault(c => c is ToolUseContentBlock) as ToolUseContentBlock;
if (toolUseBlock != null)
{
Console.WriteLine($"Tool used: {toolUseBlock.Name}");
Console.WriteLine($"Tool input: {toolUseBlock.Input}");
}
In this example, we define a get_weather tool with an input schema for location. The ToolChoice is set to AutoToolChoice, allowing the model to decide when to use the tool. After receiving the response, you can check if the tool was used by looking for a ToolUseContentBlock in the response content.
Using Extended Thinking
Claude supports an extended thinking feature that reveals the model's detailed reasoning process. This feature helps you understand how Claude arrives at its answers, especially for complex problems:
var request = new MessageRequest
{
Model = Models.ClaudeSonnet46,
Messages = new List<Message>
{
new Message(Roles.User, "What's the square root of 841, and how did you determine it?")
},
MaxTokens = 8192,
Thinking = new Thinking
{
Type = ThinkingTypes.Enabled,
BudgetTokens = 2048
}
};
var response = await client.CreateMessageAsync(request);
foreach (var block in response.Content)
{
if (block is ThinkingContentBlock thinkingBlock)
{
Console.WriteLine("Thinking Process:");
Console.WriteLine(thinkingBlock.Thinking);
Console.WriteLine($"Signature: {thinkingBlock.Signature}");
}
else if (block is TextContentBlock textBlock)
{
Console.WriteLine("Final Answer:");
Console.WriteLine(textBlock.Text);
}
}
Structured JSON Output
You can constrain Claude's response to match a specific JSON schema using OutputConfig. This is useful for extracting structured data, building typed pipelines, or ensuring consistent response formats:
var request = new MessageRequest
{
Model = Models.ClaudeSonnet46,
Messages = new List<Message>
{
new Message(Roles.User, "Give me a person named Alice who is 30 years old.")
},
MaxTokens = 1024,
OutputConfig = new OutputConfig
{
Format = new JsonOutputFormat
{
Schema = new JsonSchema
{
Type = "object",
Properties = new Dictionary<string, JsonSchema>
{
["name"] = new JsonSchema { Type = "string", Description = "The person's name" },
["age"] = new JsonSchema { Type = "integer", Description = "The person's age" }
},
Required = new[] { "name", "age" },
AdditionalProperties = false
}
}
}
};
var response = await client.CreateMessageAsync(request);
var json = (response.Content[0] as TextContentBlock).Text;
var person = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
Console.WriteLine($"Name: {person["name"]}, Age: {person["age"]}");
Note: Object schemas require
AdditionalProperties = false. Array schemas only supportMinItems/MaxItemsvalues of 0 or 1.
You can also use OutputConfig to control the model's effort level independently of structured output:
OutputConfig = new OutputConfig
{
Effort = EffortLevels.Low // "low", "medium", "high", "xhigh" (Opus 4.7/4.8 only), or "max"
}
Key Classes
AnthropicClient: The main client for interacting with the Anthropic API.MessageRequest: Represents a request to create a message.MessageResponse: Represents the response from creating a message.CountMessageTokensRequest: Represents a request to count tokens in a message.CountMessageTokensResponse: Represents the response containing token count information.StreamingMessageResponse: Represents a streaming response for real-time updates.ListResponse<T>: Generic response for list operations, used withModelfor listing available models.
Error Handling
The client throws HttpException for non-successful status codes. Make sure to handle these exceptions in your code.
Customization
You can customize the API version used by setting the AnthropicVersion property on the client:
client.AnthropicVersion = "2023-06-01";
You can also opt into betas by setting the AnthropicBeta property on the client:
client.AnthropicBeta = new[] { "beta-version-1" };
For more detailed information about the Anthropic API, please refer to the official Anthropic documentation.
| 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 was computed. 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 was computed. 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. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. 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.0
- Microsoft.Bcl.AsyncInterfaces (>= 10.0.9)
- Newtonsoft.Json (>= 13.0.4)
- Universal.Common.Json (>= 1.6.0)
- Universal.Common.Net.Http (>= 5.1.1)
- Universal.Common.Serialization (>= 2.4.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Universal.Anthropic.Client:
| Package | Downloads |
|---|---|
|
Universal.GenerativeAI.Anthropic
Implementation of generative AI abstractions using Anthropic as the model provider. |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 3.0.0.1 | 45 | 6/19/2026 |
| 3.0.0 | 98 | 6/2/2026 |
| 2.6.0.1 | 941 | 4/3/2026 |
| 2.6.0 | 150 | 4/3/2026 |
| 2.5.0 | 1,447 | 2/22/2026 |
| 2.4.0.2 | 372 | 10/3/2025 |
| 2.4.0.1 | 277 | 8/29/2025 |
| 2.4.0 | 216 | 7/28/2025 |
| 2.3.2 | 539 | 7/24/2025 |
| 2.3.1 | 553 | 7/24/2025 |
| 2.3.0 | 254 | 6/3/2025 |
| 2.2.0 | 323 | 3/13/2025 |
| 2.1.0 | 254 | 2/27/2025 |
| 2.0.1 | 184 | 2/27/2025 |
| 2.0.0 | 197 | 2/25/2025 |
| 1.0.0 | 236 | 9/18/2024 |
Updated dependencies.