Skip to content

LCORE-4765: Resolve compaction context window from provider catalog metadata - #2993

Merged
tisnik merged 3 commits into
lightspeed-core:mainfrom
max-svistunov:lcore-4765-compaction-context-window-from-catalog
Oct 23, 2026
Merged

tisnik merged 3 commits into
lightspeed-core:mainfrom
max-svistunov:lcore-4765-compaction-context-window-from-catalog

Conversation

@max-svistunov

Copy link
Copy Markdown
Contributor

Description

Conversation compaction now resolves a model's context window in src/utils/context_window.py, taking the first of: metadata.context_window on the model's inference.providers[].allowed_models entry, the inference.context_windows map, the window genai-prices lists for the provider and model, and 128,000 tokens (DEFAULT_CONTEXT_WINDOW). The map comes before genai-prices so a configured window beats the vendor maximum.

  • inference.context_windows is deprecated, not removed or rejected: it still works and, when set, logs a DEPRECATED warning at startup. The project deprecates a configuration key for at least one minor release before removing it, and the map shipped in 0.6 and 0.7. Removing it is left to a follow-up once the deprecation has shipped.
  • Migration: move each <provider id>/<model name>: N key to metadata.context_window: N on that model's allowed_models entry. The new docs/migrations/v0.8.0.md and the "Context window" section of the compaction user doc say how, and when to keep the map for now: for a provider not yet declared under inference.providers (adding an entry changes the generated OGX configuration) and for legacy two-file configurations.
  • The live compaction path (src/utils/conversation_compaction.py) uses the new resolver, and get_context_window is gone from token_estimator.py. Every configuration in the repository that sets the map keeps the same windows.
  • Behaviour change, only with compaction enabled: a model with no configured window used to never compact; it now compacts once the conversation passes threshold_ratio of its genai-prices window, or of 128K when genai-prices does not know it.
  • OGX synthesis now accepts structured allowed_models entries (name/provider_model_id/metadata). Before, it failed with TypeError: unhashable type: 'dict', which blocked the migration target in unified mode.
  • genai-prices>=0.1.12 is now a direct dependency. It was already locked through pydantic-ai-slim and pinned for Konflux, so nothing new is installed.
  • Left for later: reading the catalog tier from the provider registry once it is initialised (LCORE-4710; get_catalog_context_window is the switch point), the pydantic-ai compaction path (LCORE-4764), and removing the map.
  • Size: +791/-181. Source +301/-96, tests +335/-55, docs and READMEs +134/-12, generated schema docs +17/-18, pyproject.toml and uv.lock +4.

Type of change

  • Refactor
  • New feature
  • Bug fix
  • CVE fix
  • Optimization
  • Documentation Update
  • Configuration Update
  • Bump-up service version
  • Bump-up dependent library [pyproject.toml + uv.lock]
  • Bump-up dependent library [requirements.*.txt for Konflux]
  • Bump-up library or tool used for development (does not change the final image)
  • CI configuration change
  • Konflux configuration change
  • Unit tests improvement
  • Integration tests improvement
  • End to end tests improvement
  • Benchmarks improvement

Tools used to create PR

Identify any AI code assistants used in this PR (for transparency and review context)

  • Assisted-by: Claude Opus 4.8
  • Generated by: Claude Opus 4.8

Related Tickets & Documents

  • Related Issue # LCORE-4691
  • Closes # LCORE-4765

Checklist before requesting a review

  • I have performed a self-review of my code.
  • PR has passed all pre-merge test jobs.
  • If it is a core feature, I have added thorough tests.

Testing

  1. Run the tests of the touched code:
    uv run pytest tests/unit/utils/test_context_window.py tests/unit/utils/test_conversation_compaction.py tests/unit/utils/test_token_estimator.py tests/unit/test_lightspeed_stack.py tests/unit/test_ogx_synthesize.py: 182 passed.
    uv run pytest tests/integration/endpoints/test_compaction_*.py: 40 passed; the helper now sets the window as metadata.context_window on a vLLM provider entry.
  2. Run the full suites and compare with the base:
    • uv run pytest tests/unit: 3868 passed, 1 skipped. The branch adds 27 test cases and removes 5 in the touched files.
    • uv run pytest tests/integration --ignore=tests/integration/container_lifecycle: 326 passed.
  3. Run uv run make format (no changes) and the linters: black, ruff, pydocstyle, pylint and pyright pass. mypy reports the same errors as on the base (1 in src, 49 in tests) and nothing new.
  4. Load the four e2e compaction configurations (tests/e2e/configuration/{server,library}-mode/lightspeed-stack-compaction{,-disabled}.yaml) with AppConfig.load_configuration on the base and on this branch, and resolve each window (get_context_window on the base, resolve_context_window here): all five map keys give 2000 on both, and unlisted models (for example vllm/unknown-model) go from no window to 128000. The documentation snippets, including the migration guide's before/after pair, resolve to the same windows. With an empty configuration, openai/gpt-4.1 gets 1000000 from genai-prices and vllm/granite-3.3-8b-instruct gets the 128000 default.
  5. Run uv run python src/lightspeed_stack.py -c tests/e2e/configuration/server-mode/lightspeed-stack-compaction.yaml -d with E2E_OGX_HOSTNAME and FAISS_VECTOR_STORE_ID set: it exits 0, writes configuration.json to the current directory and logs the DEPRECATED: inference.context_windows ... warning.
  6. Run synthesis (apply_high_level_inference) on a vllm provider whose allowed_models has a dictionary entry: on the base it raises TypeError: unhashable type: 'dict'; here the OGX allow-list gets the provider_model_id and the model is registered under its name.

Not run: the e2e compaction feature and a live library-mode start with a structured allowed_models entry; both need the full stack.

The compaction trigger took a model's context window only from the
inference.context_windows map. A model missing from the map never
compacted, and the per-model setting the provider catalog already
validates, allowed_models[].metadata.context_window, was not used.

The new utils.context_window.resolve_context_window returns, in order:
metadata.context_window on the model's allowed_models entry (matched by
name, then by provider_model_id), the inference.context_windows map,
the model data bundled with genai-prices when it knows the provider and
the model, and DEFAULT_CONTEXT_WINDOW (128000). The configured tiers
come first, so a window an operator sets wins over the vendor maximum.
The result is always an int, so conversation_compaction no longer has a
branch for a model without a window: with compaction enabled, such a
model now compacts against the 128000 default instead of never.

get_context_window is removed from token_estimator. genai-prices is
declared as a direct dependency; it was already installed through
pydantic-ai-slim. The integration helper sets the window as catalog
metadata. The e2e fixtures keep the map and the same windows; only a
comment and a step docstring change.

Unit tests cover every tier, the name and provider_model_id matching,
the split of the model id at the first "/", and the default.
An allowed_models entry written as a mapping (name, provider_model_id,
metadata), which is where metadata.context_window lives, made unified
OGX synthesis fail with "TypeError: unhashable type: 'dict'" in
_register_high_level_models. Plain string entries worked.

Synthesis now reads both forms. The OGX provider's allowed_models lists
the backend ids (provider_model_id), which is what OGX filters on, and
each model is registered under its name with that provider_model_id.

A parametrized synthesis test covers a string entry, a mapping entry
and an aliased mapping entry.
…metadata

The per-model context window now belongs on the model's allowed_models
entry, as metadata.context_window. The top-level
inference.context_windows map shipped in releases, so it is deprecated
rather than removed: it keeps working as a fallback tier through the
deprecation phase.

The configuration schema describes the field as deprecated, and the
service logs one DEPRECATED warning at startup when the map is not
empty, with a link to the user documentation. The compaction user guide
gains a "Context window" section, docs/migrations/v0.8.0.md explains how
to move each entry and when to keep the map for now, and the generated
configuration and OpenAPI documents carry the new descriptions.

A unit test covers the startup warning: logged for a non-empty map, not
logged for an empty or missing one.
@coderabbitai

coderabbitai Bot commented Oct 23, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 56 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

⚙️ Run configuration
📥 Commits

Reviewing files that changed from the base of the PR and between 216019f and cb3cf08.

⛔ Files ignored due to path filters: 1
  • uv.lock is excluded by !**/*.lock
📒 Files selected for processing: 32
  • docs/devel_doc/ARCHITECTURE.md
  • docs/devel_doc/openapi.json
  • docs/devel_doc/openapi.md
  • docs/migrations/index.md
  • docs/migrations/v0.8.0.md
  • docs/models/successful_responses.json
  • docs/models/successful_responses.md
  • docs/user_doc/config.html
  • docs/user_doc/config.json
  • docs/user_doc/config.md
  • docs/user_doc/conversation_compaction.md
  • pyproject.toml
  • src/constants.py
  • src/lightspeed_stack.py
  • src/models/config.py
  • src/ogx_configuration.py
  • src/utils/README.md
  • src/utils/context_window.py
  • src/utils/conversation_compaction.py
  • src/utils/token_estimator.py
  • tests/e2e/configuration/library-mode/lightspeed-stack-compaction-disabled.yaml
  • tests/e2e/configuration/library-mode/lightspeed-stack-compaction.yaml
  • tests/e2e/configuration/server-mode/lightspeed-stack-compaction-disabled.yaml
  • tests/e2e/configuration/server-mode/lightspeed-stack-compaction.yaml
  • tests/e2e/features/steps/conversation_compaction.py
  • tests/integration/endpoints/_compaction_helpers.py
  • tests/unit/test_lightspeed_stack.py
  • tests/unit/test_ogx_synthesize.py
  • tests/unit/utils/README.md
  • tests/unit/utils/test_context_window.py
  • tests/unit/utils/test_conversation_compaction.py
  • tests/unit/utils/test_token_estimator.py
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@tisnik tisnik left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@tisnik
tisnik merged commit 409ccff into lightspeed-core:main Oct 23, 2026
42 checks passed
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