Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(in-app-agent): surface step-limit truncation instead of silent success (#16295)

Lysander Kiesel875f44e81b6d
diff --git a/packages/shared/src/features/inAppAgent/types.ts b/packages/shared/src/features/inAppAgent/types.ts
index 87cfd49f9a..4275beddcd 100644
--- a/packages/shared/src/features/inAppAgent/types.ts
+++ b/packages/shared/src/features/inAppAgent/types.ts
@@ -61,6 +61,11 @@ export enum InAppAgentRunErrorCode {
APPROVAL_SUPERSEDED = "approval_superseded",
/** Pending approval cancelled by the user (recorded on CANCELLED). */
APPROVAL_CANCELLED = "approval_cancelled",
+ /**
+ * Loop hit maxSteps without a `stop` finish — the run completed the
+ * configured wall but did not produce a final answer.
+ */
+ STEP_LIMIT = "step_limit",
}
/**
diff --git a/packages/shared/src/in-app-agent/server/tunables.ts b/packages/shared/src/in-app-agent/server/tunables.ts
index 15115e792f..c1a419aa22 100644
--- a/packages/shared/src/in-app-agent/server/tunables.ts
+++ b/packages/shared/src/in-app-agent/server/tunables.ts
@@ -15,3 +15,9 @@ export const IN_APP_AGENT_RUN_MAX_DURATION_MS = 15 * 60_000;
/** AWAITING_APPROVAL parked longer than this expires (approval_expired) on read. */
export const IN_APP_AGENT_APPROVAL_TTL_MS = 24 * 60 * 60_000;
+
+/**
+ * Sequential model calls (Mastra `maxSteps`) allowed in one turn. Shared so
+ * wrap-up, truncation detection, and the Agent constructor cannot drift.
+ */
+export const IN_APP_AGENT_MAX_STEPS = 20;
diff --git a/web/src/features/in-app-agent/components/ControlledInAppAgentWindow.tsx b/web/src/features/in-app-agent/components/ControlledInAppAgentWindow.tsx
index 001f2e907b..c9ee5fd904 100644
--- a/web/src/features/in-app-agent/components/ControlledInAppAgentWindow.tsx
+++ b/web/src/features/in-app-agent/components/ControlledInAppAgentWindow.tsx
@@ -16,33 +16,15 @@ import {
getInAppAgentQuickActionContext,
} from "@/src/features/in-app-agent/quickActions";
import {
- getBackgroundRunFailureMessage,
+ getBackgroundRunNotice,
+ getSettledActivityOutcome,
isCancellableBackgroundRun,
- type BackgroundExecutionRunView,
} from "@/src/features/in-app-agent/lib/backgroundExecutionSession";
import {
InAppAgentRunStatus,
isUnsettledInAppAgentRunStatus,
} from "@langfuse/shared/in-app-agent";
-function getBackgroundRunNotice(
- run: BackgroundExecutionRunView | null,
-): string | null {
- if (!run) {
- return null;
- }
-
- if (isCancellableBackgroundRun(run.status) && run.cancelRequested) {
- return "Stopping the run…";
- }
-
- if (run.status === InAppAgentRunStatus.FAILED) {
- return getBackgroundRunFailureMessage(run.errorCode ?? null);
- }
-
- return null;
-}
-
type ControlledInAppAgentWindowBaseProps = {
isHeaderDragHandleEnabled?: boolean;
isExpanded: boolean;
@@ -109,6 +91,7 @@ export function ControlledInAppAgentWindow(
});
const windowExecutionUi: InAppAgentWindowExecutionUi = {
notice: getBackgroundRunNotice(execution.run),
+ activityOutcome: getSettledActivityOutcome(execution.run),
stop:
execution.run && isCancellableBackgroundRun(execution.run.status)
? {
diff --git a/web/src/features/in-app-agent/components/InAppAgentBackgroundHint.tsx b/web/src/features/in-app-agent/components/InAppAgentBackgroundHint.tsx
index 38ed776881..999ce91159 100644
--- a/web/src/features/in-app-agent/components/InAppAgentBackgroundHint.tsx
+++ b/web/src/features/in-app-agent/components/InAppAgentBackgroundHint.tsx
@@ -2,7 +2,7 @@ import { BotMessageSquare, Minus } from "lucide-react";
import { Button } from "@/src/components/ui/button";
import { KeyboardShortcut } from "@/src/components/ui/keyboard-shortcut";
-import { cn } from "@/src/utils/tailwind";
+import { InAppAgentNotice } from "@/src/features/in-app-agent/components/InAppAgentNotice";
/**
* Transient nudge that the run keeps going without the drawer. Lives in the
@@ -16,43 +16,36 @@ export function InAppAgentBackgroundHint({
onMinimize: () => void;
}) {
return (
- <div className="shrink-0 px-2 pb-2">
- <div className={cn(isExpanded && "mx-auto max-w-3xl")}>
- <p
- role="status"
- className={cn(
- "border-border bg-muted/60 text-foreground animate-in fade-in flex w-full items-center gap-2 rounded-lg border px-2 py-1",
- isExpanded ? "text-sm" : "text-xs",
- )}
+ <InAppAgentNotice
+ icon={<BotMessageSquare aria-hidden="true" className="size-3 shrink-0" />}
+ isExpanded={isExpanded}
+ role="status"
+ tone="neutral"
+ action={
+ <Button
+ type="button"
+ variant="ghost"
+ size="sm"
+ aria-label="Minimize assistant"
+ className="-my-0.5 h-5 shrink-0 gap-1 px-1"
+ onClick={onMinimize}
>
- <BotMessageSquare aria-hidden="true" className="size-3 shrink-0" />
- <span className="min-w-0 flex-1">
- I keep running in the background. Feel free to minimize, I&apos;ll
- notify you when I&apos;m done or need you.
- </span>
- {/* Mirrors the header control, so the hint teaches the affordance. */}
- <Button
- type="button"
- variant="ghost"
- size="sm"
- aria-label="Minimize assistant"
- className="-my-0.5 h-5 shrink-0 gap-1 px-1"
- onClick={onMinimize}
- >
- <Minus className="size-3" />
- <KeyboardShortcut
- className="bg-transparent shadow-none"
- keys={[
- typeof navigator !== "undefined" &&
- navigator.userAgent.includes("Mac")
- ? "⌘"
- : "Ctrl",
- "I",
- ]}
- />
- </Button>
- </p>
- </div>
- </div>
+ <Minus className="size-3" />
+ <KeyboardShortcut
+ className="bg-transparent shadow-none"
+ keys={[
+ typeof navigator !== "undefined" &&
+ navigator.userAgent.includes("Mac")
+ ? "⌘"
+ : "Ctrl",
+ "I",
+ ]}
+ />
+ </Button>
+ }
+ >
+ I keep running in the background. Feel free to minimize, I&apos;ll notify
+ you when I&apos;m done or need you.
+ </InAppAgentNotice>
);
}
diff --git a/web/src/features/in-app-agent/components/InAppAgentNotice.tsx b/web/src/features/in-app-agent/components/InAppAgentNotice.tsx
new file mode 100644
index 0000000000..7971798092
--- /dev/null
+++ b/web/src/features/in-app-agent/components/InAppAgentNotice.tsx
@@ -0,0 +1,48 @@
+import type { ReactNode } from "react";
+
+import { cn } from "@/src/utils/tailwind";
+
+const NOTICE_TONE_CLASSES = {
+ neutral: "border-border bg-muted/60 text-foreground",
+ warning: "border-dark-yellow/40 bg-light-yellow text-dark-yellow",
+ danger: "border-destructive/40 bg-destructive/10 text-destructive",
+} as const;
+
+/**
+ * Composer-adjacent notice chrome. Margin lives here because this is the
+ * notice band: showing it must not shift the composer or the run indicator.
+ */
+export function InAppAgentNotice({
+ action,
+ children,
+ icon,
+ isExpanded,
+ role,
+ tone,
+}: {
+ action?: ReactNode;
+ children: ReactNode;
+ icon: ReactNode;
+ isExpanded: boolean;
+ role: "status" | "alert";
+ tone: keyof typeof NOTICE_TONE_CLASSES;
+}) {
+ return (
+ <div className="shrink-0 px-1.5 pb-2">
+ <div className={cn(isExpanded && "mx-auto max-w-3xl")}>
+ <div
+ role={role}
+ className={cn(
+ "flex w-full items-center gap-2 rounded-lg border px-2 py-1",
+ isExpanded ? "text-sm" : "text-xs",
+ NOTICE_TONE_CLASSES[tone],
+ )}
+ >
+ <span className="flex items-center self-stretch">{icon}</span>
+ <span className="min-w-0 flex-1 leading-snug">{children}</span>
+ {action}
+ </div>
+ </div>
+ </div>
+ );
+}
diff --git a/web/src/features/in-app-agent/components/InAppAgentWindow.stories.tsx b/web/src/features/in-app-agent/components/InAppAgentWindow.stories.tsx
index 22476efa77..28c88d5097 100644
--- a/web/src/features/in-app-agent/components/InAppAgentWindow.stories.tsx
+++ b/web/src/features/in-app-agent/components/InAppAgentWindow.stories.tsx
@@ -3,6 +3,8 @@ import { type ReactNode, useEffect, useRef, useState } from "react";
import { expect, fn, screen, userEvent, waitFor, within } from "storybook/test";
import {
IN_APP_AGENT_GENERIC_ERROR_MESSAGE,
+ InAppAgentRunErrorCode,
+ InAppAgentRunStatus,
type AgUiMessage,
} from "@langfuse/shared/in-app-agent";
import {
@@ -21,6 +23,10 @@ import {
useInAppAgentWindowShellPanelControl,
} from "./InAppAgentWindowShell";
import { getDrawerMessages } from "./utils/utils";
+import {
+ getBackgroundRunNotice,
+ getSettledActivityOutcome,
+} from "@/src/features/in-app-agent/lib/backgroundExecutionSession";
function InAppAgentWindowStoryShell({
children,
@@ -1433,6 +1439,244 @@ export const Error = meta.story({
},
});
+const stepLimitRun = {
+ id: "run-1",
+ status: InAppAgentRunStatus.SUCCEEDED,
+ errorCode: InAppAgentRunErrorCode.STEP_LIMIT,
+ cancelRequested: false,
+};
+
+export const StepLimit = meta.story({
+ args: {
+ selectedConversationId: "conversation-1",
+ executionUi: {
+ notice: getBackgroundRunNotice(stepLimitRun),
+ activityOutcome: getSettledActivityOutcome(stepLimitRun),
+ stop: null,
+ },
+ messages: [
+ {
+ id: "user-1",
+ role: "user",
+ content: {
+ type: "text",
+ text: "Investigate yesterday's errors.",
+ },
+ },
+ {
+ id: "assistant-reasoning-1",
+ timestamp: new Date("2026-08-06T15:20:00.000Z").getTime(),
+ role: "assistant",
+ content: {
+ type: "reasoning",
+ text: "Checking yesterday first.",
+ isStreaming: false,
+ },
+ },
+ {
+ id: "assistant-text-1",
+ runId: "run-0",
+ timestamp: new Date("2026-08-06T15:20:12.000Z").getTime(),
+ role: "assistant",
+ content: {
+ type: "text",
+ text: "Yesterday was quiet.",
+ },
+ },
+ {
+ id: "user-2",
+ role: "user",
+ content: {
+ type: "text",
+ text: "Keep inspecting traces until you find the spike.",
+ },
+ },
+ {
+ id: "assistant-tool-2",
+ timestamp: new Date("2026-08-06T15:26:26.000Z").getTime(),
+ role: "assistant",
+ content: {
+ type: "toolGroup",
+ tools: [
+ {
+ type: "tool",
+ name: "langfuse_getTraces",
+ status: "succeeded",
+ args: JSON.stringify({ limit: 10 }),
+ result: JSON.stringify({ data: [] }),
+ },
+ {
+ type: "tool",
+ name: "langfuse_queryMetrics",
+ status: "succeeded",
+ args: JSON.stringify({ view: "traces" }),
+ result: JSON.stringify({ data: [] }),
+ },
+ ],
+ },
+ },
+ {
+ id: "assistant-text-2",
+ timestamp: new Date("2026-08-06T15:27:17.000Z").getTime(),
+ role: "assistant",
+ content: {
+ type: "text",
+ text: "Latency is up on the last two batches, but I still need to inspect the remaining traces.",
+ },
+ },
+ ],
+ },
+ play: async ({ canvasElement }) => {
+ const canvas = within(canvasElement);
+
+ await expect(canvas.getByRole("status")).toHaveTextContent(
+ "Too many steps in one turn",
+ );
+ await expect(canvas.getByRole("status")).toHaveTextContent(
+ "Send another message",
+ );
+ await expect(
+ canvas.getByRole("button", { name: "Worked for 12s" }),
+ ).toBeVisible();
+ await expect(
+ canvas.getByRole("button", { name: "Stopped after 51s" }),
+ ).toBeVisible();
+ await expect(
+ canvas.getByRole("textbox", { name: "Message the assistant" }),
+ ).toBeEnabled();
+ },
+});
+
+const failedRun = {
+ id: "run-1",
+ status: InAppAgentRunStatus.FAILED,
+ errorCode: InAppAgentRunErrorCode.RUN_TIMEOUT,
+ cancelRequested: false,
+};
+
+export const Failed = meta.story({
+ args: {
+ selectedConversationId: "conversation-1",
+ executionUi: {
+ notice: getBackgroundRunNotice(failedRun),
+ activityOutcome: getSettledActivityOutcome(failedRun),
+ stop: null,
+ },
+ messages: [
+ {
+ id: "user-1",
+ role: "user",
+ content: {
+ type: "text",
+ text: "Investigate latency",
+ },
+ },
+ {
+ id: "assistant-reasoning",
+ timestamp: new Date("2026-08-06T15:26:26.000Z").getTime(),
+ role: "assistant",
+ content: {
+ type: "reasoning",
+ text: "Checking the slow traces.",
+ isStreaming: false,
+ },
+ },
+ {
+ id: "assistant-answer",
+ runId: "run-1",
+ timestamp: new Date("2026-08-06T15:27:17.000Z").getTime(),
+ role: "assistant",
+ content: {
+ type: "text",
+ text: "Still inspecting the remaining traces.",
+ },
+ },
+ ],
+ },
+ play: async ({ canvasElement }) => {
+ const canvas = within(canvasElement);
+
+ await expect(canvas.getByRole("status")).toHaveTextContent(
+ "The run exceeded the maximum duration.",
+ );
+ await expect(
+ canvas.getByRole("button", { name: "Failed after 51s" }),
+ ).toBeVisible();
+ await expect(
+ canvas.getByRole("textbox", { name: "Message the assistant" }),
+ ).toBeEnabled();
+ },
+});
+
+const failedBeforeFirstTokenRun = {
+ id: "run-2",
+ status: InAppAgentRunStatus.FAILED,
+ errorCode: InAppAgentRunErrorCode.QUEUE_TIMEOUT,
+ cancelRequested: false,
+};
+
+export const FailedBeforeFirstToken = meta.story({
+ args: {
+ selectedConversationId: "conversation-1",
+ executionUi: {
+ notice: getBackgroundRunNotice(failedBeforeFirstTokenRun),
+ activityOutcome: getSettledActivityOutcome(failedBeforeFirstTokenRun),
+ stop: null,
+ },
+ messages: [
+ {
+ id: "user-1",
+ role: "user",
+ content: {
+ type: "text",
+ text: "Investigate yesterday's errors.",
+ },
+ },
+ {
+ id: "assistant-reasoning-1",
+ timestamp: new Date("2026-08-06T15:20:00.000Z").getTime(),
+ role: "assistant",
+ content: {
+ type: "reasoning",
+ text: "Checking yesterday first.",
+ isStreaming: false,
+ },
+ },
+ {
+ id: "assistant-text-1",
+ runId: "run-1",
+ timestamp: new Date("2026-08-06T15:20:12.000Z").getTime(),
+ role: "assistant",
+ content: {
+ type: "text",
+ text: "Yesterday was quiet.",
+ },
+ },
+ {
+ id: "user-2",
+ role: "user",
+ content: {
+ type: "text",
+ text: "Now look at today's traces.",
+ },
+ },
+ ],
+ },
+ play: async ({ canvasElement }) => {
+ const canvas = within(canvasElement);
+
+ await expect(canvas.getByRole("status")).toHaveTextContent(
+ "No worker picked this up",
+ );
+ await expect(
+ canvas.getByRole("button", { name: "Worked for 12s" }),
+ ).toBeVisible();
+ await expect(
+ canvas.queryByRole("button", { name: /Failed after/ }),
+ ).not.toBeInTheDocument();
+ },
+});
+
/**
* Every activity state in the row it belongs to, sharing one fixed-width slot
* so the column stays straight despite the dots being narrower than the icons.
@@ -1558,19 +1802,38 @@ export const BackgroundRun = meta.story({
},
});
+const cancelledRun = {
+ id: "run-1",
+ status: InAppAgentRunStatus.CANCELLED,
+ errorCode: InAppAgentRunErrorCode.CANCELLED,
+ cancelRequested: false,
+};
+
+const backgroundStopUserMessage = {
+ id: "user-1",
+ role: "user" as const,
+ content: {
+ type: "text" as const,
+ text: "Summarize recent ingestion errors.",
+ },
+};
+
+const backgroundStopReasoning = {
+ id: "assistant-reasoning",
+ timestamp: new Date("2026-08-06T15:26:26.000Z").getTime(),
+ role: "assistant" as const,
+ content: {
+ type: "reasoning" as const,
+ text: "I'll look at recent ingestion errors first.",
+ isStreaming: false,
+ },
+};
+
export const BackgroundRunStops = meta.story({
args: {
isAssistantTurnInProgress: true,
- messages: [
- {
- id: "user-1",
- role: "user",
- content: {
- type: "text",
- text: "Summarize recent ingestion errors.",
- },
- },
- ],
+ selectedConversationId: "conversation-1",
+ messages: [backgroundStopUserMessage, backgroundStopReasoning],
},
render: function Render(args) {
const [phase, setPhase] = useState<"running" | "stopping" | "settled">(
@@ -1599,17 +1862,12 @@ export const BackgroundRunStops = meta.story({
messages={
isSettled
? [
- {
- id: "user-1",
- role: "user",
- content: {
- type: "text",
- text: "Summarize recent ingestion errors.",
- },
- },
+ backgroundStopUserMessage,
+ backgroundStopReasoning,
{
id: "assistant-1",
runId: "run-1",
+ timestamp: new Date("2026-08-06T15:27:17.000Z").getTime(),
role: "assistant",
content: {
type: "text",
@@ -1621,9 +1879,16 @@ export const BackgroundRunStops = meta.story({
}
executionUi={
isSettled
- ? { notice: null, stop: null }
+ ? {
+ notice: getBackgroundRunNotice(cancelledRun),
+ activityOutcome: getSettledActivityOutcome(cancelledRun),
+ stop: null,
+ }
: {
- notice: phase === "stopping" ? "Stopping the run…" : null,
+ notice:
+ phase === "stopping"
+ ? { text: "Stopping the run…", tone: "info" }
+ : null,
stop: {
status: phase === "stopping" ? "stopping" : "available",
onStop: () => {
@@ -1635,6 +1900,21 @@ export const BackgroundRunStops = meta.story({
/>
);
},
+ play: async ({ canvasElement }: { canvasElement: HTMLElement }) => {
+ const canvas = within(canvasElement);
+
+ await userEvent.click(canvas.getByRole("button", { name: "Stop run" }));
+ await expect(canvas.getByRole("status")).toHaveTextContent(
+ "Stopping the run…",
+ );
+ await waitFor(
+ () =>
+ expect(
+ canvas.getByRole("button", { name: "Stopped after 51s" }),
+ ).toBeVisible(),
+ { timeout: 3_000 },
+ );
+ },
});
export const LoadingConversation = meta.story({
diff --git a/web/src/features/in-app-agent/components/InAppAgentWindow.tsx b/web/src/features/in-app-agent/components/InAppAgentWindow.tsx
index fb0b4b66d2..e4eff626ae 100644
--- a/web/src/features/in-app-agent/components/InAppAgentWindow.tsx
+++ b/web/src/features/in-app-agent/components/InAppAgentWindow.tsx
@@ -17,6 +17,7 @@ import {
History,
Info,
Maximize2,
+ TriangleAlert,
Minimize2,
Minus,
Plus,
@@ -54,8 +55,10 @@ import type {
import { IN_APP_AGENT_GENERIC_ERROR_MESSAGE } from "@langfuse/shared/in-app-agent";
import type { InAppAgentScreenContextDescription } from "@/src/features/in-app-agent/context";
import type { InAppAgentActivityByConversationId } from "@/src/features/in-app-agent/lib/inAppAgentActivity";
+import type { SettledActivityOutcome } from "@/src/features/in-app-agent/lib/backgroundExecutionSession";
import { ConversationActivityIndicator } from "@/src/features/in-app-agent/components/ConversationActivityIndicator";
import { InAppAgentBackgroundHint } from "@/src/features/in-app-agent/components/InAppAgentBackgroundHint";
+import { InAppAgentNotice } from "@/src/features/in-app-agent/components/InAppAgentNotice";
import { useInAppAgentBackgroundHint } from "@/src/features/in-app-agent/lib/useInAppAgentBackgroundHint";
import { InAppAgentToolCallCard } from "@/src/features/in-app-agent/components/InAppAgentToolCallCard";
import {
@@ -524,23 +527,44 @@ function formatWorkedDuration(totalSeconds: number) {
return `${totalSeconds}s`;
}
+function getSettledActivityGroupLabel({
+ durationSeconds,
+ outcome,
+}: {
+ durationSeconds: number | null;
+ outcome: SettledActivityOutcome;
+}) {
+ const duration =
+ durationSeconds === null ? null : formatWorkedDuration(durationSeconds);
+
+ if (outcome === "stopped") {
+ return duration ? `Stopped after ${duration}` : "Stopped";
+ }
+
+ if (outcome === "failed") {
+ return duration ? `Failed after ${duration}` : "Failed";
+ }
+
+ return duration ? `Worked for ${duration}` : "Activity";
+}
+
function getActivityGroupLabel({
durationSeconds,
hasDetails,
isAwaitingApproval,
isInProgress,
+ outcome,
toolNames,
}: {
durationSeconds: number | null;
hasDetails: boolean;
isAwaitingApproval: boolean;
isInProgress: boolean;
+ outcome: SettledActivityOutcome;
toolNames: string[];
}) {
if (!isInProgress) {
- return durationSeconds === null
- ? "Activity"
- : `Worked for ${formatWorkedDuration(durationSeconds)}`;
+ return getSettledActivityGroupLabel({ durationSeconds, outcome });
}
// The run has stopped and owes the user a decision, so it must not keep
@@ -560,6 +584,7 @@ function AssistantActivityGroup({
isCompact,
isInProgress,
messages,
+ outcome,
startTimestamp,
}: {
endTimestamp?: number;
@@ -567,6 +592,7 @@ function AssistantActivityGroup({
isCompact: boolean;
isInProgress: boolean;
messages: InAppAgentWindowMessage[];
+ outcome: SettledActivityOutcome;
startTimestamp?: number;
}) {
const hasDetails = messages.length > 0;
@@ -585,6 +611,7 @@ function AssistantActivityGroup({
hasDetails,
isAwaitingApproval,
isInProgress,
+ outcome,
toolNames,
});
@@ -812,8 +839,15 @@ type InAppAgentWindowCloseButtonProps =
onClose: () => void;
};
+export type InAppAgentWindowNotice = {
+ text: string;
+ tone: "info" | "warning";
+};
+
export type InAppAgentWindowExecutionUi = {
- notice: string | null;
+ notice: InAppAgentWindowNotice | null;
+ /** How the latest settled turn ended. Earlier turns stay "worked". */
+ activityOutcome?: SettledActivityOutcome;
stop: {
status: "available" | "stopping";
onStop: () => void;
@@ -897,41 +931,34 @@ function InAppAgentRateLimitError({
}, [error.retryAt]);
return (
- <div
+ <InAppAgentNotice
+ icon={<Info aria-hidden="true" className="size-3 shrink-0" />}
+ isExpanded={isExpanded}
role="alert"
- className={cn(
- "border-border bg-muted/60 text-foreground w-full rounded-lg border px-2 py-1",
- isExpanded ? "text-sm" : "text-xs",
- )}
+ tone="neutral"
>
- <div className="space-y-0.5">
- <p className="font-bold">
+ <span className="space-y-0.5">
+ <span className="block font-bold">
You&apos;ve reached the assistant request limit
- </p>
- <p>Try again in about {formatApproximateDuration(secondsRemaining)}.</p>
- </div>
- </div>
+ </span>
+ <span className="block">
+ Try again in about {formatApproximateDuration(secondsRemaining)}.
+ </span>
+ </span>
+ </InAppAgentNotice>
);
}
function InAppAgentIssueNotice({ isExpanded }: { isExpanded: boolean }) {
return (
- <div className="shrink-0 px-2 pb-2">
- <div className={cn(isExpanded && "mx-auto max-w-3xl")}>
- <p
- role="alert"
- className={cn(
- "border-destructive/40 bg-destructive/10 text-destructive flex w-full items-center gap-2 rounded-lg border px-2 py-1",
- isExpanded ? "text-sm" : "text-xs",
- )}
- >
- <Info aria-hidden="true" className="size-3 shrink-0" />
- <span className="min-w-0 flex-1">
- {IN_APP_AGENT_GENERIC_ERROR_MESSAGE}
- </span>
- </p>
- </div>
- </div>
+ <InAppAgentNotice
+ icon={<Info aria-hidden="true" className="size-3 shrink-0" />}
+ isExpanded={isExpanded}
+ role="alert"
+ tone="danger"
+ >
+ {IN_APP_AGENT_GENERIC_ERROR_MESSAGE}
+ </InAppAgentNotice>
);
}
@@ -1018,6 +1045,16 @@ export function InAppAgentWindow(props: InAppAgentWindowProps) {
() => buildConversationDisplayItems(messages, isRunUnsettled),
[messages, isRunUnsettled],
);
+ const lastUserIndex = displayItems.findLastIndex(
+ (item) => item.type === "user",
+ );
+ // activityOutcome is for the latest turn. If that turn never produced an
+ // activity group (failed or cancelled before the first assistant token),
+ // do not stamp the outcome onto an earlier turn.
+ const lastSettledActivityIndex = displayItems.findLastIndex(
+ (item, index) =>
+ item.type === "activity" && !item.isInProgress && index > lastUserIndex,
+ );
const hasSettledAssistantReply = displayItems.some(
(item) => item.type === "assistant" && item.isFinalAnswer,
);
@@ -1373,7 +1410,7 @@ export function InAppAgentWindow(props: InAppAgentWindowProps) {
) : null}
<ol className="flex w-full flex-col gap-1 pb-4">
- {displayItems.map((item) => {
+ {displayItems.map((item, index) => {
if (item.type === "user") {
return (
<li
@@ -1407,6 +1444,11 @@ export function InAppAgentWindow(props: InAppAgentWindowProps) {
isAwaitingApproval={
item.isInProgress && isAwaitingApproval
}
+ outcome={
+ index === lastSettledActivityIndex
+ ? (executionUi.activityOutcome ?? "worked")
+ : "worked"
+ }
/>
</li>
);
@@ -1476,22 +1518,20 @@ export function InAppAgentWindow(props: InAppAgentWindowProps) {
</div>
) : null}
{backgroundNotice ? (
- <div className="shrink-0 px-2 pb-2">
- <div className={cn(isExpanded && "mx-auto max-w-3xl")}>
- <p
- role="status"
- className={cn(
- "border-border bg-muted/60 text-foreground-tertiary flex w-full items-center gap-1 rounded-lg border px-2 py-1",
- isExpanded ? "text-sm" : "text-xs",
- )}
- >
+ <InAppAgentNotice
+ icon={
+ backgroundNotice.tone === "warning" ? (
+ <TriangleAlert aria-hidden="true" className="size-3 shrink-0" />
+ ) : (
<Info aria-hidden="true" className="size-3 shrink-0" />
- <span className="min-w-0" title={backgroundNotice}>
- {backgroundNotice}
- </span>
- </p>
- </div>
- </div>
+ )
+ }
+ isExpanded={isExpanded}
+ role="status"
+ tone={backgroundNotice.tone === "warning" ? "warning" : "neutral"}
+ >
+ <span title={backgroundNotice.text}>{backgroundNotice.text}</span>
+ </InAppAgentNotice>
) : null}
{error?.type === "generic" && (
<InAppAgentIssueNotice isExpanded={isExpanded} />
@@ -1506,15 +1546,8 @@ export function InAppAgentWindow(props: InAppAgentWindowProps) {
/>
) : null}
{error?.type === "rate_limit" && (
- <div
- className={cn(
- "shrink-0 px-2 pb-2",
- isAssistantTurnInProgress && "pt-2",
- )}
- >
- <div className={cn(isExpanded && "mx-auto max-w-3xl")}>
- <InAppAgentRateLimitError error={error} isExpanded={isExpanded} />
- </div>
+ <div className={cn(isAssistantTurnInProgress && "pt-2")}>
+ <InAppAgentRateLimitError error={error} isExpanded={isExpanded} />
</div>
)}
{isAssistantTurnInProgress && pendingToolCalls.length === 0 ? (
diff --git a/web/src/features/in-app-agent/lib/backgroundExecutionSession.ts b/web/src/features/in-app-agent/lib/backgroundExecutionSession.ts
index e1cf59e4df..6617e949aa 100644
--- a/web/src/features/in-app-agent/lib/backgroundExecutionSession.ts
+++ b/web/src/features/in-app-agent/lib/backgroundExecutionSession.ts
@@ -576,6 +576,68 @@ export function getBackgroundRunFailureMessage(
);
}
+export type BackgroundRunNoticeTone = "info" | "warning";
+
+export type BackgroundRunNotice = {
+ text: string;
+ tone: BackgroundRunNoticeTone;
+};
+
+const STEP_LIMIT_NOTICE =
+ "The assistant had to stop before finishing this answer. Too many steps in one turn. Send another message to continue.";
+
+export function getBackgroundRunNotice(
+ run: BackgroundExecutionRunView | null,
+): BackgroundRunNotice | null {
+ if (!run) {
+ return null;
+ }
+
+ if (isCancellableBackgroundRun(run.status) && run.cancelRequested) {
+ return { text: "Stopping the run…", tone: "info" };
+ }
+
+ if (run.status === InAppAgentRunStatus.FAILED) {
+ return {
+ text: getBackgroundRunFailureMessage(run.errorCode ?? null),
+ tone: "info",
+ };
+ }
+
+ if (
+ run.status === InAppAgentRunStatus.SUCCEEDED &&
+ run.errorCode === InAppAgentRunErrorCode.STEP_LIMIT
+ ) {
+ return { text: STEP_LIMIT_NOTICE, tone: "warning" };
+ }
+
+ return null;
+}
+
+export type SettledActivityOutcome = "worked" | "stopped" | "failed";
+
+export function getSettledActivityOutcome(
+ run: BackgroundExecutionRunView | null,
+): SettledActivityOutcome {
+ if (!run) {
+ return "worked";
+ }
+
+ if (
+ (run.status === InAppAgentRunStatus.SUCCEEDED &&
+ run.errorCode === InAppAgentRunErrorCode.STEP_LIMIT) ||
+ run.status === InAppAgentRunStatus.CANCELLED
+ ) {
+ return "stopped";
+ }
+
+ if (run.status === InAppAgentRunStatus.FAILED) {
+ return "failed";
+ }
+
+ return "worked";
+}
+
const BACKGROUND_RUN_FAILURE_MESSAGES: Readonly<Record<string, string>> = {
[InAppAgentRunErrorCode.ENQUEUE_FAILED]: "Couldn't start the run. Try again.",
[InAppAgentRunErrorCode.QUEUE_TIMEOUT]:
diff --git a/worker/src/features/in-app-agent/executeInAppAgentRun.test.ts b/worker/src/features/in-app-agent/executeInAppAgentRun.test.ts
index 4259e50c2d..ea23fab6f2 100644
--- a/worker/src/features/in-app-agent/executeInAppAgentRun.test.ts
+++ b/worker/src/features/in-app-agent/executeInAppAgentRun.test.ts
@@ -52,7 +52,7 @@ type AgentScenario = (ctx: {
};
onEvent: (event: unknown) => Promise<void> | void;
onApprovedToolCallExecuted?: () => Promise<void> | void;
- onComplete: () => Promise<void>;
+ onComplete: (outcome?: { truncatedByStepLimit?: boolean }) => Promise<void>;
onAbort: () => Promise<void>;
onError: (error: unknown) => Promise<void>;
onFinish: () => Promise<void>;
@@ -463,6 +463,23 @@ describe("executeInAppAgentRun", () => {
expect((merged!.event as { delta?: string }).delta).toBe("Hello world");
});
+ it("records step_limit on SUCCEEDED when the loop hits the cap without a stop finish", async () => {
+ const { projectId, run } = await seedBackgroundRun();
+
+ scenarioRef.current = async ({ options }) => {
+ await options.onEvent(textChunk("Still calling tools"));
+ await options.onComplete({ truncatedByStepLimit: true });
+ await options.onFinish();
+ };
+
+ await executeInAppAgentRun({ projectId, runId: run.id });
+
+ const finished = await getRun(projectId, run.id);
+ expect(finished.status).toBe("SUCCEEDED");
+ expect(finished.errorCode).toBe("step_limit");
+ expect(finished.errorMessage).toMatch(/step limit/i);
+ });
+
it("acknowledges duplicate delivery without executing (claim CAS returns no row)", async () => {
const { projectId, run } = await seedBackgroundRun({ status: "RUNNING" });
diff --git a/worker/src/features/in-app-agent/executeInAppAgentRun.ts b/worker/src/features/in-app-agent/executeInAppAgentRun.ts
index 05dd1d3349..cf817a79c4 100644
--- a/worker/src/features/in-app-agent/executeInAppAgentRun.ts
+++ b/worker/src/features/in-app-agent/executeInAppAgentRun.ts
@@ -450,11 +450,18 @@ export async function executeInAppAgentRun(params: {
return flushPersistedRunEvents();
},
onMcpToolCallCompleted: sandboxToolCallFiles.processToolCall,
- onComplete: async () => {
+ onComplete: async (outcome) => {
await flushPersistedRunEvents(
interruptRequest
? { status: InAppAgentRunStatus.AWAITING_APPROVAL }
- : { status: InAppAgentRunStatus.SUCCEEDED },
+ : outcome?.truncatedByStepLimit
+ ? {
+ status: InAppAgentRunStatus.SUCCEEDED,
+ errorCode: InAppAgentRunErrorCode.STEP_LIMIT,
+ errorMessage:
+ "The run reached the step limit before a final answer",
+ }
+ : { status: InAppAgentRunStatus.SUCCEEDED },
);
},
onAbort: async () => {
diff --git a/worker/src/features/in-app-agent/runtime/agent.test.ts b/worker/src/features/in-app-agent/runtime/agent.test.ts
index 985fbbb0a7..442ebcdab3 100644
--- a/worker/src/features/in-app-agent/runtime/agent.test.ts
+++ b/worker/src/features/in-app-agent/runtime/agent.test.ts
@@ -10,6 +10,7 @@ import {
IN_APP_AGENT_TOOL_REJECTION_ERROR_CODE,
} from "@langfuse/shared/in-app-agent";
import { createInAppAgentToolPolicy } from "@langfuse/shared/in-app-agent/server/mcpPolicy";
+import { IN_APP_AGENT_MAX_STEPS } from "@langfuse/shared/in-app-agent/server/tunables";
import { patchMastraApprovalChunks, type createAgUiStream } from "./agent";
import {
createInAppAgentSandbox,
@@ -51,6 +52,33 @@ const getAgentTools = (
): MockedAgentTools | undefined =>
agentConfig?.tools as MockedAgentTools | undefined;
+type MockedAgentConfig = {
+ instructions?: (() => string) | string;
+ model?: {
+ doStream: (options: unknown) => Promise<{
+ stream: ReadableStream<unknown>;
+ }>;
+ };
+ defaultOptions?: {
+ onIterationComplete?: (context: {
+ iteration: number;
+ maxIterations?: number;
+ isFinal: boolean;
+ finishReason: string;
+ }) => unknown;
+ };
+};
+
+const getLastAgentConfig = () =>
+ vi.mocked(Agent).mock.calls.at(-1)?.[0] as MockedAgentConfig | undefined;
+
+const readAgentInstructions = (agentConfig: MockedAgentConfig | undefined) => {
+ const instructions = agentConfig?.instructions;
+ return typeof instructions === "function"
+ ? instructions()
+ : (instructions ?? "");
+};
+
const adapterEvents = vi.hoisted(() => ({
items: [] as AgUiEvent[],
cleanup: vi.fn().mockResolvedValue(undefined),
@@ -604,6 +632,57 @@ describe("createAgUiStream", () => {
).toHaveBeenNthCalledWith(2, finishPart);
});
+ it("injects wrap-up instructions after the penultimate Mastra iteration", async () => {
+ await initializeBasicTracedAgent("run-step-limit-wrap-up");
+ const agentConfig = getLastAgentConfig();
+ const onIterationComplete =
+ agentConfig?.defaultOptions?.onIterationComplete;
+ expect(onIterationComplete).toEqual(expect.any(Function));
+
+ expect(readAgentInstructions(agentConfig)).not.toContain(
+ "Do not call any more tools",
+ );
+
+ const wrapUp = await onIterationComplete?.({
+ iteration: IN_APP_AGENT_MAX_STEPS - 1,
+ maxIterations: IN_APP_AGENT_MAX_STEPS,
+ isFinal: false,
+ finishReason: "tool-calls",
+ });
+
+ expect(wrapUp).toEqual({
+ feedback: expect.stringContaining("<step_limit_wrap_up>"),
+ });
+ expect(readAgentInstructions(agentConfig)).toContain(
+ "Do not call any more tools",
+ );
+ });
+
+ it("does not treat provider stream finishes as Mastra steps", async () => {
+ await initializeBasicTracedAgent("run-step-limit-retry");
+ const agentConfig = getLastAgentConfig();
+ const model = agentConfig?.model;
+ expect(model).toBeDefined();
+
+ bedrockMocks.streamParts = [
+ {
+ type: "finish",
+ finishReason: { unified: "tool-calls", raw: "tool_use" },
+ },
+ ];
+
+ for (let i = 0; i < IN_APP_AGENT_MAX_STEPS - 1; i++) {
+ const result = await model!.doStream({});
+ for await (const _part of result.stream) {
+ // Drain provider finishes that used to be counted as steps.
+ }
+ }
+
+ expect(readAgentInstructions(agentConfig)).not.toContain(
+ "Do not call any more tools",
+ );
+ });
+
it("serializes valid events including adapter snapshots and reasoning messages", async () => {
const { createAgUiStream } = await import("./agent");
const input = {
@@ -799,7 +878,7 @@ describe("createAgUiStream", () => {
);
const agentConfig = vi.mocked(Agent).mock.calls[0]?.[0];
expect(agentConfig?.defaultOptions).toMatchObject({
- maxSteps: expect.any(Number),
+ maxSteps: IN_APP_AGENT_MAX_STEPS,
providerOptions: {
bedrock: {
additionalModelRequestFields: {
@@ -1030,7 +1109,7 @@ describe("createAgUiStream", () => {
const { Agent } = await import("@mastra/core/agent");
const agentConfig = vi.mocked(Agent).mock.calls[0]?.[0];
expect(agentConfig?.defaultOptions).toMatchObject({
- maxSteps: expect.any(Number),
+ maxSteps: IN_APP_AGENT_MAX_STEPS,
});
expect(agentConfig?.defaultOptions).not.toHaveProperty("providerOptions");
});
diff --git a/worker/src/features/in-app-agent/runtime/agent.ts b/worker/src/features/in-app-agent/runtime/agent.ts
index 6cd2396469..c2acb2dda0 100644
--- a/worker/src/features/in-app-agent/runtime/agent.ts
+++ b/worker/src/features/in-app-agent/runtime/agent.ts
@@ -11,6 +11,7 @@ import {
type InAppAgentToolApprovalRequest,
} from "@langfuse/shared/in-app-agent";
import { getToolFailureMessage } from "@langfuse/shared/in-app-agent/server/toolErrors";
+import { IN_APP_AGENT_MAX_STEPS } from "@langfuse/shared/in-app-agent/server/tunables";
import type { AgUiRunAgentInput, ResumeForwardedProps } from "./types";
import { createManualToolApprovalRunInput } from "./human-in-the-loop";
import type {
@@ -56,7 +57,6 @@ import type { InAppAgentModelConfig } from "@langfuse/shared/in-app-agent/server
const ASSISTANT_TITLE = "Langfuse Assistant";
const IN_APP_AGENT_SYSTEM_PROMPT_NAME = "in-app-agent-system-prompt";
-const MAX_AGENT_STEPS = 20;
const BEDROCK_CLAUDE_MODEL_ID_PART = "anthropic.claude";
const LANGFUSE_DOCS_MCP_URL = "https://langfuse.com/api/mcp";
const IN_APP_AGENT_MCP_USER_AGENT = "langfuse-in-app-agent";
@@ -154,6 +154,27 @@ The sandbox workspace from earlier turns in this conversation expired and has be
- Do not assume a path exists because you created it earlier in this conversation. Read it first, and recreate what you still need.
</sandbox_workspace_reset>`;
+const STEP_LIMIT_WRAP_UP_INSTRUCTION = `<step_limit_wrap_up>
+This is your final step. Do not call any more tools. Summarize what you have found and give the user a complete final answer now.
+</step_limit_wrap_up>`;
+
+export type InAppAgentCompleteOutcome = {
+ truncatedByStepLimit: boolean;
+};
+
+type StepLimitState = {
+ iteration: number;
+ lastFinishReason: string | undefined;
+ wrapUp: boolean;
+};
+
+function isTruncatedByStepLimit(state: StepLimitState): boolean {
+ return (
+ state.iteration >= IN_APP_AGENT_MAX_STEPS &&
+ state.lastFinishReason !== "stop"
+ );
+}
+
// Adaptive thinking is the default for every Claude model so new generations
// work without maintaining a model list. Older models that only support
// thinking.type.enabled (e.g. haiku 4.5) reject adaptive with a 400 — the
@@ -181,7 +202,7 @@ type CreateAgUiStreamOptions = {
onEvent?: (event: AgUiEvent) => void | Promise<void>;
onMcpToolCallCompleted?: (toolCall: CompletedInAppAgentMcpToolCall) => void;
onApprovedToolCallExecuted?: () => void | Promise<void>;
- onComplete?: () => void | Promise<void>;
+ onComplete?: (outcome?: InAppAgentCompleteOutcome) => void | Promise<void>;
onAbort?: () => void | Promise<void>;
onError?: (error: unknown) => void | Promise<void>;
onFinish?: () => void | Promise<void>;
@@ -262,20 +283,21 @@ export async function createAgUiStream(params: {
recordInstrumentation("recordAvailableSkills", (instrumentation) =>
instrumentation.recordAvailableSkills?.(LANGFUSE_IN_APP_AGENT_SKILLS),
);
- const onModelCallStart = instrumentation
- ? (options: unknown) => {
- recordInstrumentation("recordModelCallStart", (instrumentation) =>
- instrumentation.recordModelCallStart?.(options),
- );
- }
- : undefined;
- const onModelStreamPart = instrumentation
- ? (part: unknown) => {
- recordInstrumentation("recordModelStreamPart", (instrumentation) =>
- instrumentation.recordModelStreamPart?.(part),
- );
- }
- : undefined;
+ const stepLimitState: StepLimitState = {
+ iteration: 0,
+ lastFinishReason: undefined,
+ wrapUp: false,
+ };
+ const onModelCallStart = (options: unknown) => {
+ recordInstrumentation("recordModelCallStart", (instrumentation) =>
+ instrumentation.recordModelCallStart?.(options),
+ );
+ };
+ const onModelStreamPart = (part: unknown) => {
+ recordInstrumentation("recordModelStreamPart", (instrumentation) =>
+ instrumentation.recordModelStreamPart?.(part),
+ );
+ };
const onToolExecutionStart = instrumentation
? (toolCallId: string) => {
recordInstrumentation("recordToolExecutionStart", (instrumentation) =>
@@ -551,6 +573,7 @@ export async function createAgUiStream(params: {
onModelStreamPart,
onToolExecutionStart,
onToolExecutionEnd,
+ stepLimitState,
})
.then(async (initialAdapter) => {
if (ending || closed || params.signal.aborted) {
@@ -622,6 +645,7 @@ export async function createAgUiStream(params: {
onModelStreamPart,
onToolExecutionStart,
onToolExecutionEnd,
+ stepLimitState,
});
if (ending || closed || params.signal.aborted) {
@@ -750,13 +774,26 @@ export async function createAgUiStream(params: {
closeController(
streamedRunError === null
? () => {
+ const truncatedByStepLimit =
+ isTruncatedByStepLimit(stepLimitState);
recordInstrumentation("end", (instrumentation) =>
- instrumentation.end({}),
+ instrumentation.end(
+ truncatedByStepLimit
+ ? {
+ result: {
+ truncatedByStepLimit: true,
+ finishReason: stepLimitState.lastFinishReason,
+ },
+ }
+ : {},
+ ),
);
recordInstrumentation("flush", (instrumentation) =>
instrumentation.flush(),
);
- return params.options.onComplete?.();
+ return params.options.onComplete?.({
+ truncatedByStepLimit,
+ });
}
: () => {
recordInstrumentation("flush", (instrumentation) =>
@@ -941,6 +978,7 @@ async function createMastraAdapter(params: {
onModelStreamPart?: (part: unknown) => void;
onToolExecutionStart?: (toolCallId: string) => void;
onToolExecutionEnd?: (toolCallId: string) => void;
+ stepLimitState: StepLimitState;
}) {
const bedrock = createAmazonBedrock({
...(params.options.model.region
@@ -1083,14 +1121,41 @@ async function createMastraAdapter(params: {
id: "langfuse-in-app-assistant",
name: ASSISTANT_TITLE,
instructions: () =>
- [params.instructions, developerGuidance].filter(Boolean).join("\n\n"),
+ [
+ params.instructions,
+ developerGuidance,
+ params.stepLimitState.wrapUp
+ ? STEP_LIMIT_WRAP_UP_INSTRUCTION
+ : undefined,
+ ]
+ .filter(Boolean)
+ .join("\n\n"),
model,
skills: LANGFUSE_IN_APP_AGENT_SKILLS,
tools,
maxRetries: 2,
defaultOptions: {
abortSignal: params.signal,
- maxSteps: MAX_AGENT_STEPS,
+ maxSteps: IN_APP_AGENT_MAX_STEPS,
+ // Mastra's logical step counter — not provider doStream/finish parts,
+ // which retry and inflate independently of maxSteps.
+ onIterationComplete: ({
+ iteration,
+ maxIterations,
+ isFinal,
+ finishReason,
+ }) => {
+ params.stepLimitState.iteration = iteration;
+ params.stepLimitState.lastFinishReason = finishReason;
+ if (
+ iteration === (maxIterations ?? IN_APP_AGENT_MAX_STEPS) - 1 &&
+ !isFinal
+ ) {
+ params.stepLimitState.wrapUp = true;
+ // instructions() is snapshotted at stream start; feedback reaches the next call.
+ return { feedback: STEP_LIMIT_WRAP_UP_INSTRUCTION };
+ }
+ },
...(params.options.sandboxWorkspaceWasReset
? { system: SANDBOX_WORKSPACE_RESET_INSTRUCTION }
: {}),