Skip to main content

Module context_budget

Module context_budget 

Source
Expand description

Context-window budgeting: estimating prompt token cost, deciding whether a request fits, and clamping the completion length to what remains.

Enums§

Verdict
The outcome of assessing a prompt against a context window.

Constants§

BUILTIN_CONTEXT_WINDOW
The context window Apple’s built-in model serves: fixed at 4096 tokens per session. The single source for every consumer — the runtime facade’s budget, the apple-foundation scanner’s record hint, and this module’s policy — so the value cannot drift apart.
COMPLETION_FLOOR
The minimum number of tokens reserved for the completion when deciding if a prompt fits the window.

Functions§

assess
Decide whether a prompt of prompt_characters fits window, and clamp the completion length to the space that remains.
effective_window
The effective context window for record, honoring a per-runtime policy. A built-in model has a fixed window; other runtimes derive it from the record’s declared context length (and, for Ollama, a caller override).
estimated_tokens
A rough token estimate for characters of text (about four characters per token).
prompt_characters
Count the characters a chat/completion payload will send: the content of each message plus a top-level prompt string, if present.
stored_context_length
The user-set context length stored in the record’s parameter values, if any.