affectUI › Components › Agents

Context Compaction

An agent's context window filling up and being compacted. A bar on top is the window – every entry a slice as wide as its tokens, in the color of its kind, with a dashed mark where it compacts on its own; under it the entries in order: system prompt, tools, what you wrote, what the agent said, tool results, files. While it compacts, the entries that go shimmer, then fold into one summary row – the rows slide up into it, their slices squeeze together and shrink to the summary's size, and the numbers drop with them. Point at a row or a slice and the other lights up; Compact now does it by hand.

Part of affectUI Pro. Install: npx shadcn@latest add https://www.affectui.com/r/context-compaction.json · Import from @/components/ui/context-compaction

When to use

  • Showing what fills an agent's context during a long run – and what happens when it compacts
  • Making auto-compaction visible: which entries went into the summary and how much it saved
  • A side panel next to an agent chat or a coding agent's log

When not to use

  • What fills the context by category, with files and tools to remove – use Context Window
  • How a run's tokens and cost add up step by step – use Cost Waterfall
  • What an agent remembers across runs – use Memory Panel

Usage

import { ContextCompaction, type ContextEntry } from "@/components/ui/context-compaction";

const entries: ContextEntry[] = [
  { id: "sys", kind: "system", label: "System prompt", tokens: 3200 },
  { id: "tools", kind: "tools", label: "14 tool definitions", tokens: 9800 },
  { id: "u1", kind: "user", label: "The cart total is off by a cent", tokens: 400 },
  { id: "f1", kind: "file", label: "src/cart/cart.ts", tokens: 18400 },
  { id: "t1", kind: "tool", label: "run_tests · 48 tests", tokens: 26300 },
];

export function Context() {
  return <ContextCompaction title="Fix the cart total" model="Claude Sonnet" entries={entries} limit={200_000} />;
}

Props

NameTypeDefaultDescription
entriesContextEntry[]What sits in the window, in order – { id, kind, label, tokens, replaces?, before?, count?, apps?, total? }. kind is system, tools, user, assistant, tool, file or summary. apps – { name, path?, icon?, color?, tile? } – shows up to three overlapping logo tiles and +N for the rest of total; path is a one-color logo on a 24 grid, icon any node (a multi-color logo, a glyph)
limitnumberThe window's size in tokens
thresholdnumberShare of the limit where it compacts on its own – the dashed mark; from there the percentage turns amber. Default 0.8
compactingstring[]Ids of the entries being compacted right now – they shimmer, and the bar says Compacting
titlestringThe name in the bar – default Context
modelstringA small tag beside the title, e.g. the model
onCompact() => voidShows the last row with the colors and a Compact now button
rowsnumberRows the list shows before it scrolls – default 7
classNamestringExtra class on the root – set the color variables on it, e.g. --ui-cpx-k-file

Keyboard

  • Tab: One stop for the list, then Compact now
  • ↑ / ↓: The entry before or after – its slice lights up, a summary or a row with apps shows its card
  • Enter / Space: Keeps a row's card open; Esc closes it
  • Home / End: The first or the newest entry
  • Space / Enter: Compact now, on the button

Accessibility

  • The bar is a meter – "148K of 200K tokens"
  • Every entry reads its kind, what it is and its tokens – a summary also what it was before
  • When a summary has folded its entries, a polite live region says how many and from how much to how much
  • A row with apps or a summary opens a card on hover, focus or click – every app it brings in, or the entries the summary holds with their sizes
  • Focus rings show for the keyboard only (:focus-visible)

Responsive

  • The bar and the rows fit the width; labels shorten with an ellipsis
  • Narrow: the colors in the last row and the before-numbers on summaries go
  • Dark mode built in: switches when an ancestor (usually <html>) has the class dark or data-theme="dark"

Requirements

  • React 19

Related

  • Context Window – What fills the model's context, as a field of cells – hover a part to light it up, remove files, and compact the conversation while its cells shimmer and clear
  • Cost Waterfall – Where the money and tokens of an agent run go – every LLM call, tool call and sub-agent as a step in a waterfall, with model, tokens, cost and time, the most expensive step on top and a switch between cost and tokens
  • Memory Panel – What the assistant remembers about you, grouped and editable – a master switch, category filters, and on every memory edit in place, pause or forget with Undo on the tray
  • Agent Timing – An agent run as a timing diagram – time runs left to right, you and every agent a row with its own signal line. Idle it rests on the baseline, thinking it steps up a little, calling a tool all the way up with the tool's name under the line, waiting on you a dashed amber step – one line per row, rounded where it steps and gliding to each new level, a soft fill under it fading down, so steps that touch flow into each other. The step still running ends in a softly pulsing dot on the now line, and beside every name stands what it does right now. Hand-offs are dashed lines straight to the other row, drawn in as they happen, with a small white dot where they land. Point at a step or a hand-off for what it was, when and how long