Skip to content

Kimi K3 (k3) resolves to 200K instead of 1M context in TUI meter and compaction budget #577

Closed

Description

@wiktorwiesek

Problem

Kimi Code (api.kimi.com/coding) serves its flagship K3 model under the bare id k3. The live catalog correctly reports context_length = 1048576, and the provider instance's own context_window() (openrouter_provider_impl.rs) already honors it via the models cache / disk cache. But every global resolution path — the TUI context meter (update_context_limit_for_model), compaction budget derivation, model switching — goes through context_limit_for_model_with_provider → CONTEXT_LIMIT_CACHE + static classifiers, which resolve k3 to DEFAULT_CONTEXT_LIMIT (200K).

Symptom: status bar shows 41k/200k while running k3; compaction budgets are derived from a window ~5x too small.

Repro: configure default_provider = "kimi", default_model = "k3" with a valid KIMI_API_KEY, start jcode, look at the context indicator.

Root causes

  1. Direct OpenAI-compatible profile catalogs (kimi_models.json, Moonshot, Z.AI, ...) never seed the global CONTEXT_LIMIT_CACHE — only the Anthropic/OpenAI OAuth catalogs and [providers.*] config (jcode does not adhere to custom context limit #421/Configured context_window is ignored for GPT models in named providers #541) do. The context_length values are read for route building and dropped.
  2. open_weight_family_context_limit only matches ids containing kimi (K2 family, 256K), so the bare k3 id falls through to the 200K default even with no catalog involved.

Workaround (confirmed working)

A [providers.<anything>] config profile pinning context_window = 1048576 for k3 seeds the cache via populate_context_limits_from_config.

Proposed fix (ready patch + tests)

Branch: https://github.com/wiktorwiesek/jcode/tree/fix/kimi-k3-context-window
Compare: master...wiktorwiesek:jcode:fix/kimi-k3-context-window

  • jcode-base: when building routes from a direct profile's catalog disk cache, seed the global context-limit cache from each entry's context_length under the same key shapes as config seeding (bare id + <profile>:<id>). Extracted as a pure, unit-tested helper.
  • jcode-provider-core: add the Kimi K3 family to open_weight_family_context_limit: k3 / kimi-k3 → 1,048,576; k3-256k → 262,144 (checked before the broader K2 kimi match; the dynamic cache still wins when populated).

Tests: new unit tests in both crates; cargo test -p jcode-base --lib provider:: -- --test-threads=1 → 159 passed, 0 failed; cargo test -p jcode-provider-core all pass; clippy clean.

(I could not open a PR — createPullRequest returns FORBIDDEN for my account on this repo, while issues work — so per CONTRIBUTING.md I'm filing this as an issue with the ready patch as reference. Happy to open the PR if access is sorted out.)

Unrelated pre-existing flake noticed while testing

provider::tests::test_available_models_display_uses_route_models_and_filters_placeholder_rows fails under parallel test execution on a machine with a populated ~/.jcode and provider API keys in the environment — on clean master too (passes with --test-threads=1). It reads env-derived global state without the test-env lock.

Activity

  1. added
    bug
    priority: medium
    triage: reproducible
    autonomous: clear
    recurring-theme
    on Jul 24, 2026
  2. 1jehuang commented on Jul 24, 2026

    @1jehuang
    Owner

    Fixed the classifier half of this: open_weight_family_context_limit now maps k3 / kimi-k3 to 1,048,576 and k3-256k to 262,144, checked before the broader K2 kimi match, so context_limit_for_model("k3") resolves to 1M on the global path used by the TUI meter and compaction budget. Regression tests added (cargo test -p jcode-provider-core: 106 passed). The broader catalog-seeding half (direct OpenAI-compatible profiles seeding CONTEXT_LIMIT_CACHE from context_length) is tracked with #578, which is the same recurring shape. Thanks for the thorough writeup and the ready patch.


    — Jcode agent (automated triage), on behalf of @1jehuang

  3. 1jehuang commented on Jul 24, 2026

    @1jehuang
    Owner

    Note: the fix is committed on branch triage/2026-07-24-batch (pushed). GitHub's pull-request creation API is currently returning 500s repo-wide, so the PR link will follow once that clears.


    — Jcode agent (automated triage), on behalf of @1jehuang

  4. 1jehuang commented on Jul 24, 2026

    @1jehuang
    Owner

    GitHub's PR API recovered: the fix is now in #580.


    — Jcode agent (automated triage), on behalf of @1jehuang

  5. added a commit that references this issue on Jul 24, 2026
    53dc1bc
  6. 1jehuang commented on Jul 25, 2026

    @1jehuang
    Owner

    Fixed structurally in 1ce99fa35 rather than by adding another prefix entry.

    All three instances of this bug (#450 Sonnet 5, #577 Kimi K3, #578 Opus 5) shared one shape: a hardcoded prefix list gated context resolution and failed closed at DEFAULT_CONTEXT_LIMIT (200K), so every new model silently reported a ~5x too-small window until someone edited a list.

    What changed in jcode-provider-core:

    • anthropic_context_mode now parses family/version (same approach anthropic_reasoning_caps already used) instead of matching prefixes, and defaults optimistically to Native1M for version 5+ generations.
    • base_is_known_claude_model accepts any versioned claude-* id, so the classifier is consulted rather than skipped for unrecognized ids.
    • New anthropic_context_mode_is_verified separates live-probed knowledge from optimistic guesses, establishing an explicit precedence: verified pin > live catalog / user config > optimistic guess, and never the 200K default. Verified 200K-capped models like claude-sonnet-4-5 still beat the catalog, which over-advertises 1M for them.
    • Kimi: the bare k3 id served by api.kimi.com/coding is now recognized, and K3+ resolves to its real 1M window instead of the K2-era 256K.

    The regression tests assert the failure shape, not today's ids: unreleased generations (claude-opus-6, claude-opus-7-20270101, claude-haiku-5, ...) must never fall back to the default. A future model release should no longer need a code change.

  7. 1 remaining item

  8. added a commit that references this issue on Oct 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    autonomous: clearbugpriority: mediumrecurring-themetriage: reproducible

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions