Skip to content

Commit c78612c

Browse files
t3dotggclaude
andcommitted
feat(web): thread details context meter fills up and has its own toggle
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 96c21aa commit c78612c

5 files changed

Lines changed: 127 additions & 83 deletions

File tree

‎apps/web/src/components/chat/ThreadDetailsPanel.tsx‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ import { ThreadDetailsSection } from "./ThreadDetailsSection";
2323
import { ThreadAutomationsPanel } from "./ThreadAutomationsPanel";
2424
import { ThreadRelationshipsPanel } from "./ThreadRelationshipsControl";
2525
import { ThreadServersPanel } from "./ThreadServersPanel";
26-
import { ThreadDetailsUsage, type ThreadDetailsUsageInput } from "./ThreadDetailsUsage";
26+
import {
27+
ThreadDetailsContext,
28+
ThreadDetailsUsage,
29+
type ThreadDetailsUsageInput,
30+
} from "./ThreadDetailsUsage";
2731
import { ThreadDetailsItemsMenuItems } from "./ThreadDetailsItemsMenu";
2832
import { resolveThreadDetailsVisibility, THREAD_DETAILS_ITEMS } from "./threadDetailsItems";
2933
import { useClientSettings } from "../../hooks/useSettings";
@@ -254,6 +258,10 @@ export function ThreadDetailsPanel(props: ThreadDetailsPanelProps) {
254258

255259
{visible.usage ? <ThreadDetailsUsage {...props.usage} /> : null}
256260

261+
{visible.context && props.usage.contextWindow ? (
262+
<ThreadDetailsContext usage={props.usage.contextWindow} />
263+
) : null}
264+
257265
{/* Shown only when no section above has content, so an emptied card can be refilled. */}
258266
<div className="p-2 group-has-[>section>:last-child:not(:empty)]/details-items:hidden">
259267
<ThreadDetailsControl

‎apps/web/src/components/chat/ThreadDetailsUsage.tsx‎

Lines changed: 100 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { type ContextWindowSnapshot, formatContextWindowTokens } from "../../lib
2020
import { cn } from "../../lib/utils";
2121
import { formatUpcomingTimestamp } from "../../timestampFormat";
2222
import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
23+
import { formatContextWindowCompactionMessage } from "./ContextWindowMeter.logic";
2324
import { ThreadDetailsSection } from "./ThreadDetailsSection";
2425

2526
const PACE_LABEL: Record<LimitPace, string> = {
@@ -35,16 +36,14 @@ export interface ThreadDetailsUsageInput {
3536
readonly contextWindow: ContextWindowSnapshot | null;
3637
}
3738

38-
/**
39-
* The selected provider's limit windows and the thread's context window.
40-
* Every row reads as room left, so a longer bar always means more headroom.
41-
*/
39+
type UsageTone = "error" | "warning" | null;
40+
41+
/** The selected provider's limit windows. Each row reads as room left. */
4242
export function ThreadDetailsUsage({
4343
providerInstanceId,
4444
providers,
4545
sources,
46-
contextWindow,
47-
}: ThreadDetailsUsageInput) {
46+
}: Omit<ThreadDetailsUsageInput, "contextWindow">) {
4847
const nowMinute = useNowMinute();
4948
const now = Date.parse(`${nowMinute}:00Z`);
5049
const report =
@@ -56,28 +55,83 @@ export function ThreadDetailsUsage({
5655
const limits =
5756
report?.accounts.find((entry) => entry.instanceId === providerInstanceId)?.limits ??
5857
providers.find((provider) => provider.instanceId === providerInstanceId)?.usageLimits;
59-
const notice = limits ? limitsNotice(limits) : null;
60-
if (!limits && !contextWindow) return null;
58+
if (!limits) return null;
59+
const notice = limitsNotice(limits);
6160

6261
return (
6362
<ThreadDetailsSection
6463
headingId="thread-details-usage-heading"
65-
title="Usage"
64+
title="Usage limits"
6665
showHeading={false}
6766
>
6867
<div className="grid grid-cols-[auto_minmax(3rem,1fr)_auto] gap-x-3 text-xs">
6968
{notice ? (
7069
<UsageRow
7170
label="Limits"
72-
left={null}
71+
fill={null}
72+
tone={null}
7373
value="Unavailable"
7474
tooltip={<span className="text-foreground">{notice}</span>}
7575
/>
76-
) : null}
77-
{limits && !notice
78-
? limits.windows.map((window) => <LimitRow key={window.id} window={window} now={now} />)
79-
: null}
80-
{contextWindow ? <ContextRow usage={contextWindow} spaced={limits !== undefined} /> : null}
76+
) : (
77+
limits.windows.map((window) => <LimitRow key={window.id} window={window} now={now} />)
78+
)}
79+
</div>
80+
</ThreadDetailsSection>
81+
);
82+
}
83+
84+
/** The thread's context window. The bar fills as context is used; the tick marks auto-compaction. */
85+
export function ThreadDetailsContext({ usage }: { readonly usage: ContextWindowSnapshot }) {
86+
const maxTokens = usage.maxTokens != null && usage.maxTokens > 0 ? usage.maxTokens : null;
87+
const used =
88+
maxTokens !== null && usage.usedPercentage !== null
89+
? Math.round(Math.max(0, Math.min(100, usage.usedPercentage)))
90+
: null;
91+
const threshold = usage.autoCompactThreshold ?? null;
92+
const tick =
93+
maxTokens !== null && threshold !== null && threshold > 0 && threshold < maxTokens
94+
? (threshold / maxTokens) * 100
95+
: null;
96+
const usedTokens = formatContextWindowTokens(usage.usedTokens);
97+
return (
98+
<ThreadDetailsSection
99+
headingId="thread-details-context-heading"
100+
title="Context window"
101+
showHeading={false}
102+
>
103+
<div className="grid grid-cols-[auto_minmax(3rem,1fr)_auto] gap-x-3 text-xs">
104+
<UsageRow
105+
label="Context"
106+
fill={used}
107+
tone={used === null ? null : used >= 90 ? "error" : used >= 80 ? "warning" : null}
108+
tick={tick}
109+
muted
110+
value={
111+
maxTokens !== null
112+
? `${usedTokens} / ${formatContextWindowTokens(maxTokens)}`
113+
: usedTokens
114+
}
115+
ariaValue={used !== null ? `${used}% used` : `${usedTokens} tokens used`}
116+
tooltip={
117+
<>
118+
<span className="text-foreground">
119+
{used !== null ? `Context: ${used}% used` : `Context: ${usedTokens} tokens used`}
120+
</span>
121+
{maxTokens !== null ? (
122+
<span className="text-muted-foreground">
123+
{usage.usedTokens.toLocaleString("en-US")} of {maxTokens.toLocaleString("en-US")}{" "}
124+
tokens
125+
</span>
126+
) : null}
127+
{threshold !== null && threshold > 0 ? (
128+
<span className="text-muted-foreground">
129+
{formatContextWindowCompactionMessage(null, threshold)}
130+
</span>
131+
) : null}
132+
</>
133+
}
134+
/>
81135
</div>
82136
</ThreadDetailsSection>
83137
);
@@ -97,7 +151,10 @@ function LimitRow({
97151
return (
98152
<UsageRow
99153
label={window.label}
100-
left={left}
154+
fill={left}
155+
tone={left <= 10 ? "error" : left <= 20 ? "warning" : null}
156+
value={`${left}%`}
157+
ariaValue={`${left}% left`}
101158
tooltip={
102159
<>
103160
<span className="text-foreground">
@@ -116,76 +173,43 @@ function LimitRow({
116173
);
117174
}
118175

119-
function ContextRow({
120-
usage,
121-
spaced,
122-
}: {
123-
readonly usage: ContextWindowSnapshot;
124-
readonly spaced: boolean;
125-
}) {
126-
const maxTokens = usage.maxTokens ?? null;
127-
const left =
128-
maxTokens !== null && usage.usedPercentage !== null
129-
? Math.round(100 - Math.max(0, Math.min(100, usage.usedPercentage)))
130-
: null;
131-
const used = formatContextWindowTokens(usage.usedTokens);
132-
return (
133-
<UsageRow
134-
label="Context"
135-
left={left}
136-
muted
137-
className={cn(spaced && "mt-2")}
138-
tooltip={
139-
<>
140-
{left !== null ? <span className="text-foreground">Context: {left}% left</span> : null}
141-
<span className={left !== null ? "text-muted-foreground" : "text-foreground"}>
142-
{maxTokens !== null
143-
? `${used} of ${formatContextWindowTokens(maxTokens)} tokens used`
144-
: `${used} tokens used`}
145-
</span>
146-
</>
147-
}
148-
value={left === null ? used : undefined}
149-
/>
150-
);
151-
}
152-
153176
/** One label, hairline, and value row; the whole row opens the details tooltip. */
154177
function UsageRow({
155178
label,
156-
left,
157-
tooltip,
179+
fill,
180+
tone,
181+
tick = null,
158182
value,
183+
ariaValue = value,
184+
tooltip,
159185
muted = false,
160-
className,
161186
}: {
162187
readonly label: string;
163-
readonly left: number | null;
188+
/** Bar fill from 0 to 100. Null hides the bar. */
189+
readonly fill: number | null;
190+
readonly tone: UsageTone;
191+
/** A marker on the bar, from 0 to 100. */
192+
readonly tick?: number | null;
193+
readonly value: string;
194+
readonly ariaValue?: string;
164195
readonly tooltip: ReactNode;
165-
/** Replaces the percentage when there is none. */
166-
readonly value?: string | undefined;
167196
readonly muted?: boolean;
168-
readonly className?: string | undefined;
169197
}) {
170-
const tone = left === null ? null : left <= 10 ? "error" : left <= 20 ? "warning" : null;
171198
return (
172199
<Tooltip>
173200
<TooltipTrigger
174201
render={
175202
<div
176203
tabIndex={0}
177-
aria-label={`${label}: ${value ?? `${left}% left`}`}
178-
className={cn(
179-
"col-span-3 grid h-6 cursor-default grid-cols-subgrid items-center rounded-lg px-2.5 outline-none hover:bg-black/[0.035] focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring data-popup-open:bg-black/[0.035] dark:hover:bg-white/[0.08] dark:data-popup-open:bg-white/[0.08]",
180-
className,
181-
)}
204+
aria-label={`${label}: ${ariaValue}`}
205+
className="col-span-3 grid h-6 cursor-default grid-cols-subgrid items-center rounded-lg px-2.5 outline-none hover:bg-black/[0.035] focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring data-popup-open:bg-black/[0.035] dark:hover:bg-white/[0.08] dark:data-popup-open:bg-white/[0.08]"
182206
/>
183207
}
184208
>
185209
<span className="max-w-28 truncate text-muted-foreground">{label}</span>
186-
{left !== null ? (
210+
{fill !== null ? (
187211
<span className="relative h-[3px] rounded-full bg-muted-foreground/30 dark:bg-muted-foreground/20">
188-
{left > 0 ? (
212+
{fill > 0 ? (
189213
<span
190214
className={cn(
191215
"absolute inset-y-0 left-0 rounded-full",
@@ -197,7 +221,14 @@ function UsageRow({
197221
? "bg-muted-foreground"
198222
: "bg-foreground/80",
199223
)}
200-
style={{ width: `max(${left}%, 6px)` }}
224+
style={{ width: `max(${fill}%, 6px)` }}
225+
/>
226+
) : null}
227+
{tick !== null ? (
228+
<span
229+
aria-hidden
230+
className="absolute -top-[3px] h-[9px] w-px -translate-x-1/2 bg-foreground/60"
231+
style={{ left: `${tick}%` }}
201232
/>
202233
) : null}
203234
</span>
@@ -206,17 +237,17 @@ function UsageRow({
206237
className={cn(
207238
// Text-only rows span the bar column so they never widen the shared value column.
208239
"text-end font-medium tabular-nums",
209-
left === null && "col-span-2",
240+
fill === null && "col-span-2",
210241
tone === "error"
211242
? "text-error-foreground"
212243
: tone === "warning"
213244
? "text-warning-foreground"
214-
: left === null
245+
: fill === null
215246
? "text-muted-foreground"
216247
: "text-foreground/80",
217248
)}
218249
>
219-
{value ?? `${left}%`}
250+
{value}
220251
</span>
221252
</TooltipTrigger>
222253
{/* Rows sit 8px inside the card; clear its edge instead of overlapping it. */}

‎apps/web/src/components/chat/threadDetailsItems.test.ts‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,17 @@ const servers = THREAD_DETAILS_ITEMS.find((item) => item.id === "servers")!;
1111

1212
describe("thread details items", () => {
1313
it.each([
14-
{ legacy: false, items: {}, usage: false },
15-
{ legacy: true, items: {}, usage: true },
16-
{ legacy: true, items: { usage: false }, usage: false },
17-
{ legacy: false, items: { usage: true, retired: false }, usage: true },
18-
])("resolves Usage from overrides and the retired Labs flag: %o", (row) => {
14+
{ legacy: false, items: {}, usage: false, context: false },
15+
{ legacy: true, items: {}, usage: true, context: true },
16+
{ legacy: true, items: { usage: false }, usage: false, context: true },
17+
{ legacy: false, items: { usage: true, retired: false }, usage: true, context: false },
18+
])("resolves Usage and Context from overrides and the retired Labs flag: %o", (row) => {
1919
const visibility = resolveThreadDetailsVisibility({
2020
threadDetailsItems: row.items,
2121
threadDetailsUsageEnabled: row.legacy,
2222
});
2323
expect(visibility.usage).toBe(row.usage);
24+
expect(visibility.context).toBe(row.context);
2425
expect(visibility.servers).toBe(true);
2526
});
2627

‎apps/web/src/components/chat/threadDetailsItems.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ export const THREAD_DETAILS_ITEMS = [
1111
{ id: "servers", label: "Servers", defaultVisible: true },
1212
{ id: "automations", label: "Automations", defaultVisible: true },
1313
{ id: "lineage", label: "Lineage", defaultVisible: true },
14-
{ id: "usage", label: "Usage", defaultVisible: false },
14+
{ id: "usage", label: "Usage limits", defaultVisible: false },
15+
{ id: "context", label: "Context window", defaultVisible: false },
1516
] as const;
1617

1718
export type ThreadDetailsItemId = (typeof THREAD_DETAILS_ITEMS)[number]["id"];
@@ -23,9 +24,11 @@ type ThreadDetailsItemSettings = Pick<
2324
"threadDetailsItems" | "threadDetailsUsageEnabled"
2425
>;
2526

26-
// People who turned on the retired Labs toggle keep Usage until they change it here.
27+
// People who turned on the retired Labs toggle keep Usage and Context until they change them here.
2728
function defaultVisible(item: ThreadDetailsItem, settings: ThreadDetailsItemSettings) {
28-
return item.id === "usage" ? settings.threadDetailsUsageEnabled : item.defaultVisible;
29+
return item.id === "usage" || item.id === "context"
30+
? settings.threadDetailsUsageEnabled
31+
: item.defaultVisible;
2932
}
3033

3134
export function resolveThreadDetailsVisibility(
@@ -53,7 +56,7 @@ export function isDefaultThreadDetailsVisibility(visibility: ThreadDetailsVisibi
5356
return THREAD_DETAILS_ITEMS.every((item) => visibility[item.id] === item.defaultVisible);
5457
}
5558

56-
/** Also clears the retired Labs flag, so Usage returns to off. */
59+
/** Also clears the retired Labs flag, so Usage and Context return to off. */
5760
export const RESET_THREAD_DETAILS_ITEMS = {
5861
threadDetailsItems: {},
5962
threadDetailsUsageEnabled: false,

‎docs/user/usage.md‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,12 @@ current model's limits without leaving the conversation. The result opens above
120120
closes when you dismiss it or send your next message. It uses the same snapshot as **Usage → Limits**, so it does not run the agent or refresh
121121
anything. The command is offered only for providers that appear under **Usage → Limits**.
122122

123-
To keep them in view on web and desktop, turn on **Usage** in **Settings → Appearance → Thread details**, or right-click the thread details card.
123+
To keep them in view on web and desktop, turn on **Usage limits** in **Settings → Appearance → Thread details**, or right-click the thread details card.
124124
The bottom of the thread details card then shows how much is left in each limit window of the
125-
current provider account and in the thread's context window. Some windows, such as a weekly Opus
126-
limit, count only one model. Hover a row to see when it resets, your pace, or
127-
the token count.
125+
current provider account. Some windows, such as a weekly Opus limit, count only one model. Hover a
126+
row to see when it resets and your pace. Turn on **Context window** to also show the thread's
127+
context use against the model's limit. A tick on the bar marks where the provider compacts
128+
automatically, when it reports that point.
128129

129130
OpenCode Go reports its session, weekly, and monthly allowance when OpenCode runs locally in
130131
the environment. T3 cannot report limits for external OpenCode servers because their credentials

0 commit comments

Comments
 (0)