Supports all endpoints of the API, including batch requests, count tokens, and prompt caching.
Usage
1 2 3 4 5 6 7 8 9101112131415
usingAnthropic;usingvarclient=newAnthropicClient(apiKey);varmessageParams=newCreateMessageParams(){Model=newModel(ModelVariant6.Claude35SonnetLatest),Messages=[newInputMessage(InputMessageRole.User,"What's the weather like today?"),newInputMessage(InputMessageRole.Assistant,"Sure! Could you please provide me with your location?"),newInputMessage(InputMessageRole.User,"Dubai, UAE")],MaxTokens=250};varresponse=awaitclient.Messages.MessagesPostAsync(messageParams);
usingCSharpToJsonSchema;publicenumUnit{Celsius,Fahrenheit,}publicclassWeather{publicstringLocation{get;set;}=string.Empty;publicdoubleTemperature{get;set;}publicUnitUnit{get;set;}publicstringDescription{get;set;}=string.Empty;}[GenerateJsonSchema]publicinterfaceIWeatherFunctions{[Description("Get the current weather in a given location")]publicWeatherGetCurrentWeather([Description("The city and state, e.g. San Francisco, CA")]stringlocation,Unitunit=Unit.Celsius);[Description("Get the current weather in a given location")]publicTask<Weather>GetCurrentWeatherAsync([Description("The city and state, e.g. San Francisco, CA")]stringlocation,Unitunit=Unit.Celsius,CancellationTokencancellationToken=default);}publicclassWeatherService:IWeatherFunctions{publicWeatherGetCurrentWeather(stringlocation,Unitunit=Unit.Celsius){returnnewWeather{Location=location,Temperature=22.0,Unit=unit,Description="Sunny",};}publicTask<Weather>GetCurrentWeatherAsync(stringlocation,Unitunit=Unit.Celsius,CancellationTokencancellationToken=default){returnTask.FromResult(newWeather{Location=location,Temperature=22.0,Unit=unit,Description="Sunny",});}}
usingAnthropic;usingvarclient=newAnthropicClient(apiKey);varservice=newWeatherService();vartools=service.AsTools();varmessageParams=newCreateMessageParams(){Model=newModel(ModelVariant6.Claude35SonnetLatest),Messages=[newInputMessage(InputMessageRole.User,"What is the current temperature in Dubai, UAE in Celsius?")],MaxTokens=4096,System="You are a helpful weather assistant.",ToolChoice=newToolChoice(newToolChoiceAuto()),Tools=tools};varresponse=awaitclient.Messages.MessagesPostAsync(messageParams);messageParams.Messages.Add(response.AsInputMessage());foreach(vartoolUseinresponse.Content.Value2!.Where(x=>x.IsToolUse).Select(x=>x.ToolUse)){varjson=awaitservice.CallAsync(functionName:toolUse!.Name,argumentsAsJson:toolUse.Input.AsJson());messageParams.Messages.Add(json.AsToolCall(toolUse));}response=awaitclient.Messages.MessagesPostAsync(messageParams);
Microsoft.Extensions.AI
The SDK implements IChatClient for seamless integration with the .NET AI ecosystem:
usingvarclient=GetAuthenticatedChatClient();varresponse=awaitclient.GetResponseAsync(messages:[newChatMessage(ChatRole.User,"Generate 5 random words.")],newChatOptions{ModelId=DefaultModel,ResponseFormat=ChatResponseFormatForType<StringArraySchema>(),Tools=newList<AITool>(),});Console.WriteLine(response.ToString());
Chat Client Five Random Words Streaming
1 2 3 4 5 6 7 8 910111213141516
usingvarclient=GetAuthenticatedChatClient();varenumerable=client.GetStreamingResponseAsync(messages:[newChatMessage(ChatRole.User,"Generate 5 random words.")],newChatOptions{ModelId=DefaultModel,});vardeltas=newList<string>();awaitforeach(varresponseinenumerable){Console.Write(response.ToString());deltas.Add(response.ToString());}
Chat Client Five Random Words
1 2 3 4 5 6 7 8 910
usingvarclient=GetAuthenticatedChatClient();varresponse=awaitclient.GetResponseAsync(messages:[newChatMessage(ChatRole.User,"Generate 5 random words.")],newChatOptions{ModelId=DefaultModel,});Console.WriteLine(response.ToString());
Complete History
1 2 3 4 5 6 7 8 910111213
usingvarclient=newAnthropicClient(apiKey);varresponse=awaitclient.MessagesPostAsync(model:DefaultModel,messages:["What's the weather like today?","Sure! Could you please provide me with your location?".AsAssistantMessage(),"Dubai, UAE",],maxTokens:300,temperature:0);Console.WriteLine(response.AsSimpleText());
Five Random Words
123456789
usingvarclient=newAnthropicClient(apiKey);varresponse=awaitclient.MessagesPostAsync(model:DefaultModel,messages:["Generate 5 random words."],maxTokens:300,temperature:0);Console.WriteLine(response.AsSimpleText());
Streaming
1 2 3 4 5 6 7 8 910111213141516
usingvarclient=newAnthropicClient(apiKey);varenumerable=client.CreateMessageAsStreamAsync(newCreateMessageParams{Model=DefaultModel,Messages=["Once upon a time"],MaxTokens=250,});vardeltas=newList<string>();awaitforeach(varresponseinenumerable){Console.Write(response.ContentBlockDelta?.Delta.TextDelta?.Text);deltas.Add(response.ContentBlockDelta?.Delta.TextDelta?.Text??string.Empty);}