Skip to content

Named OpenAI-compatible provider over-budgets context when request model is provider-qualified #403

Closed

Description

@WarlockSyno

Bug Description

A named OpenAI-compatible provider with an explicit per-model context_window can still over-budget context and send a request larger than the provider's actual runtime context.

This appears related to #352 and #366, but still occurs on v0.31.2 when the runtime request model is provider-qualified.

Environment

  • jcode: v0.31.2 (446be1d3)
  • build time: 2026-06-25 01:41:13 +0000
  • OS: Linux x86_64
  • Provider type: named openai-compatible
  • Backend: llama.cpp OpenAI-compatible server

Config

~/.jcode/config.toml:

[provider]
default_provider = "cachyai-a2000"
default_model = "qwen3.6-35b-a2000-128k"

[providers.cachyai-a2000]
type = "openai-compatible"
base_url = "http://10.15.15.53:8080/v1"
auth = "none"
default_model = "qwen3.6-35b-a2000-128k"
requires_api_key = false
model_catalog = true

[[providers.cachyai-a2000.models]]
id = "qwen3.6-35b-a2000-128k"
context_window = 131072

Evidence

Startup logs show the configured context is recognized under the bare model id:

Context limit cache: qwen3.6-35b-a2000-128k = 131k

jcode provider current --provider-profile cachyai-a2000 reports:

requested_provider    openai-compatible
resolved_provider     cachyai-a2000
selected_model        qwen3.6-35b-a2000-128k

However, the failing chat request reports the model as provider-qualified:

Model: cachyai-a2000:qwen3.6-35b-a2000-128k

The request exceeded the actual served context and was rejected by llama.cpp:

OpenAI-compatible chat request failed
Endpoint: http://10.15.15.53:8080/v1/chat/completions
Model: cachyai-a2000:qwen3.6-35b-a2000-128k
Auth: local endpoint (no auth)
Status: 400 Bad Request
Response: {"error":{"code":400,"message":"request (137486 tokens) exceeds available context size (131072 tokens), try increasing it","type":"exceed_context_size_error","n_prompt_tokens":137486,"n_ctx":131072}}

The llama.cpp /v1/models response exposes both runtime and training context:

{
  "id": "qwen3.6-35b-a2000-128k",
  "meta": {
    "n_ctx": 131072,
    "n_ctx_train": 262144
  }
}

Expected Behavior

jcode should not send a prompt larger than the configured/runtime context.

For named OpenAI-compatible profiles, the explicit model config should apply to both:

qwen3.6-35b-a2000-128k

and the provider-qualified runtime id:

cachyai-a2000:qwen3.6-35b-a2000-128k

Also, when reading llama.cpp-compatible catalog metadata, context budgeting should prefer the runtime context:

meta.n_ctx

over the training/architecture context:

meta.n_ctx_train

Actual Behavior

jcode recognizes/cache-logs the configured context under the bare id:

qwen3.6-35b-a2000-128k = 131k

but later sends a request using the provider-qualified id:

cachyai-a2000:qwen3.6-35b-a2000-128k

and allows a prompt of 137486 tokens against a backend with n_ctx=131072.

Suspected Root Cause

The context-limit lookup path appears to miss the configured context_window when the runtime model id is provider-qualified.

Likely mismatch:

cache/config key: qwen3.6-35b-a2000-128k
runtime lookup key: cachyai-a2000:qwen3.6-35b-a2000-128k

This may cause fallback to catalog/default context metadata. With llama.cpp, meta.n_ctx_train can be larger than the actually served meta.n_ctx, so using n_ctx_train for request budgeting can overrun the server.

Suggested Fix

Normalize provider-qualified model ids during context lookup, or try both full and bare ids:

context_limit_for("cachyai-a2000:qwen3.6-35b-a2000-128k")
    .or_else(|| context_limit_for("qwen3.6-35b-a2000-128k"))

For llama.cpp/OpenAI-compatible model catalogs, prefer runtime context:

configured context_window
  > meta.n_ctx
  > context_length
  > meta.n_ctx_train

n_ctx_train should not be treated as the active served context.

Activity

  1. WarlockSyno commented on Jun 27, 2026

    @WarlockSyno
    Author

    Fix available on my fork: WarlockSyno:jcode:fix/issue-403-named-provider-context-window (commit 92b778e9).

    I traced the over-budgeting to two causes:

    1. fork() dropped the per-model config. MultiProvider::fork() runs on every client/session-recovery/compaction/tool/ambient cycle and rebuilt the OpenRouter arm via new(), which leaves static_context_limits empty — so the configured context_window was lost and budgeting fell back to the catalog/heuristic. Fixed with fork_typed(), which clones the configured provider and preserves static_context_limits (and all config-derived fields).

    2. The catalog parser ignored llama.cpp's meta. llama.cpp nests context under meta: { n_ctx, n_ctx_train }, but the parser only read flat context_length keys (which llama.cpp omits), so it resolved to None and budgeting hit the qwen3 family heuristic (262144) instead of the served runtime context (131072). The parser now prefers meta.n_ctx > flat keys > meta.n_ctx_train.

    (The provider-qualified id (<profile>:<model>) turned out not to be the cause — it's a routing token that strip_session_profile_prefix reduces to the bare id before any context lookup.)

    Couldn't open a PR directly here (permission-restricted on this repo), but the branch is reviewable/compareable at: master...WarlockSyno:jcode:fix/issue-403-named-provider-context-window

  2. added
    bug
    priority: medium
    triage: reproducible
    autonomous: likely
    recurring-theme
    triage: fixed-pending-release
    and removed
    triage: reproducible
    on Jun 28, 2026
  3. 1jehuang commented on Jun 28, 2026

    @1jehuang
    Owner

    Released in v0.32.0. Run jcode update to get it.

  4. removed
    triage: fixed-pending-release
    on Jun 28, 2026
  5. PwrBank commented on Jun 29, 2026

    @PwrBank

    Having this issue as well on v0.32.0

    Here is my config.toml

    [provider]
    default_provider = "ornith-box-1"
    default_model = "/opt/models/ornith-1.0-35b-Q4_K_M.gguf"
    context_window = 131072
    
    [providers.ornith-box-1]
    type = "openai-compatible"
    base_url = "http://10.10.4.127:8080/v1"
    auth = "none"
    default_model = "/opt/models/ornith-1.0-35b-Q4_K_M.gguf"
    requires_api_key = false
    
    [[providers.ornith-box-1.models]]
    id = "/opt/models/ornith-1.0-35b-Q4_K_M.gguf"
    context_window = 131072
    
    [providers.ornith-box-2]
    type = "openai-compatible"
    base_url = "http://10.10.5.72:8080/v1"
    auth = "none"
    default_model = "/opt/models/ornith-1.0-35b-Q4_K_M.gguf"
    requires_api_key = false
    
    [[providers.ornith-box-2.models]]
    id = "/opt/models/ornith-1.0-35b-Q4_K_M.gguf"
    context_window = 131072

    Shows a context window of 200K even though I've set it to 131K everywhere. Seems to be pulling it out of the training context value from the model itself.

  6. PwrBank commented on Jun 29, 2026

    @PwrBank

    Also hitting the over available context size issue...

    Let me rewrite the conftest to use a proper async wrapper around synchronous SQLite, avoiding aiosqlite’s global connection cache entirely.
      ✓ write src/backend/tests/conftest.py (+70 -69) · 1.7k tok
    ┌─ diff
    │ +"""Test configuration with fresh SQLite database per test."""
    │ +import asyncio
    │ -from sqlalchemy import text
    │ -from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker
    │ -from sqlalchemy.pool import StaticPool
    │ +from contextlib import asynccontextmanager
    │ ... 127 more changes ...
    │ + # Clean up the temp file
    │ + if os.path.exists(db_path):
    │ + try:
    │ + os.remove(db_path)
    │ + except OSError:
    │ + pass
    └─ (+70 -69 total)
    
    OpenAI-compatible chat request failed
      endpoint: http://10.10.4.127:8080/v1/chat/completions
      model: ornith-box-1:/opt/models/ornith-1.0-35b-Q4_K_M.gguf
      auth: local endpoint (no auth)
      status: 400 Bad Request
      response: {"error":{"code":400,"message":"request (131263 tokens) exceeds the available context size (131072 tokens), try increasing it","type":"exceed_context_size_error","n_prompt_tokens":131263,"n_ctx":131072}}
    Hint: check network connectivity, DNS/TLS, that the base URL includes the API version (usually /v1), and that the model exists on the provider.
    

    It hit that around 90K context.

  7. added a commit that references this issue on Oct 4, 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: likelybugpriority: mediumrecurring-theme

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions