Fit your whole repo into any small model's token window — context selection for free/small LLMs.
Project description
tokenfit
Fit your whole repo into any small model's token window.
tokenfit is a context-selection pre-processor for free / small LLMs. Point it at
your project's markdown + code, ask a question, and it returns the most relevant slice
of your codebase — packed to fit a tight token budget — so a 7B model with an 8k window
answers as if it read the whole repo.
Why
GitHub Copilot moved to usage-based token billing (June 2026), pushing developers toward cheap open-source models on HuggingFace. But free/small models have tiny context windows — dump your whole repo at them and they choke or truncate.
Existing tools (tiny-agents, AGENTS.md, SKILL.md) inject context raw. tokenfit is
the missing retrieval layer that makes those models punch above their weight. It's a
pre-processor: it builds the optimal prompt, then hands it to your model or agent
framework — it does not trust a weak model to call a retrieval tool correctly.
How it works
query
│
▼
1. INGEST load AGENTS.md / SKILL.md / docs / code → chunk
2. INDEX embed chunks (BAAI/bge-small, local) → persist
3. RETRIEVE cosine top-k semantic search
4. BUDGET tokenizer-aware fit to N tokens + citations
│
▼
optimal prompt → any HuggingFace model
Does it actually beat just dumping the files? Yes.
We ran the free Qwen2.5-Coder-7B against psf/requests
— ~150,000 tokens of code, ~19× bigger than an 8000-token budget — comparing two ways
of feeding the model, across 10 questions (tokenfit eval --compare):
- Naive — concatenate the files and truncate to 8000 tokens.
- Retrieved — let tokenfit pick the relevant ~2000 tokens.
| Naive (8000 tok) | tokenfit retrieved (~2000 tok) | |
|---|---|---|
| Wins (of 10) | 1 (1 tie) | 9 |
| Cites the right source file | rarely | almost always |
| Tokens per call | 8000 | ~2000 (≈4× cheaper) |
| Failure modes | "context doesn't provide info", quoted the changelog, once answered in Chinese, once invented a class that doesn't exist | accurate, code-grounded answers |
Why naive collapses: the whole 8000-token budget filled up with HISTORY.md (the
changelog) and never reached a single source file. tokenfit semantically skips the noise
and fetches the right module — so it's both more accurate and ~4× cheaper per call.
📂 Full side-by-side transcripts in EXAMPLES.md.
Install
pip install tokenfit
Set a HuggingFace token with "Make calls to Inference Providers" permission:
export HF_TOKEN=hf_your_token_here # bash
$env:HF_TOKEN = "hf_your_token_here" # PowerShell
Verify it before you run anything:
tokenfit auth # checks the token is set and valid
tokenfit auth --ping # also makes a 1-token call to confirm inference access
Quickstart (CLI)
The fastest way — no Python required:
# Ask a question: tokenfit retrieves the right context AND gets the model's answer
tokenfit ask "How does the auth flow work?" --repo ./my-project
# Just print the selected context (no model call, pipe it anywhere)
tokenfit context "auth flow" --repo ./my-project
# Pre-build / refresh the index for a repo
tokenfit index --repo ./my-project --rebuild
Useful flags: --budget 8000 (token budget), --top-k 12 (chunks retrieved),
--model Qwen/Qwen2.5-Coder-7B-Instruct (any HF model), --rebuild (re-index).
Progress prints to stderr, so the answer/context on stdout stays clean for piping.
tokenfit indexes common source + doc file types out of the box (Python, JS/TS, Go,
Rust, Java, C#, C/C++, Ruby, PHP, Swift, GDScript, shell, plus md/yaml/toml/json…).
Indexing a different language? Add globs with --include:
tokenfit ask "How does combat work?" --repo ./my-godot-game --include "*.gd" --rebuild
📂 See EXAMPLES.md for real output — a free 7B model explaining a Godot game's movement code, grounded in the actual source.
Quickstart (Python)
from tokenfit import pack
from tokenfit.models import TokenfitModel
# Select the best ~8k tokens of context for a question
context = pack.build(
query="How does the auth flow work?",
repo="./my-project",
budget=8000,
)
# Feed it to any small HF model
model = TokenfitModel(model="Qwen/Qwen2.5-Coder-7B-Instruct")
answer = model.chat(
system="You are a coding assistant for THIS project. Use only the provided context.",
user=f"{context}\n\nQUESTION: How does the auth flow work?",
)
print(answer)
Validation harness
tokenfit ships with an eval harness that compares naive truncation vs retrieved context on your own repo — the experiment that proves the approach is worth it:
tokenfit eval --repo ./my-project --mode naive
tokenfit eval --repo ./my-project --mode retrieved
Each run writes a graded comparison sheet to tokenfit/eval/results/. Score the answers
1–5 and compare. Edit tokenfit/eval/dataset/questions.yaml to fit your project.
Roadmap
- Phase 0 — eval harness + naive baseline
- Phase 1 — semantic retrieval (chunk → embed → retrieve → budget)
- Phase 2 — hybrid BM25 + rerank + summarization for oversized chunks
- Phase 3 —
tiny-agents/smolagentsadapters, optional Chroma backend
See idea.md for the rationale and plan.md for the full plan.
Development
git clone https://github.com/shubham10divakar/tokenfit
cd tokenfit
pip install -e ".[dev]"
python -m tests.test_pipeline # dep-free regression test
License
MIT — see LICENSE.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tokenfit-1.1.0.tar.gz.
File metadata
- Download URL: tokenfit-1.1.0.tar.gz
- Upload date:
- Size: 23.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3bb821dc7524ec4fb661fd67dd7f56f6de030aaaa535dd05e37d30a94ab6bc7a
|
|
| MD5 |
c5b4236cb4c4a1b3f28883a9982284d7
|
|
| BLAKE2b-256 |
93b61c249f6900101819439a5b83786af5b34db5d883f52209dba3692b6849a2
|
File details
Details for the file tokenfit-1.1.0-py3-none-any.whl.
File metadata
- Download URL: tokenfit-1.1.0-py3-none-any.whl
- Upload date:
- Size: 21.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
073f124c9c19c25509b76597d9fab259a4517e14fbbe4cb87a309ae241a1d5fa
|
|
| MD5 |
b53b86defdbe4884294273fdaeac9b8a
|
|
| BLAKE2b-256 |
da467b812fc860b32c37ca33b2626a8b464f2638e09a562e8f91d53aab79a621
|