https://github.com/tryagi/anthropic
C# SDK based on Anthropic OpenAPI specification
https://github.com/tryagi/anthropic
ai anthropic api autosdk claude claude-api csharp dotnet generated langchain langchain-dotnet llm llms nswag openapi sdk tokenizer
Last synced: 5 months ago
JSON representation
C# SDK based on Anthropic OpenAPI specification
- Host: GitHub
- URL: https://github.com/tryagi/anthropic
- Owner: tryAGI
- License: mit
- Created: 2023-07-14T23:21:11.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2025-05-12T07:32:11.000Z (5 months ago)
- Last Synced: 2025-05-13T01:15:44.780Z (5 months ago)
- Topics: ai, anthropic, api, autosdk, claude, claude-api, csharp, dotnet, generated, langchain, langchain-dotnet, llm, llms, nswag, openapi, sdk, tokenizer
- Language: C#
- Homepage: https://tryagi.github.io/Anthropic/
- Size: 1.2 MB
- Stars: 25
- Watchers: 3
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Anthropic
[](https://www.nuget.org/packages/Anthropic/)
[](https://github.com/tryAGI/Anthropic/actions/workflows/dotnet.yml)
[](https://github.com/tryAGI/Anthropic/blob/main/LICENSE.txt)
[](https://discord.gg/Ca2xhfBf3v)## Features 🔥
- Fully generated C# SDK based on [official OpenAPI specification](https://raw.githubusercontent.com/anthropics/anthropic-sdk-typescript/refs/heads/main/.stats.yml) using [AutoSDK](https://github.com/HavenDV/OpenApiGenerator)
- Automatic releases of new preview versions if there was an update to the OpenAPI specification
- Source generator to define tools natively through C# interfaces
- All modern .NET features - nullability, trimming, NativeAOT, etc.
- Support .Net Framework/.Net Standard 2.0
- Supporting [Microsoft.Extensions.AI](https://devblogs.microsoft.com/dotnet/introducing-microsoft-extensions-ai-preview/)
- Supports all endpoints of the API, including batch requests, count tokens, and prompt caching.## Usage
```csharp
using Anthropic;using var client = new AnthropicClient(apiKey);
var messageParams = new CreateMessageParams()
{
Model = new Model(ModelVariant6.Claude35SonnetLatest),
Messages = [
new InputMessage(InputMessageRole.User, "What's the weather like today?"),
new InputMessage(InputMessageRole.Assistant, "Sure! Could you please provide me with your location?"),
new InputMessage(InputMessageRole.User, "Dubai, UAE")
],
MaxTokens = 250
};
var response = await client.Messages.MessagesPostAsync(messageParams);
```### Tools
```csharp
using CSharpToJsonSchema;public enum Unit
{
Celsius,
Fahrenheit,
}public class Weather
{
public string Location { get; set; } = string.Empty;
public double Temperature { get; set; }
public Unit Unit { get; set; }
public string Description { get; set; } = string.Empty;
}[GenerateJsonSchema]
public interface IWeatherFunctions
{
[Description("Get the current weather in a given location")]
public Weather GetCurrentWeather(
[Description("The city and state, e.g. San Francisco, CA")] string location,
Unit unit = Unit.Celsius);
[Description("Get the current weather in a given location")]
public Task GetCurrentWeatherAsync(
[Description("The city and state, e.g. San Francisco, CA")] string location,
Unit unit = Unit.Celsius,
CancellationToken cancellationToken = default);
}public class WeatherService : IWeatherFunctions
{
public Weather GetCurrentWeather(string location, Unit unit = Unit.Celsius)
{
return new Weather
{
Location = location,
Temperature = 22.0,
Unit = unit,
Description = "Sunny",
};
}
public Task GetCurrentWeatherAsync(string location, Unit unit = Unit.Celsius, CancellationToken cancellationToken = default)
{
return Task.FromResult(new Weather
{
Location = location,
Temperature = 22.0,
Unit = unit,
Description = "Sunny",
});
}
}
```
```csharp
using Anthropic;using var client = new AnthropicClient(apiKey);
var service = new WeatherService();
var tools = service.AsTools();var messageParams = new CreateMessageParams()
{
Model = new Model(ModelVariant6.Claude35SonnetLatest),
Messages = [new InputMessage(InputMessageRole.User, "What is the current temperature in Dubai, UAE in Celsius?")],
MaxTokens = 4096,
System = "You are a helpful weather assistant.",
ToolChoice = new ToolChoice(new ToolChoiceAuto()),
Tools = tools
};
var response = await client.Messages.MessagesPostAsync(messageParams);messageParams.Messages.Add(response.AsInputMessage());
foreach (var toolUse in response.Content.Value2!
.Where(x => x.IsToolUse)
.Select(x => x.ToolUse))
{
var json = await service.CallAsync(
functionName: toolUse!.Name,
argumentsAsJson: toolUse.Input.AsJson());
messageParams.Messages.Add(json.AsToolCall(toolUse));
}response = await client.Messages.MessagesPostAsync(messageParams);
```## Support
Priority place for bugs: https://github.com/tryAGI/Anthropic/issues
Priority place for ideas and general questions: https://github.com/tryAGI/Anthropic/discussions
Discord: https://discord.gg/Ca2xhfBf3v## Acknowledgments

This project is supported by JetBrains through the [Open Source Support Program](https://jb.gg/OpenSourceSupport).

This project is supported by CodeRabbit through the [Open Source Support Program](https://github.com/marketplace/coderabbitai).