tokenrail 1.0.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,37 @@
1
+ name: CI
2
+
3
+ on:
4
+ push:
5
+ branches: [main]
6
+ pull_request:
7
+
8
+ jobs:
9
+ test:
10
+ runs-on: ubuntu-latest
11
+ strategy:
12
+ fail-fast: false
13
+ matrix:
14
+ python-version: ["3.10", "3.11", "3.12", "3.13", "3.14"]
15
+ steps:
16
+ - uses: actions/checkout@v4
17
+ - uses: astral-sh/setup-uv@v5
18
+ with:
19
+ python-version: ${{ matrix.python-version }}
20
+ - name: Run tests
21
+ run: uv run --python ${{ matrix.python-version }} pytest -q
22
+
23
+ lint:
24
+ runs-on: ubuntu-latest
25
+ steps:
26
+ - uses: actions/checkout@v4
27
+ - uses: astral-sh/setup-uv@v5
28
+ - name: Ruff check
29
+ run: uv run ruff check src tests
30
+
31
+ build:
32
+ runs-on: ubuntu-latest
33
+ steps:
34
+ - uses: actions/checkout@v4
35
+ - uses: astral-sh/setup-uv@v5
36
+ - name: Build sdist and wheel
37
+ run: uv build
@@ -0,0 +1,43 @@
1
+ name: Release
2
+
3
+ on:
4
+ push:
5
+ tags: ["v*"]
6
+
7
+ jobs:
8
+ build:
9
+ runs-on: ubuntu-latest
10
+ steps:
11
+ - uses: actions/checkout@v4
12
+ - uses: astral-sh/setup-uv@v5
13
+ - name: Check tag matches package version
14
+ run: |
15
+ PKG_VERSION=$(grep -m1 '^version' pyproject.toml | cut -d'"' -f2)
16
+ TAG_VERSION="${GITHUB_REF_NAME#v}"
17
+ if [ "$PKG_VERSION" != "$TAG_VERSION" ]; then
18
+ echo "Tag $GITHUB_REF_NAME does not match pyproject.toml version $PKG_VERSION" >&2
19
+ exit 1
20
+ fi
21
+ - name: Run tests
22
+ run: uv run pytest -q
23
+ - name: Build sdist and wheel
24
+ run: uv build
25
+ - uses: actions/upload-artifact@v4
26
+ with:
27
+ name: dist
28
+ path: dist/
29
+
30
+ publish:
31
+ needs: build
32
+ runs-on: ubuntu-latest
33
+ environment:
34
+ name: pypi
35
+ url: https://pypi.org/p/tokenrail
36
+ permissions:
37
+ id-token: write
38
+ steps:
39
+ - uses: actions/download-artifact@v4
40
+ with:
41
+ name: dist
42
+ path: dist/
43
+ - uses: pypa/gh-action-pypi-publish@release/v1
@@ -0,0 +1,14 @@
1
+ # Python-generated files
2
+ __pycache__/
3
+ *.py[oc]
4
+ build/
5
+ dist/
6
+ wheels/
7
+ *.egg-info
8
+
9
+ # Virtual environments
10
+ .venv
11
+
12
+ # Other
13
+ .env
14
+ sample_out*
@@ -0,0 +1 @@
1
+ 3.12
@@ -0,0 +1,61 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project are documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [1.0.0] - 2026-06-11
9
+
10
+ First stable release. The public API surface is now covered by semantic
11
+ versioning: `RailClient`, `BatchExecutor`, `batch_items_from_queries`,
12
+ `RollingMetricsMonitor`, `ResultsJsonlSink`, `PerRequestJsonSink`,
13
+ `OpenAIProvider`, and the types exported from `tokenrail`.
14
+
15
+ ### Added
16
+
17
+ - `py.typed` marker — the package now ships inline type information (PEP 561).
18
+ - `tokenrail.__version__`.
19
+ - Docstrings across the public API.
20
+ - Complete packaging metadata (license, classifiers, project URLs).
21
+ - CI test workflow across Python 3.10–3.14.
22
+ - First release published to [PyPI](https://pypi.org/project/tokenrail/);
23
+ releases are published automatically from `v*` tags via PyPI Trusted
24
+ Publishing.
25
+
26
+ ### Changed
27
+
28
+ - Minimum supported Python lowered from 3.11 to 3.10.
29
+ - `catalog.get_model_pricing` no longer carries a dead `service_tier` branch;
30
+ non-default tiers explicitly fall back to default-tier pricing.
31
+
32
+ ## [0.2.1] - 2026-06-10
33
+
34
+ ### Added
35
+
36
+ - Client-side RPM and TPM submit throttling in `BatchExecutor`
37
+ (`max_rpm` / `max_tpm`).
38
+
39
+ ### Removed
40
+
41
+ - vLLM provider support; the library is OpenAI-only.
42
+
43
+ ## [0.1.3] - 2026-05-20
44
+
45
+ ### Changed
46
+
47
+ - Removed the custom OpenAI retry loop in favor of the SDK's built-in
48
+ `max_retries`.
49
+
50
+ ## [0.1.2] - 2026-05-15
51
+
52
+ ### Added
53
+
54
+ - ETA progress reporting in `RollingMetricsMonitor`.
55
+
56
+ ## [0.1.0] - 2026-05-13
57
+
58
+ ### Added
59
+
60
+ - Initial release: `RailClient`, `BatchExecutor`, rolling metrics monitor,
61
+ JSONL and per-request sinks, model capability/pricing catalog.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Takumi Shibata
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,143 @@
1
+ Metadata-Version: 2.4
2
+ Name: tokenrail
3
+ Version: 1.0.0
4
+ Summary: Thin client and batch execution helpers for OpenAI Responses API workloads.
5
+ Project-URL: Homepage, https://github.com/takumi0shibata/tokenrail
6
+ Project-URL: Repository, https://github.com/takumi0shibata/tokenrail
7
+ Project-URL: Issues, https://github.com/takumi0shibata/tokenrail/issues
8
+ Project-URL: Changelog, https://github.com/takumi0shibata/tokenrail/blob/main/CHANGELOG.md
9
+ Author: Takumi Shibata
10
+ License-Expression: MIT
11
+ License-File: LICENSE
12
+ Keywords: batch,cost-tracking,llm,openai,rate-limit,responses-api,token
13
+ Classifier: Development Status :: 5 - Production/Stable
14
+ Classifier: Intended Audience :: Developers
15
+ Classifier: Operating System :: OS Independent
16
+ Classifier: Programming Language :: Python :: 3
17
+ Classifier: Programming Language :: Python :: 3.10
18
+ Classifier: Programming Language :: Python :: 3.11
19
+ Classifier: Programming Language :: Python :: 3.12
20
+ Classifier: Programming Language :: Python :: 3.13
21
+ Classifier: Programming Language :: Python :: 3.14
22
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
23
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
24
+ Classifier: Typing :: Typed
25
+ Requires-Python: >=3.10
26
+ Requires-Dist: openai>=1.108.0
27
+ Description-Content-Type: text/markdown
28
+
29
+ # tokenrail
30
+
31
+ [![CI](https://github.com/takumi0shibata/tokenrail/actions/workflows/ci.yml/badge.svg)](https://github.com/takumi0shibata/tokenrail/actions/workflows/ci.yml)
32
+ [![PyPI](https://img.shields.io/pypi/v/tokenrail)](https://pypi.org/project/tokenrail/)
33
+ [![Python 3.10+](https://img.shields.io/badge/python-3.10%2B-blue)](https://pypi.org/project/tokenrail/)
34
+ [![License: MIT](https://img.shields.io/badge/license-MIT-green)](LICENSE)
35
+
36
+ `tokenrail` is a small Python library for running OpenAI Responses API jobs with a `client.responses.create(...)`-style surface.
37
+
38
+ It focuses on:
39
+
40
+ - thread-based OpenAI batch execution
41
+ - client-side RPM / TPM submit throttling
42
+ - per-model token / cost monitoring with ETA progress reporting
43
+ - resumable JSONL and per-request result writing
44
+
45
+ Fully typed (PEP 561), supports Python 3.10+.
46
+
47
+ ## Installation
48
+
49
+ ```bash
50
+ uv add tokenrail
51
+ # or
52
+ pip install tokenrail
53
+ ```
54
+
55
+ To track an unreleased revision instead, depend on the Git repository directly:
56
+
57
+ ```toml
58
+ [tool.uv.sources]
59
+ tokenrail = { git = "https://github.com/takumi0shibata/tokenrail", tag = "v1.0.0" }
60
+ ```
61
+
62
+ Set your OpenAI API key in the consuming project before use:
63
+
64
+ ```bash
65
+ export OPENAI_API_KEY=...
66
+ ```
67
+
68
+ ## Quick start
69
+
70
+ ```python
71
+ from tokenrail import BatchExecutor, ResultsJsonlSink, PerRequestJsonSink, RailClient, RollingMetricsMonitor
72
+ from tokenrail.executor import batch_items_from_queries
73
+
74
+ client = RailClient.openai(max_retries=6)
75
+
76
+ queries = {
77
+ "1": [{"role": "user", "content": "Summarize this paper in 3 bullets."}],
78
+ "2": [{"role": "user", "content": "Extract the key assumptions."}],
79
+ }
80
+
81
+ items = batch_items_from_queries(
82
+ queries,
83
+ model="gpt-5.4-mini-2026-03-17",
84
+ reasoning_effort="medium",
85
+ verbosity="low",
86
+ )
87
+
88
+ # Consolidate only the necessary elements from all processing results into a single file.
89
+ result_sink = ResultsJsonlSink(
90
+ "out/results.jsonl",
91
+ projector=lambda response: {
92
+ "id": response.id,
93
+ "text": response.output_text,
94
+ "model": response.model,
95
+ "usage": response.usage.to_dict(),
96
+ },
97
+ )
98
+ # Save the raw output of each query.
99
+ per_request_sink = PerRequestJsonSink("out/")
100
+
101
+ executor = BatchExecutor(
102
+ client=client,
103
+ max_workers=16,
104
+ max_rpm=500,
105
+ max_tpm=200_000,
106
+ sinks=[result_sink, per_request_sink],
107
+ monitor=RollingMetricsMonitor(),
108
+ )
109
+
110
+ stats = executor.run(items)
111
+ print(stats.to_dict())
112
+ ```
113
+
114
+ ## Configuration notes
115
+
116
+ - `max_retries` configures the OpenAI Python SDK client's built-in retry behavior. `tokenrail` does not add its own retry loop on top.
117
+ - `max_rpm` and `max_tpm` are optional client-side submit limits. When a limit is set, `BatchExecutor` waits before submitting more work instead of raising its effective concurrency above the configured rate.
118
+ - Request failures are captured as error records (written to sinks and counted in stats) rather than raised, so one failing item does not abort the batch.
119
+ - `base_url` is passed through to the OpenAI Python SDK for callers that need an SDK-level custom endpoint.
120
+
121
+ ## Resume behavior
122
+
123
+ `BatchExecutor` reads completed ids from the first configured sink before it starts. Re-running the same job with the same output path skips records that are already present, then writes only the remaining requests.
124
+
125
+ If you use a custom `projector` with `ResultsJsonlSink`, make sure it keeps an `"id"` field — resume relies on it.
126
+
127
+ ## Cost tracking
128
+
129
+ - Costs are estimated from a checked-in per-model pricing table (`tokenrail.catalog`). Models without a pricing entry get `cost=None`; prices may lag behind OpenAI's official pricing page, which is always authoritative.
130
+ - OpenAI cost allocation is inferred from `billing.payer` in the response body. When `payer == "openai"`, the nominal request cost is counted as OpenAI-covered rather than developer-billed.
131
+ - `reasoning_effort` is gated to `gpt-5` / `o`-series style models in the checked-in capability registry.
132
+
133
+ ## Development
134
+
135
+ ```bash
136
+ uv sync
137
+ uv run pytest
138
+ uv run ruff check src tests
139
+ ```
140
+
141
+ ## License
142
+
143
+ [MIT](LICENSE)
@@ -0,0 +1,115 @@
1
+ # tokenrail
2
+
3
+ [![CI](https://github.com/takumi0shibata/tokenrail/actions/workflows/ci.yml/badge.svg)](https://github.com/takumi0shibata/tokenrail/actions/workflows/ci.yml)
4
+ [![PyPI](https://img.shields.io/pypi/v/tokenrail)](https://pypi.org/project/tokenrail/)
5
+ [![Python 3.10+](https://img.shields.io/badge/python-3.10%2B-blue)](https://pypi.org/project/tokenrail/)
6
+ [![License: MIT](https://img.shields.io/badge/license-MIT-green)](LICENSE)
7
+
8
+ `tokenrail` is a small Python library for running OpenAI Responses API jobs with a `client.responses.create(...)`-style surface.
9
+
10
+ It focuses on:
11
+
12
+ - thread-based OpenAI batch execution
13
+ - client-side RPM / TPM submit throttling
14
+ - per-model token / cost monitoring with ETA progress reporting
15
+ - resumable JSONL and per-request result writing
16
+
17
+ Fully typed (PEP 561), supports Python 3.10+.
18
+
19
+ ## Installation
20
+
21
+ ```bash
22
+ uv add tokenrail
23
+ # or
24
+ pip install tokenrail
25
+ ```
26
+
27
+ To track an unreleased revision instead, depend on the Git repository directly:
28
+
29
+ ```toml
30
+ [tool.uv.sources]
31
+ tokenrail = { git = "https://github.com/takumi0shibata/tokenrail", tag = "v1.0.0" }
32
+ ```
33
+
34
+ Set your OpenAI API key in the consuming project before use:
35
+
36
+ ```bash
37
+ export OPENAI_API_KEY=...
38
+ ```
39
+
40
+ ## Quick start
41
+
42
+ ```python
43
+ from tokenrail import BatchExecutor, ResultsJsonlSink, PerRequestJsonSink, RailClient, RollingMetricsMonitor
44
+ from tokenrail.executor import batch_items_from_queries
45
+
46
+ client = RailClient.openai(max_retries=6)
47
+
48
+ queries = {
49
+ "1": [{"role": "user", "content": "Summarize this paper in 3 bullets."}],
50
+ "2": [{"role": "user", "content": "Extract the key assumptions."}],
51
+ }
52
+
53
+ items = batch_items_from_queries(
54
+ queries,
55
+ model="gpt-5.4-mini-2026-03-17",
56
+ reasoning_effort="medium",
57
+ verbosity="low",
58
+ )
59
+
60
+ # Consolidate only the necessary elements from all processing results into a single file.
61
+ result_sink = ResultsJsonlSink(
62
+ "out/results.jsonl",
63
+ projector=lambda response: {
64
+ "id": response.id,
65
+ "text": response.output_text,
66
+ "model": response.model,
67
+ "usage": response.usage.to_dict(),
68
+ },
69
+ )
70
+ # Save the raw output of each query.
71
+ per_request_sink = PerRequestJsonSink("out/")
72
+
73
+ executor = BatchExecutor(
74
+ client=client,
75
+ max_workers=16,
76
+ max_rpm=500,
77
+ max_tpm=200_000,
78
+ sinks=[result_sink, per_request_sink],
79
+ monitor=RollingMetricsMonitor(),
80
+ )
81
+
82
+ stats = executor.run(items)
83
+ print(stats.to_dict())
84
+ ```
85
+
86
+ ## Configuration notes
87
+
88
+ - `max_retries` configures the OpenAI Python SDK client's built-in retry behavior. `tokenrail` does not add its own retry loop on top.
89
+ - `max_rpm` and `max_tpm` are optional client-side submit limits. When a limit is set, `BatchExecutor` waits before submitting more work instead of raising its effective concurrency above the configured rate.
90
+ - Request failures are captured as error records (written to sinks and counted in stats) rather than raised, so one failing item does not abort the batch.
91
+ - `base_url` is passed through to the OpenAI Python SDK for callers that need an SDK-level custom endpoint.
92
+
93
+ ## Resume behavior
94
+
95
+ `BatchExecutor` reads completed ids from the first configured sink before it starts. Re-running the same job with the same output path skips records that are already present, then writes only the remaining requests.
96
+
97
+ If you use a custom `projector` with `ResultsJsonlSink`, make sure it keeps an `"id"` field — resume relies on it.
98
+
99
+ ## Cost tracking
100
+
101
+ - Costs are estimated from a checked-in per-model pricing table (`tokenrail.catalog`). Models without a pricing entry get `cost=None`; prices may lag behind OpenAI's official pricing page, which is always authoritative.
102
+ - OpenAI cost allocation is inferred from `billing.payer` in the response body. When `payer == "openai"`, the nominal request cost is counted as OpenAI-covered rather than developer-billed.
103
+ - `reasoning_effort` is gated to `gpt-5` / `o`-series style models in the checked-in capability registry.
104
+
105
+ ## Development
106
+
107
+ ```bash
108
+ uv sync
109
+ uv run pytest
110
+ uv run ruff check src tests
111
+ ```
112
+
113
+ ## License
114
+
115
+ [MIT](LICENSE)
@@ -0,0 +1,67 @@
1
+ [project]
2
+ name = "tokenrail"
3
+ version = "1.0.0"
4
+ description = "Thin client and batch execution helpers for OpenAI Responses API workloads."
5
+ readme = "README.md"
6
+ license = "MIT"
7
+ license-files = ["LICENSE"]
8
+ authors = [
9
+ { name = "Takumi Shibata" },
10
+ ]
11
+ requires-python = ">=3.10"
12
+ keywords = [
13
+ "openai",
14
+ "responses-api",
15
+ "batch",
16
+ "llm",
17
+ "rate-limit",
18
+ "token",
19
+ "cost-tracking",
20
+ ]
21
+ classifiers = [
22
+ "Development Status :: 5 - Production/Stable",
23
+ "Intended Audience :: Developers",
24
+ "Operating System :: OS Independent",
25
+ "Programming Language :: Python :: 3",
26
+ "Programming Language :: Python :: 3.10",
27
+ "Programming Language :: Python :: 3.11",
28
+ "Programming Language :: Python :: 3.12",
29
+ "Programming Language :: Python :: 3.13",
30
+ "Programming Language :: Python :: 3.14",
31
+ "Topic :: Scientific/Engineering :: Artificial Intelligence",
32
+ "Topic :: Software Development :: Libraries :: Python Modules",
33
+ "Typing :: Typed",
34
+ ]
35
+ dependencies = [
36
+ "openai>=1.108.0",
37
+ ]
38
+
39
+ [project.urls]
40
+ Homepage = "https://github.com/takumi0shibata/tokenrail"
41
+ Repository = "https://github.com/takumi0shibata/tokenrail"
42
+ Issues = "https://github.com/takumi0shibata/tokenrail/issues"
43
+ Changelog = "https://github.com/takumi0shibata/tokenrail/blob/main/CHANGELOG.md"
44
+
45
+ [build-system]
46
+ requires = ["hatchling>=1.27.0"]
47
+ build-backend = "hatchling.build"
48
+
49
+ [tool.hatch.build.targets.wheel]
50
+ packages = ["src/tokenrail"]
51
+
52
+ [dependency-groups]
53
+ dev = [
54
+ "pytest>=8.0.0",
55
+ "python-dotenv>=1.2.2",
56
+ "ruff>=0.8.0",
57
+ ]
58
+
59
+ [tool.pytest.ini_options]
60
+ testpaths = ["tests"]
61
+
62
+ [tool.ruff]
63
+ line-length = 120
64
+ target-version = "py310"
65
+
66
+ [tool.ruff.lint]
67
+ select = ["E", "F", "W", "I", "UP", "B"]
@@ -0,0 +1,31 @@
1
+ """Thin client and batch execution helpers for OpenAI Responses API workloads.
2
+
3
+ tokenrail wraps the OpenAI Responses API with a ``client.responses.create(...)``-style
4
+ surface and adds thread-based batch execution, client-side RPM/TPM submit throttling,
5
+ per-model token/cost monitoring, and resumable JSONL / per-request result writing.
6
+ """
7
+
8
+ from .client import RailClient
9
+ from .executor import BatchExecutor, batch_items_from_queries
10
+ from .monitor import RollingMetricsMonitor
11
+ from .providers import OpenAIProvider
12
+ from .sinks import PerRequestJsonSink, ResultsJsonlSink
13
+ from .types import BatchItem, CostBreakdown, NormalizedResponse, StatsSnapshot, UsageBreakdown
14
+
15
+ __version__ = "1.0.0"
16
+
17
+ __all__ = [
18
+ "__version__",
19
+ "BatchExecutor",
20
+ "BatchItem",
21
+ "CostBreakdown",
22
+ "NormalizedResponse",
23
+ "OpenAIProvider",
24
+ "PerRequestJsonSink",
25
+ "RailClient",
26
+ "ResultsJsonlSink",
27
+ "RollingMetricsMonitor",
28
+ "StatsSnapshot",
29
+ "UsageBreakdown",
30
+ "batch_items_from_queries",
31
+ ]