Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 72a4636

Browse files
authoredNov 4, 2024
docs(readme): mention new token counting endpoint (#728)
1 parent 14e4244 commit 72a4636

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed
 

‎README.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,19 @@ Alternatively, you can use `client.messages.create(..., stream=True)` which only
165165

166166
## Token counting
167167

168-
You can see the exact usage for a given request through the `usage` response property, e.g.
168+
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.
169+
170+
```py
171+
count = client.beta.messages.count_tokens(
172+
model="claude-3-5-sonnet-20241022",
173+
messages=[
174+
{"role": "user", "content": "Hello, world"}
175+
]
176+
)
177+
count.input_tokens # 10
178+
```
179+
180+
You can also see the exact usage for a given request through the `usage` response property, e.g.
169181

170182
```py
171183
message = client.messages.create(...)

0 commit comments

Comments
 (0)
Please sign in to comment.