web-count-token 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,81 @@
1
+ /**
2
+ * count-web-token
3
+ *
4
+ * Browser-compatible token counter for LLM content blocks.
5
+ * Uses js-tiktoken (pure JavaScript) instead of tiktoken (WASM) for browser compatibility.
6
+ *
7
+ * This package is standalone and does not depend on @anthropic-ai/sdk.
8
+ * The content block types are defined inline and are duck-type compatible
9
+ * with Anthropic SDK's ContentBlockParam types.
10
+ */
11
+ export interface TextBlock {
12
+ type: "text";
13
+ text: string;
14
+ }
15
+ export interface ImageSource {
16
+ type: "base64";
17
+ media_type: string;
18
+ data: string;
19
+ }
20
+ export interface ImageBlock {
21
+ type: "image";
22
+ source: ImageSource;
23
+ }
24
+ export interface ToolUseBlock {
25
+ type: "tool_use";
26
+ id: string;
27
+ name: string;
28
+ input: unknown;
29
+ }
30
+ export interface ToolResultContentBlock {
31
+ type: "text" | "image";
32
+ text?: string;
33
+ [key: string]: unknown;
34
+ }
35
+ export interface ToolResultBlock {
36
+ type: "tool_result";
37
+ tool_use_id: string;
38
+ content?: string | ToolResultContentBlock[];
39
+ is_error?: boolean;
40
+ }
41
+ export type ContentBlock = TextBlock | ImageBlock | ToolUseBlock | ToolResultBlock;
42
+ export interface CountTokensOptions {
43
+ /**
44
+ * Fudge factor to multiply the raw token count by.
45
+ * Accounts for tiktoken not always being accurate.
46
+ * @default 1.5
47
+ */
48
+ fudgeFactor?: number;
49
+ }
50
+ /**
51
+ * Count tokens in an array of content blocks.
52
+ *
53
+ * Supports text, image, tool_use, and tool_result blocks.
54
+ * Uses the o200k_base tokenizer model (same as GPT-4o / Claude).
55
+ *
56
+ * @param content - Array of content blocks to count tokens for
57
+ * @param options - Optional configuration
58
+ * @returns Promise resolving to the estimated token count
59
+ *
60
+ * @example
61
+ * ```ts
62
+ * import { countTokens } from "count-web-token"
63
+ *
64
+ * const tokens = await countTokens([
65
+ * { type: "text", text: "Hello, world!" }
66
+ * ])
67
+ * console.log(tokens) // e.g. 6
68
+ * ```
69
+ */
70
+ export declare function countTokens(content: ContentBlock[], options?: CountTokensOptions): Promise<number>;
71
+ /**
72
+ * Count tokens in a plain text string.
73
+ *
74
+ * Convenience wrapper that creates a text content block and counts tokens.
75
+ *
76
+ * @param text - The text string to count tokens for
77
+ * @param options - Optional configuration
78
+ * @returns Promise resolving to the estimated token count
79
+ */
80
+ export declare function countTextTokens(text: string, options?: CountTokensOptions): Promise<number>;
81
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AASH,MAAM,WAAW,SAAS;IACtB,IAAI,EAAE,MAAM,CAAA;IACZ,IAAI,EAAE,MAAM,CAAA;CACf;AAED,MAAM,WAAW,WAAW;IACxB,IAAI,EAAE,QAAQ,CAAA;IACd,UAAU,EAAE,MAAM,CAAA;IAClB,IAAI,EAAE,MAAM,CAAA;CACf;AAED,MAAM,WAAW,UAAU;IACvB,IAAI,EAAE,OAAO,CAAA;IACb,MAAM,EAAE,WAAW,CAAA;CACtB;AAED,MAAM,WAAW,YAAY;IACzB,IAAI,EAAE,UAAU,CAAA;IAChB,EAAE,EAAE,MAAM,CAAA;IACV,IAAI,EAAE,MAAM,CAAA;IACZ,KAAK,EAAE,OAAO,CAAA;CACjB;AAED,MAAM,WAAW,sBAAsB;IACnC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAA;IACtB,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;CACzB;AAED,MAAM,WAAW,eAAe;IAC5B,IAAI,EAAE,aAAa,CAAA;IACnB,WAAW,EAAE,MAAM,CAAA;IACnB,OAAO,CAAC,EAAE,MAAM,GAAG,sBAAsB,EAAE,CAAA;IAC3C,QAAQ,CAAC,EAAE,OAAO,CAAA;CACrB;AAED,MAAM,MAAM,YAAY,GAAG,SAAS,GAAG,UAAU,GAAG,YAAY,GAAG,eAAe,CAAA;AAMlF,MAAM,WAAW,kBAAkB;IAC/B;;;;OAIG;IACH,WAAW,CAAC,EAAE,MAAM,CAAA;CACvB;AAkED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAsB,WAAW,CAC7B,OAAO,EAAE,YAAY,EAAE,EACvB,OAAO,GAAE,kBAAuB,GACjC,OAAO,CAAC,MAAM,CAAC,CAwCjB;AAED;;;;;;;;GAQG;AACH,wBAAsB,eAAe,CACjC,IAAI,EAAE,MAAM,EACZ,OAAO,GAAE,kBAAuB,GACjC,OAAO,CAAC,MAAM,CAAC,CAEjB"}
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "web-count-token",
3
+ "version": "0.1.0",
4
+ "description": "Browser-compatible token counter for LLM content blocks using js-tiktoken",
5
+ "type": "module",
6
+ "main": "dist/web-count-token.cjs",
7
+ "module": "dist/web-count-token.js",
8
+ "types": "dist/index.d.ts",
9
+ "files": [
10
+ "dist/**"
11
+ ],
12
+ "exports": {
13
+ ".": {
14
+ "types": "./dist/index.d.ts",
15
+ "import": "./dist/web-count-token.js",
16
+ "require": "./dist/web-count-token.cjs"
17
+ }
18
+ },
19
+ "scripts": {
20
+ "build": "vite build && tsc --emitDeclarationOnly --outDir dist",
21
+ "dev": "vite build --watch"
22
+ },
23
+ "dependencies": {
24
+ "js-tiktoken": "^1.0.15"
25
+ },
26
+ "devDependencies": {
27
+ "typescript": "5.8.3",
28
+ "vite": "6.3.5",
29
+ "vite-plugin-dts": "^4.5.4"
30
+ },
31
+ "keywords": [
32
+ "tiktoken",
33
+ "token-counter",
34
+ "llm",
35
+ "browser",
36
+ "js-tiktoken"
37
+ ],
38
+ "license": "MIT"
39
+ }