Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

dx(frontend/copilot): dev-only token/context devtool in the composer tray (#14157)

Abhimanyu Yadav8c267c28e2f4
diff --git a/autogpt_platform/frontend/.env.default b/autogpt_platform/frontend/.env.default
index a91e5bfb9f..f6c0d82517 100644
--- a/autogpt_platform/frontend/.env.default
+++ b/autogpt_platform/frontend/.env.default
@@ -55,6 +55,11 @@ NEXT_PUBLIC_LAUNCHDARKLY_CLIENT_ID=687ab1372f497809b131e06e
# Debugging
NEXT_PUBLIC_REACT_QUERY_DEVTOOL=true
+# Copilot token/context devtool badge. Only ever renders on a development build
+# (`pnpm dev`) or the cloud dev deployment — production builds, including the
+# self-hosted single-container image, never show it whatever this is set to.
+# Set false to hide it in dev too; inlined at build time, so it needs a rebuild.
+NEXT_PUBLIC_TOKEN_DEVTOOL=true
NEXT_PUBLIC_GA_MEASUREMENT_ID=G-FH2XK2W4GN
# Google Drive Picker
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/copilotStreamTransport.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/copilotStreamTransport.test.ts
index df8140647a..6b6c092032 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/copilotStreamTransport.test.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/copilotStreamTransport.test.ts
@@ -1,8 +1,14 @@
-import { describe, expect, it, vi } from "vitest";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+
+// Drives the devtool gate. Defaults to off so the bulk of these tests
+// exercise the plain fetch path rather than the usage-capturing wrapper.
+const isDevelopmentBuild = vi.fn(() => false);
vi.mock("@/services/environment", () => ({
environment: {
getAGPTServerBaseUrl: () => "http://test.local",
+ isDevelopmentBuild: () => isDevelopmentBuild(),
+ isDev: () => false,
},
}));
@@ -10,9 +16,25 @@ vi.mock("../helpers", () => ({
getCopilotAuthHeaders: async () => ({ "x-test": "auth" }),
}));
+const createUsageCapturingFetch = vi.fn((_sessionId: string) => vi.fn());
+
+vi.mock("../tokenDevtool/usageTap", () => ({
+ createUsageCapturingFetch: (sessionId: string) =>
+ createUsageCapturingFetch(sessionId),
+}));
+
import { createCopilotTransport } from "../copilotStreamTransport";
import { buildKickoffMessage } from "../expertKickoff";
+beforeEach(() => {
+ isDevelopmentBuild.mockReturnValue(false);
+ createUsageCapturingFetch.mockClear();
+});
+
+afterEach(() => {
+ vi.unstubAllEnvs();
+});
+
const UUID_RE =
/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
@@ -34,6 +56,23 @@ function lastMessage(text: string, metadata?: unknown) {
];
}
+describe("copilotStreamTransport devtool wiring", () => {
+ it("installs the usage-capturing fetch when the devtool gate is on", () => {
+ vi.stubEnv("NEXT_PUBLIC_TOKEN_DEVTOOL", "true");
+ isDevelopmentBuild.mockReturnValue(true);
+
+ createCopilotTransport({ sessionId: "s-1", ...makeRefs() });
+
+ expect(createUsageCapturingFetch).toHaveBeenCalledWith("s-1");
+ });
+
+ it("leaves fetch alone when the devtool gate is off", () => {
+ createCopilotTransport({ sessionId: "s-1", ...makeRefs() });
+
+ expect(createUsageCapturingFetch).not.toHaveBeenCalled();
+ });
+});
+
describe("copilotStreamTransport.prepareSendMessagesRequest", () => {
it("reaches fetch when crypto.randomUUID is unavailable on a LAN HTTP origin", async () => {
const originalCrypto = globalThis.crypto;
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatContainer/ChatContainer.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatContainer/ChatContainer.tsx
index b08daed956..562c9bb28b 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatContainer/ChatContainer.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatContainer/ChatContainer.tsx
@@ -27,6 +27,9 @@ import { ArchivedExpertNotice } from "./components/ArchivedExpertNotice";
import { SharedChatNotice } from "./components/SharedChatNotice";
import { useAutoOpenArtifacts } from "./useAutoOpenArtifacts";
import type { ExpertIdentity } from "../../useExpertMap";
+import { isTokenDevtoolEnabled } from "../../tokenDevtool/gate";
+import { updateHistoryBreakdown } from "../../tokenDevtool/store";
+import { breakdownCacheKey } from "../../tokenDevtool/tokenMath";
import { useAreWorkspaceFileCardsOpen } from "../../useAreWorkspaceFileCardsOpen";
import {
getKickoffAttemptToken,
@@ -196,6 +199,18 @@ export const ChatContainer = ({
return () => ro.disconnect();
}, [isLimitReached]);
+ // Token devtool: estimate the context from loaded history so the badge
+ // shows a value before the first live turn. Guarded by breakdownCacheKey so
+ // it does not run per stream delta — serializing every part is not free.
+ const devtoolBreakdownKeyRef = useRef("");
+ useEffect(() => {
+ if (!sessionId || !isTokenDevtoolEnabled() || messages.length === 0) return;
+ const key = breakdownCacheKey(sessionId, messages, isStreaming);
+ if (key === devtoolBreakdownKeyRef.current) return;
+ devtoolBreakdownKeyRef.current = key;
+ updateHistoryBreakdown(sessionId, messages);
+ }, [sessionId, messages, isStreaming]);
+
// Retry: re-send the last user message (used by ErrorCard on transient errors).
const handleRetry = useCallback(() => {
const lastUserMsg = [...messages].reverse().find((m) => m.role === "user");
@@ -325,6 +340,7 @@ export const ChatContainer = ({
droppedFiles={droppedFiles}
onDroppedFilesConsumed={onDroppedFilesConsumed}
hasSession={!!sessionId}
+ sessionId={sessionId}
/>
</div>
</TooltipTrigger>
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/ChatInput.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/ChatInput.tsx
index 5aff9d38a6..10352717c2 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/ChatInput.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/ChatInput.tsx
@@ -43,6 +43,8 @@ import { RecordingButton } from "./components/RecordingButton";
import { RecordingIndicator } from "./components/RecordingIndicator";
import { WorkspaceFilePicker } from "./components/WorkspaceFilePicker/WorkspaceFilePicker";
import { useCopilotUIStore } from "../../store";
+import { isTokenDevtoolEnabled } from "../../tokenDevtool/gate";
+import { TokenDevtoolBadge } from "../TokenDevtoolBadge/TokenDevtoolBadge";
import { getFilesFromClipboard } from "./helpers";
import { useChatInput } from "./useChatInput";
import { useChatMentions } from "./useChatMentions";
@@ -72,6 +74,8 @@ interface Props {
onDroppedFilesConsumed?: () => void;
/** When true, the dry-run toggle is disabled (session is active and immutable). */
hasSession?: boolean;
+ /** Session id for the dev-only token badge in the tray. */
+ sessionId?: string | null;
/** When true, the submit button is hidden until there is something to send. */
hideSubmitWhenEmpty?: boolean;
/** Recipient picker chip rendered before the mode chips (new-task state). */
@@ -91,6 +95,7 @@ export function ChatInput({
droppedFiles,
onDroppedFilesConsumed,
hasSession = false,
+ sessionId = null,
hideSubmitWhenEmpty = false,
recipientPicker,
}: Props) {
@@ -267,8 +272,12 @@ export function ChatInput({
? "Transcribing..."
: placeholder;
+ // Narrows to string, so neither render site needs to re-test sessionId.
+ const devtoolSessionId = isTokenDevtoolEnabled() ? sessionId : null;
const hasTrayItems =
- (showModeToggle && !isStreaming) || (showDryRunToggle && !hasSession);
+ (showModeToggle && !isStreaming) ||
+ (showDryRunToggle && !hasSession) ||
+ Boolean(devtoolSessionId);
const canSend =
!disabled &&
@@ -396,6 +405,14 @@ export function ChatInput({
onToggle={handleToggleDryRun}
/>
)}
+ {/* ComposerTray renders only under the brain-dump flag, so the
+ badge is duplicated here to stay reachable in both layouts. */}
+ {!isBrainDumpEnabled && devtoolSessionId && (
+ <TokenDevtoolBadge
+ sessionId={devtoolSessionId}
+ className="ml-auto"
+ />
+ )}
</PromptInputTools>
<div className="flex items-center gap-4">
@@ -477,6 +494,12 @@ export function ChatInput({
onToggle={handleToggleDryRun}
/>
)}
+ {devtoolSessionId && (
+ <TokenDevtoolBadge
+ sessionId={devtoolSessionId}
+ className="ml-auto"
+ />
+ )}
</ComposerTray>
)}
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/__tests__/ChatInput.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/__tests__/ChatInput.test.tsx
index 3ab7ae5b18..c190cbb71f 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/__tests__/ChatInput.test.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/__tests__/ChatInput.test.tsx
@@ -92,6 +92,12 @@ vi.mock("@/services/feature-flags/use-get-flag", () => ({
useGetFlag: () => mockFlagValue,
}));
+// Off by default so the rest of the suite sees the production-build behaviour.
+let mockTokenDevtoolEnabled = false;
+vi.mock("../../../tokenDevtool/gate", () => ({
+ isTokenDevtoolEnabled: () => mockTokenDevtoolEnabled,
+}));
+
vi.mock("@/components/molecules/Toast/use-toast", () => ({
toast: vi.fn(),
useToast: () => ({ toast: vi.fn(), dismiss: vi.fn() }),
@@ -255,9 +261,43 @@ afterEach(() => {
mockCopilotLlmModel = "standard";
mockCopilotLlmAuthProvider = "platform";
mockFlagValue = false;
+ mockTokenDevtoolEnabled = false;
mockInitialPrompt = null;
});
+describe("ChatInput token devtool badge", () => {
+ it("renders the badge while the brain-dump tray is disabled", () => {
+ // The tray is brain-dump-only; the badge must not depend on that flag.
+ mockFlagValue = false;
+ mockTokenDevtoolEnabled = true;
+ render(<ChatInput onSend={mockOnSend} sessionId="session-1" />);
+
+ expect(screen.getByRole("button", { name: /Token devtool/ })).toBeDefined();
+ });
+
+ it("renders the badge inside the tray when brain dump is enabled", () => {
+ mockFlagValue = true;
+ mockTokenDevtoolEnabled = true;
+ render(<ChatInput onSend={mockOnSend} sessionId="session-1" />);
+
+ expect(screen.getByRole("button", { name: /Token devtool/ })).toBeDefined();
+ });
+
+ it("stays hidden when the devtool gate is off", () => {
+ mockTokenDevtoolEnabled = false;
+ render(<ChatInput onSend={mockOnSend} sessionId="session-1" />);
+
+ expect(screen.queryByRole("button", { name: /Token devtool/ })).toBeNull();
+ });
+
+ it("stays hidden before a session exists", () => {
+ mockTokenDevtoolEnabled = true;
+ render(<ChatInput onSend={mockOnSend} sessionId={null} />);
+
+ expect(screen.queryByRole("button", { name: /Token devtool/ })).toBeNull();
+ });
+});
+
describe("ChatInput mode toggle", () => {
it("does not render mode toggle when flag is disabled", () => {
mockFlagValue = false;
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/TokenDevtoolBadge/TokenDevtoolBadge.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/TokenDevtoolBadge/TokenDevtoolBadge.tsx
new file mode 100644
index 0000000000..ceacab6c88
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/TokenDevtoolBadge/TokenDevtoolBadge.tsx
@@ -0,0 +1,116 @@
+"use client";
+
+import { Icon } from "@/components/atoms/Icon/Icon";
+import {
+ Popover,
+ PopoverContent,
+ PopoverTrigger,
+} from "@/components/molecules/Popover/Popover";
+import { cn } from "@/lib/utils";
+import { DashboardSpeed02Icon } from "@hugeicons/core-free-icons";
+import { useState } from "react";
+import { useTokenDevtoolStore } from "../../tokenDevtool/store";
+import {
+ AUTOCOMPACT_TOKENS,
+ MODEL_CONTEXT_WINDOW,
+ breakdownTotal,
+ displayContext,
+ formatTokenCount,
+} from "../../tokenDevtool/tokenMath";
+import { BreakdownSection } from "./components/BreakdownSection";
+import { ContextBar } from "./components/ContextBar";
+import { MiniBar } from "./components/MiniBar";
+import { TurnRow } from "./components/TurnRow";
+
+interface Props {
+ sessionId: string;
+ /** Alignment is the tray's concern — the badge renders into two
+ * structurally different parents. */
+ className?: string;
+}
+
+/** Dev-only context readout in the composer tray. The bar tracks the
+ * estimated live context against the model window, with a marker at the
+ * backend's autocompact trigger — cross it and the next turn is expected
+ * to summarize. The popover splits the estimate by source and lists
+ * per-turn usage; ⟲ marks a turn where the stream carried a
+ * `context_compaction` tool call. */
+export function TokenDevtoolBadge({ sessionId, className }: Props) {
+ const turns = useTokenDevtoolStore((s) => s.turnsBySession[sessionId]);
+ const breakdown = useTokenDevtoolStore(
+ (s) => s.breakdownBySession[sessionId],
+ );
+ const liveContext = useTokenDevtoolStore(
+ (s) => s.liveContextBySession[sessionId],
+ );
+ const compacted = useTokenDevtoolStore(
+ (s) => s.compactedBySession[sessionId] ?? false,
+ );
+ const [open, setOpen] = useState(false);
+ const seed = breakdown ? breakdownTotal(breakdown) : undefined;
+ const context = displayContext(liveContext ?? null, compacted, seed);
+ // The rows describe the loaded-history seed, so showing them next to a
+ // headline sourced from the live cache-write sum would read as one total
+ // that does not add up. Only render them while the seed IS the headline.
+ const showBreakdown = breakdown !== undefined && context === seed;
+
+ return (
+ <Popover open={open} onOpenChange={setOpen}>
+ <PopoverTrigger asChild>
+ <button
+ type="button"
+ aria-label={`Token devtool, context ${
+ context === null ? "unknown" : formatTokenCount(context)
+ }`}
+ className={cn(
+ "flex items-center gap-1.5 rounded-full px-2 py-0.5 font-mono text-xs text-zinc-500 transition-colors hover:bg-zinc-200/60 hover:text-zinc-700",
+ className,
+ )}
+ >
+ <Icon icon={DashboardSpeed02Icon} size={14} />
+ {context === null ? "ctx —" : `ctx ~${formatTokenCount(context)}`}
+ {context !== null && <MiniBar context={context} />}
+ </button>
+ </PopoverTrigger>
+
+ <PopoverContent
+ side="top"
+ align="end"
+ sideOffset={8}
+ className="w-80 rounded-2xl border-zinc-200/70 p-3 shadow-[0_16px_40px_-24px_rgba(0,0,0,0.25)]"
+ >
+ <div className="flex items-baseline justify-between pb-2">
+ <span className="text-sm font-medium text-zinc-800">
+ Context window
+ </span>
+ <span className="font-mono text-xs text-zinc-500">
+ {context === null ? "—" : `~${formatTokenCount(context)}`} /{" "}
+ {formatTokenCount(MODEL_CONTEXT_WINDOW)}
+ </span>
+ </div>
+ <ContextBar context={context ?? 0} />
+ <p className="pt-1 font-mono text-[10px] text-zinc-400">
+ assumes a {formatTokenCount(MODEL_CONTEXT_WINDOW)} window; the backend
+ threshold is configurable
+ </p>
+ <p className="pb-2.5 pt-0.5 text-right font-mono text-xs text-amber-500">
+ summarizes ~{formatTokenCount(AUTOCOMPACT_TOKENS)}
+ </p>
+
+ {showBreakdown && <BreakdownSection breakdown={breakdown} />}
+
+ {!turns?.length ? (
+ <p className="text-sm text-zinc-500">
+ Live per-turn data starts with your next message.
+ </p>
+ ) : (
+ <div className="flex max-h-40 flex-col gap-1 overflow-y-auto">
+ {turns.map((turn, i) => (
+ <TurnRow key={`${turn.at}-${i}`} index={i} turn={turn} />
+ ))}
+ </div>
+ )}
+ </PopoverContent>
+ </Popover>
+ );
+}
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/TokenDevtoolBadge/__tests__/TokenDevtoolBadge.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/TokenDevtoolBadge/__tests__/TokenDevtoolBadge.test.tsx
new file mode 100644
index 0000000000..762f4dad8e
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/TokenDevtoolBadge/__tests__/TokenDevtoolBadge.test.tsx
@@ -0,0 +1,276 @@
+import { render, screen } from "@/tests/integrations/test-utils";
+import userEvent from "@testing-library/user-event";
+import { afterEach, describe, expect, it } from "vitest";
+import { useTokenDevtoolStore } from "../../../tokenDevtool/store";
+import type {
+ ContextBreakdown,
+ TokenTurn,
+} from "../../../tokenDevtool/tokenMath";
+import { MODEL_CONTEXT_WINDOW } from "../../../tokenDevtool/tokenMath";
+import { windowPercent } from "../helpers";
+import { TokenDevtoolBadge } from "../TokenDevtoolBadge";
+
+const SESSION = "session-1";
+
+// BASE_CONTEXT_ESTIMATE (65k) + 5k + 3k + 2k = 75k.
+const BREAKDOWN: ContextBreakdown = {
+ userTokens: 5000,
+ assistantTokens: 3000,
+ toolTokens: 2000,
+};
+
+function turn(overrides: Partial<TokenTurn> = {}): TokenTurn {
+ return {
+ promptTokens: 0,
+ completionTokens: 0,
+ cacheReadTokens: 0,
+ cacheCreationTokens: 0,
+ compacted: false,
+ at: 1,
+ ...overrides,
+ };
+}
+
+function seed({
+ turns,
+ breakdown,
+ sessionId = SESSION,
+}: {
+ turns?: TokenTurn[];
+ breakdown?: ContextBreakdown;
+ sessionId?: string;
+}) {
+ reset();
+ useTokenDevtoolStore.setState({
+ breakdownBySession: breakdown ? { [sessionId]: breakdown } : {},
+ });
+ // Route turns through record() so the derived live-context sum and the
+ // sticky compacted flag stay consistent with production.
+ turns?.forEach((t) => useTokenDevtoolStore.getState().record(sessionId, t));
+}
+
+function reset() {
+ useTokenDevtoolStore.setState({
+ turnsBySession: {},
+ breakdownBySession: {},
+ liveContextBySession: {},
+ compactedBySession: {},
+ sessionOrder: [],
+ });
+}
+
+function getTrigger() {
+ return screen.getByRole("button", { name: /Token devtool/ });
+}
+
+async function openPopover() {
+ await userEvent.click(getTrigger());
+ return screen.findByText("Context window");
+}
+
+afterEach(reset);
+
+describe("TokenDevtoolBadge trigger", () => {
+ it("shows the em-dash state when the session has no data", () => {
+ seed({ turns: [turn({ cacheCreationTokens: 40000 })], sessionId: "other" });
+
+ render(<TokenDevtoolBadge sessionId={SESSION} />);
+
+ expect(getTrigger().textContent).toContain("ctx —");
+ expect(getTrigger().textContent).not.toContain("~");
+ });
+
+ it("shows the seeded history estimate before any live turn", () => {
+ seed({ breakdown: BREAKDOWN });
+
+ render(<TokenDevtoolBadge sessionId={SESSION} />);
+
+ expect(getTrigger().textContent).toContain("ctx ~75k");
+ });
+
+ it("keeps the seeded estimate while the live cache-write sum is below it", () => {
+ seed({
+ breakdown: BREAKDOWN,
+ turns: [turn({ cacheCreationTokens: 20000 })],
+ });
+
+ render(<TokenDevtoolBadge sessionId={SESSION} />);
+
+ expect(getTrigger().textContent).toContain("ctx ~75k");
+ });
+
+ it("switches to the live cache-write sum once it exceeds the seed", () => {
+ seed({
+ breakdown: BREAKDOWN,
+ turns: [
+ turn({ cacheCreationTokens: 20000 }),
+ turn({ cacheCreationTokens: 90000 }),
+ ],
+ });
+
+ render(<TokenDevtoolBadge sessionId={SESSION} />);
+
+ expect(getTrigger().textContent).toContain("ctx ~110k");
+ });
+
+ it("drops the seed and restarts the sum after a compaction turn", () => {
+ seed({
+ breakdown: BREAKDOWN,
+ turns: [
+ turn({ cacheCreationTokens: 90000 }),
+ turn({ cacheCreationTokens: 40000, compacted: true }),
+ ],
+ });
+
+ render(<TokenDevtoolBadge sessionId={SESSION} />);
+
+ expect(getTrigger().textContent).toContain("ctx ~40k");
+ });
+});
+
+describe("TokenDevtoolBadge popover", () => {
+ it("reveals the context window readout against the model window", async () => {
+ seed({ breakdown: BREAKDOWN });
+
+ render(<TokenDevtoolBadge sessionId={SESSION} />);
+ expect(screen.queryByText("Context window")).toBeNull();
+
+ await openPopover();
+
+ expect(screen.getByText(/~75k \/ 200k/)).toBeDefined();
+ expect(screen.getByText("summarizes ~100k")).toBeDefined();
+ // The threshold caveat is visible text, not a title on a 1px div.
+ expect(
+ screen.getByText(/assumes a 200k window; the backend threshold/),
+ ).toBeDefined();
+ });
+
+ it("shows an em-dash readout when the session has no data", async () => {
+ render(<TokenDevtoolBadge sessionId={SESSION} />);
+ await openPopover();
+
+ expect(screen.getByText(/^— \/ 200k$/)).toBeDefined();
+ });
+
+ it("splits the seeded estimate into labelled breakdown rows", async () => {
+ seed({ breakdown: BREAKDOWN });
+
+ render(<TokenDevtoolBadge sessionId={SESSION} />);
+ await openPopover();
+
+ expect(screen.getByText("system + tools + skills")).toBeDefined();
+ expect(screen.getByText("fixed est.")).toBeDefined();
+ expect(screen.getByText("~65k")).toBeDefined();
+ expect(screen.getByText("your messages")).toBeDefined();
+ expect(screen.getByText("~5k")).toBeDefined();
+ expect(screen.getByText("assistant replies")).toBeDefined();
+ expect(screen.getByText("~3k")).toBeDefined();
+ expect(screen.getByText("tool calls + results")).toBeDefined();
+ expect(screen.getByText("~2k")).toBeDefined();
+ });
+
+ it("omits the breakdown section until the history estimate exists", async () => {
+ seed({ turns: [turn({ cacheCreationTokens: 20000 })] });
+
+ render(<TokenDevtoolBadge sessionId={SESSION} />);
+ await openPopover();
+
+ expect(screen.queryByText("your messages")).toBeNull();
+ expect(screen.queryByText("system + tools + skills")).toBeNull();
+ });
+
+ it("hints that live data starts next message when no turns exist", async () => {
+ seed({ breakdown: BREAKDOWN });
+
+ render(<TokenDevtoolBadge sessionId={SESSION} />);
+ await openPopover();
+
+ expect(
+ screen.getByText("Live per-turn data starts with your next message."),
+ ).toBeDefined();
+ });
+
+ it("lists per-turn input, output, and cache-write usage", async () => {
+ seed({
+ turns: [
+ turn({
+ promptTokens: 1200,
+ completionTokens: 300,
+ cacheReadTokens: 40000,
+ cacheCreationTokens: 2000,
+ }),
+ turn({
+ promptTokens: 500,
+ completionTokens: 120,
+ cacheReadTokens: 60000,
+ cacheCreationTokens: 4500,
+ at: 2,
+ }),
+ ],
+ });
+
+ render(<TokenDevtoolBadge sessionId={SESSION} />);
+ await openPopover();
+
+ expect(
+ screen.queryByText("Live per-turn data starts with your next message."),
+ ).toBeNull();
+ expect(screen.getByText("#1")).toBeDefined();
+ expect(screen.getByText("in 43.2k")).toBeDefined();
+ expect(screen.getByText("out 300")).toBeDefined();
+ expect(screen.getByText("w 2k")).toBeDefined();
+ expect(screen.getByText("#2")).toBeDefined();
+ expect(screen.getByText("in 65k")).toBeDefined();
+ expect(screen.getByText("out 120")).toBeDefined();
+ expect(screen.getByText("w 4.5k")).toBeDefined();
+ });
+
+ it("marks a compacted turn with the ⟲ glyph and only that turn", async () => {
+ seed({
+ turns: [
+ turn({ cacheCreationTokens: 90000 }),
+ turn({ cacheCreationTokens: 40000, compacted: true, at: 2 }),
+ ],
+ });
+
+ render(<TokenDevtoolBadge sessionId={SESSION} />);
+ await openPopover();
+
+ const markers = screen.getAllByText("transcript summarized this turn");
+ expect(markers).toHaveLength(1);
+ expect(markers[0].parentElement?.textContent).toContain("⟲");
+ expect(screen.getByText("#2")).toBeDefined();
+ });
+});
+
+describe("TokenDevtoolBadge autocompact threshold", () => {
+ // The amber fill is the tool's primary "you are about to be summarized"
+ // signal, so both sides of the threshold are pinned.
+ async function fillClassesAtContext(userTokens: number) {
+ seed({ breakdown: { userTokens, assistantTokens: 0, toolTokens: 0 } });
+ const { container } = render(<TokenDevtoolBadge sessionId={SESSION} />);
+ await openPopover();
+ return Array.from(container.ownerDocument.querySelectorAll("div, span"))
+ .map((node) => node.className)
+ .filter((name) => typeof name === "string");
+ }
+
+ it("turns the bar amber at or above the autocompact trigger", async () => {
+ // 65k base + 40k = 105k, over the 100k trigger.
+ const classes = await fillClassesAtContext(40_000);
+ expect(classes.some((name) => name.includes("bg-amber-400"))).toBe(true);
+ });
+
+ it("keeps the bar neutral below the autocompact trigger", async () => {
+ // 65k base + 5k = 70k, under the trigger.
+ const classes = await fillClassesAtContext(5_000);
+ expect(classes.some((name) => name.includes("bg-zinc-800"))).toBe(true);
+ });
+});
+
+describe("windowPercent", () => {
+ it("clamps a context beyond the model window to 100", () => {
+ expect(windowPercent(MODEL_CONTEXT_WINDOW * 2)).toBe(100);
+ expect(windowPercent(MODEL_CONTEXT_WINDOW / 2)).toBe(50);
+ });
+});
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/TokenDevtoolBadge/components/BreakdownSection.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/TokenDevtoolBadge/components/BreakdownSection.tsx
new file mode 100644
index 0000000000..29b174f5f4
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/TokenDevtoolBadge/components/BreakdownSection.tsx
@@ -0,0 +1,69 @@
+import {
+ BASE_CONTEXT_ESTIMATE,
+ breakdownTotal,
+ formatTokenCount,
+ type ContextBreakdown,
+} from "../../../tokenDevtool/tokenMath";
+import { BREAKDOWN_COLORS } from "../helpers";
+
+interface Props {
+ breakdown: ContextBreakdown;
+}
+
+export function BreakdownSection({ breakdown }: Props) {
+ const rows = [
+ {
+ label: "system + tools + skills",
+ tokens: BASE_CONTEXT_ESTIMATE,
+ color: BREAKDOWN_COLORS.system,
+ note: "fixed est.",
+ },
+ {
+ label: "your messages",
+ tokens: breakdown.userTokens,
+ color: BREAKDOWN_COLORS.user,
+ },
+ {
+ label: "assistant replies",
+ tokens: breakdown.assistantTokens,
+ color: BREAKDOWN_COLORS.assistant,
+ },
+ {
+ label: "tool calls + results",
+ tokens: breakdown.toolTokens,
+ color: BREAKDOWN_COLORS.tools,
+ },
+ ];
+ const total = breakdownTotal(breakdown);
+
+ return (
+ <div className="pb-2.5">
+ <div className="flex h-1.5 w-full gap-px overflow-hidden rounded-full">
+ {rows.map((row) => (
+ <div
+ key={row.label}
+ className={row.color}
+ style={{ width: `${(row.tokens / total) * 100}%` }}
+ />
+ ))}
+ </div>
+ <div className="flex flex-col gap-0.5 pt-1.5">
+ {rows.map((row) => (
+ <div
+ key={row.label}
+ className="flex items-baseline gap-1.5 font-mono text-xs"
+ >
+ <span className={`size-1.5 shrink-0 rounded-full ${row.color}`} />
+ <span className="text-zinc-600">{row.label}</span>
+ {row.note && (
+ <span className="text-[10px] text-zinc-400">{row.note}</span>
+ )}
+ <span className="ml-auto text-zinc-500">
+ ~{formatTokenCount(row.tokens)}
+ </span>
+ </div>
+ ))}
+ </div>
+ </div>
+ );
+}
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/TokenDevtoolBadge/components/ContextBar.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/TokenDevtoolBadge/components/ContextBar.tsx
new file mode 100644
index 0000000000..49b1132e83
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/TokenDevtoolBadge/components/ContextBar.tsx
@@ -0,0 +1,29 @@
+import {
+ AUTOCOMPACT_TOKENS,
+ MODEL_CONTEXT_WINDOW,
+} from "../../../tokenDevtool/tokenMath";
+import { isOverAutocompact, windowPercent } from "../helpers";
+
+interface Props {
+ context: number;
+}
+
+export function ContextBar({ context }: Props) {
+ const threshold = (AUTOCOMPACT_TOKENS / MODEL_CONTEXT_WINDOW) * 100;
+ return (
+ <div className="relative h-2 w-full rounded-full bg-zinc-100">
+ <div
+ className={
+ "absolute inset-y-0 left-0 rounded-full transition-all " +
+ (isOverAutocompact(context) ? "bg-amber-400" : "bg-zinc-800")
+ }
+ style={{ width: `${windowPercent(context)}%` }}
+ />
+ <div
+ aria-hidden
+ className="absolute inset-y-0 w-px bg-amber-400"
+ style={{ left: `${threshold}%` }}
+ />
+ </div>
+ );
+}
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/TokenDevtoolBadge/components/MiniBar.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/TokenDevtoolBadge/components/MiniBar.tsx
new file mode 100644
index 0000000000..3b9f5d8dee
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/TokenDevtoolBadge/components/MiniBar.tsx
@@ -0,0 +1,19 @@
+import { isOverAutocompact, windowPercent } from "../helpers";
+
+interface Props {
+ context: number;
+}
+
+export function MiniBar({ context }: Props) {
+ return (
+ <span className="relative h-1 w-10 overflow-hidden rounded-full bg-zinc-200">
+ <span
+ className={
+ "absolute inset-y-0 left-0 rounded-full " +
+ (isOverAutocompact(context) ? "bg-amber-400" : "bg-zinc-500")
+ }
+ style={{ width: `${windowPercent(context)}%` }}
+ />
+ </span>
+ );
+}
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/TokenDevtoolBadge/components/TurnRow.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/TokenDevtoolBadge/components/TurnRow.tsx
new file mode 100644
index 0000000000..8416ace12f
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/TokenDevtoolBadge/components/TurnRow.tsx
@@ -0,0 +1,33 @@
+import {
+ formatTokenCount,
+ turnInputTokens,
+ type TokenTurn,
+} from "../../../tokenDevtool/tokenMath";
+
+interface Props {
+ index: number;
+ turn: TokenTurn;
+}
+
+export function TurnRow({ index, turn }: Props) {
+ return (
+ <div className="flex items-baseline gap-2 font-mono text-xs">
+ <span className="w-6 shrink-0 text-zinc-500">#{index + 1}</span>
+ {turn.compacted && (
+ <span className="text-amber-500">
+ <span aria-hidden>⟲</span>
+ <span className="sr-only">transcript summarized this turn</span>
+ </span>
+ )}
+ <span className="text-zinc-800">
+ in {formatTokenCount(turnInputTokens(turn))}
+ </span>
+ <span className="text-zinc-500">
+ out {formatTokenCount(turn.completionTokens)}
+ </span>
+ <span className="ml-auto text-zinc-400">
+ w {formatTokenCount(turn.cacheCreationTokens)}
+ </span>
+ </div>
+ );
+}
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/TokenDevtoolBadge/helpers.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/TokenDevtoolBadge/helpers.ts
new file mode 100644
index 0000000000..ccffbafc92
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/TokenDevtoolBadge/helpers.ts
@@ -0,0 +1,21 @@
+import {
+ AUTOCOMPACT_TOKENS,
+ MODEL_CONTEXT_WINDOW,
+} from "../../tokenDevtool/tokenMath";
+
+export function windowPercent(context: number): number {
+ return Math.min(100, (context / MODEL_CONTEXT_WINDOW) * 100);
+}
+
+/** Shared by both bars so the "you are about to be summarized" threshold
+ * cannot drift between them. */
+export function isOverAutocompact(context: number): boolean {
+ return context >= AUTOCOMPACT_TOKENS;
+}
+
+export const BREAKDOWN_COLORS = {
+ system: "bg-zinc-400",
+ user: "bg-sky-400",
+ assistant: "bg-violet-400",
+ tools: "bg-emerald-400",
+};
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/copilotStreamTransport.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/copilotStreamTransport.ts
index 9a517103f3..7992eb5aa9 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/copilotStreamTransport.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/copilotStreamTransport.ts
@@ -6,6 +6,8 @@ import { v4 as uuidv4 } from "uuid";
import { createSmoothingTransform } from "./copilotStreamSmoothing";
import { getKickoffExpertIdFromMetadata } from "./expertKickoff";
import { getCopilotAuthHeaders } from "./helpers";
+import { isTokenDevtoolEnabled } from "./tokenDevtool/gate";
+import { createUsageCapturingFetch } from "./tokenDevtool/usageTap";
import type { CopilotLlmModel, CopilotMode } from "./store";
export interface MutableValue<T> {
@@ -63,6 +65,11 @@ export function createCopilotTransport({
return new SmoothedCopilotChatTransport({
api: baseUrl,
+ // Dev-only: tee the raw SSE stream so `: usage {...}` comments (dropped
+ // by the AI SDK parser) feed the token devtool badge.
+ ...(isTokenDevtoolEnabled()
+ ? { fetch: createUsageCapturingFetch(sessionId) }
+ : {}),
prepareSendMessagesRequest: async ({ messages }) => {
const last = messages[messages.length - 1];
const kickoffExpertId = getKickoffExpertIdFromMetadata(last.metadata);
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/tokenDevtool/__tests__/gate.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/tokenDevtool/__tests__/gate.test.ts
new file mode 100644
index 0000000000..dd225481d7
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/tokenDevtool/__tests__/gate.test.ts
@@ -0,0 +1,57 @@
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+
+const isDevelopmentBuild = vi.fn(() => false);
+const isDev = vi.fn(() => false);
+
+vi.mock("@/services/environment", () => ({
+ environment: {
+ isDevelopmentBuild: () => isDevelopmentBuild(),
+ isDev: () => isDev(),
+ },
+}));
+
+import { isTokenDevtoolEnabled } from "../gate";
+
+beforeEach(() => {
+ isDevelopmentBuild.mockReturnValue(false);
+ isDev.mockReturnValue(false);
+ vi.stubEnv("NEXT_PUBLIC_TOKEN_DEVTOOL", "true");
+});
+
+afterEach(() => {
+ vi.unstubAllEnvs();
+});
+
+describe("isTokenDevtoolEnabled", () => {
+ it("is on for a development build", () => {
+ isDevelopmentBuild.mockReturnValue(true);
+ expect(isTokenDevtoolEnabled()).toBe(true);
+ });
+
+ it("is on for the cloud dev deployment", () => {
+ isDev.mockReturnValue(true);
+ expect(isTokenDevtoolEnabled()).toBe(true);
+ });
+
+ it("is off for a production build even when the flag is unset", () => {
+ vi.stubEnv("NEXT_PUBLIC_TOKEN_DEVTOOL", undefined);
+ expect(isTokenDevtoolEnabled()).toBe(false);
+ });
+
+ // The self-hosted single-container image builds with NODE_ENV=production and
+ // NEXT_PUBLIC_BEHAVE_AS=LOCAL, and copies .env.default (which sets the flag
+ // to true) into .env. Gating on the build type is what keeps it out.
+ it("stays off for a self-hosted production build that opted in", () => {
+ vi.stubEnv("NEXT_PUBLIC_TOKEN_DEVTOOL", "true");
+ expect(isTokenDevtoolEnabled()).toBe(false);
+ });
+
+ it.each(["false", "FALSE", " False ", "0", "off"])(
+ "the %s kill switch wins over a development build",
+ (value) => {
+ isDevelopmentBuild.mockReturnValue(true);
+ vi.stubEnv("NEXT_PUBLIC_TOKEN_DEVTOOL", value);
+ expect(isTokenDevtoolEnabled()).toBe(false);
+ },
+ );
+});
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/tokenDevtool/__tests__/tokenDevtool.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/tokenDevtool/__tests__/tokenDevtool.test.ts
new file mode 100644
index 0000000000..fca3700ade
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/tokenDevtool/__tests__/tokenDevtool.test.ts
@@ -0,0 +1,478 @@
+import { afterEach, describe, expect, it, vi } from "vitest";
+import { updateHistoryBreakdown, useTokenDevtoolStore } from "../store";
+import {
+ BASE_CONTEXT_ESTIMATE,
+ breakdownCacheKey,
+ breakdownTotal,
+ computeBreakdown,
+ displayContext,
+ formatTokenCount,
+ turnInputTokens,
+ type TokenTurn,
+} from "../tokenMath";
+import {
+ createUsageCapturingFetch,
+ isCompactionLine,
+ parseUsageComment,
+} from "../usageTap";
+
+const USAGE_LINE =
+ ': usage {"type":"usage","promptTokens":1200,"completionTokens":300,"totalTokens":1500,"cacheReadTokens":40000,"cacheCreationTokens":2000}';
+
+function turn(overrides: Partial<TokenTurn> = {}): TokenTurn {
+ return {
+ promptTokens: 0,
+ completionTokens: 0,
+ cacheReadTokens: 0,
+ cacheCreationTokens: 0,
+ compacted: false,
+ at: 1,
+ ...overrides,
+ };
+}
+
+afterEach(() => {
+ useTokenDevtoolStore.setState({
+ turnsBySession: {},
+ breakdownBySession: {},
+ liveContextBySession: {},
+ compactedBySession: {},
+ sessionOrder: [],
+ });
+ vi.unstubAllGlobals();
+});
+
+function liveContextOf(sessionId: string) {
+ const state = useTokenDevtoolStore.getState();
+ return {
+ context: state.liveContextBySession[sessionId] ?? null,
+ compacted: state.compactedBySession[sessionId] ?? false,
+ };
+}
+
+describe("parseUsageComment", () => {
+ it("parses a usage comment into turn usage", () => {
+ const usage = parseUsageComment(USAGE_LINE);
+ expect(usage).not.toBeNull();
+ expect(usage!.promptTokens).toBe(1200);
+ expect(usage!.completionTokens).toBe(300);
+ expect(usage!.cacheReadTokens).toBe(40000);
+ expect(usage!.cacheCreationTokens).toBe(2000);
+ expect(turnInputTokens({ ...usage!, compacted: false, at: 1 })).toBe(43200);
+ });
+
+ it("ignores data lines and other comments", () => {
+ expect(parseUsageComment('data: {"type":"text-delta"}')).toBeNull();
+ expect(parseUsageComment(": keepalive")).toBeNull();
+ // No closing brace — rejected by the regex before JSON.parse is reached.
+ expect(parseUsageComment(": usage {broken")).toBeNull();
+ });
+
+ it("ignores a well-shaped comment whose payload is invalid JSON", () => {
+ expect(parseUsageComment(': usage {"promptTokens":}')).toBeNull();
+ });
+
+ it("does not flag a turn because the assistant quoted the tool name", () => {
+ expect(
+ isCompactionLine(
+ 'data: {"type":"text-delta","delta":"I will run context_compaction next"}',
+ ),
+ ).toBe(false);
+ });
+
+ it("flags a real compaction tool call", () => {
+ expect(
+ isCompactionLine(
+ 'data: {"type":"tool-input-start","toolCallId":"c1","toolName":"context_compaction"}',
+ ),
+ ).toBe(true);
+ });
+
+ it("coerces missing or negative counts to zero", () => {
+ const usage = parseUsageComment(': usage {"promptTokens":-5}');
+ expect(usage).not.toBeNull();
+ expect(usage!.promptTokens).toBe(0);
+ expect(usage!.completionTokens).toBe(0);
+ });
+});
+
+describe("store.record", () => {
+ function record(sessionId: string, turns: TokenTurn[]) {
+ turns.forEach((t) => useTokenDevtoolStore.getState().record(sessionId, t));
+ }
+
+ it("sums cache writes across turns", () => {
+ record("s", [
+ turn({ cacheCreationTokens: 30000 }),
+ turn({ cacheCreationTokens: 5000 }),
+ ]);
+ expect(liveContextOf("s").context).toBe(35000);
+ });
+
+ it("restarts the sum on a compaction turn", () => {
+ record("s", [
+ turn({ cacheCreationTokens: 90000 }),
+ turn({ cacheCreationTokens: 20000, compacted: true }),
+ turn({ cacheCreationTokens: 4000 }),
+ ]);
+ expect(liveContextOf("s").context).toBe(24000);
+ });
+
+ // The per-turn list is capped for display. The estimate must not be, or a
+ // long session would silently lose both its post-compaction restart point
+ // and the fact that it ever compacted.
+ it("keeps the estimate exact after the compaction turn is evicted", () => {
+ record("s", [turn({ cacheCreationTokens: 20000, compacted: true })]);
+ for (let i = 0; i < 55; i++) {
+ record("s", [turn({ cacheCreationTokens: 1000 })]);
+ }
+ const { context, compacted } = liveContextOf("s");
+ expect(useTokenDevtoolStore.getState().turnsBySession["s"]).toHaveLength(
+ 50,
+ );
+ expect(
+ useTokenDevtoolStore.getState().turnsBySession["s"],
+ ).not.toContainEqual(expect.objectContaining({ compacted: true }));
+ expect(context).toBe(75000);
+ expect(compacted).toBe(true);
+ });
+
+ // Turns are capped per session; the session keys need a ceiling too, or a
+ // long-lived tab that browses many threads grows monotonically.
+ it("evicts the oldest sessions past the retention cap", () => {
+ for (let i = 0; i < 25; i++) {
+ record(`s${i}`, [turn({ cacheCreationTokens: 1 })]);
+ }
+ const tracked = Object.keys(useTokenDevtoolStore.getState().turnsBySession);
+ expect(tracked).toHaveLength(20);
+ expect(tracked).toContain("s24");
+ expect(tracked).not.toContain("s0");
+ });
+
+ // Retention is driven by one shared LRU, so a session evicted from the turn
+ // maps cannot linger in the breakdown map.
+ it("prunes every map to the same set of sessions", () => {
+ for (let i = 0; i < 25; i++) {
+ useTokenDevtoolStore.getState().setBreakdown(`s${i}`, {
+ userTokens: 1,
+ assistantTokens: 0,
+ toolTokens: 0,
+ });
+ record(`s${i}`, [turn({ cacheCreationTokens: 1 })]);
+ }
+ const state = useTokenDevtoolStore.getState();
+ const keys = Object.keys(state.turnsBySession).sort();
+ expect(keys).toHaveLength(20);
+ expect(Object.keys(state.breakdownBySession).sort()).toEqual(keys);
+ expect(Object.keys(state.liveContextBySession).sort()).toEqual(keys);
+ expect(Object.keys(state.compactedBySession).sort()).toEqual(keys);
+ });
+
+ it("tracks sessions independently", () => {
+ record("a", [turn({ cacheCreationTokens: 10 })]);
+ record("b", [turn({ cacheCreationTokens: 20, compacted: true })]);
+ expect(liveContextOf("a")).toEqual({ context: 10, compacted: false });
+ expect(liveContextOf("b")).toEqual({ context: 20, compacted: true });
+ });
+});
+
+describe("displayContext", () => {
+ it("shows the seed before any live turns", () => {
+ expect(displayContext(null, false, 90000)).toBe(90000);
+ expect(displayContext(null, false, undefined)).toBeNull();
+ });
+
+ it("keeps the seed until the live estimate exceeds it", () => {
+ expect(displayContext(2000, false, 90000)).toBe(90000);
+ expect(displayContext(95000, false, 90000)).toBe(95000);
+ });
+
+ it("drops the seed once a compaction is observed", () => {
+ expect(displayContext(70000, true, 90000)).toBe(70000);
+ });
+
+ it("reports nothing when a compacted session has no live turns", () => {
+ expect(displayContext(null, true, 90000)).toBeNull();
+ });
+});
+
+describe("breakdownCacheKey", () => {
+ const messages = [{ role: "user", parts: [{ type: "text", text: "hi" }] }];
+
+ it("changes when the session changes at an identical message count", () => {
+ expect(breakdownCacheKey("a", messages, false)).not.toBe(
+ breakdownCacheKey("b", messages, false),
+ );
+ });
+
+ it("changes when the last message grows parts in place", () => {
+ const before = [{ role: "assistant", parts: [{ type: "text" }] }];
+ const after = [
+ { role: "assistant", parts: [{ type: "text" }, { type: "tool-x" }] },
+ ];
+ expect(breakdownCacheKey("a", before, false)).not.toBe(
+ breakdownCacheKey("a", after, false),
+ );
+ });
+
+ // Assistant text grows in place, so counts alone never change — settling
+ // the turn is what forces the final recompute.
+ it("changes when the turn settles at unchanged counts", () => {
+ const streaming = [
+ { role: "assistant", parts: [{ type: "text", text: "a" }] },
+ ];
+ const settled = [
+ {
+ role: "assistant",
+ parts: [{ type: "text", text: "a much longer reply" }],
+ },
+ ];
+ expect(breakdownCacheKey("a", streaming, true)).not.toBe(
+ breakdownCacheKey("a", settled, false),
+ );
+ });
+
+ it("is stable for the same session and the same parts", () => {
+ expect(breakdownCacheKey("a", messages, false)).toBe(
+ breakdownCacheKey("a", messages, false),
+ );
+ });
+});
+
+describe("computeBreakdown", () => {
+ it("splits history by user text, assistant text, and tool parts", () => {
+ const toolPart = { type: "tool-run_agent", output: "y".repeat(100) };
+ const breakdown = computeBreakdown([
+ { role: "user", parts: [{ type: "text", text: "x".repeat(400) }] },
+ { role: "assistant", parts: [{ type: "text", text: "z".repeat(200) }] },
+ { role: "assistant", parts: [toolPart] },
+ ]);
+ expect(breakdown.userTokens).toBe(100);
+ expect(breakdown.assistantTokens).toBe(50);
+ expect(breakdown.toolTokens).toBe(
+ Math.ceil(JSON.stringify(toolPart).length / 4),
+ );
+ expect(breakdownTotal(breakdown)).toBe(
+ BASE_CONTEXT_ESTIMATE + 100 + 50 + breakdown.toolTokens,
+ );
+ });
+
+ it("skips a circular tool part instead of throwing", () => {
+ const part: Record<string, unknown> = { type: "tool-x" };
+ part.self = part;
+ expect(() =>
+ computeBreakdown([{ role: "assistant", parts: [part] }]),
+ ).not.toThrow();
+ expect(
+ computeBreakdown([{ role: "assistant", parts: [part] }]).toolTokens,
+ ).toBe(0);
+ });
+
+ // The per-part memo must not pin a count for a tool part the SDK is still
+ // filling in, or the tool row would freeze at its input-streaming size.
+ it("recounts a tool part that is still streaming", () => {
+ const part: Record<string, unknown> = {
+ type: "tool-x",
+ state: "input-streaming",
+ };
+ const first = computeBreakdown([
+ { role: "assistant", parts: [part] },
+ ]).toolTokens;
+ part.output = "y".repeat(400);
+ const second = computeBreakdown([
+ { role: "assistant", parts: [part] },
+ ]).toolTokens;
+ expect(second).toBeGreaterThan(first);
+ });
+
+ it("counts assistant reasoning as assistant text", () => {
+ const breakdown = computeBreakdown([
+ {
+ role: "assistant",
+ parts: [{ type: "reasoning", text: "a".repeat(40) }],
+ },
+ ]);
+ expect(breakdown.assistantTokens).toBe(10);
+ expect(breakdown.toolTokens).toBe(0);
+ });
+});
+
+describe("updateHistoryBreakdown", () => {
+ it("overwrites the session breakdown on each call", () => {
+ updateHistoryBreakdown("s1", [
+ { role: "user", parts: [{ type: "text", text: "hi" }] },
+ ]);
+ updateHistoryBreakdown("s1", [
+ { role: "user", parts: [{ type: "text", text: "x".repeat(80) }] },
+ ]);
+ expect(
+ useTokenDevtoolStore.getState().breakdownBySession["s1"].userTokens,
+ ).toBe(20);
+ });
+});
+
+describe("formatTokenCount", () => {
+ it("formats counts across magnitudes", () => {
+ expect(formatTokenCount(512)).toBe("512");
+ expect(formatTokenCount(1000)).toBe("1k");
+ expect(formatTokenCount(45200)).toBe("45.2k");
+ // Scales to "100.0" — only the fractional zero may be trimmed.
+ expect(formatTokenCount(100_000)).toBe("100k");
+ expect(formatTokenCount(1_250_000)).toBe("1.25M");
+ expect(formatTokenCount(1_000_000)).toBe("1M");
+ });
+});
+
+describe("createUsageCapturingFetch", () => {
+ const POST = { method: "POST" } as const;
+
+ function sseResponse(chunks: string[]) {
+ const encoder = new TextEncoder();
+ const body = new ReadableStream<Uint8Array>({
+ start(controller) {
+ chunks.forEach((chunk) => controller.enqueue(encoder.encode(chunk)));
+ controller.close();
+ },
+ });
+ return new Response(body, { status: 200 });
+ }
+
+ async function drain(response: Response) {
+ const reader = response.body!.getReader();
+ let text = "";
+ const decoder = new TextDecoder();
+ while (true) {
+ const { done, value } = await reader.read();
+ if (done) break;
+ text += decoder.decode(value, { stream: true });
+ }
+ return text;
+ }
+
+ it("records usage comments while passing the stream through untouched", async () => {
+ const payload = `data: {"type":"text-delta"}\n\n${USAGE_LINE}\n\n`;
+ // Split mid-comment to prove line reassembly across chunks.
+ vi.stubGlobal(
+ "fetch",
+ vi.fn(async () => sseResponse([payload.slice(0, 40), payload.slice(40)])),
+ );
+
+ const wrapped = createUsageCapturingFetch("session-1");
+ const text = await drain(await wrapped("http://x/stream", POST));
+
+ expect(text).toBe(payload);
+ await vi.waitFor(() => {
+ const turns = useTokenDevtoolStore.getState().turnsBySession["session-1"];
+ expect(turns).toHaveLength(1);
+ expect(turns[0].promptTokens).toBe(1200);
+ expect(turns[0].compacted).toBe(false);
+ });
+ });
+
+ it("flags the turn when the stream carried a compaction tool call", async () => {
+ const payload =
+ 'data: {"type":"tool-input-start","toolCallId":"c1","toolName":"context_compaction"}\n\n' +
+ `${USAGE_LINE}\n\n`;
+ vi.stubGlobal(
+ "fetch",
+ vi.fn(async () => sseResponse([payload])),
+ );
+
+ const wrapped = createUsageCapturingFetch("session-2");
+ await drain(await wrapped("http://x/stream", POST));
+
+ await vi.waitFor(() => {
+ const turns = useTokenDevtoolStore.getState().turnsBySession["session-2"];
+ expect(turns).toHaveLength(1);
+ expect(turns[0].compacted).toBe(true);
+ });
+ });
+
+ it("releases the underlying stream when the consumer aborts", async () => {
+ let sourceCancelled = false;
+ const controller = new AbortController();
+ // Never closes on its own — only cancellation can release it.
+ const body = new ReadableStream<Uint8Array>({
+ start(c) {
+ c.enqueue(new TextEncoder().encode('data: {"type":"text-delta"}\n\n'));
+ },
+ cancel() {
+ sourceCancelled = true;
+ },
+ });
+ vi.stubGlobal(
+ "fetch",
+ vi.fn(async () => new Response(body, { status: 200 })),
+ );
+
+ const wrapped = createUsageCapturingFetch("session-abort");
+ const response = await wrapped("http://x/stream", {
+ ...POST,
+ signal: controller.signal,
+ });
+
+ // A tee'd source is only released once BOTH branches let go — and the
+ // branch's own cancel() promise does not settle until then, so this is
+ // deliberately not awaited yet.
+ const consumerCancelled = response.body!.cancel();
+ expect(sourceCancelled).toBe(false);
+
+ // Without the abort listener the tap would read on and hold the source.
+ controller.abort();
+ await consumerCancelled;
+
+ await vi.waitFor(() => expect(sourceCancelled).toBe(true));
+ });
+
+ // The AI SDK's reconnect path GETs the active turn replayed from "0-0", so
+ // tapping it would record the same `: usage {...}` twice and permanently
+ // inflate the running estimate.
+ it("leaves the reconnect GET untapped", async () => {
+ const payload = `${USAGE_LINE}\n\n`;
+ vi.stubGlobal(
+ "fetch",
+ vi.fn(async () => sseResponse([payload])),
+ );
+
+ const wrapped = createUsageCapturingFetch("session-resume");
+ const text = await drain(
+ await wrapped("http://x/stream", { method: "GET" }),
+ );
+
+ expect(text).toBe(payload);
+ expect(
+ useTokenDevtoolStore.getState().turnsBySession["session-resume"],
+ ).toBeUndefined();
+ });
+
+ it("records a usage comment that arrives without a trailing newline", async () => {
+ vi.stubGlobal(
+ "fetch",
+ vi.fn(async () => sseResponse([USAGE_LINE])),
+ );
+
+ const wrapped = createUsageCapturingFetch("session-flush");
+ await drain(await wrapped("http://x/stream", POST));
+
+ await vi.waitFor(() => {
+ const turns =
+ useTokenDevtoolStore.getState().turnsBySession["session-flush"];
+ expect(turns).toHaveLength(1);
+ expect(turns[0].promptTokens).toBe(1200);
+ });
+ });
+
+ it("returns error responses untouched", async () => {
+ vi.stubGlobal(
+ "fetch",
+ vi.fn(async () => new Response("nope", { status: 500 })),
+ );
+ const wrapped = createUsageCapturingFetch("session-1");
+ const response = await wrapped("http://x/stream", POST);
+ expect(response.status).toBe(500);
+ expect(await response.text()).toBe("nope");
+ expect(
+ useTokenDevtoolStore.getState().turnsBySession["session-1"],
+ ).toBeUndefined();
+ });
+});
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/tokenDevtool/gate.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/tokenDevtool/gate.ts
new file mode 100644
index 0000000000..62601c0c43
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/tokenDevtool/gate.ts
@@ -0,0 +1,21 @@
+import { environment } from "@/services/environment";
+
+/** Dev-only. Requires a development build (`pnpm dev`) or the cloud dev
+ * deployment. Deliberately does NOT key off `environment.isLocal()`: that is
+ * the default branch of `getBehaveAs()`, so it is true for any
+ * NEXT_PUBLIC_BEHAVE_AS that is not exactly "CLOUD" — including the
+ * self-hosted single-container image, which builds with NODE_ENV=production
+ * and NEXT_PUBLIC_BEHAVE_AS=LOCAL. Gating on the build type keeps the badge
+ * and the SSE tap out of every production build.
+ *
+ * NEXT_PUBLIC_TOKEN_DEVTOOL=false turns it off on top of that. Next inlines
+ * NEXT_PUBLIC_* at build time, so flipping it needs a rebuild. */
+export function isTokenDevtoolEnabled(): boolean {
+ if (isDisabled(process.env.NEXT_PUBLIC_TOKEN_DEVTOOL)) return false;
+ return environment.isDevelopmentBuild() || environment.isDev();
+}
+
+function isDisabled(value: string | undefined): boolean {
+ const normalized = value?.trim().toLowerCase();
+ return normalized === "false" || normalized === "0" || normalized === "off";
+}
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/tokenDevtool/store.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/tokenDevtool/store.ts
new file mode 100644
index 0000000000..35b39782b0
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/tokenDevtool/store.ts
@@ -0,0 +1,117 @@
+import { create } from "zustand";
+import {
+ computeBreakdown,
+ type ContextBreakdown,
+ type MessageLike,
+ type TokenTurn,
+} from "./tokenMath";
+
+/** Display cap for the per-turn list in the popover. The context estimate is
+ * kept separately precisely so this cap cannot silently drop turns it
+ * depends on. */
+const KEPT_TURNS = 50;
+
+/** Turns are capped per session; without this the session keys themselves
+ * would grow without bound as a long-lived tab visits more threads. */
+const KEPT_SESSIONS = 20;
+
+interface TokenDevtoolState {
+ turnsBySession: Record<string, TokenTurn[]>;
+ breakdownBySession: Record<string, ContextBreakdown>;
+ /** Running cache-write sum since the session's last compaction. */
+ liveContextBySession: Record<string, number>;
+ /** Sticky: once a session has compacted, the loaded-history seed is stale
+ * for good — it must not un-stick when the compaction turn scrolls out of
+ * the kept window. */
+ compactedBySession: Record<string, boolean>;
+ /** Least-recently-touched first. Single source of truth for retention, so
+ * the maps cannot prune to different key sets. */
+ sessionOrder: string[];
+ record: (sessionId: string, turn: TokenTurn) => void;
+ setBreakdown: (sessionId: string, breakdown: ContextBreakdown) => void;
+}
+
+/** Moves sessionId to most-recent and reports which sessions fall off the
+ * end. Every map prunes against this one answer. */
+function touchSession(order: string[], sessionId: string) {
+ const next = [...order.filter((id) => id !== sessionId), sessionId];
+ if (next.length <= KEPT_SESSIONS) return { order: next, dropped: EMPTY };
+ return {
+ order: next.slice(-KEPT_SESSIONS),
+ dropped: new Set(next.slice(0, next.length - KEPT_SESSIONS)),
+ };
+}
+
+const EMPTY: ReadonlySet<string> = new Set();
+
+function without<T>(
+ entries: Record<string, T>,
+ dropped: ReadonlySet<string>,
+): Record<string, T> {
+ if (dropped.size === 0) return entries;
+ return Object.fromEntries(
+ Object.entries(entries).filter(([key]) => !dropped.has(key)),
+ );
+}
+
+export const useTokenDevtoolStore = create<TokenDevtoolState>((set) => ({
+ turnsBySession: {},
+ breakdownBySession: {},
+ liveContextBySession: {},
+ compactedBySession: {},
+ sessionOrder: [],
+ record(sessionId, turn) {
+ set((state) => {
+ const { order, dropped } = touchSession(state.sessionOrder, sessionId);
+ return {
+ sessionOrder: order,
+ breakdownBySession: without(state.breakdownBySession, dropped),
+ turnsBySession: {
+ ...without(state.turnsBySession, dropped),
+ [sessionId]: [...(state.turnsBySession[sessionId] ?? []), turn].slice(
+ -KEPT_TURNS,
+ ),
+ },
+ // A compaction turn restarts the sum: the compacted context is
+ // re-written to cache in that same turn.
+ liveContextBySession: {
+ ...without(state.liveContextBySession, dropped),
+ [sessionId]: turn.compacted
+ ? turn.cacheCreationTokens
+ : (state.liveContextBySession[sessionId] ?? 0) +
+ turn.cacheCreationTokens,
+ },
+ compactedBySession: {
+ ...without(state.compactedBySession, dropped),
+ [sessionId]:
+ turn.compacted || (state.compactedBySession[sessionId] ?? false),
+ },
+ };
+ });
+ },
+ setBreakdown(sessionId, breakdown) {
+ set((state) => {
+ const { order, dropped } = touchSession(state.sessionOrder, sessionId);
+ return {
+ sessionOrder: order,
+ turnsBySession: without(state.turnsBySession, dropped),
+ liveContextBySession: without(state.liveContextBySession, dropped),
+ compactedBySession: without(state.compactedBySession, dropped),
+ breakdownBySession: {
+ ...without(state.breakdownBySession, dropped),
+ [sessionId]: breakdown,
+ },
+ };
+ });
+ },
+}));
+
+/** Recompute the session's history breakdown from the loaded messages. */
+export function updateHistoryBreakdown(
+ sessionId: string,
+ messages: readonly MessageLike[],
+) {
+ useTokenDevtoolStore
+ .getState()
+ .setBreakdown(sessionId, computeBreakdown(messages));
+}
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/tokenDevtool/tokenMath.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/tokenDevtool/tokenMath.ts
new file mode 100644
index 0000000000..70fc5c2f49
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/tokenDevtool/tokenMath.ts
@@ -0,0 +1,163 @@
+/** One turn's token usage, captured from the backend's `: usage {...}` SSE
+ * comment. The AI SDK parser drops comment lines, so the transport's fetch
+ * taps the raw stream before parsing (see createUsageCapturingFetch).
+ *
+ * The backend accumulates usage across EVERY API step of the turn (each
+ * tool round-trip re-reads the context), so prompt/cache sums overcount
+ * the live context — the store's running cache-write sum tracks that. */
+export interface TokenTurn {
+ promptTokens: number;
+ completionTokens: number;
+ cacheReadTokens: number;
+ cacheCreationTokens: number;
+ /** The stream carried a `context_compaction` tool call this turn — the
+ * backend summarized the transcript before/while answering. */
+ compacted: boolean;
+ at: number;
+}
+
+/** Claude-family window the copilot engines run against. Devtool-only
+ * constants — the real values live in backend config (see
+ * claude_agent_autocompact_pct_override, default 50% of the window). */
+export const MODEL_CONTEXT_WINDOW = 200_000;
+export const AUTOCOMPACT_TOKENS = 100_000;
+
+/** System prompt + tool definitions the SDK always carries — backend config
+ * notes the post-compaction floor is ≈65-110K, so 65K is the low bound. */
+export const BASE_CONTEXT_ESTIMATE = 65_000;
+
+/** Total input across all of the turn's API steps (uncached + cache reads +
+ * cache writes). A cost-side number, not the live context size. */
+export function turnInputTokens(turn: TokenTurn): number {
+ return turn.promptTokens + turn.cacheReadTokens + turn.cacheCreationTokens;
+}
+
+export function formatTokenCount(count: number): string {
+ if (count < 1000) return String(count);
+ if (count < 1_000_000) return `${trimZeros((count / 1000).toFixed(1))}k`;
+ return `${trimZeros((count / 1_000_000).toFixed(2))}M`;
+}
+
+/** Trailing zeros are only noise in the fractional part — "1.0" -> "1". An
+ * integer string is returned untouched, so "100" does not become "1". */
+function trimZeros(value: string): string {
+ return value.includes(".") ? value.replace(/\.?0+$/, "") : value;
+}
+
+/** Char-based (~4 chars/token) split of the loaded conversation history.
+ * System prompt, tool definitions, and injected context blocks (skills,
+ * memory, team roster) never reach the frontend — they live in the fixed
+ * BASE_CONTEXT_ESTIMATE floor instead. */
+export interface ContextBreakdown {
+ userTokens: number;
+ assistantTokens: number;
+ toolTokens: number;
+}
+
+export function breakdownTotal(breakdown: ContextBreakdown): number {
+ return (
+ BASE_CONTEXT_ESTIMATE +
+ breakdown.userTokens +
+ breakdown.assistantTokens +
+ breakdown.toolTokens
+ );
+}
+
+/** Displayed context: prefer the live cache-write estimate, but until it
+ * can exceed the history estimate (a fresh page load starts the live sum
+ * at zero), keep showing the history number. Once a compaction is observed
+ * the history estimate is stale by definition and the live one wins: the
+ * compaction turn re-writes the whole summarized context to cache, so that
+ * turn's write IS the new context — clamping it to a floor would over-report
+ * a genuinely small post-compaction context. */
+export function displayContext(
+ liveContext: number | null,
+ compacted: boolean,
+ historyEstimate: number | undefined,
+): number | null {
+ if (compacted) return liveContext;
+ if (liveContext === null && historyEstimate === undefined) return null;
+ return Math.max(liveContext ?? 0, historyEstimate ?? 0);
+}
+
+/** Chars per token for the char-based history estimate. */
+const CHARS_PER_TOKEN = 4;
+
+export interface MessageLike {
+ role?: string;
+ /** `text` is widened to unknown because parts arrive from the AI SDK: tool
+ * parts carry arbitrary payloads and are not guaranteed to hold a string
+ * here, which is what partChars() runtime-checks. */
+ parts?: Array<{ type?: string; text?: unknown; state?: string }>;
+}
+
+/** Guards the breakdown recompute, which walks the whole loaded history and
+ * so must not run per stream delta. Carries:
+ * - the session, so switching to a thread with an identical message count
+ * still recomputes;
+ * - the last message's part count, because the SDK appends a turn's tool
+ * calls and results into the existing assistant message, not a new one;
+ * - whether the turn has settled, because assistant text grows in place
+ * without adding a part — deltas are deliberately not tracked, so this is
+ * what forces exactly one recompute when the turn finishes. */
+export function breakdownCacheKey(
+ sessionId: string,
+ messages: readonly MessageLike[],
+ isStreaming: boolean,
+): string {
+ const parts = messages.at(-1)?.parts?.length ?? 0;
+ return `${sessionId}:${messages.length}:${parts}:${isStreaming ? "live" : "settled"}`;
+}
+
+export function computeBreakdown(
+ messages: readonly MessageLike[],
+): ContextBreakdown {
+ let user = 0;
+ let assistant = 0;
+ let tools = 0;
+ for (const message of messages) {
+ for (const part of message?.parts ?? []) {
+ const chars = partChars(part);
+ if (part?.type === "text" || part?.type === "reasoning") {
+ if (message.role === "user") user += chars;
+ else assistant += chars;
+ } else {
+ tools += chars;
+ }
+ }
+ }
+ return {
+ userTokens: Math.ceil(user / CHARS_PER_TOKEN),
+ assistantTokens: Math.ceil(assistant / CHARS_PER_TOKEN),
+ toolTokens: Math.ceil(tools / CHARS_PER_TOKEN),
+ };
+}
+
+/** Tool parts are the big ones and computeBreakdown re-walks the whole loaded
+ * history on every recompute, so serializing them each time is O(history) per
+ * call. AI SDK part objects are stable references across re-renders, so
+ * caching per object collapses that to O(new parts). */
+const partCharCache = new WeakMap<object, number>();
+
+function partChars(part: {
+ type?: string;
+ text?: unknown;
+ state?: string;
+}): number {
+ if (typeof part?.text === "string") return part.text.length;
+ if (part === null || typeof part !== "object") return 0;
+ // A tool part still filling in its input/output is mutated in place, so
+ // caching it would pin a count that is about to change.
+ const cacheable =
+ part.state === undefined || part.state.endsWith("available");
+ const cached = cacheable ? partCharCache.get(part) : undefined;
+ if (cached !== undefined) return cached;
+ let chars = 0;
+ try {
+ chars = JSON.stringify(part)?.length ?? 0;
+ } catch {
+ chars = 0; // Circular/unserializable part — skip it, this is an estimate.
+ }
+ if (cacheable) partCharCache.set(part, chars);
+ return chars;
+}
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/tokenDevtool/usageTap.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/tokenDevtool/usageTap.ts
new file mode 100644
index 0000000000..4ea72e4724
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/tokenDevtool/usageTap.ts
@@ -0,0 +1,133 @@
+import { useTokenDevtoolStore } from "./store";
+import type { TokenTurn } from "./tokenMath";
+
+const USAGE_COMMENT = /^:\s*usage\s+(\{.*\})$/;
+const COMPACTION_TOOL = "context_compaction";
+const COLON = 58;
+const MAX_BUFFERED_LINE = 1_000_000;
+
+export function parseUsageComment(
+ line: string,
+): Omit<TokenTurn, "compacted" | "at"> | null {
+ const match = USAGE_COMMENT.exec(line.trim());
+ if (!match) return null;
+ try {
+ const raw = JSON.parse(match[1]) as Record<string, unknown>;
+ return {
+ promptTokens: toCount(raw.promptTokens),
+ completionTokens: toCount(raw.completionTokens),
+ cacheReadTokens: toCount(raw.cacheReadTokens),
+ cacheCreationTokens: toCount(raw.cacheCreationTokens),
+ };
+ } catch {
+ return null;
+ }
+}
+
+function toCount(value: unknown): number {
+ return typeof value === "number" && Number.isFinite(value) && value >= 0
+ ? value
+ : 0;
+}
+
+/** True only for a `data:` chunk that actually calls the compaction tool.
+ * Substring-matching the raw line would also fire on an assistant reply that
+ * merely quotes the tool name, which would wrongly invalidate the session's
+ * history seed for the rest of the thread. */
+export function isCompactionLine(line: string): boolean {
+ if (!line.includes(COMPACTION_TOOL)) return false;
+ const payload = line.slice(line.indexOf(":") + 1);
+ try {
+ const chunk = JSON.parse(payload) as { toolName?: unknown };
+ return chunk?.toolName === COMPACTION_TOOL;
+ } catch {
+ return false;
+ }
+}
+
+/** Wraps fetch so the copilot SSE stream is teed: the AI SDK consumes one
+ * branch untouched while the other is scanned for usage comments and
+ * compaction tool calls. The tap must never break the chat — every failure
+ * path degrades to "no data". */
+export function createUsageCapturingFetch(sessionId: string): typeof fetch {
+ return async (input, init) => {
+ const response = await fetch(input, init);
+ // Only tap the POST that streams a fresh turn. The AI SDK's reconnect
+ // path re-GETs the active turn from "0-0", replaying the same
+ // `: usage {...}` comment — recording it again would permanently inflate
+ // the running context estimate.
+ if (init?.method?.toUpperCase() !== "POST") return response;
+ if (!response.ok || !response.body) return response;
+ const [main, tap] = response.body.tee();
+ void scanForUsage(tap, sessionId, init?.signal);
+ return new Response(main, {
+ status: response.status,
+ statusText: response.statusText,
+ headers: response.headers,
+ });
+ };
+}
+
+async function scanForUsage(
+ stream: ReadableStream<Uint8Array>,
+ sessionId: string,
+ signal?: AbortSignal | null,
+): Promise<void> {
+ const reader = stream.getReader();
+ // Both tee branches share one source, so a tap that kept reading after the
+ // consumer aborted would hold the connection open. Cancel through the
+ // reader — the branch is locked, so tap.cancel() would throw.
+ const cancelTap = () => void reader.cancel().catch(() => {});
+ signal?.addEventListener("abort", cancelTap);
+ if (signal?.aborted) cancelTap();
+ const decoder = new TextDecoder();
+ let buffer = "";
+ // Usage arrives at turn end, after any compaction tool events — so a
+ // marker seen earlier in the stream belongs to the next recorded turn.
+ let sawCompaction = false;
+ const scan = (lines: string[]) => {
+ for (const line of lines) {
+ // Thousands of lines per turn on the app's most latency-sensitive
+ // path: reject the `data:` majority on one char before doing any
+ // trimming, regex, or JSON work.
+ if (line.charCodeAt(0) === COLON) {
+ const usage = parseUsageComment(line);
+ if (!usage) continue;
+ useTokenDevtoolStore.getState().record(sessionId, {
+ ...usage,
+ compacted: sawCompaction,
+ at: Date.now(),
+ });
+ sawCompaction = false;
+ } else if (isCompactionLine(line)) {
+ sawCompaction = true;
+ }
+ }
+ };
+ try {
+ while (true) {
+ const { done, value } = await reader.read();
+ if (done) break;
+ buffer += decoder.decode(value, { stream: true });
+ // SSE always delimits with newlines, so this only trips on a malformed
+ // upstream. Dropping the partial line is fine — this is an estimate.
+ if (buffer.length > MAX_BUFFERED_LINE) buffer = "";
+ const lines = buffer.split("\n");
+ buffer = lines.pop() ?? "";
+ scan(lines);
+ }
+ // A stream that ends without a trailing newline leaves the last line —
+ // which is where the usage comment lives — sitting in the buffer.
+ if (buffer) scan([buffer]);
+ } catch {
+ // Devtool tap only — swallow so it can never surface as a chat error.
+ } finally {
+ signal?.removeEventListener("abort", cancelTap);
+ // Cancel, not just releaseLock: tee() buffers every remaining chunk for a
+ // branch nobody reads, so bailing out mid-stream without cancelling would
+ // queue the rest of the turn (tool results run to megabytes) for the life
+ // of the request.
+ await reader.cancel().catch(() => {});
+ reader.releaseLock();
+ }
+}