Skip to content

Commit 49f0011

Browse files
fix(tui): use effective input limit for context %
For models with a separate input limit (e.g. gpt-5.3-codex: 400k context, 272k input), the context % divided by limit.context while compaction uses limit.input (session/overflow.ts usable()), so the % was too low. Add contextLimit() that prefers limit.input when set, and use it in the prompt footer, sidebar, subagent footer, and run limits map. Closes anomalyco#51251 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 6df0d5d commit 49f0011

6 files changed

Lines changed: 48 additions & 5 deletions

File tree

‎packages/opencode/src/cli/cmd/run/runtime.boot.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ const layer = Layer.effect(
112112
const limits = Object.fromEntries(
113113
providers.flatMap((provider) =>
114114
Object.entries(provider.models ?? {}).flatMap(([modelID, info]) => {
115-
const limit = info?.limit?.context
115+
// Use limit.input when available (for split-window models), otherwise limit.context
116+
const limit = (info?.limit?.input && info.limit.input > 0) ? info.limit.input : info?.limit?.context
116117
if (typeof limit !== "number" || limit <= 0) {
117118
return []
118119
}

‎packages/tui/src/component/prompt/index.tsx‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import { type AutocompleteRef, Autocomplete } from "./autocomplete"
3939
import { useRenderer, useTerminalDimensions, type JSX } from "@opentui/solid"
4040
import type { AssistantMessage, FilePart, UserMessage } from "@opencode-ai/sdk/v2"
4141
import { Locale } from "../../util/locale"
42+
import { contextLimit } from "../../util/model"
4243
import { errorMessage } from "../../util/error"
4344
import { formatDuration } from "../../util/format"
4445
import { createColors, createFrames } from "../../ui/spinner"
@@ -273,7 +274,8 @@ export function Prompt(props: PromptProps) {
273274
if (tokens <= 0) return
274275

275276
const model = sync.data.provider.find((item) => item.id === last.providerID)?.models[last.modelID]
276-
const pct = model?.limit.context ? `${Math.round((tokens / model.limit.context) * 100)}%` : undefined
277+
const limit = contextLimit(model)
278+
const pct = limit ? `${Math.round((tokens / limit) * 100)}%` : undefined
277279
const cost = session?.cost ?? 0
278280
return {
279281
context: pct ? `${Locale.number(tokens)} (${pct})` : Locale.number(tokens),

‎packages/tui/src/feature-plugins/sidebar/context.tsx‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { AssistantMessage } from "@opencode-ai/sdk/v2"
22
import type { TuiPlugin, TuiPluginApi } from "@opencode-ai/plugin/tui"
33
import type { BuiltinTuiPlugin } from "../builtins"
44
import { createMemo } from "solid-js"
5+
import { contextLimit } from "../../util/model"
56

67
const id = "internal:sidebar-context"
78

@@ -28,9 +29,10 @@ function View(props: { api: TuiPluginApi; session_id: string }) {
2829
const tokens =
2930
last.tokens.input + last.tokens.output + last.tokens.reasoning + last.tokens.cache.read + last.tokens.cache.write
3031
const model = props.api.state.provider.find((item) => item.id === last.providerID)?.models[last.modelID]
32+
const limit = contextLimit(model)
3133
return {
3234
tokens,
33-
percent: model?.limit.context ? Math.round((tokens / model.limit.context) * 100) : null,
35+
percent: limit ? Math.round((tokens / limit) * 100) : null,
3436
}
3537
})
3638

‎packages/tui/src/routes/session/subagent-footer.tsx‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { useTheme } from "../../context/theme"
55
import { SplitBorder } from "../../ui/border"
66
import type { AssistantMessage } from "@opencode-ai/sdk/v2"
77
import { Locale } from "../../util/locale"
8+
import { contextLimit } from "../../util/model"
89
import { useTerminalDimensions } from "@opentui/solid"
910
import { useCommandShortcut, useOpencodeKeymap } from "../../keymap"
1011

@@ -40,7 +41,8 @@ export function SubagentFooter() {
4041
if (tokens <= 0) return
4142

4243
const model = sync.data.provider.find((item) => item.id === last.providerID)?.models[last.modelID]
43-
const pct = model?.limit.context ? `${Math.round((tokens / model.limit.context) * 100)}%` : undefined
44+
const limit = contextLimit(model)
45+
const pct = limit ? `${Math.round((tokens / limit) * 100)}%` : undefined
4446
const cost = session()?.cost ?? 0
4547

4648
const money = new Intl.NumberFormat("en-US", {

‎packages/tui/src/util/model.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,15 @@ export function name(
2626
) {
2727
return get(list, providerID, modelID)?.name ?? modelID
2828
}
29+
30+
/**
31+
* Returns the effective context limit for display purposes.
32+
* For split-window models (where input and output windows are separate),
33+
* this returns limit.input when available, otherwise limit.context.
34+
* This matches the actual prompt ceiling enforced by session/overflow.ts usable().
35+
*/
36+
export function contextLimit(model: { limit: { context: number; input?: number } } | undefined): number | undefined {
37+
if (!model) return undefined
38+
if (model.limit.input && model.limit.input > 0) return model.limit.input
39+
return model.limit.context || undefined
40+
}
Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,33 @@
11
import { describe, expect, test } from "bun:test"
2-
import { parse } from "../../src/util/model"
2+
import { parse, contextLimit } from "../../src/util/model"
33

44
describe("util.model", () => {
55
test("splits provider from a nested model identifier", () => {
66
expect(parse("provider/org/model")).toEqual({ providerID: "provider", modelID: "org/model" })
77
expect(parse("invalid")).toEqual({ providerID: "invalid", modelID: "" })
88
})
9+
10+
test("contextLimit returns input limit when available", () => {
11+
const model = { limit: { context: 400_000, input: 272_000, output: 128_000 } }
12+
expect(contextLimit(model)).toBe(272_000)
13+
})
14+
15+
test("contextLimit falls back to context limit when input is 0", () => {
16+
const model = { limit: { context: 200_000, input: 0, output: 32_000 } }
17+
expect(contextLimit(model)).toBe(200_000)
18+
})
19+
20+
test("contextLimit falls back to context limit when input is undefined", () => {
21+
const model = { limit: { context: 1_000_000, output: 128_000 } }
22+
expect(contextLimit(model)).toBe(1_000_000)
23+
})
24+
25+
test("contextLimit returns undefined for undefined model", () => {
26+
expect(contextLimit(undefined)).toBeUndefined()
27+
})
28+
29+
test("contextLimit returns undefined when both limits are 0", () => {
30+
const model = { limit: { context: 0, output: 0 } }
31+
expect(contextLimit(model)).toBeUndefined()
32+
})
933
})

0 commit comments

Comments
 (0)