code wiki / _hdl_build / nx_token_counter.nx
nx_token_counter.nx
buildroot/runtime/_hdl_build/nx_token_counter.nx
about
nx_token_counter.nx -- SOVEREIGN LLM token estimation + budget-aware context packing: the "push our books into
an LLM natively / build for RAG" foundation the library lacked (recon: BM25 + rerank exist, but no token counter
and no context-packer). tok_estimate uses the well-known ~4-chars/token rule, FLOORED by a word count, as a
deliberate slight OVER-estimate so budgeting never overflows the real context window. pack_text greedily keeps
input lines IN ORDER until the token budget is hit, and emits an honest truncation marker if the document does
not fully fit. HONEST: this is an ESTIMATE (no BPE table) for budgeting with a safety margin, not exact billing.
license_tier: ORIGINAL
dependencies 1 imports · 3 importers
imports: nx_syscalls.nx
imported by: nx_context_packer.nxnx_media_server.nxnx_token_counter_gate.nx
structs
| none |
consts
| none |
functions
| 10 | func tc_isspace(c: i64) -> i64 { if c == 32 { return 1 } if c == 10 { return 1 } if c == 9 { return 1 } if c == 13 { return 1 } return 0 } called by 1: tok_estimate |
| 13 | func tok_estimate(b: *u8, n: i64) -> i64 |
| 25 | func tc_cat(out: *u8, o: i64, s: *u8, outcap: i64) -> i64 called by 1: pack_text |
| 30 | func tc_catn(out: *u8, o: i64, v: i64, outcap: i64) -> i64 |
| 40 | func pack_text(b: *u8, n: i64, cap: i64, out: *u8, outcap: i64, incl: *i64, omit: *i64) -> i64 |