Skip to main content

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.

PyPI version Downloads Downloads/month License: MIT Python


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 3tiny-agents / smolagents adapters, 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tokenfit-1.0.0.tar.gz (21.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tokenfit-1.0.0-py3-none-any.whl (20.3 kB view details)

Uploaded Python 3

File details

Details for the file tokenfit-1.0.0.tar.gz.

File metadata

  • Download URL: tokenfit-1.0.0.tar.gz
  • Upload date:
  • Size: 21.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.5

File hashes

Hashes for tokenfit-1.0.0.tar.gz
Algorithm Hash digest
SHA256 ada146095c46f7ae161629eae1bbce46093dd64a3bdd65a444d524e1d97b42d5
MD5 dccf808ef73a25568ddbe70414260fa2
BLAKE2b-256 edf9d019a9a76b4cfb64bc72e47cad04998df8f9f21e22ca3d9f5cb1b943893d

See more details on using hashes here.

File details

Details for the file tokenfit-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: tokenfit-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 20.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.5

File hashes

Hashes for tokenfit-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 de0386aac2e08a0e3b783b5e698c6c648a096a61fd1f673808888df3a328abc5
MD5 22a21ed72c5d72c30003cd1df6802324
BLAKE2b-256 e7fcc2752941a366ded4bce2dc38b6b6a8bb9c2ff14ac49efc049078b9846e0b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page