Streaming

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
using var client = new AnthropicClient(apiKey);

var enumerable = client.CreateMessageAsStreamAsync(new CreateMessageRequest
{
    Model = CreateMessageRequestModel.Claude35Sonnet20240620,
    Messages = ["Once upon a time"],
    MaxTokens = 250,
});

var deltas = new List<string>();
await foreach (var response in enumerable)
{
    Console.Write(response.ContentBlockDelta?.Delta.TextDelta?.Text);

    deltas.Add(response.ContentBlockDelta?.Delta.TextDelta?.Text ?? string.Empty);
}