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