Skip to content

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

fix(agent): Instrument user info on in-app agent requests (#15317)

Ben Bachemc95b3f97ace1
diff --git a/web/src/__tests__/server/in-app-agent-api-route-auth.servertest.ts b/web/src/__tests__/server/in-app-agent-api-route-auth.servertest.ts
index 1e42c80874..0d95c13a94 100644
--- a/web/src/__tests__/server/in-app-agent-api-route-auth.servertest.ts
+++ b/web/src/__tests__/server/in-app-agent-api-route-auth.servertest.ts
@@ -39,6 +39,10 @@ const agentMocks = vi.hoisted(() => ({
createAgUiStream: vi.fn(),
}));
+const instrumentationMocks = vi.hoisted(() => ({
+ addUserToSpan: vi.fn(),
+}));
+
const SANDBOX_CONVERSATION_WRITE_LOCK_MESSAGE =
"Sandbox-enabled conversations become read-only after 8 hours. Start a new conversation to continue.";
@@ -80,6 +84,11 @@ vi.mock("@/src/features/natural-language-filters/server/utils", () => ({
getLangfuseClient: langfuseClientMocks.getLangfuseClient,
}));
+vi.mock("@langfuse/shared/src/server", async (importOriginal) => ({
+ ...(await importOriginal<Record<string, unknown>>()),
+ addUserToSpan: instrumentationMocks.addUserToSpan,
+}));
+
describe("in-app agent public API route auth", () => {
beforeEach(() => {
vi.clearAllMocks();
@@ -184,6 +193,21 @@ describe("in-app agent public API route auth", () => {
).toEqual(tools);
});
+ it("adds the authenticated user to the request span", async () => {
+ authMocks.getServerSession.mockResolvedValue(
+ createInAppAgentSession({ orgId: "org-1", projectId: "project-1" }),
+ );
+
+ const { default: handler } =
+ await import("@/src/ee/features/in-app-agent/server/handler");
+ await handler(new Request("http://localhost/api/in-app-agent"));
+
+ expect(instrumentationMocks.addUserToSpan).toHaveBeenCalledWith({
+ userId: "user-1",
+ email: "[email protected]",
+ });
+ });
+
it("passes validated resume forwarded props without requiring a user message", async () => {
const originalCloudRegion = env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION;
const originalBedrockModel = env.LANGFUSE_AWS_BEDROCK_MODEL;
@@ -791,6 +815,10 @@ describe("in-app agent public API route auth", () => {
orgId: org.id,
projectId: project.id,
});
+ const sessionUser = session.user;
+ if (!sessionUser) {
+ throw new Error("Expected an authenticated session user");
+ }
authMocks.getServerSession.mockResolvedValue(session);
rateLimitMocks.rateLimitRequest.mockResolvedValue({
isRateLimited: () => true,
@@ -850,6 +878,13 @@ describe("in-app agent public API route auth", () => {
}),
"in-app-agent-run",
);
+ expect(instrumentationMocks.addUserToSpan).toHaveBeenLastCalledWith({
+ userId: sessionUser.id,
+ email: sessionUser.email,
+ projectId: project.id,
+ orgId: org.id,
+ plan: "cloud:team",
+ });
} finally {
(env as any).NEXT_PUBLIC_LANGFUSE_CLOUD_REGION = originalCloudRegion;
(env as any).LANGFUSE_AWS_BEDROCK_MODEL = originalBedrockModel;
diff --git a/web/src/ee/features/in-app-agent/server/handler.ts b/web/src/ee/features/in-app-agent/server/handler.ts
index 9e81204ae3..338b9c4fe0 100644
--- a/web/src/ee/features/in-app-agent/server/handler.ts
+++ b/web/src/ee/features/in-app-agent/server/handler.ts
@@ -79,6 +79,7 @@ import {
} from "@langfuse/shared";
import { prisma } from "@langfuse/shared/src/db";
import {
+ addUserToSpan,
logger,
redis,
TableViewService,
@@ -106,6 +107,11 @@ export default async function handler(request: Request) {
const user = session.user;
const userId = user.id;
+ addUserToSpan({
+ userId,
+ email: user.email ?? undefined,
+ });
+
if (!env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION) {
throw new BaseError(
"PreconditionFailedError",
@@ -243,13 +249,21 @@ export default async function handler(request: Request) {
false,
);
- // TODO: Add an additional user-level cap once the rate-limit service supports non-org keys.
- const rateLimitScope = getInAppAgentRateLimitScope(
+ const rateLimitScope = getInAppAgentApiAccessScope(
user,
projectId,
project.organization,
);
+ addUserToSpan({
+ userId,
+ email: user.email ?? undefined,
+ projectId: rateLimitScope.projectId ?? undefined,
+ orgId: rateLimitScope.orgId,
+ plan: rateLimitScope.plan,
+ });
+
+ // TODO: Add an additional user-level cap once the rate-limit service supports non-org keys.
const rateLimitResponse = await rateLimitInAppAgentRequest(
rateLimitScope,
"in-app-agent-run",
@@ -661,7 +675,7 @@ function getInAppAgentUserAccess(
};
}
-function getInAppAgentRateLimitScope(
+function getInAppAgentApiAccessScope(
user: SessionUser,
projectId: string,
projectOrganization: {