tritobol
trim + token + bolster — a lightweight Python token preprocessor that removes redundant prompt text and keeps the most recent context when an input is too long.
tritobol does not summarize or rewrite your content. It removes repeated
material, whitespace noise, and decorative separators. It conservatively
compresses supported code/configuration blocks only when local validation
passes, and — only if a hard token budget is still exceeded — slides the window
forward so the newest tokens survive.
By default, token counts use the bundled DeepSeek V4.1 Flash tokenizer (129,280-token vocabulary). To optimize for another model, load its compatible tokenizer locally; the optimizer, trimming pipeline, and token-budget logic are independent of any one provider.
Install
The repository and clone directory are named tritobol-token_optimizer-;
the Python package and CLI command are named tritobol. To install from a
source clone and start the guided workflow:
git clone https://github.com/pizzafeastbuddies/tritobol-token_optimizer-.git
cd tritobol-token_optimizer-
python -m pip install -e .
tritobol
For a non-editable source install, run this from the repository root instead:
python -m pip install .
Publishing to PyPI (maintainers)
Configure a PyPI trusted publisher for the pizzafeastbuddies/tritobol-token_optimizer-
repository, using .github/workflows/publish.yml and the GitHub Actions
environment pypi. Publishing is triggered by a non-prerelease GitHub release;
the release tag must match the package version in pyproject.toml (for example,
v0.1.0).
The guide asks for one UTF-8 text file, optionally asks for a maximum token
budget, and shows token counts plus a unified diff before saving. A budget may
discard the oldest text when the optimized result is still too long; a blank
budget keeps the normal no-hard-limit behavior. The suggested output is a
separate *.optimized.* file; Tritobol never overwrites the source and will
not replace an existing output. Saving requires an explicit confirmation.
Press Enter at the file prompt or use Ctrl+C/Ctrl+D (Ctrl+Z then Enter on
Windows) to cancel. The same flow can be started with tritobol --interactive.
When stdin is redirected, no-argument invocation continues to read the piped
text as before.
The only runtime dependency is tokenizers.
The tokenizer JSON is bundled with the package (about 6 MB); no model weights are
downloaded.
Use another model's tokenizer
HuggingFaceTokenizer loads a local tokenizer.json supported by the
tokenizers library. Supply a tokenizer
file that matches the model you intend to use; there is no automatic model-name
lookup or network download.
from tritobol import HuggingFaceTokenizer, Tritobol
tokenizer = HuggingFaceTokenizer(
"path/to/model/tokenizer.json",
model_name="my-model", # optional label for logs and inspection
)
tool = Tritobol(tokenizer=tokenizer)
result = tool.optimize(prompt, max_tokens=8192)
print(tool.count_tokens(prompt)) # counted with my-model's tokenizer
The CLI accepts the same local tokenizer file:
tritobol prompt.txt --tokenizer path/to/tokenizer.json --model-name my-model --max-tokens 8192
Applications can also provide a custom tokenizer object implementing
count(text) and tail(text, max_tokens); tail should return a
WindowResult. Token counts cover the supplied text only. They do not include
provider-specific chat templates, message framing, tools, or API-side overhead.
Use a tokenizer compatible with the target model and account for those additional
inputs separately when enforcing a request's actual context limit.
Code and configuration safety
The default strategy is task_type="auto": code-like inputs use a more
conservative duplicate threshold, while long-form documents use a lower
threshold to remove more repeated prose. You can explicitly select
"coding" or "document" on Tritobol.optimize() or optimize_text().
Fenced Python blocks longer than four non-empty lines have blank lines and
immediately repeated ordinary comment-only lines removed; unique explanatory
comments are kept. # type:, # noqa, # pylint:, and
# pragma: directives are retained. The candidate must pass Python's local
syntax compiler or that segment is restored. JSON blocks are compacted only
after a successful parse. YAML blocks are changed only when PyYAML is already
installed and both parsed values match; otherwise they remain unchanged.
Short snippets and unsupported languages remain protected. Repeated code blocks
are not deduplicated. A caller-supplied hard token limit may still truncate any
final output, so avoid setting one below the size needed for a complete code
segment.
Each OptimizationResult includes redaction-safe segments logs with status,
reason, and token counts. The CLI also supports a JSON report with
--report report.json.
Important implementation sections
tritobol/core.pycontains the deterministic optimization pipeline, token statistics, segment logs, and the optional tail-window behavior.tritobol/tokenizer.pyprovides the bundled and local-tokenizer adapters; token counts are based on the configured tokenizer, not an estimate.tritobol/batch.pyhandles local files and project trees, validates supported code/configuration output, and writes to a separate destination.tritobol/__main__.pyimplements the CLI, including preview diffs and JSON reports.benchmarks/run_benchmarks.pycontains fixed synthetic benchmark inputs and the calculations for token savings and designated-phrase retention.
Sensitive values
Credential-shaped values such as common API-key/token assignments, recognizable
key prefixes, PEM private-key blocks, and explicit CONFIDENTIAL: or
BEGIN/END CONFIDENTIAL markers are scanned locally by default.
sensitive_data="skip" protects detected values from text normalization and
duplicate removal while leaving them unchanged in the output (unless a hard
token window later discards that part).
sensitive_data="mask" replaces detections with [REDACTED]; "off" disables
the scanner. This is a conservative pattern scanner, not a general detector of
confidential prose, and it is not a substitute for a dedicated secret scanner.
Sensitive values are never included in segment log details.
Quick start
from tritobol import Tritobol
tool = Tritobol()
raw = """
The deployment checklist starts with a database backup.
The deployment checklist starts with a database backup.
After the backup, run the migrations and check the health endpoint.
"""
result = tool.optimize(raw, max_tokens=4096)
print(result.text)
print(result.stats.summary())
# tokens 36 -> 22 | saved 14 (38.9%) | chars 198 -> 124 | ...
optimize() always returns an OptimizationResult with:
| Field | Meaning |
|---|---|
result.text |
Cleaned and possibly windowed text |
result.stats.original_tokens |
Token count before optimization |
result.stats.optimized_tokens |
Token count after optimization |
result.stats.saved_tokens |
Absolute token reduction |
result.stats.saved_percent |
Reduction as a percentage |
result.stats.truncated |
Whether the sliding window ran |
result.warning |
The warning text, even if printing was disabled |
How it works
tritobol normalizes text, removes redundant content, counts tokens with the configured tokenizer, and keeps the newest context if the optional token budget is exceeded. See the runnable demo for complete examples. From the project root, run it with:
python examples/demo.py
Sliding window for long texts
When the trimmed text is still longer than max_tokens, tritobol discards the
earliest tokens and keeps the latest context. It prints one clear
warning to stderr:
WARNING: tritobol truncated the input because it exceeded max_tokens=80: kept the latest 80 tokens and discarded the earliest 619 tokens (input was 699 tokens).
long_transcript = "\n\n".join(
f"Section {i}: " + "detail " * 30 for i in range(20)
)
result = tool.optimize(long_transcript, max_tokens=80)
print(result.stats.truncated) # True
print(result.stats.discarded_tokens) # 619
print("Section 19" in result.text) # True - newest context survives
print("Section 0" in result.text) # False - oldest context was dropped
The cut is made at a tokenizer character offset, so the returned text is not decoded through token IDs and does not acquire replacement characters.
To suppress the print while still receiving result.warning, pass
warn_on_truncation=False. To capture the warning yourself, pass a text stream
with warning_stream=my_stream.
Roadmap
The current release focuses on local text cleanup, token counting, prompt optimization, preview, and JSON reporting. Planned areas for future development include:
- CLI conventions and machine-readable output suitable for standalone use and optional integration with the KeyShield workflow.
- More conservative code and security-audit handling, with tests for preserving identifiers, file paths, vulnerability descriptions, and key findings.
- Compression-risk assessment and rollback to the original input when a result fails validation or is judged too risky.
- Project-level caching and Markdown report export.
- Lower-friction installation across local machines, WSL, and CI, while being explicit that accurate model-specific token counts require the matching tokenizer.
These are goals, not features of the current release. Tritobol does not yet provide risk-scored rollback, project caching, Markdown reports, or automatic model/vocabulary selection. Review previews and test outputs before using optimized text in security-sensitive workflows.
Command line
For an interactive, single-file walkthrough, run tritobol with no arguments
in a terminal (or pass --interactive). It uses Tritobol's default tokenizer
and optimization settings, and does not set a token budget unless you enter
one in response to the optional prompt.
# Read a file, write optimized text to stdout, stats to stderr
tritobol prompt.txt
# Apply an 8,192-token budget and write to a file
tritobol prompt.txt --max-tokens 8192 -o optimized.txt
# Read from stdin
type prompt.txt | tritobol --max-tokens 4096
# Explicitly allow trimming inside fenced Markdown code blocks
tritobol prompt.txt --no-protect-code-blocks
# Process a project into a separate output directory, honoring .gitignore
tritobol . --output optimized --report optimization-report.json
# Explicitly mask detected credential-shaped values
tritobol prompt.txt --sensitive-data mask
# Preview the changes without writing an optimized file
tritobol prompt.txt --diff
If -o is omitted, only the optimized text goes to stdout. Warnings and token
statistics go to stderr, so shell pipelines remain clean.
--diff instead writes a unified diff to stdout and never writes an output
file; it accepts one file or stdin, and cannot be combined with -o or project
directory processing. Review the diff before explicitly running again with
-o optimized.txt to save a separate copy.
Project mode writes supported text/configuration files into a separate output
tree, copies unsupported files unchanged, skips symbolic links, and reports
per-file token savings and protected/compressed segments. Aggregate token
counts cover optimized supported text files; copied unsupported files are
listed as protected but are not token-counted. Report paths are project-relative
to avoid including the user's full local filesystem path. The sensitive scanner
applies only to files that are optimized. It recognizes common
.gitignore globs, directory patterns, negation, and nested ignore files; it
does not claim complete parity with every Git ignore edge case. The processing
is local and makes no network requests. Supported optimization file types are
Markdown/plain-text documents, Python, JSON, and YAML. YAML validation is
available only when PyYAML is already installed; Tritobol does not install or
download it automatically.
What gets trimmed
The default pipeline is deterministic and conservative:
- Fenced Markdown code blocks — short snippets and unsupported languages
are preserved; supported Python and JSON blocks are selectively compressed
and parsed before acceptance. A hard token budget can still truncate final
text; disable code handling with
Tritobol(protect_code_blocks=False)or--no-protect-code-blocks. - Invisible characters — BOM, zero-width characters, and control bytes in prose.
- HTML comments —
<!-- ... -->blocks in prose. - Whitespace noise — indentation, trailing spaces, and repeated blank lines in prose.
- Decorative separators — lines made only of
=,-,*,_,#, etc. - Duplicate paragraphs — exact repeats after whitespace/case normalization.
- Duplicate lines — repeated substantive lines; the first occurrence wins.
- Duplicate sentences — repeated sentences, including inside paragraphs.
- Sliding window — only when
max_tokensis supplied and still exceeded.
Short strings are protected from duplicate removal. The default threshold is 24 characters for coding-oriented input and 12 for document-oriented input; you can explicitly adjust the base threshold:
tool = Tritobol(min_duplicate_chars=40)
Because only redundant material is removed, savings depend on the input. Noisy
prompts may save more than already-clean prose, which may save nothing. The
included synthetic benchmark gives reproducible measurements for its fixed
examples; those results should not be generalized to arbitrary prompts or
treated as a semantic-quality guarantee. A hard max_tokens budget can
produce a larger reduction by discarding earlier context.
Reproducible benchmark
Run the bundled local benchmark from the project root:
python -m benchmarks.run_benchmarks
python -m benchmarks.run_benchmarks --json
It uses three fixed, synthetic examples and the bundled DeepSeek V4.1 Flash tokenizer, with no model/API calls and no hard token budget. For each example and in aggregate it reports original and optimized token counts, saved tokens and percentage, plus retention of explicitly listed phrases. Phrase retention is a literal substring check, not an assessment of meaning, factual accuracy, or model performance. This small fixture suite is useful for detecting behavior changes against stable examples; it is not a representative corpus or a claim about real-world savings or information preservation.
API surface
from tritobol import (
DeepSeekTokenizer,
HuggingFaceTokenizer,
Tritobol,
optimize_text,
)
tool = Tritobol()
tool.count_tokens(text) # exact count for the configured tokenizer
tool.trim(text) # redundancy removal only
tool.fit_window(text, max_tokens) # sliding window only
tool.optimize(text, max_tokens=None) # trim + optional window + stats
optimize_text(text, max_tokens=1024) # convenience wrapper
# Existing DeepSeekTokenizer remains available; HuggingFaceTokenizer accepts
# another compatible local tokenizer.json.
other_tool = Tritobol(
tokenizer=HuggingFaceTokenizer("path/to/tokenizer.json")
)
Process a project tree locally from Python:
from pathlib import Path
from tritobol import process_path
report = process_path(
"my-project",
"optimized-project",
respect_gitignore=True,
task_type="auto",
sensitive_data="skip",
)
print(report.summary())
report.write_json(Path("optimization-report.json"))
Tests
python -m unittest discover -s tests -v
The test cases print original token count, optimized token count, and saved percentage for each scenario:
[low noise / whitespace] original=45 optimized=39 saved=13.3%
[duplicate paragraphs] original=112 optimized=62 saved=44.6%
[high noise / repeated blocks] original=180 optimized=42 saved=76.7%
[long text / sliding window] original=700 optimized=80 saved=88.6%
A runnable demonstration is also included:
python examples/demo.py
Bundled tokenizer provenance
The bundled file is tokenizer.json from the official
deepseek-ai/DeepSeek-V4.1-Flash
checkpoint:
- Vocabulary: 129,280 tokens
- License: MIT, Copyright (c) 2023 DeepSeek
See tritobol/data/README.md and tritobol/data/LICENSE.deepseek-tokenizer for
details.
Analysis & details
Please click project_overview for analyses & details.
License
The tritobol source is MIT licensed. The bundled tokenizer retains DeepSeek's MIT license notice.
Web application (local UI)
A local-first browser interface lives in tritobol_app/. It includes account
registration and sign-in; account data is stored in the installation's SQLite
database, while prompt history stays in the browser. To run it from a source
clone, install the optional app dependency and start the local server:
python -m pip install -e ".[app]"
python -m tritobol_app
On Windows, you can instead double-click start.bat (Python 3.10+ required).
The app binds to 127.0.0.1 and uses Flask's development server; it is for
local use only.
Deployment status
Public deployment, including Netlify deployment, is not ready and is deferred. After the hackathon winner announcements, interested team members may decide whether to continue the project and plan deployment. No public hosting is currently available.
See docs/APPLICATION.md for more about the local app.
Metadata
Release files for tritobol 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| tritobol-0.1.0.tar.gz | 2.0 MB | With provenance | Details for tritobol-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| tritobol-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for tritobol-0.1.0-py3-none-any.whl |
Total release size: 3.9 MB
Release files / tritobol-0.1.0.tar.gz
| Download URL | tritobol-0.1.0.tar.gz |
|---|---|
| Size | 2.0 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
2c08f69b93db4e89c1e2404dc89efee52b5c70ecd65b2de99e567f6a1080f341
|
|
BLAKE2b-256 checksum How to use checksums |
9f13a8481877541206a63fe5dbc67aec2c5050d47d181fcf8369a1bb0d4bc4c7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 21, 2026.
Transparency logRelease files / tritobol-0.1.0-py3-none-any.whl
| Download URL | tritobol-0.1.0-py3-none-any.whl |
|---|---|
| Size | 1.9 MB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
879979b6d7e4b5b067d74cf4668fa937aebe424d6c07e67cbd6287b5692d493d
|
|
BLAKE2b-256 checksum How to use checksums |
bc89a8fa8d4edf57367c08866e958369561334b81496fb461f0faa3a0e872225
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 21, 2026.
Transparency logSecurity for tritobol 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for tritobol-0.1.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 21, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.