code wiki / _hdl_build / nx_token_counter.nx

nx_token_counter.nx

buildroot/runtime/_hdl_build/nx_token_counter.nx

3383 B67 linesdepth 2pulls 2 transitivereach 3 importersview sourcekind librarytopic token
docsdependenciesstructsconstsfunctions

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

nx_syscalls.nx nx_token_counter.nx nx_context_packer.nx nx_media_server.nx nx_token_counter_gate.nx

imports: nx_syscalls.nx

imported by: nx_context_packer.nxnx_media_server.nxnx_token_counter_gate.nx

structs

none

consts

none

functions

10func 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
13func tok_estimate(b: *u8, n: i64) -> i64
called by 2: pack_textmain calls 1: tc_isspace
25func tc_cat(out: *u8, o: i64, s: *u8, outcap: i64) -> i64
called by 1: pack_text
30func tc_catn(out: *u8, o: i64, v: i64, outcap: i64) -> i64
called by 1: pack_text calls 1: sys_mmap
40func pack_text(b: *u8, n: i64, cap: i64, out: *u8, outcap: i64, incl: *i64, omit: *i64) -> i64