Skip to content

llm_hub: add upfront WARN-ONLY input-overflow check against max_context - #1943

Draft
arash77 wants to merge 2 commits into
bgruening:masterfrom
arash77:llm-hub-context-window-check
Draft

llm_hub: add upfront WARN-ONLY input-overflow check against max_context#1943
arash77 wants to merge 2 commits into
bgruening:masterfrom
arash77:llm-hub-context-window-check

Conversation

@arash77

@arash77 arash77 commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds an upfront, WARN-ONLY input-overflow check to the llm_hub tool. Before sending the request to the LLM, the tool now estimates the input token count locally with tiktoken (cl100k_base, offline — no proxy calls) and compares it to the selected model's context window. If the estimate exceeds the window, it prints a WARNING to stderr but still proceeds — the check never blocks the job.

This surfaces the most common failure mode (input too large → silently truncated/empty output) before the user waits on a long generation, while remaining purely advisory.

What changed

  • llm_hub.py: new estimate_input_tokens() + a module-level guard. Counts text content only; image tokens are not counted (no reliable local estimate), so the count is a lower bound for multimodal inputs. Uses disallowed_special=() so literal <|endoftext|>-style sequences don't crash the counter. All failure modes (tiktoken unavailable, network-restricted node unable to fetch the BPE vocab on first use, malformed max_context) are swallowed silently — the check is advisory and never aborts the job.
  • New max_context column (7th) in the genai_models data table: sample .loc, tool_data_table_conf.xml.sample, and .loc documentation updated. max_context is the model's full context window in tokens (e.g. 128000); leave empty or 0 if unknown, in which case the check is skipped for that model.
  • macros.xml / llm_hub.xml: add a tiktoken conda dependency (@TIKTOKEN_VERSION@ 0.13.0) and bump the Galaxy version suffix 3 → 4.

Deployment note (verified against a live Galaxy)

tool_data_table_conf.xml and genai_models.loc must be updated together. This was tested end-to-end against a running Galaxy (ToolDataTableManager + Cheetah NameMapper):

  • A 7-column <columns> declaration against a .loc row with fewer than 7 tab-separated fields causes Galaxy to reject the entire row (Line N ... is invalid). Pad the max_context field with an empty value if unknown — that is the supported "I don't know the window" state.
  • A 6-column declaration against this 7-column .loc makes $model.fields.max_context raise NotFound, so the tool form fails to render.

The .loc.sample now documents this coupling explicitly.

Testing

  • planemo testAll 1 test(s) executed passed. llm_hub (Test #1): passed
  • Standalone logic tests pass: parsing (128000, 128,000, 0, empty, whitespace, non-numeric), token estimation (text, multimodal-with-image, special-token safety), and graceful None return when tiktoken is unavailable.

Backward compatibility

Existing 6-column genai_models.loc deployments keep working provided admins update tool_data_table_conf.xml to the 7-column declaration and pad each row to 7 fields (empty max_context is fine). The existing tool test (no API key configured) is unchanged and still passes.


FOR CONTRIBUTOR:

  • I have read the CONTRIBUTING.md document and this tool is appropriate for the tools-iuc repo.
  • License permits unrestricted use (educational + commercial)
  • This PR adds a new tool or tool collection
  • This PR updates an existing tool or tool collection
  • This PR does something else (explain below)

@bgruening

Copy link
Copy Markdown
Owner

Existing 6-column genai_models.loc deployments keep working provided admins update tool_data_table_conf.xml to the 7-column declaration and pad each row to 7 fields (empty max_context is fine). The existing tool test (no API key configured) is unchanged and still passes.

This is all automatics, we can not do that :(
We would need to change the table name again.

@arash77

arash77 commented Jul 30, 2026

Copy link
Copy Markdown
Contributor Author

This is all automatics, we can not do that :( We would need to change the table name again.

Is it worth doing, then? Our providers could actually supply this via an API endpoint like /model/info (which carries max_input_tokens), but we would have to ask them to add it.

@arash77
arash77 force-pushed the llm-hub-context-window-check branch from 6803a4f to ebe1618 Compare July 31, 2026 08:39
@bgruening

Copy link
Copy Markdown
Owner

Is CZ supporting it and UFR not? I would ask UFR to add it.

@arash77

arash77 commented Jul 31, 2026

Copy link
Copy Markdown
Contributor Author

Is CZ supporting it and UFR not? I would ask UFR to add it.

Neither supports it right now.

@bgruening

Copy link
Copy Markdown
Owner

Interesting, ok lets ask them and see how complicated that is.

@arash77

arash77 commented Aug 11, 2026

Copy link
Copy Markdown
Contributor Author

ok RZ looked into it and it seems customizing the api pass through routes is a premium freature in LiteLLM!

Estimate the input token count locally with tiktoken (cl100k_base, offline)
and compare it to a new max_context column in the genai_models data table.
When the estimate exceeds the model's context window, warn on stderr but
still proceed -- the check never blocks the job. Image tokens are not
counted, so the estimate is a lower bound for multimodal inputs. The check
is skipped silently when max_context is empty/0/unknown or tiktoken is
unavailable, so it is purely advisory.

Add a 7th max_context column to the genai_models data table (sample, conf,
and .loc documentation), a tiktoken conda dependency, and bump the Galaxy
version suffix.

Document that tool_data_table_conf.xml and genai_models.loc must be updated
together: a row with fewer than 7 tab-separated fields is rejected (pad
max_context with an empty value if unknown), and a 6-column declaration
makes the tool form fail to render.
@arash77
arash77 force-pushed the llm-hub-context-window-check branch from ebe1618 to ffb37c0 Compare August 11, 2026 14:55
The max_context column cannot be added to genai_models in place: that .loc
file is generated automatically, and the 6- and 7-column layouts are mutually
incompatible (a 7-column <columns> declaration rejects every 6-field row, and
a 6-column declaration against a 7-field row makes the tool form fail to
render). Extending it would therefore require every deployment to update
tool_data_table_conf.xml and the generated .loc in lockstep.

Rename the table instead, so the 7-column table lives alongside the untouched
genai_models. Existing deployments keep working with no coordinated change,
and admins opt in by generating the new file with the max_context field.

Rename tool-data/genai_models.loc.sample accordingly, repoint the three
from_data_table references, and document the migration path in the .loc
sample header and README.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants