Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(desktop): gate user spend limit requests (#89106)

Shy Alter2f24fc1da731
diff --git a/products/desktop/packages/ui/src/features/billing/useUserSpendLimit.test.tsx b/products/desktop/packages/ui/src/features/billing/useUserSpendLimit.test.tsx
new file mode 100644
index 000000000000..b6b3d55b059c
--- /dev/null
+++ b/products/desktop/packages/ui/src/features/billing/useUserSpendLimit.test.tsx
@@ -0,0 +1,73 @@
+import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
+import { renderHook, waitFor } from "@testing-library/react";
+import type { ReactNode } from "react";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+const mocks = vi.hoisted(() => ({
+ enabled: false,
+ getUserSpendLimit: vi.fn(),
+ setUserSpendLimit: vi.fn(),
+}));
+
+vi.mock("@posthog/ui/features/auth/authClient", () => ({
+ useOptionalAuthenticatedClient: () => ({
+ getUserSpendLimit: mocks.getUserSpendLimit,
+ setUserSpendLimit: mocks.setUserSpendLimit,
+ }),
+}));
+
+vi.mock("@posthog/ui/features/feature-flags/useFeatureFlag", () => ({
+ useFeatureFlag: () => mocks.enabled,
+}));
+
+import { useSetUserSpendLimit, useUserSpendLimit } from "./useUserSpendLimit";
+
+function wrapper({ children }: { children: ReactNode }) {
+ const queryClient = new QueryClient({
+ defaultOptions: { queries: { retry: false } },
+ });
+ return (
+ <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
+ );
+}
+
+describe("useUserSpendLimit", () => {
+ beforeEach(() => {
+ mocks.enabled = false;
+ mocks.getUserSpendLimit.mockReset();
+ mocks.setUserSpendLimit.mockReset();
+ mocks.getUserSpendLimit.mockResolvedValue({
+ available: true,
+ limitUsd: null,
+ windowSeconds: null,
+ });
+ });
+
+ it("does not call the endpoint while its rollout flag is off", () => {
+ renderHook(() => useUserSpendLimit(), { wrapper });
+
+ expect(mocks.getUserSpendLimit).not.toHaveBeenCalled();
+ });
+
+ it("calls the endpoint when its rollout flag is on", async () => {
+ mocks.enabled = true;
+
+ renderHook(() => useUserSpendLimit(), { wrapper });
+
+ await waitFor(() => expect(mocks.getUserSpendLimit).toHaveBeenCalledOnce());
+ });
+
+ it("does not write to the endpoint while its rollout flag is off", async () => {
+ const { result } = renderHook(() => useSetUserSpendLimit(), { wrapper });
+
+ await expect(
+ result.current.mutateAsync({ limitUsd: 10, windowSeconds: 60 }),
+ ).resolves.toEqual({
+ available: false,
+ limitUsd: null,
+ windowSeconds: null,
+ });
+
+ expect(mocks.setUserSpendLimit).not.toHaveBeenCalled();
+ });
+});
diff --git a/products/desktop/packages/ui/src/features/billing/useUserSpendLimit.ts b/products/desktop/packages/ui/src/features/billing/useUserSpendLimit.ts
index 4976063495d6..7567b7daf33f 100644
--- a/products/desktop/packages/ui/src/features/billing/useUserSpendLimit.ts
+++ b/products/desktop/packages/ui/src/features/billing/useUserSpendLimit.ts
@@ -1,8 +1,11 @@
import type { UserSpendLimit } from "@posthog/api-client/spend-limit";
import { useOptionalAuthenticatedClient } from "@posthog/ui/features/auth/authClient";
import { AUTH_SCOPED_QUERY_META } from "@posthog/ui/features/auth/useCurrentUser";
+import { useFeatureFlag } from "@posthog/ui/features/feature-flags/useFeatureFlag";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
+export const USER_SPEND_LIMIT_FLAG = "ai-gateway-user-spend-limit";
+
export const USER_SPEND_LIMIT_QUERY_KEY = [
"billing",
"user-spend-limit",
@@ -11,13 +14,16 @@ export const USER_SPEND_LIMIT_QUERY_KEY = [
/** The stop line the gateway holds, which is what actually refuses spend. */
export function useUserSpendLimit() {
const client = useOptionalAuthenticatedClient();
+ const enabled = useFeatureFlag(USER_SPEND_LIMIT_FLAG);
return useQuery({
queryKey: USER_SPEND_LIMIT_QUERY_KEY,
queryFn: () => {
if (!client) throw new Error("Not authenticated");
return client.getUserSpendLimit();
},
- enabled: client !== null,
+ // This endpoint can arrive independently of Desktop. Do not request it
+ // until its production deployment has been verified.
+ enabled: client !== null && enabled,
staleTime: 60_000,
retry: false,
// The stop limit is per user: drop it on any auth transition so a new
@@ -33,6 +39,7 @@ export function useUserSpendLimit() {
*/
export function useSetUserSpendLimit() {
const client = useOptionalAuthenticatedClient();
+ const enabled = useFeatureFlag(USER_SPEND_LIMIT_FLAG);
const queryClient = useQueryClient();
return useMutation({
mutationFn: (input: {
@@ -40,6 +47,13 @@ export function useSetUserSpendLimit() {
windowSeconds: number;
}): Promise<UserSpendLimit> => {
if (!client) throw new Error("Not authenticated");
+ if (!enabled) {
+ return Promise.resolve({
+ available: false,
+ limitUsd: null,
+ windowSeconds: null,
+ });
+ }
return input.limitUsd === null
? client.clearUserSpendLimit()
: client.setUserSpendLimit(input.limitUsd, input.windowSeconds);
diff --git a/products/desktop/packages/ui/src/features/settings/sections/SpendLimitsSettings.tsx b/products/desktop/packages/ui/src/features/settings/sections/SpendLimitsSettings.tsx
index 6c38ad700a21..f7eb33ff117a 100644
--- a/products/desktop/packages/ui/src/features/settings/sections/SpendLimitsSettings.tsx
+++ b/products/desktop/packages/ui/src/features/settings/sections/SpendLimitsSettings.tsx
@@ -11,10 +11,12 @@ import {
useSpendTotals,
} from "@posthog/ui/features/billing/useSpendTotals";
import {
+ USER_SPEND_LIMIT_FLAG,
USER_SPEND_LIMIT_QUERY_KEY,
useSetUserSpendLimit,
useUserSpendLimit,
} from "@posthog/ui/features/billing/useUserSpendLimit";
+import { useFeatureFlag } from "@posthog/ui/features/feature-flags/useFeatureFlag";
import { SettingsSubsection } from "@posthog/ui/features/settings/components/SettingsSubsection";
import { SpendLimitCard } from "@posthog/ui/features/settings/sections/SpendLimitCard";
import { useSettingsStore } from "@posthog/ui/features/settings/settingsStore";
@@ -30,6 +32,7 @@ import { useEffect } from "react";
const MONTH_WINDOW_SECONDS = 30 * 24 * 60 * 60;
export function SpendLimitsSettings() {
+ const enabled = useFeatureFlag(USER_SPEND_LIMIT_FLAG);
const spendLimits = useSettingsStore((state) => state.spendLimits);
const setSpendLimits = useSettingsStore((state) => state.setSpendLimits);
const totals = useSpendTotals();
@@ -81,6 +84,10 @@ export function SpendLimitsSettings() {
);
};
+ if (!enabled) {
+ return null;
+ }
+
return (
<SpendLimitsSettingsView
spendLimits={spendLimits}