1import type * as bedrock from "@distilled.cloud/aws/bedrock-runtime";
2import type * as Effect from "effect/Effect";
3import * as Binding from "../../Binding.ts";
4
5/**
6 * The `CountTokens` request with the binding-injected `modelId` made
7 * optional: it defaults to the first bound model id and may be overridden
8 * per call with any of the bound model ids.
9 */
10export interface CountTokensRequest extends Omit<
11 bedrock.CountTokensRequest,
12 "modelId"
13> {
14 /**
15 * The model whose tokenizer counts the input. Must be one of the model
16 * ids the binding was created with (IAM is scoped to exactly those).
17 * @default the first bound model id
18 */
19 modelId?: string;
20}
21
22/**
23 * Runtime binding for `bedrock-runtime:CountTokens` — count the input tokens
24 * a `Converse` or `InvokeModel` request would consume, using the bound
25 * model's tokenizer, without invoking the model (and without inference
26 * cost).
27 *
28 * The binding grants the function `bedrock:CountTokens` scoped to exactly
29 * the bound models.
30 *
31 * Only a subset of models support token counting, addressed by their BARE
32 * foundation-model id (e.g. `anthropic.claude-haiku-4-5-20251001-v1:0`) —
33 * Amazon Nova models and cross-region inference-profile ids are rejected
34 * with a `ValidationException` ("The provided model doesn't support
35 * counting tokens").
36 *
37 * ### Counting Tokens
38 * **Example:** Count Tokens for a Converse Request
39 * ```typescript
40 * // init
41 * const countTokens = yield* Bedrock.CountTokens(
42 * "anthropic.claude-haiku-4-5-20251001-v1:0",
43 * );
44 *
45 * // runtime
46 * const result = yield* countTokens({
47 * input: {
48 * converse: {
49 * messages: [{ role: "user", content: [{ text: "Say hello." }] }],
50 * },
51 * },
52 * });
53 * const tokens = result.inputTokens;
54 * ```
55 *
56 * **Example:** Count Tokens for a Raw InvokeModel Payload
57 * ```typescript
58 * const result = yield* countTokens({
59 * input: {
60 * invokeModel: {
61 * body: JSON.stringify({
62 * messages: [{ role: "user", content: [{ text: "Say hello." }] }],
63 * }),
64 * },
65 * },
66 * });
67 * ```
68 *
69 * @binding
70 */
71export interface CountTokens extends Binding.Service<
72 CountTokens,
73 "AWS.Bedrock.CountTokens",
74 (
75 model: string,
76 ...additionalModels: string[]
77 ) => Effect.Effect<
78 (
79 request: CountTokensRequest,
80 ) => Effect.Effect<bedrock.CountTokensResponse, bedrock.CountTokensError>
81 >
82> {}
83export const CountTokens = Binding.Service<CountTokens>(
84 "AWS.Bedrock.CountTokens",
85);