Skip to content

Commit

Permalink
docs(readme): mention new token counting endpoint (#728)
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertCraigie authored Nov 4, 2024
1 parent 14e4244 commit 72a4636
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(...)
Expand Down

0 comments on commit 72a4636

Please sign in to comment.