Skip to content

BYOK models cannot declare true context window: auto-compact fires at ~180k on a 1M-context model #68

Description

@gordonrust

Problem

When running NCode in BYOK mode against an OpenAI-compatible endpoint whose model has a 1M context window (e.g. OPENAI_BASE_URL=https://api.kimi.com/coding/v1, OPENAI_MODEL=k3 — the endpoint self-reports context_length: 1048576 via /models), auto-compaction triggers at roughly 167k–179k tokens, compacting ~5x earlier than the model requires.

Root cause

getContextWindowForModel() falls back to MODEL_CONTEXT_WINDOW_DEFAULT = 200_000 for any model string that is not Noumena-managed, has no [1m] suffix, and has no capability entry (src/utils/context.ts:12,54-112). The auto-compact threshold derives entirely from this local assumption:

  • effective window = window − min(maxOutputTokens, 20_000) summary reserve (src/services/compact/autoCompact.ts:51-56)
  • threshold = effective − 13_000 buffer (autoCompact.ts:70,84)

The server-advertised context length is never consulted.

What users can do today

  • OPENAI_MODEL="k3[1m]" works as a client-side opt-in (context.ts:84-86) and the suffix is stripped before the API call (openAICompatInferenceClient.ts:240) — but this is undocumented for BYOK users and abuses a suffix meant for Anthropic 1M opt-in.
  • CLAUDE_CODE_MAX_CONTEXT_TOKENS would be the natural knob, but it is gated behind isInternalBuild() (context.ts:62-70), so external builds ignore it.
  • CLAUDE_CODE_AUTO_COMPACT_WINDOW is a min() cap only — it can lower, never raise (autoCompact.ts:39-45).

Suggested fixes (any of)

  1. Ungate CLAUDE_CODE_MAX_CONTEXT_TOKENS for external builds, or
  2. Read context_length from the OpenAI-compat /models response and use it as the window for BYOK models, or
  3. Document [1m] as the supported way to declare large-context BYOK models.

Environment

  • external build, BYOK via OPENAI_API_KEY + OPENAI_BASE_URL
  • model k3, server-reported context 1,048,576; locally assumed 200,000

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions