Module: Legion::LLM::Router::InputBound
- Extended by:
- Legion::Logging::Helper
- Includes:
- Legion::Logging::Helper
- Defined in:
- lib/legion/llm/router/input_bound.rb
Overview
Conservative provider-neutral pre-selection input token upper bound (SSOT v3 ยง9.1).
The bound is deliberately over-estimated: one token cannot require more than one input byte under any currently admitted encoding contract. We therefore sum UTF-8 byte lengths of all textual content and canonical JSON-serialised byte lengths of structured inputs, then add the configured framing overhead. No chars/4 heuristic, no tokenizer call, no Float arithmetic, no provider-specific estimation.
This bound gates hard admission decisions before a lane is selected. Existing TokenEstimation/ContextAccounting helpers remain available for telemetry and post-selection curation but their output cannot authorize a lane.
Class Method Summary collapse
-
.call(messages: nil, system: nil, tools: nil, tool_choice: nil, thinking: nil, response_format: nil, operation_payload: nil, framing_overhead_tokens: 0) ⇒ Integer
Returns a nonnegative Integer upper bound on the number of tokens required to represent all supplied inputs.
Class Method Details
.call(messages: nil, system: nil, tools: nil, tool_choice: nil, thinking: nil, response_format: nil, operation_payload: nil, framing_overhead_tokens: 0) ⇒ Integer
Returns a nonnegative Integer upper bound on the number of tokens required to represent all supplied inputs.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/legion/llm/router/input_bound.rb', line 38 def call( messages: nil, system: nil, tools: nil, tool_choice: nil, thinking: nil, response_format: nil, operation_payload: nil, framing_overhead_tokens: 0, ** ) total = 0 # System prompt total = Integer(total) + text_bytes(system) # Messages: each message may have String content or Array-of-content-blocks Array().each do |msg| total += (msg) end # Structured inputs serialised to canonical JSON total += serialized_bytes(tools) unless nil_or_empty?(tools) total += serialized_bytes(tool_choice) unless nil_or_empty?(tool_choice) total += serialized_bytes(thinking) unless nil_or_empty?(thinking) total += serialized_bytes(response_format) unless nil_or_empty?(response_format) total += serialized_bytes(operation_payload) unless nil_or_empty?(operation_payload) # Framing overhead (caller-supplied configured Integer) overhead = Integer(framing_overhead_tokens.to_i) raise ArgumentError, "framing_overhead_tokens must be nonnegative, got #{overhead}" if overhead.negative? total += overhead log.debug("[llm][input_bound] action=compute total_bytes=#{total}") total end |