From de326abe2575c2fd9c7e63aab0083fbb0b12cfa1 Mon Sep 17 00:00:00 2001 From: Stevan Freeborn <65925598+StevanFreeborn@users.noreply.github.com> Date: Thu, 2 Jan 2025 20:52:52 -0600 Subject: [PATCH] docs: add count message tokens example to README.md --- README.md | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e5a3580..a05c5fe 100644 --- a/README.md +++ b/README.md @@ -105,9 +105,38 @@ The primary use case for working with the Anthropic API is to create a message i > [!NOTE] > The following examples assume that you have already created an instance of the `AnthropicApiClient` class named `client`. You can also find these snippets in the examples directory. +### Count Message Tokens + +The `AnthropicApiClient` exposes a method named `CountMessageTokensAsync` that can be used to count the number of tokens in a message. The method requires a `CountMessageTokensRequest` instance as a parameter. + +```csharp +using AnthropicClient; +using AnthropicClient.Models; + +var response = await client.CountMessageTokensAsync(new CountMessageTokensRequest( + AnthropicModels.Claude3Haiku, + [ + new( + MessageRole.User, + [new TextContent("Please write a haiku about the ocean.")] + ) + ] +)); + +if (response.IsFailure) +{ + Console.WriteLine("Failed to count message tokens"); + Console.WriteLine("Error Type: {0}", response.Error.Error.Type); + Console.WriteLine("Error Message: {0}", response.Error.Error.Message); + return; +} + +Console.WriteLine("Token Count: {0}", response.Value.InputTokens); +``` + ### Create a message -The `AnthropicApiClient` exposes a single method named `CreateMessageAsync` that can be used to create a message. The method requires a `MessageRequest` or a `StreamMessageRequest` instance as a parameter. The `MessageRequest` class is used to create a message whose response is not streamed and the `StreamMessageRequest` class is used to create a message whose response is streamed. The `MessageRequest` instance's properties can be set to configure how the message is created. +The `AnthropicApiClient` exposes a method named `CreateMessageAsync` that can be used to create a message. The method requires a `MessageRequest` or a `StreamMessageRequest` instance as a parameter. The `MessageRequest` class is used to create a message whose response is not streamed and the `StreamMessageRequest` class is used to create a message whose response is streamed. The `MessageRequest` instance's properties can be set to configure how the message is created. #### Non-Streaming