tokenwise-py 0.1.0__tar.gz

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,9 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 episodely
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,59 @@
1
+ Metadata-Version: 2.4
2
+ Name: tokenwise-py
3
+ Version: 0.1.0
4
+ Summary: Estimate tokens and cost of a prompt across popular LLMs.
5
+ Author: episodely
6
+ License: MIT
7
+ Project-URL: Homepage, https://github.com/episodely/tokenwise
8
+ Project-URL: Issues, https://github.com/episodely/tokenwise/issues
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: Operating System :: OS Independent
12
+ Requires-Python: >=3.8
13
+ Description-Content-Type: text/markdown
14
+ License-File: LICENSE
15
+ Dynamic: license-file
16
+
17
+ # tokenwise
18
+
19
+ Estimate how many tokens a prompt is and what it costs on popular LLMs. Pure Python, no dependencies, no network.
20
+
21
+ ```
22
+ $ echo "Summarize this article in three bullet points." | python -m tokenwise
23
+ ~12 tokens (46 chars)
24
+ gemini-flash $0.000001
25
+ ...
26
+ ```
27
+
28
+ ## Install
29
+
30
+ ```
31
+ pip install tokenwise-py
32
+ ```
33
+
34
+ Or copy `tokenwise.py` into your project, it has no dependencies.
35
+
36
+ ## Usage
37
+
38
+ ```
39
+ python -m tokenwise prompt.txt --out 500 # include expected output tokens
40
+ python -m tokenwise prompt.txt --json
41
+ ```
42
+
43
+ ```python
44
+ from tokenwise import estimate_tokens, cost
45
+ n = estimate_tokens(text)
46
+ cost(n, 500, "claude-sonnet")
47
+ ```
48
+
49
+ ## Accuracy
50
+
51
+ It is a heuristic (chars/4 and word-piece blend, denser for non-Latin scripts like Arabic). Expect roughly 15% error against real tokenizers. Good for budgeting, not billing. Prices live in the `PRICES` dict at the top of `tokenwise.py`; update them to match current provider pricing.
52
+
53
+ ## Test
54
+
55
+ ```
56
+ python -m pytest
57
+ ```
58
+
59
+ MIT licensed.
@@ -0,0 +1,43 @@
1
+ # tokenwise
2
+
3
+ Estimate how many tokens a prompt is and what it costs on popular LLMs. Pure Python, no dependencies, no network.
4
+
5
+ ```
6
+ $ echo "Summarize this article in three bullet points." | python -m tokenwise
7
+ ~12 tokens (46 chars)
8
+ gemini-flash $0.000001
9
+ ...
10
+ ```
11
+
12
+ ## Install
13
+
14
+ ```
15
+ pip install tokenwise-py
16
+ ```
17
+
18
+ Or copy `tokenwise.py` into your project, it has no dependencies.
19
+
20
+ ## Usage
21
+
22
+ ```
23
+ python -m tokenwise prompt.txt --out 500 # include expected output tokens
24
+ python -m tokenwise prompt.txt --json
25
+ ```
26
+
27
+ ```python
28
+ from tokenwise import estimate_tokens, cost
29
+ n = estimate_tokens(text)
30
+ cost(n, 500, "claude-sonnet")
31
+ ```
32
+
33
+ ## Accuracy
34
+
35
+ It is a heuristic (chars/4 and word-piece blend, denser for non-Latin scripts like Arabic). Expect roughly 15% error against real tokenizers. Good for budgeting, not billing. Prices live in the `PRICES` dict at the top of `tokenwise.py`; update them to match current provider pricing.
36
+
37
+ ## Test
38
+
39
+ ```
40
+ python -m pytest
41
+ ```
42
+
43
+ MIT licensed.
@@ -0,0 +1,27 @@
1
+ [build-system]
2
+ requires = ["setuptools>=61"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "tokenwise-py"
7
+ version = "0.1.0"
8
+ description = "Estimate tokens and cost of a prompt across popular LLMs."
9
+ readme = "README.md"
10
+ license = { text = "MIT" }
11
+ requires-python = ">=3.8"
12
+ authors = [{ name = "episodely" }]
13
+ classifiers = [
14
+ "License :: OSI Approved :: MIT License",
15
+ "Programming Language :: Python :: 3",
16
+ "Operating System :: OS Independent",
17
+ ]
18
+
19
+ [project.urls]
20
+ Homepage = "https://github.com/episodely/tokenwise"
21
+ Issues = "https://github.com/episodely/tokenwise/issues"
22
+
23
+ [project.scripts]
24
+ tokenwise = "tokenwise:main"
25
+
26
+ [tool.setuptools]
27
+ py-modules = ["tokenwise"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,53 @@
1
+ """Estimate tokens and cost of a prompt without any dependency."""
2
+ import re, sys, json, argparse
3
+
4
+ # USD per 1M tokens (input, output). Edit prices.json-style dict to match current pricing.
5
+ PRICES = {
6
+ "gpt-4o": (2.50, 10.00),
7
+ "gpt-4o-mini": (0.15, 0.60),
8
+ "claude-sonnet": (3.00, 15.00),
9
+ "claude-haiku": (0.80, 4.00),
10
+ "gemini-flash": (0.10, 0.40),
11
+ }
12
+ _WORD = re.compile(r"\w+|[^\w\s]", re.UNICODE)
13
+
14
+ def estimate_tokens(text: str) -> int:
15
+ """Heuristic: max of chars/4 and ~1.3 tokens per word-piece. Within ~15% for English."""
16
+ if not text:
17
+ return 0
18
+ by_chars = len(text) / 4
19
+ by_words = len(_WORD.findall(text)) * 1.15
20
+ # non-latin scripts (e.g. Arabic) tokenize denser: count them closer to 1 token per 2 chars
21
+ nonlatin = sum(1 for c in text if ord(c) > 0x024F)
22
+ adj = nonlatin / 2 - nonlatin / 4
23
+ return int(round(max(by_chars, by_words) + adj))
24
+
25
+ def cost(tokens_in: int, tokens_out: int = 0, model: str = "gpt-4o") -> float:
26
+ if model not in PRICES:
27
+ raise ValueError(f"unknown model {model!r}; known: {', '.join(sorted(PRICES))}")
28
+ pin, pout = PRICES[model]
29
+ return (tokens_in * pin + tokens_out * pout) / 1_000_000
30
+
31
+ def main(argv=None):
32
+ p = argparse.ArgumentParser(prog="tokenwise", description="Estimate tokens and cost.")
33
+ p.add_argument("file", nargs="?", help="file to read (default: stdin)")
34
+ p.add_argument("--out", type=int, default=0, help="expected output tokens")
35
+ p.add_argument("--json", action="store_true")
36
+ a = p.parse_args(argv)
37
+ try:
38
+ text = open(a.file, encoding="utf-8").read() if a.file else sys.stdin.read()
39
+ except OSError as e:
40
+ print(f"tokenwise: {e}", file=sys.stderr)
41
+ return 2
42
+ n = estimate_tokens(text)
43
+ rows = {m: round(cost(n, a.out, m), 6) for m in PRICES}
44
+ if a.json:
45
+ print(json.dumps({"tokens": n, "cost_usd": rows}, indent=2))
46
+ else:
47
+ print(f"~{n} tokens ({len(text)} chars)")
48
+ for m, c in sorted(rows.items(), key=lambda x: x[1]):
49
+ print(f" {m:<14} ${c:.6f}")
50
+ return 0
51
+
52
+ if __name__ == "__main__":
53
+ sys.exit(main())
@@ -0,0 +1,59 @@
1
+ Metadata-Version: 2.4
2
+ Name: tokenwise-py
3
+ Version: 0.1.0
4
+ Summary: Estimate tokens and cost of a prompt across popular LLMs.
5
+ Author: episodely
6
+ License: MIT
7
+ Project-URL: Homepage, https://github.com/episodely/tokenwise
8
+ Project-URL: Issues, https://github.com/episodely/tokenwise/issues
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: Operating System :: OS Independent
12
+ Requires-Python: >=3.8
13
+ Description-Content-Type: text/markdown
14
+ License-File: LICENSE
15
+ Dynamic: license-file
16
+
17
+ # tokenwise
18
+
19
+ Estimate how many tokens a prompt is and what it costs on popular LLMs. Pure Python, no dependencies, no network.
20
+
21
+ ```
22
+ $ echo "Summarize this article in three bullet points." | python -m tokenwise
23
+ ~12 tokens (46 chars)
24
+ gemini-flash $0.000001
25
+ ...
26
+ ```
27
+
28
+ ## Install
29
+
30
+ ```
31
+ pip install tokenwise-py
32
+ ```
33
+
34
+ Or copy `tokenwise.py` into your project, it has no dependencies.
35
+
36
+ ## Usage
37
+
38
+ ```
39
+ python -m tokenwise prompt.txt --out 500 # include expected output tokens
40
+ python -m tokenwise prompt.txt --json
41
+ ```
42
+
43
+ ```python
44
+ from tokenwise import estimate_tokens, cost
45
+ n = estimate_tokens(text)
46
+ cost(n, 500, "claude-sonnet")
47
+ ```
48
+
49
+ ## Accuracy
50
+
51
+ It is a heuristic (chars/4 and word-piece blend, denser for non-Latin scripts like Arabic). Expect roughly 15% error against real tokenizers. Good for budgeting, not billing. Prices live in the `PRICES` dict at the top of `tokenwise.py`; update them to match current provider pricing.
52
+
53
+ ## Test
54
+
55
+ ```
56
+ python -m pytest
57
+ ```
58
+
59
+ MIT licensed.
@@ -0,0 +1,9 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ tokenwise.py
5
+ tokenwise_py.egg-info/PKG-INFO
6
+ tokenwise_py.egg-info/SOURCES.txt
7
+ tokenwise_py.egg-info/dependency_links.txt
8
+ tokenwise_py.egg-info/entry_points.txt
9
+ tokenwise_py.egg-info/top_level.txt
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ tokenwise = tokenwise:main
@@ -0,0 +1 @@
1
+ tokenwise