CountTokens
Source:
src/AWS/Bedrock/CountTokens.ts
Runtime binding for bedrock-runtime:CountTokens — count the input tokens
a Converse or InvokeModel request would consume, using the bound
model’s tokenizer, without invoking the model (and without inference
cost).
The binding grants the function bedrock:CountTokens scoped to exactly
the bound models.
Only a subset of models support token counting, addressed by their BARE
foundation-model id (e.g. anthropic.claude-haiku-4-5-20251001-v1:0) —
Amazon Nova models and cross-region inference-profile ids are rejected
with a ValidationException (“The provided model doesn’t support
counting tokens”).
Counting Tokens
Section titled “Counting Tokens”Count Tokens for a Converse Request
// initconst countTokens = yield* Bedrock.CountTokens( "anthropic.claude-haiku-4-5-20251001-v1:0",);
// runtimeconst result = yield* countTokens({ input: { converse: { messages: [{ role: "user", content: [{ text: "Say hello." }] }], }, },});const tokens = result.inputTokens;Count Tokens for a Raw InvokeModel Payload
const result = yield* countTokens({ input: { invokeModel: { body: JSON.stringify({ messages: [{ role: "user", content: [{ text: "Say hello." }] }], }), }, },});