feat(model-host): accurate prompt token counting + pre-flight context-fit gate (WOR-1671)#715
Merged
Merged
Conversation
…er and gate over-context prompts The token-counting and chat-template primitives shipped but had no callers, so budgets and context-fit for locally served models still ran on a chars/4 heuristic and an over-long prompt only failed opaquely inside the engine. This wires the real tokenizer into the managed-local request path. - Acquisition: the pull now best-effort prefetches tokenizer.json and tokenizer_config.json alongside a non-GGUF model (GGUF embeds its tokenizer), mirroring the config.json prefetch. A miss is non-fatal and leaves the request path on its estimate. - Cache: a process-wide TokenizerCache parses a model's tokenizer.json once and reuses it for every request (they are multi-megabyte files). - Runtime: a launched deployment captures its tokenizer paths and served context window into a ServingContext, reachable per deployment through a defaulted PreparedDeploymentRuntime method and a manager accessor. - Dispatch: before forwarding a managed-local chat request, the gateway renders the model's chat_template (matching what the engine tokenizes) and counts the prompt against the model's own tokenizer. A prompt that exceeds the served context window is refused pre-flight with a legible context_length_exceeded error instead of a cold path to an engine rejection. A model that shipped no tokenizer, a non-chat body, or a template that fails to render all fall back safely (concatenated content, then the existing estimate). Chat-template rendering is used only to make the count accurate; the served engines (vLLM, SGLang, llama-server) apply the template themselves on /v1/chat/completions, so nothing renders on the way to the engine. Wiring the accurate count into the pre-reservation budget path (it resolves before the managed lane) is a follow-up.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
The token-counting (
tokenizers) and chat-template (minijinja) primitives shipped a while ago but had zero callers, so budgets and context-fit for locally served models still ran on achars/4heuristic, and an over-long prompt only failed opaquely inside the engine. This wires the real tokenizer into the managed-local request path.Four layers:
tokenizer.json+tokenizer_config.jsonalongside a non-GGUF model (GGUF embeds its tokenizer), mirroring the existingconfig.jsonprefetch. A miss is non-fatal.TokenizerCacheparses a model'stokenizer.jsononce and reuses it per request (they're multi-MB files).ServingContext, reachable per deployment via a defaultedPreparedDeploymentRuntimemethod (no blast radius) and a manager accessor.chat_template(matching what the engine tokenizes) and counts the prompt against the model's own tokenizer. A prompt exceeding the served context window is refused pre-flight with a legiblecontext_length_exceedederror instead of a cold path to an engine rejection.Graceful degradation (three safe tiers)
brokentemplate → count concatenated message content (small, safe undercount).Scoping notes
/v1/chat/completions, so nothing renders on the way to the engine — therender_chat_templateprimitive is exercised here for counting and remains available for a future raw-prompt engine.Tests
Cache load/reuse/miss;
render_prompt_for_counttemplate + fallback + malformed-template paths;PromptTokenFit::fits()boundary (a prompt that exactly fills the window leaves no room to generate). Full gate green: nextest 6984/6984, clippy-D warnings, rustdoc, schema + capability checks.Offline-verifiable: the counting mechanism is deterministic against a real
tokenizer.json; no GPU needed.