feat(tui): show OpenCode Console usage limits (#58731)

This commit is contained in:
Adam 2026-10-24 11:19:56 -05:00 • committed by GitHub
parent e42f6d1016
commit 5e4aa55cda
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 948 additions and 30 deletions

View file

@ -34,6 +34,7 @@ import type {
SessionInboxCompaction,
ShellInfo,
SkillInfo,
UsageLimitsInfo,
VcsInfo,
OpenCodeEvent,
OpenCodeClient,
@ -103,6 +104,9 @@ type LocationData = {
provider?: ProviderInfo[]
reference?: ReferenceInfo[]
websearch?: WebSearchProvider[]
// The Go allowance and Console budgets of the OpenCode Console workspace the location is signed in to; null when it
// isn't signed in.
usageLimits?: UsageLimitsInfo | null
// Currently running shell commands for this location, keyed by shell id. Entries are removed
// once the command exits or is deleted, so this only ever holds in-flight shells.
shell?: Record<string, ShellWithLocation>
@ -942,6 +946,12 @@ export function createData(config: CreateDataInput) {
return
}
case "session.step.failed":
// A budget refusal means the cached usage limits are out of date, which is what the refusal sends people to.
if (
event.location &&
(event.data.error.type === "provider.budget-limit" || event.data.error.type === "provider.model-budget-limit")
)
refreshUsageLimits(event.location)
message.editAssistant(event.data.sessionID, event.data.assistantMessageID, (assistant) => {
assistant.time.completed = event.created
assistant.finish = event.data.finish ?? "error"
@ -1227,6 +1237,7 @@ export function createData(config: CreateDataInput) {
Object.keys(store.location).forEach((key) => {
const ref = JSON.parse(key) as [string, string | null]
const location = { directory: ref[0], workspaceID: ref[1] ?? undefined }
refreshUsageLimits(location)
if (event.type === "credential.updated") {
result.location.integration.invalidate(location)
refresh(() => result.location.integration.sync(location))
@ -1311,6 +1322,7 @@ export function createData(config: CreateDataInput) {
refresh(() => result.location.reference.sync(location))
break
case "integration.updated":
refreshUsageLimits(location)
result.location.integration.invalidate(location)
result.location.model.invalidate(location)
result.location.provider.invalidate(location)
@ -1348,6 +1360,15 @@ export function createData(config: CreateDataInput) {
// A cached per-location catalog. `sync` loads once per invalidation, keyed by the
// effective location, and publishes under the server's canonical location; `alias`
// also publishes under the requested key when the two differ.
// Reloads usage limits only where something has read them, since each load asks OpenCode Console.
function refreshUsageLimits(location: LocationRef) {
const loaded =
store.location[locationKey(location)]?.usageLimits !== undefined ||
sync.has(`location.usageLimits:${locationKey(location)}`)
usageLimits.invalidate(location)
if (loaded) refresh(() => usageLimits.sync(location))
}
function locationResource<Field extends keyof LocationData>(
field: Field,
load: (location: ReturnType<typeof locationQuery>) => Promise<{ location: LocationRef; data: LocationData[Field] }>,
@ -1371,6 +1392,7 @@ export function createData(config: CreateDataInput) {
}
const vcs = locationResource("vcs", (location) => api().vcs.get({ location }))
const usageLimits = locationResource("usageLimits", (location) => api().usage.limits({ location }))
const shells = locationResource("shell", async (location) => {
const response = await api().shell.list({ location })
const ref = { directory: response.location.directory }
@ -1982,6 +2004,7 @@ export function createData(config: CreateDataInput) {
result.session.form.invalidate("global", location)
},
vcs: { info: vcs.list, sync: vcs.sync, invalidate: vcs.invalidate },
usage: { limits: usageLimits.list, sync: usageLimits.sync, invalidate: usageLimits.invalidate },
agent: locationResource("agent", (location) => api().agent.list({ location })),
command: locationResource("command", (location) => api().command.list({ location })),
config: locationResource("config", async (location) => ({

View file

@ -65,6 +65,7 @@ import { PermissionProvider } from "./context/permission"
import { DialogModel } from "./component/dialog-model"
import { useConnected } from "./component/use-connected"
import { DialogMcp } from "./component/dialog-mcp"
import { DialogUsage } from "./component/usage"
import { DialogStatus } from "./component/dialog-status"
import { DialogConfig } from "./component/dialog-config"
import { DialogDebug } from "./component/dialog-debug"
@ -155,6 +156,7 @@ const appBindingCommands = [
"model.cycle_favorite_reverse",
"agent.list",
"mcp.list",
"usage.show",
"agent.cycle",
"agent.cycle.reverse",
"variant.cycle",
@ -950,6 +952,15 @@ function App() {
dialog.replace(() => <DialogAgent />)
},
},
{
name: "usage.show",
title: "View usage",
category: "Agent",
slash: { name: "usage" },
run: () => {
dialog.replace(() => <DialogUsage />)
},
},
{
name: "mcp.list",
title: "MCP servers",

View file

@ -1,5 +1,5 @@
import type { ModelInfo } from "@opencode/client"
import { createMemo, createSignal, Show } from "solid-js"
import { createMemo, createSignal, onMount, Show } from "solid-js"
import { useLocal } from "../context/local"
import { DialogSelect } from "../ui/dialog-select"
import { useDialog } from "../ui/dialog"
@ -13,6 +13,8 @@ import { useLocation } from "../context/location"
import { useNewDesigns } from "../config"
import { DialogFreeModels } from "./dialog-model-free"
import { FavoriteMarker, ModelDetail, isFreeModel, prioritizeFavorites, sortModelOptions } from "./model-options"
import { DialogUsage, useUsageLimits } from "./usage"
import { formatDollars, usageLevel } from "../util/usage-limits"
export function DialogModel(props: { providerID?: string }) {
const local = useLocal()
@ -60,6 +62,19 @@ function DialogConnectedModels(props: {
const newDesigns = useNewDesigns()
const data = useData()
const location = useLocation()
const dialog = useDialog()
const limits = useUsageLimits()
// Spend moves with every request, so the picker shows fresh limits.
onMount(() => limits.load(true))
const usage = (model: ModelInfo) => {
const value = limits.usage(model)
if (value) {
const level = usageLevel(value, model)
return { text: `${formatDollars(value.left)} left`, danger: level === "out" || level === "reached" }
}
if (limits.unlimited(model)) return { text: "No limit" }
return undefined
}
const [query, setQuery] = createSignal("")
// Under new designs, favoriting does not reorder the list until the dialog is reopened.
const favoritesAtOpen = [...local.model.favorite()]
@ -105,7 +120,7 @@ function DialogConnectedModels(props: {
description: provider?.name ?? model.providerID,
category,
footer: trailing(model, isFavorite(item)),
detail: newDesigns() ? () => <ModelDetail model={model} /> : undefined,
detail: newDesigns() ? () => <ModelDetail model={model} usage={usage(model)} /> : undefined,
onSelect: () => {
props.onSelect(model.providerID, model.id)
},
@ -141,7 +156,7 @@ function DialogConnectedModels(props: {
category: connected() ? (freeGroup(model) ? "Free" : (provider?.name ?? model.providerID)) : undefined,
footer: trailing(model, favorite),
free: isFreeModel(model),
detail: newDesigns() ? () => <ModelDetail model={model} /> : undefined,
detail: newDesigns() ? () => <ModelDetail model={model} usage={usage(model)} /> : undefined,
onSelect() {
props.onSelect(model.providerID, model.id)
},
@ -212,6 +227,13 @@ function DialogConnectedModels(props: {
selection: "none",
onTrigger: props.onConnect,
},
{
command: "model.dialog.usage",
title: newDesigns() ? "usage" : "Usage",
selection: "none",
hidden: !limits.info(),
onTrigger: () => dialog.replace(() => <DialogUsage />),
},
{
command: "model.dialog.favorite",
title: newDesigns() ? (option) => (isFavorite(option?.value) ? "unfavorite" : "favorite") : "Favorite",

View file

@ -45,8 +45,15 @@ export function FavoriteMarker() {
return <span style={{ fg: theme.text.formfield.selected }}>*</span>
}
/** Price, input modalities, context window and reasoning support for the selected model. */
export function ModelDetail(props: { model: ModelInfo; price?: boolean }) {
/**
* Price, input modalities, context window and reasoning support for the selected model, after what its usage limits
* leave when `usage` says so.
*/
export function ModelDetail(props: {
model: ModelInfo
price?: boolean
usage?: { readonly text: string; readonly danger?: boolean }
}) {
const theme = useTheme().surface("dialog")
const price = () => {
if (isFreeModel(props.model)) return "Free"
@ -60,24 +67,41 @@ export function ModelDetail(props: { model: ModelInfo; price?: boolean }) {
tokens(props.model.limit.context),
...(props.model.variants.length > 0 ? ["Reasoning"] : []),
].filter(Boolean)
const list = (items: string[]) => (
<text fg={theme.text.faint} wrapMode="none" flexShrink={0}>
<For each={items}>
{(fact, index) => (
<>
{index() > 0 && <span style={{ fg: theme.hue.neutral[400] }}> · </span>}
{fact}
</>
)}
</For>
</text>
)
return (
<box flexDirection="row" justifyContent={props.price === false ? "flex-start" : "space-between"} gap={2}>
<Show when={props.price !== false}>
<text fg={theme.text.faint} wrapMode="none">
{price()}
</text>
</Show>
<text fg={theme.text.faint} wrapMode="none" flexShrink={0}>
<For each={facts()}>
{(fact, index) => (
<>
{index() > 0 && <span style={{ fg: theme.hue.neutral[400] }}> · </span>}
{fact}
</>
)}
</For>
</text>
</box>
<Show
when={props.usage}
fallback={
<box flexDirection="row" justifyContent={props.price === false ? "flex-start" : "space-between"} gap={2}>
<Show when={props.price !== false}>
<text fg={theme.text.faint} wrapMode="none">
{price()}
</text>
</Show>
{list(facts())}
</box>
}
>
{(usage) => (
<box flexDirection="row" justifyContent="space-between" gap={2}>
<text fg={usage().danger ? theme.text.feedback.error.base : theme.text.muted} wrapMode="none">
{usage().text}
</text>
{list([...(price() && price() !== "Free" ? [`${price()} per 1M`] : [price()]), ...facts()].filter(Boolean))}
</box>
)}
</Show>
)
}

View file

@ -70,6 +70,7 @@ import { directoryRecentValue } from "../../prompt/directory-completion"
import { useWorkingDirectoryActions } from "../../ui/working-directory-actions"
import { truncateFilePath } from "../../ui/file-path"
import { PromptMetadataRow } from "./metadata"
import { limitLabel, useUsageLimits } from "../usage"
export type PromptProps = {
sessionID?: string
@ -1570,6 +1571,20 @@ export function Prompt(props: PromptProps) {
variant: undefined,
},
)
// What the workspace's limits leave for the selected model, once it is running low. Only OpenCode Console limits
// models, so only a model it serves loads them.
const limits = useUsageLimits()
createEffect(() => {
const selected = local.model.current()
if (selected && limits.served(selected.providerID) && limits.info() === undefined) limits.load()
})
const limit = createMemo(() => {
const selected = local.model.current()
const model = limits
.models()
.find((item) => item.providerID === selected?.providerID && item.id === selected?.modelID)
return model ? limitLabel(limits.usage(model), model) : undefined
})
const highlight = createMemo(() => {
if (muted()) return theme.border.base
if (store.mode === "shell") return theme.text.action.primary.selected
@ -1845,6 +1860,7 @@ export function Prompt(props: PromptProps) {
model={promptDisplay().modelLabel}
provider={promptDisplay().providerLabel}
variant={promptDisplay().variant}
limit={limit()}
muted={!!muted()}
highlight={highlight()}
agentAlpha={agentMetaAlpha()}

View file

@ -12,6 +12,8 @@ export function PromptMetadataRow(props: {
model: string
provider: string
variant?: string
/** What the workspace's limits leave for the model, once it is running low. */
limit?: { readonly text: string; readonly danger: boolean }
muted: boolean
highlight: RGBA
agentAlpha: number
@ -30,6 +32,7 @@ export function PromptMetadataRow(props: {
model: props.model,
provider: props.provider,
variant: props.variant,
limit: props.limit?.text,
})
})
@ -79,6 +82,18 @@ export function PromptMetadataRow(props: {
</>
)}
</Show>
<Show when={layout().limit ? props.limit : undefined}>
{(limit) => (
<text
flexShrink={0}
marginLeft={1}
wrapMode="none"
fg={fade(limit().danger ? theme.text.feedback.error.base : theme.text.muted, props.modelAlpha)}
>
{limit().text}
</text>
)}
</Show>
</box>
</Show>
</Show>
@ -96,6 +111,7 @@ type Layout = {
model: string
provider?: string
variant?: string
limit?: string
}
function promptMetadataLayout(input: {
@ -106,25 +122,28 @@ function promptMetadataLayout(input: {
model: string
provider: string
variant?: string
limit?: string
}) {
const agent = input.terminalWidth < 44 ? undefined : input.agent
const provider = input.terminalWidth < 44 ? "" : input.provider
const limit = input.limit
const candidates: Layout[] = [
{ agent, auto: input.auto, model: input.model, provider, variant: input.variant },
{ agent, model: input.model, provider, variant: input.variant },
{ agent, auto: input.auto, model: input.model, provider, variant: input.variant, limit },
{ agent, model: input.model, provider, variant: input.variant, limit },
{
agent,
model: input.model,
provider: provider.split(" / ").at(-1) ?? provider,
variant: input.variant,
limit,
},
{ agent, model: input.model, variant: input.variant },
{ agent, model: input.model, variant: input.variant, limit },
]
const fit = candidates.find((candidate) => stringWidth(text(candidate)) <= input.width)
if (fit) return fit
const prefix = agent ? `${agent} · ` : ""
const suffix = input.variant ? ` · ${input.variant}` : ""
const suffix = `${input.variant ? ` · ${input.variant}` : ""}${limit ? ` ${limit}` : ""}`
return {
agent,
model: Locale.truncateWidth(
@ -132,6 +151,7 @@ function promptMetadataLayout(input: {
Math.max(9, input.width - stringWidth(prefix) - stringWidth(suffix)),
).replace(/\s+…$/, "…"),
variant: input.variant,
limit,
}
}
@ -142,5 +162,6 @@ function text(input: Layout) {
...(input.model ? [...(input.agent ? ["·"] : []), input.model] : []),
...(input.provider ? [input.provider] : []),
...(input.variant ? ["·", input.variant] : []),
...(input.limit ? [` ${input.limit}`] : []),
].join(" ")
}

View file

@ -0,0 +1,304 @@
import type { ModelInfo, SessionMessageAssistant } from "@opencode/client"
import { TextAttributes } from "@opentui/core"
import { createEffect, createMemo, createSignal, For, Match, onCleanup, onMount, Show, Switch } from "solid-js"
import { useData } from "../context/data"
import { Keymap } from "../context/keymap"
import { useLocal } from "../context/local"
import { useLocation } from "../context/location"
import { useTheme } from "../context/theme"
import { SplitBorder } from "../ui/border"
import { DialogHeader, DialogLayout, useDialog } from "../ui/dialog"
import { DialogSelect } from "../ui/dialog-select"
import { useToast } from "../ui/toast"
import {
formatDollars,
formatReset,
limitScope,
modelUsage,
refusalReset,
servedByConsole,
usageLevel,
type ModelUsage,
} from "../util/usage-limits"
/**
* The OpenCode Console limits on this location's models. Limits load once; `refresh` reloads them, since spend moves
* with every request, and keeps showing the ones already loaded meanwhile.
*/
export function useUsageLimits() {
const data = useData()
const location = useLocation()
const ref = () => location.ref ?? data.location.default()
const providers = createMemo(() => new Map((data.location.provider.list(ref()) ?? []).map((item) => [item.id, item])))
const info = () => data.location.usage.limits(ref())
const usage = (model: ModelInfo) => {
const value = info()
return value ? modelUsage(value, model, providers().get(model.providerID)) : undefined
}
return {
/** Undefined until loaded, and null when the location isn't signed in to OpenCode Console. */
info,
load: (refresh = false) => {
if (refresh) data.location.usage.invalidate(ref())
void data.location.usage.sync(ref()).catch(() => undefined)
},
models: () => data.location.model.list(ref()) ?? [],
usage,
served: (providerID: string) => servedByConsole(providers().get(providerID)),
/** A paid model OpenCode Console serves without any limit on it. */
unlimited: (model: ModelInfo) =>
Boolean(info()) &&
servedByConsole(providers().get(model.providerID)) &&
!model.cost.every((cost) => cost.input === 0) &&
usage(model) === undefined,
}
}
/** How a model's limits read where they are short on space: nothing while there's plenty left. */
export function limitLabel(usage: ModelUsage | undefined, model: ModelInfo) {
if (!usage) return undefined
const level = usageLevel(usage, model)
if (level === "plenty") return undefined
if (level === "reached") return { text: "Limit reached", danger: true }
return { text: `${formatDollars(usage.left)} left`, danger: level === "out" }
}
/** `/usage`: the models the workspace limits, with what each has left. */
export function DialogUsage() {
const limits = useUsageLimits()
const dialog = useDialog()
const theme = useTheme().surface("dialog")
onMount(() => limits.load(true))
const key = (model: ModelInfo) => `${model.providerID}/${model.id}`
const rows = createMemo(() =>
limits
.models()
.flatMap((model) => {
const usage = limits.usage(model)
return usage ? [{ model, usage, level: usageLevel(usage, model) }] : []
})
.toSorted((a, b) => a.model.name.localeCompare(b.model.name)),
)
// Spend shared between models would look counted twice on every row, so only the selected one shows it.
const [active, setActive] = createSignal<string>()
const selected = () => active() ?? (rows()[0] ? key(rows()[0].model) : undefined)
const resets = () => Math.min(...rows().map((row) => row.usage.resetsAt))
return (
<Switch>
<Match when={limits.info()}>
{(info) => (
<DialogSelect
title="Usage"
description={[
info().workspace.name ? `Workspace: ${info().workspace.name}` : undefined,
rows().length > 0 ? `Resets ${formatReset(resets())}` : undefined,
]
.filter(Boolean)
.join(" · ")}
placeholder="Search models"
options={rows().map((row) => ({
title: row.model.name,
value: key(row.model),
footer: selected() === key(row.model) ? `${formatDollars(row.usage.left)} left` : undefined,
footerColor: row.level === "out" || row.level === "reached" ? theme.text.feedback.error.base : undefined,
onSelect: () => dialog.replace(() => <DialogUsageModel model={row.model} />),
}))}
onMove={(option) => setActive(option.value)}
emptyView={
<text fg={theme.text.muted} wrapMode="word">
No limits apply to your models. Paid models stop only if the workspace runs out of credits.
</text>
}
/>
)}
</Match>
<Match when={limits.info() === null}>
<DialogLayout header={<DialogHeader title="Usage" />}>
<text fg={theme.text.muted} wrapMode="word">
Sign in to OpenCode Console with /connect to see your workspace's usage limits.
</text>
</DialogLayout>
</Match>
<Match when={true}>
<DialogLayout header={<DialogHeader title="Usage" />}>
<text fg={theme.text.muted}>Loading…</text>
</DialogLayout>
</Match>
</Switch>
)
}
/** Every limit on one model, the one setting what's left marked. */
export function DialogUsageModel(props: { model: ModelInfo }) {
const limits = useUsageLimits()
const theme = useTheme().surface("dialog")
const usage = () => limits.usage(props.model)
const short = () => {
const value = usage()
if (!value) return false
const level = usageLevel(value, props.model)
return level === "out" || level === "reached"
}
const workspace = () => limits.info()?.workspace.name
return (
<DialogLayout
header={
<DialogHeader
titleView={
<text fg={theme.text.base}>
<span style={{ fg: theme.text.muted }}>Usage › </span>
{props.model.name}
</text>
}
/>
}
>
<Show when={usage()} fallback={<text fg={theme.text.muted}>No limits apply to {props.model.name}.</text>}>
{(value) => (
<box gap={1}>
<box>
<text fg={short() ? theme.text.feedback.error.base : theme.text.base} attributes={TextAttributes.BOLD}>
{formatDollars(value().left)} remaining
</text>
<text fg={theme.text.muted}>Resets {formatReset(value().resetsAt)}</text>
</box>
<box>
<text fg={theme.text.muted}>Limits</text>
<For each={value().limits}>
{(item, index) => (
<box>
<text fg={theme.text.base}>
<span
style={{
fg:
index() > 0
? theme.text.muted
: value().reached
? theme.text.feedback.error.base
: theme.text.feedback.warning.base,
}}
>
{index() === 0 ? "•" : "-"}{" "}
</span>
{formatDollars(item.spent)} used of {formatDollars(item.budget.limit)}
</text>
<box paddingLeft={2}>
<text fg={theme.text.muted} wrapMode="word">
{limitScope(item.budget, workspace())}
</text>
</box>
</box>
)}
</For>
</box>
</box>
)}
</Show>
</DialogLayout>
)
}
// Each warning shows once per model while OpenCode runs, however many sessions use the model.
const warned = new Set<string>()
/** Reloads limits as the session's turns end, and warns once as the selected model runs low and once at its limit. */
export function useUsageAlerts(sessionID: () => string) {
const data = useData()
const local = useLocal()
const toast = useToast()
const keymap = Keymap.use()
const limits = useUsageLimits()
onCleanup(
data.on("session.execution.succeeded", (event) => {
if (event.data.sessionID === sessionID() && limits.info()) limits.load(true)
}),
)
createEffect(() => {
const selected = local.model.current()
const model = limits
.models()
.find((item) => item.providerID === selected?.providerID && item.id === selected?.modelID)
const usage = model && limits.usage(model)
if (!model || !usage) return
const level = usageLevel(usage, model)
if (level === "plenty") return
const key = `${model.providerID}/${model.id}:${level === "reached" ? "reached" : "low"}`
if (warned.has(key)) return
warned.add(key)
if (level === "reached") {
toast.show({
title: `${model.name} limit reached`,
message: `Resets ${formatReset(usage.resetsAt)}`,
variant: "error",
action: { label: "Switch model", run: () => keymap.dispatch("model.list") },
})
return
}
toast.show({
title: `Running low on ${model.name}`,
message: `${formatDollars(usage.left)} left · resets ${formatReset(usage.resetsAt)}`,
variant: "warning",
action: { label: "View usage", run: () => keymap.dispatch("usage.show") },
})
})
}
/** Why a turn stopped at a usage limit or for lack of credits, and how to keep going; nothing for other errors. */
export function useLimitNotice(message: () => SessionMessageAssistant) {
const limits = useUsageLimits()
const shortcut = Keymap.useShortcut("model.list")
return createMemo(() => {
const value = message()
const error = value.error
if (!error) return
const reset = refusalReset(error)
const resets = reset === undefined ? "" : `Resets ${formatReset(reset)}. `
const name =
limits.models().find((item) => item.providerID === value.model.providerID && item.id === value.model.id)?.name ??
value.model.id
if (error.type === "provider.model-budget-limit")
return { title: `${name} usage limit reached`, body: `${resets}Switch model with`, key: shortcut() ?? "/models" }
if (error.type === "provider.budget-limit")
return {
title: "Usage limit reached",
body: `${resets}Paid models are paused. Switch to a free model with`,
key: shortcut() ?? "/models",
}
if (error.status === 402 && limits.served(value.model.providerID))
return {
title: `${limits.info()?.workspace.name ?? "Your workspace"} is out of credits`,
body: "Paid models are paused until an admin adds credits in OpenCode Console.",
}
return undefined
})
}
export function UsageLimitNotice(props: { notice: { title: string; body: string; key?: string } }) {
const theme = useTheme()
return (
<box
marginLeft={2}
border={["left"]}
customBorderChars={SplitBorder.customBorderChars}
borderColor={theme.border.base}
paddingLeft={1}
paddingTop={1}
paddingBottom={1}
>
<text fg={theme.text.base}>{props.notice.title}</text>
<text fg={theme.text.muted} wrapMode="word">
{props.notice.body}
<Show when={props.notice.key}>
{(key) => (
<>
{" "}
<span style={{ fg: theme.text.base }}>{key()}</span> to continue.
</>
)}
</Show>
</text>
</box>
)
}

View file

@ -163,12 +163,14 @@ export const Definitions = {
"stash.delete": keybind("ctrl+d", "Delete stash entry"),
"model.dialog.provider": keybind("ctrl+a", "Open provider list from model dialog"),
"model.dialog.favorite": keybind("ctrl+f", "Toggle model favorite status"),
"model.dialog.usage": keybind("ctrl+u", "Open usage limits from model dialog"),
"model.list": keybind("<leader>m", "List available models"),
"model.cycle_recent": keybind("f2", "Next recently used model"),
"model.cycle_recent_reverse": keybind("shift+f2", "Previous recently used model"),
"model.cycle_favorite": keybind("none", "Next favorite model"),
"model.cycle_favorite_reverse": keybind("none", "Previous favorite model"),
"mcp.list": keybind("none", "List MCP servers"),
"usage.show": keybind("none", "View usage limits"),
"provider.connect": keybind("none", "Connect integration"),
"agent.list": keybind("<leader>a", "List agents"),
"agent.cycle": keybind("shift+tab", "Next agent"),

View file

@ -0,0 +1,47 @@
import { Plugin } from "@opencode/plugin/tui"
import { createMemo, Show } from "solid-js"
import { useUsageLimits } from "../../component/usage"
import { useLocal } from "../../context/local"
import { formatDollars, usageLevel } from "../../util/usage-limits"
/** What the workspace's limits leave for the selected model; hidden when no limit applies to it. */
export function SidebarLimit(props: { context: Plugin.Context }) {
const theme = props.context.theme
const local = useLocal()
const limits = useUsageLimits()
const state = createMemo(() => {
const selected = local.model.current()
const model = limits
.models()
.find((item) => item.providerID === selected?.providerID && item.id === selected?.modelID)
const usage = model && limits.usage(model)
if (!model || !usage) return
const level = usageLevel(usage, model)
return { left: usage.left, danger: level === "out" || level === "reached" }
})
return (
<Show when={state()}>
{(value) => (
<box>
<text fg={theme.text.base}>
<b>Limit</b>
</text>
<text fg={value().danger ? theme.text.feedback.error.base : theme.text.muted}>
{formatDollars(value().left)} remaining
</text>
</box>
)}
</Show>
)
}
export default Plugin.define({
id: "opencode.sidebar.limit",
setup(context) {
context.ui.slot({
append: "sidebar.content",
render: () => <SidebarLimit context={context} />,
})
},
})

View file

@ -3,6 +3,7 @@ import PromptBtw from "../feature-plugins/prompt/btw"
import PromptFooter from "../feature-plugins/prompt/footer"
import SidebarContext from "../feature-plugins/sidebar/context"
import SidebarFooter from "../feature-plugins/sidebar/footer"
import SidebarLimit from "../feature-plugins/sidebar/limit"
import SidebarMcp from "../feature-plugins/sidebar/mcp"
import SidebarTodo from "../feature-plugins/sidebar/todo"
import DiffViewer from "../feature-plugins/system/diff-viewer"
@ -18,6 +19,7 @@ export const builtins = [
PromptFooter,
PromptBtw,
SidebarContext,
SidebarLimit,
SidebarMcp,
SidebarTodo,
SidebarFooter,

View file

@ -71,6 +71,7 @@ import { DialogMessage } from "./dialog-message"
import { DialogFork } from "./dialog-fork"
import { DialogTimeline } from "./dialog-timeline"
import { DialogGoUpsell } from "../../component/dialog-go-upsell"
import { UsageLimitNotice, useLimitNotice, useUsageAlerts } from "../../component/usage"
import { useStorage } from "../../context/storage"
import { Composer } from "./composer"
import { filetype } from "../../util/filetype"
@ -635,6 +636,7 @@ export function Session(props: {
{ initial: {} },
)
const handledFreeLimits = new Set<string>()
useUsageAlerts(() => route.sessionID)
let sessionDisposed = false
onCleanup(() => {
sessionDisposed = true
@ -2027,19 +2029,22 @@ function AssistantFooter(props: { message: SessionMessageAssistant }) {
)
const interrupted = createMemo(() => props.message.error?.message === "Step interrupted")
const freeLimit = createMemo(() => props.message.error?.type === "provider.free-limit")
const limitNotice = useLimitNotice(() => props.message)
return (
<>
<Show when={props.message.error && !interrupted() && !props.message.retry}>
<Show
when={freeLimit()}
<Switch
fallback={
<box paddingLeft={3}>
<text fg={theme.text.feedback.error.base}>Error: {errorMessage(props.message.error)}</text>
</box>
}
>
<FreeLimitNotice message={props.message} />
</Show>
<Match when={freeLimit()}>
<FreeLimitNotice message={props.message} />
</Match>
<Match when={limitNotice()}>{(notice) => <UsageLimitNotice notice={notice()} />}</Match>
</Switch>
</Show>
<AssistantRetry retry={props.message.retry} />
<box paddingLeft={3} marginTop={props.message.retry || (props.message.error && !interrupted()) ? 1 : 0}>

View file

@ -0,0 +1,133 @@
import type { UsageLimitsBudget, UsageLimitsInfo } from "@opencode/client"
/** A model as the usage limits read it: its OpenCode IDs, its price and context window, and its provider. */
export type LimitedModel = {
readonly id: string
readonly providerID: string
readonly cost: ReadonlyArray<{ readonly input: number }>
readonly limit: { readonly context: number }
}
export type LimitedProvider = { readonly id: string; readonly integrationID?: string }
/** One limit on a model: what has been spent against it and what is left. */
export type ModelLimit = {
readonly budget: UsageLimitsBudget
readonly spent: number
readonly left: number
}
/** What a model has left: the limit closest to running out sets it, since any limit that runs out stops the model. */
export type ModelUsage = {
readonly left: number
readonly reached: boolean
readonly resetsAt: number
/** Every limit on the model, the one that sets `left` first. */
readonly limits: readonly ModelLimit[]
}
/**
* `low` means what's left won't cover three requests that fill the model's context, `out` that it won't cover one. Both
* depend on the model: a few dollars is plenty for a cheap model and almost nothing for an expensive one.
*/
export type UsageLevel = "plenty" | "low" | "out" | "reached"
/** OpenCode Console serves the model, so the workspace's limits can apply; OpenCode Go bills its own allowance. */
export function servedByConsole(provider: LimitedProvider | undefined) {
return provider?.integrationID === "opencode" && provider.id !== "opencode-go"
}
/** The limits on a paid model, or nothing when none applies; free models stay usable whatever is spent. */
export function modelUsage(
info: UsageLimitsInfo,
model: LimitedModel,
provider: LimitedProvider | undefined,
): ModelUsage | undefined {
if (model.cost.length > 0 && model.cost.every((cost) => cost.input === 0)) return undefined
const limits = info.budgets
.flatMap((budget): ModelLimit[] => {
if (budget.coverage === "all") return servedByConsole(provider) ? [limit(budget, budget.spent)] : []
const entry = budget.models.find((item) => item.providerID === model.providerID && item.modelID === model.id)
if (!entry) return []
return [limit(budget, budget.per === "model" ? (entry.spent ?? 0) : budget.spent)]
})
.toSorted((a, b) => a.left - b.left)
const binding = limits[0]
if (!binding) return undefined
return {
left: binding.left,
reached: limits.some((item) => item.spent >= item.budget.limit),
resetsAt: Math.min(...limits.map((item) => item.budget.resetsAt)),
limits,
}
}
export function usageLevel(usage: ModelUsage, model: LimitedModel): UsageLevel {
if (usage.reached || usage.left <= 0) return "reached"
const request = (model.limit.context / 1_000_000) * (model.cost[0]?.input ?? 0)
if (usage.left < request) return "out"
if (usage.left < request * 3) return "low"
return "plenty"
}
/** Dollars as limits read: whole amounts without cents, and a sliver as "<$0.01" rather than "$0.00". */
export function formatDollars(value: number) {
if (value > 0 && value < 0.01) return "<$0.01"
return new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD",
minimumFractionDigits: Number.isInteger(value) ? 0 : 2,
maximumFractionDigits: 2,
}).format(value)
}
/** When limits reset: the day, with the time unless it is midnight here, or just the time when that is today. */
export function formatReset(at: number) {
const date = new Date(at)
// ICU puts a narrow no-break space before AM/PM, which terminals may draw at the wrong width.
const time = date.toLocaleTimeString("en-US", { hour: "numeric", minute: "2-digit" }).replace(/\u202f/g, " ")
if (date.toDateString() === new Date().toDateString()) return `${time} today`
const day = date.toLocaleDateString("en-US", { month: "short", day: "numeric" })
return date.getHours() === 0 && date.getMinutes() === 0 ? day : `${day}, ${time}`
}
/** When a refused request's limit resets: OpenCode Console names it `resetsAt` in every protocol's error envelope. */
export function refusalReset(error: { readonly response?: { readonly body: string } } | undefined) {
const at = Date.parse(/"resetsAt"\s*:\s*"([^"]+)"/.exec(error?.response?.body ?? "")?.[1] ?? "")
return Number.isNaN(at) ? undefined : at
}
/**
* Who spends from a limit and, when it pools spend across models, which ones. A limit on each model separately names
* only who spends from it.
*/
export function limitScope(budget: UsageLimitsBudget, workspace: string | undefined) {
const who =
budget.scope === "each"
? "Only you"
: budget.shared
? `Everyone in ${workspace ?? "the workspace"}`
: "You and others"
if (budget.per === "model") return who
return `${who} · ${limitModels(budget)}`
}
function limitModels(budget: UsageLimitsBudget) {
if (budget.coverage === "all") return "All models"
const vendors = budget.vendors.length > 0 ? list(budget.vendors) : undefined
const price = budget.inputPriceAtLeast === undefined ? undefined : `${formatDollars(budget.inputPriceAtLeast)} per 1M`
if (vendors && price) return `${vendors} models over ${price}`
if (vendors) return `All ${vendors} models`
if (price) return `Models over ${price}`
const names = budget.models.map((model) => model.name)
if (names.length > 3) return `${names.slice(0, 3).join(", ")} +${names.length - 3} more`
return names.join(", ")
}
function list(items: readonly string[]) {
return new Intl.ListFormat("en-US", { style: "long", type: "conjunction" }).format(items)
}
function limit(budget: UsageLimitsBudget, spent: number): ModelLimit {
return { budget, spent, left: Math.max(0, budget.limit - spent) }
}

View file

@ -160,6 +160,11 @@ export function createFetch(override?: FetchHandler, events?: ReturnType<typeof
})
if (url.pathname === "/api/reference")
return json({ location: { directory, project: { id: "proj_test", directory, canonical: directory } }, data: [] })
if (url.pathname === "/api/experimental/usage/limits")
return json({
location: { directory, project: { id: "proj_test", directory, canonical: directory } },
data: null,
})
if (url.pathname === "/api/websearch/provider") {
return json({ location: { directory, project: { id: "proj_test", directory, canonical: directory } }, data: [] })
}

View file

@ -0,0 +1,162 @@
import { test } from "bun:test"
import { createAppFixture } from "./fixture/app"
import { tmpdir } from "./fixture/fixture"
import { directory, json } from "./fixture/tui-client"
const location = { directory, project: { id: "proj_test", directory, canonical: directory } }
const model = (id: string, name: string, input: number) => ({
id,
modelID: id,
providerID: "opencode",
name,
status: "active",
enabled: true,
cost: [{ input, output: input * 5, cache: { read: 0, write: 0 } }],
capabilities: { input: ["text"], output: ["text"], reasoning: false, toolCall: true, attachment: false },
limit: { context: 1_000_000, output: 64_000 },
variants: [],
time: { released: 1 },
})
test("shows what the workspace's limits leave and why a refused turn stopped", async () => {
await using state = await tmpdir()
const session = {
id: "ses_usage_limits",
projectID: "proj_test",
location: { directory },
title: "Usage limits",
agent: "build",
model: { providerID: "opencode", id: "gpt-5.6-sol" },
cost: 0,
tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } },
time: { created: 1, updated: 1 },
}
const resetsAt = "2026-11-01T00:00:00.000Z"
// One request that fills Sol's context costs $4, so the $6 left won't cover three.
const limits = {
workspace: { id: "wrk_1", name: "Acme", usageURL: "https://opencode.ai/wrk_1/activity/overview" },
budgets: [
{
id: "picked",
limit: 50,
spent: 44,
scope: "each",
per: "set",
coverage: "models",
models: [
{ providerID: "opencode", modelID: "gpt-5.6-sol", name: "GPT 5.6 Sol" },
{ providerID: "opencode", modelID: "claude-opus-5", name: "Claude Opus 5" },
],
vendors: [],
picked: true,
shared: false,
resetsAt: Date.parse(resetsAt),
},
],
}
await using setup = await createAppFixture({
width: 110,
height: 36,
state: state.path,
args: { sessionID: session.id },
config: { animations: false, tabs: { mode: "off" }, experimental: { "new-designs": true } },
fetch: (url) => {
if (url.pathname === "/api/session") return json({ data: [session], cursor: {} })
if (url.pathname === `/api/session/${session.id}`) return json({ data: session })
if (url.pathname === `/api/session/${session.id}/message`)
return json({
data: [
{
id: "msg_refused",
type: "assistant",
agent: "build",
model: session.model,
content: [],
finish: "error",
error: {
type: "provider.model-budget-limit",
message: "You've reached the spend limit for this model. Switch models to keep going.",
status: 429,
response: {
body: JSON.stringify({
type: "error",
error: { type: "BudgetLimitError", message: "A budget policy for this model has been reached." },
metadata: { coverage: "models", resetsAt },
}),
},
},
time: { created: 2, completed: 3 },
},
{ id: "msg_user", type: "user", text: "Refactor the parser", time: { created: 1 } },
],
cursor: {},
})
if ([`/api/session/${session.id}/inbox`, `/api/session/${session.id}/permission`].includes(url.pathname))
return json({ data: [] })
if (url.pathname === "/api/agent")
return json({ location, data: [{ id: "build", mode: "primary", hidden: false, permissions: [] }] })
if (url.pathname === "/api/provider")
return json({ location, data: [{ id: "opencode", integrationID: "opencode", name: "OpenCode" }] })
if (url.pathname === "/api/model")
return json({
location,
data: [model("gpt-5.6-sol", "GPT 5.6 Sol", 4), model("claude-opus-5", "Claude Opus 5", 5)],
})
if (url.pathname === "/api/integration")
return json({
location,
data: [
{
id: "opencode",
name: "OpenCode Console",
methods: [],
connections: [{ type: "credential", method: "oauth", id: "cred_console", label: "Acme" }],
},
],
})
if (url.pathname === "/api/experimental/usage/limits") return json({ location, data: limits })
return undefined
},
})
await setup.ready
await setup.waitForFrame((frame) => frame.includes("Refactor the parser"))
// The refused turn says which limit stopped it and how to keep going, and the composer warns as the model runs low.
await setup.waitForFrame(
(frame) =>
frame.includes("GPT 5.6 Sol usage limit reached") &&
frame.includes("Switch model with") &&
/GPT 5\.6 Sol OpenCode +\$6 left/.test(frame) &&
frame.includes("Running low on GPT 5.6 Sol"),
)
await setup.mockInput.typeText("/usage")
setup.mockInput.pressEnter()
// Shared money would look counted twice on every row, so only the selected model shows its amount.
await setup.waitForFrame(
(frame) =>
frame.includes("Workspace: Acme") &&
/Claude Opus 5 +\$6 left/.test(frame) &&
!/GPT 5\.6 Sol +\$6 left/.test(frame),
)
setup.mockInput.pressEnter()
await setup.waitForFrame(
(frame) =>
frame.includes("Usage › Claude Opus 5") &&
frame.includes("$6 remaining") &&
frame.includes("$44 used of $50") &&
frame.includes("Only you · GPT 5.6 Sol, Claude Opus 5"),
)
// The model picker says what's left on the model it highlights, and opens the same usage list.
setup.mockInput.pressEscape()
await setup.waitForFrame((frame) => !frame.includes("Usage › Claude Opus 5"))
await setup.mockInput.typeText("/models")
setup.mockInput.pressEnter()
await setup.waitForFrame(
(frame) =>
frame.includes("Search models") && /\$6 left +\$4\/\$20 per 1M/.test(frame) && frame.includes("usage ctrl+u"),
)
})

View file

@ -0,0 +1,139 @@
import { afterEach, describe, expect, setSystemTime, test } from "bun:test"
import type { UsageLimitsBudget, UsageLimitsInfo } from "@opencode/client"
import {
formatDollars,
formatReset,
limitScope,
modelUsage,
refusalReset,
usageLevel,
} from "../../src/util/usage-limits"
const RESET = Date.UTC(2026, 10, 1)
const budget = (input: Partial<UsageLimitsBudget> & Pick<UsageLimitsBudget, "id">): UsageLimitsBudget => ({
limit: 100,
spent: 0,
scope: "each",
per: "set",
coverage: "all",
models: [],
vendors: [],
picked: false,
shared: false,
resetsAt: RESET,
...input,
})
const info = (...budgets: UsageLimitsBudget[]): UsageLimitsInfo => ({
workspace: { id: "wrk_1", name: "Acme", usageURL: "https://opencode.ai/wrk_1/activity/overview" },
budgets,
})
// One request that fills Sol's context costs $4, so $12 covers three.
const sol = { id: "gpt-5.6-sol", providerID: "opencode", cost: [{ input: 4 }], limit: { context: 1_000_000 } }
const served = { id: "opencode", integrationID: "opencode" }
afterEach(() => setSystemTime())
describe("util.usage-limits", () => {
test("the limit closest to running out sets what a model has left", () => {
const limits = info(
budget({ id: "all", limit: 500, spent: 100 }),
budget({
id: "each",
coverage: "models",
per: "model",
limit: 50,
spent: 50,
models: [
{ providerID: "opencode", modelID: "gpt-5.6-sol", name: "GPT 5.6 Sol", spent: 20 },
{ providerID: "opencode", modelID: "claude-opus-5", name: "Claude Opus 5", spent: 50 },
],
}),
)
expect(modelUsage(limits, sol, served)).toMatchObject({
left: 30,
reached: false,
resetsAt: RESET,
limits: [
{ budget: { id: "each" }, spent: 20 },
{ budget: { id: "all" }, spent: 100 },
],
})
expect(modelUsage(limits, { ...sol, id: "claude-opus-5" }, served)).toMatchObject({ left: 0, reached: true })
// Spend on all models covers what OpenCode Console serves, but not OpenCode Go, a direct provider or a free model.
expect(
modelUsage(limits, { ...sol, providerID: "opencode-go" }, { id: "opencode-go", integrationID: "opencode" }),
).toBeUndefined()
expect(modelUsage(limits, { ...sol, providerID: "openai" }, { id: "openai" })).toBeUndefined()
expect(modelUsage(limits, { ...sol, cost: [{ input: 0 }] }, served)).toBeUndefined()
})
test("warns by how many full requests are left on the model", () => {
const level = (spent: number) => usageLevel(modelUsage(info(budget({ id: "all", spent })), sol, served)!, sol)
expect(level(80)).toBe("plenty")
expect(level(90)).toBe("low")
expect(level(97)).toBe("out")
expect(level(100)).toBe("reached")
})
test("writes dollars and resets the way limits read", () => {
expect([6, 1.2, 0.004, 0, 1240].map(formatDollars)).toEqual(["$6", "$1.20", "<$0.01", "$0", "$1,240"])
setSystemTime(new Date(2026, 9, 24, 9))
expect(formatReset(new Date(2026, 9, 24, 17).getTime())).toBe("5:00 PM today")
expect(formatReset(new Date(2026, 10, 1).getTime())).toBe("Nov 1")
expect(formatReset(new Date(2026, 9, 31, 19).getTime())).toBe("Oct 31, 7:00 PM")
})
test("says who spends from a limit and which models it pools", () => {
expect(limitScope(budget({ id: "a", coverage: "models", vendors: ["OpenAI"] }), "Acme")).toBe(
"Only you · All OpenAI models",
)
expect(limitScope(budget({ id: "b", scope: "combined", shared: true }), "Acme")).toBe(
"Everyone in Acme · All models",
)
expect(limitScope(budget({ id: "c", scope: "combined", coverage: "models", inputPriceAtLeast: 3 }), "Acme")).toBe(
"You and others · Models over $3 per 1M",
)
expect(
limitScope(
budget({
id: "d",
coverage: "models",
picked: true,
models: ["A", "B", "C", "D"].map((name) => ({ providerID: "opencode", modelID: name, name })),
}),
"Acme",
),
).toBe("Only you · A, B, C +1 more")
expect(limitScope(budget({ id: "e", coverage: "models", per: "model", vendors: ["OpenAI"] }), "Acme")).toBe(
"Only you",
)
})
test("reads when a refused request's limit resets from either error envelope", () => {
const at = "2026-11-01T00:00:00.000Z"
expect(
refusalReset({
response: { body: JSON.stringify({ type: "error", error: {}, metadata: { coverage: "all", resetsAt: at } }) },
}),
).toBe(RESET)
expect(
refusalReset({
response: {
body: JSON.stringify({
error: {
code: 429,
details: [{ reason: "BudgetLimitError", metadata: { coverage: "models", resetsAt: at } }],
},
}),
},
}),
).toBe(RESET)
expect(refusalReset({ response: { body: "{}" } })).toBeUndefined()
})
})

View file

@ -238,12 +238,14 @@ The retired `diff.toggle`, `diff.expand`, `diff.expand_all`, `diff.collapse`, an
| `stash.delete` | `ctrl+d` | Delete stash entry |
| `model.dialog.provider` | `ctrl+a` | Open provider list from model dialog |
| `model.dialog.favorite` | `ctrl+f` | Toggle model favorite status |
| `model.dialog.usage` | `ctrl+u` | Open usage limits from model dialog |
| `model.list` | `<leader>m` | List available models |
| `model.cycle_recent` | `f2` | Next recently used model |
| `model.cycle_recent_reverse` | `shift+f2` | Previous recently used model |
| `model.cycle_favorite` | `none` | Next favorite model |
| `model.cycle_favorite_reverse` | `none` | Previous favorite model |
| `mcp.list` | `none` | List MCP servers |
| `usage.show` | `none` | View usage limits |
| `provider.connect` | `none` | Connect integration |
| `agent.list` | `<leader>a` | List agents |
| `agent.cycle` | `shift+tab` | Next agent |