Kimi OAuth: auto context tier for k3 (k3-256k → k3 wire upgrade) - #416
Kimi OAuth: auto context tier for k3 (k3-256k → k3 wire upgrade)#416Acters wants to merge 2 commits into
Conversation
The Kimi coding endpoint serves K3 as k3-256k (256k context, cheaper) and k3 (1M context, stated to consume 2x the quota — not independently verified). Switching from k3-256k to k3 seamlessly upgrades the prompt cache server-side. 256k is large enough for most tasks, so exposing a single user-facing k3 model and automatically upgrading the wire model ID once the session context passes the 256k cutoff (256k window minus the default compaction reserve) keeps the bulk of sessions on the cheaper variant and avoids compaction on long-horizon tasks. - Add Model.wireModelId; openai-completions sends it on the wire while recording the registry id on assistant messages - New k3-context-tier module derives the effective model (256k tier with wireModelId k3-256k, or 1M tier) from current context tokens - AgentSession applies the tier on model set/cycle/refresh/restore and session creation, and upgrades instead of compacting on both the threshold and overflow paths; upgrade runs even when compaction is disabled since no context reduction is involved - Cutoff is fixed at the default compaction threshold of the 256k window, so a user-lowered compaction threshold compacts first and effectively disables the upgrade - Emit context_window_upgrade session event; interactive mode shows a status line and the footer reflects the new window - OAuth discovery never surfaces k3-256k as a separate selectable model
Implementation PlanAnalysisThe Kimi for Coding OAuth endpoint serves K3 under two wire model IDs: Design decisions:
Deliverables (implemented)
Acceptance criteria
Testing approach
Risks and open questions
Plan created by mach6 |
Code ReviewCriticalNone. Important
Suggestions
Strengths
Agents run: code-reviewer, error-auditor, test-reviewer, completeness-checker, simplifier Reviewed by mach6 |
Review AssessmentAssessed against review comment, verified against the actual code at Classifications
Action Plan
Assessment by mach6 |
- Threshold-path upgrade now fires even when compaction is disabled: the upgrade is model-capability management, not context reduction - Resume re-derives the tier from restored session context instead of always starting in the 256k tier; compacted sessions return to the cheaper 256k wire tier (auto and manual compaction paths) - models.json contextWindow overrides disable automatic tiering instead of being silently replaced - A user-lowered compaction threshold preempts the upgrade even when a single response jumps straight past the cutoff - Extract duplicated last-assistant-message removal into a helper and drop a redundant upgrade guard - Tests: SDK bootstrap/resume, post-compaction tier return, override passthrough, jump precedence, pre-prompt path, non-K3 disabled-overflow regression, interactive status line, exact event payload assertions
Progress UpdateImplemented the genuine findings from the review assessment, scoped per maintainer-delegate review: Behavioral fixes
Cleanups
Tests — 7 new + 3 strengthened: SDK bootstrap/resume, post-compaction tier return, override passthrough, jump precedence, pre-prompt path, non-K3 disabled-overflow regression, interactive status line + footer invalidation, exact event payload/count assertions, disabled-overflow retry assertion (findings 4–7, 12–14) Deferred per assessment: uncancellable retry timer (pre-existing pattern in master's compaction retry), exact-cutoff integration test, mock fidelity, derived-tier thinking test. Verified: biome clean, tsgo clean, full suites pass (coding-agent 2766, ai 326). Commit: Progress tracked by mach6 |
Ready for maintainer publish@aebrer — this PR is complete from the contributor side and ready for your publish decision (version bump, merge, tag, release are all yours). State:
Behavior in one paragraph: the single selectable One pre-existing note: Progress tracked by mach6 |
|
During the making of this PR, I was able to catch the model from going out of scope and overreaching. I further refined my local workflow to introduce tighter contributor role specific restrictions for the LLM to reason over issues without overstepping maintainer/owner boundary and stricter out-of-scope discipline. I was able to complete this PR within the 256k context window. This proves that the 256k context is perfectly acceptable for even medium difficulty tasks and allows users(including me) to get the most out of their kimi code quota. |
Closes #415
The Kimi for Coding OAuth endpoint serves K3 as
k3-256k(256k context, cheaper) andk3(1M context, stated 2× quota), and switching fromk3-256ktok3seamlessly upgrades the prompt cache server-side. This PR exposes a single user-facingk3model and automatically upgrades the wire model ID instead of compacting once session context passes the 256k cutoff (256k window minus the default compaction reserve):Model.wireModelId; the openai-completions provider sends it on the wire while recording the registry ID on assistant messagesk3-context-tier.tsderives the effective model from current context tokens;AgentSessionapplies it on model set/cycle/refresh/restore/creation and upgrades ahead of compaction on both threshold and overflow paths (also when compaction is disabled, since no context reduction is involved)context_window_upgradesession event with interactive-mode status line; footer reflects the effective windowk3-256kas a separate selectable modelScoped to
kimi-coding-oauthonly —k3-256kis exclusive to the Kimi for Coding OAuth endpoint; the pay-per-token Moonshot AI Platform does not expose it.Implementation plan posted as a comment below.