Full (text, streaming, tool calling, images, PDFs, thinking)
IChatClient
Installation
1
dotnetaddpackagetryAGI.Anthropic
Basic Usage
1 2 3 4 5 6 7 8 91011121314
usingAnthropic;usingMicrosoft.Extensions.AI;usingvarclient=newAnthropicClient(apiKey);IChatClientchatClient=client;varresponse=awaitchatClient.GetResponseAsync("What is the capital of France?",newChatOptions{ModelId="claude-sonnet-4-20250514",});Console.WriteLine(response.Text);
Streaming
1 2 3 4 5 6 7 8 91011
IChatClientchatClient=client;awaitforeach(varupdateinchatClient.GetStreamingResponseAsync("Write a short poem about coding.",newChatOptions{ModelId="claude-sonnet-4-20250514",})){Console.Write(update.Text);}
Tool Calling
Define tools using CSharpToJsonSchema and pass them via ChatOptions:
1 2 3 4 5 6 7 8 91011121314151617181920
usingCSharpToJsonSchema;[GenerateJsonSchema]publicinterfaceIWeatherTools{[Description("Gets the current weather for a location.")]stringGetWeather([Description("The city name")]stringcity);}IChatClientchatClient=client;varservice=newWeatherToolsService();varresponse=awaitchatClient.GetResponseAsync("What's the weather in Paris?",newChatOptions{ModelId="claude-sonnet-4-20250514",Tools=service.AsTools().AsAITools(),});
Image Input
1 2 3 4 5 6 7 8 91011121314
IChatClientchatClient=client;varresponse=awaitchatClient.GetResponseAsync([ new ChatMessage(ChatRole.User, [ new ImageContent(imageBytes, "image/png"), new TextContent("Describe this image."), ]),],newChatOptions{ModelId="claude-sonnet-4-20250514",});
Extended Thinking
Enable extended thinking through AdditionalProperties:
1 2 3 4 5 6 7 8 9101112
IChatClientchatClient=client;varresponse=awaitchatClient.GetResponseAsync("Solve this step by step: What is 147 * 283?",newChatOptions{ModelId="claude-sonnet-4-20250514",AdditionalProperties=newAdditionalPropertiesDictionary{["thinking"]=true,},});
Token Usage
Token usage information is available on the response: