From 72a4636a7798170d69e7551ba58a0213d82d1711 Mon Sep 17 00:00:00 2001 From: Robert Craigie Date: Mon, 4 Nov 2024 17:52:27 +0000 Subject: [PATCH] docs(readme): mention new token counting endpoint (#728) --- README.md | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 50b96194..b8e0aa53 100644 --- a/README.md +++ b/README.md @@ -165,7 +165,19 @@ Alternatively, you can use `client.messages.create(..., stream=True)` which only ## Token counting -You can see the exact usage for a given request through the `usage` response property, e.g. +To get the token count for a message without creating it you can use the `client.beta.messages.count_tokens()` method. This takes the same `messages` list as the `.create()` method. + +```py +count = client.beta.messages.count_tokens( + model="claude-3-5-sonnet-20241022", + messages=[ + {"role": "user", "content": "Hello, world"} + ] +) +count.input_tokens # 10 +``` + +You can also see the exact usage for a given request through the `usage` response property, e.g. ```py message = client.messages.create(...)