mirror of
https://github.com/anthropics/skills.git
synced 2026-08-02 13:05:28 +08:00
b29e7cf65e
* Update claude-api skill: Claude Opus 5 - Add Claude Opus 5 (`claude-opus-5`) as the default model across all SDK examples, model tables, pricing, and migration guidance. - Add an Opus 5 migration section covering the API changes that come with it, and move Opus 4.8 into the previous-generation slot. - Drop the Opus 4.7 fast-mode guidance, which no longer applies. - Scope the server-side fallbacks beta header to the current version. * Update claude-api skill: Opus 5 fast-follow corrections - Server-side refusal fallbacks are available on Claude Platform on AWS, not just the Claude API. Correct the availability statements in SKILL.md, the migration guide, and the platform-availability table. - Simplify the effort guidance: start at `high` (the API default) and sweep down, rather than starting at `xhigh` for coding work. - Add a time-to-first-token section with the prompt instruction that reduces pre-answer thinking on latency-sensitive routes. - Replace the two separate thinking-disabled mitigations with the single combined instruction that covers both failure modes.
57 lines
1.6 KiB
Markdown
57 lines
1.6 KiB
Markdown
# Token Counting
|
||
|
||
Use the `count_tokens` endpoint (`POST /v1/messages/count_tokens`) for accurate
|
||
token counts against Claude models. Token counts are **model-specific** — pass
|
||
the same model ID you'll use for inference.
|
||
|
||
**Do not use `tiktoken`.** It's OpenAI's tokenizer. It undercounts Claude
|
||
tokens by ~15–20% on typical text, and by much more on code or non-English
|
||
input. Any estimate from `tiktoken`, `gpt-tokenizer`, or similar is wrong for
|
||
Claude.
|
||
|
||
## Count a file or string
|
||
|
||
```python
|
||
from anthropic import Anthropic
|
||
|
||
client = Anthropic()
|
||
resp = client.messages.count_tokens(
|
||
model="claude-opus-5",
|
||
messages=[{"role": "user", "content": open("CLAUDE.md").read()}],
|
||
)
|
||
print(resp.input_tokens)
|
||
```
|
||
|
||
TypeScript: `await client.messages.countTokens({model, messages})` →
|
||
`.input_tokens`. See `{lang}/claude-api/README.md` for other SDKs.
|
||
|
||
## CLI
|
||
|
||
```sh
|
||
ant messages count-tokens --model claude-opus-5 \
|
||
--message '{role: user, content: "@./CLAUDE.md"}' \
|
||
--transform input_tokens -r
|
||
```
|
||
|
||
## Diffing a file across two versions
|
||
|
||
The endpoint is stateless — count each version separately and subtract:
|
||
|
||
```python
|
||
from anthropic import Anthropic
|
||
import subprocess
|
||
|
||
client = Anthropic()
|
||
def count(text: str) -> int:
|
||
return client.messages.count_tokens(
|
||
model="claude-opus-5",
|
||
messages=[{"role": "user", "content": text}],
|
||
).input_tokens
|
||
|
||
before = subprocess.check_output(["git", "show", "HEAD:CLAUDE.md"], text=True)
|
||
after = open("CLAUDE.md").read()
|
||
print(count(after) - count(before))
|
||
```
|
||
|
||
Full docs: see the Token Counting entry in `shared/live-sources.md`.
|