Repository navigation
Extending TokenCounter
A
TokenCountertells the budget fitter how many tokens a piece of text costs, so views fit the model you actually use. The default islen(text) / 4rounded up;Config(token_counter="tiktoken")uses tiktoken (extra), and any object with acount(text) -> intmethod can be passed in. Pass it assg.Guru(..., token_counter=...); an explicit counter survives config changes.
class TokenCounter(Protocol):
def count(self, text: str) -> int: ...Shipped: CharTokenCounter (the default heuristic) and TiktokenCounter(encoding="cl100k_base")
(extra tiktoken), both in schemaguru.scope.
class WordCounter:
"""Count tokens as words x 1.3: closer than chars/4 for some models' tokenizers."""
def count(self, text: str) -> int:
return math.ceil(len(text.split()) * 1.3)
guru = sg.Guru(reader=reader, store="shop.db", dialect="duckdb", token_counter=WordCounter())Verified on the shop fixture: views render and view.tokens is counted by the custom counter.
A counter should be deterministic (same text, same count) or views stop being byte-stable, and
it should not undercount: the fitter trusts it to keep a view inside budget_tokens.
def test_counter_is_deterministic_and_monotone():
c = WordCounter()
assert c.count("a b c") == c.count("a b c")
assert c.count("a b c d e f") >= c.count("a b c")
def test_view_fits_budget(guru):
assert guru.context("orders", budget_tokens=300).tokens <= 300SchemaGuru is open source under the MIT License, Copyright (c) 2026 Rupak Chakraborty · Repository · Issues · This wiki is generated from docs/wiki/; edit it there, not here.
Start here
Components
- Catalog and Crawl
- Store
- Measurement
- Sampling
- Join Graph
- Path Search and Hub Guard
- Waypoint Rules
- Drafting
- PII and Sampling Policy
- Overlays
- Scoping
- Rendering
- Refresh and Drift
- Feedback and Guard
- Server
- Agent Integration
Using it
Warehouses
- BigQuery
- Snowflake
- Redshift
- Databricks
- PostgreSQL
- DuckDB
- ClickHouse
- SQL Server / Synapse
- Oracle
- Trino
- MySQL / MariaDB
Extending
- CatalogReader
- Store
- Retriever and Embedder
- TokenCounter
- LLMProvider
- PII Detector
- FailureClassifier
- FactKind
- Profile and Format
- ChangeClassifier
- JoinCandidateSource (planned)
- Authenticator
Project