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(agent): allow standard-tier fallback for flex runs (#106630)

Georgiy Tarasovb462aa21b815
diff --git a/products/desktop/packages/agent/src/server/agent-server.configure-environment.test.ts b/products/desktop/packages/agent/src/server/agent-server.configure-environment.test.ts
index 69c9c2208c99..39855a0a0cf4 100644
--- a/products/desktop/packages/agent/src/server/agent-server.configure-environment.test.ts
+++ b/products/desktop/packages/agent/src/server/agent-server.configure-environment.test.ts
@@ -525,7 +525,9 @@ describe("AgentServer.configureEnvironment on the Go ai-gateway", () => {
it("leaves an unlisted product on the Python gateway, slug and all", () => {
process.env.AI_GATEWAY_PRODUCTS = "signals_scout";
- const env = buildServer().configureEnvironment({ isInternal: false });
+ const env = buildServer({ serviceTier: "flex" }).configureEnvironment({
+ isInternal: false,
+ });
expect(env.anthropicBaseUrl).toBe(
"https://gateway.us.posthog.com/posthog_code",
@@ -533,6 +535,12 @@ describe("AgentServer.configureEnvironment on the Go ai-gateway", () => {
expect(env.anthropicCustomHeaders).toContain(
"x-posthog-property-task_internal",
);
+ expect(env.openaiCustomHeaders).not.toHaveProperty(
+ "X-PostHog-Service-Tier",
+ );
+ expect(env.openaiCustomHeaders).not.toHaveProperty(
+ "X-PostHog-Flex-Fallback",
+ );
});
it.each([
@@ -601,15 +609,33 @@ describe("AgentServer.configureEnvironment on the Go ai-gateway", () => {
// The gateway writes the tier into the OpenAI body from this header, so a
// run that loses it silently runs on the standard queue and a flex trial
// measures nothing. Codex-only: the Claude header lines never carry it.
- it("sends a configured service tier as X-PostHog-Service-Tier on the OpenAI record", () => {
- const env = buildServer({ serviceTier: "flex" }).configureEnvironment({
- originProduct: "signal_report",
- aiStage: "scout",
- });
+ it.each([
+ { serviceTier: "flex", fallback: "standard" },
+ { serviceTier: "default", fallback: undefined },
+ { serviceTier: "priority", fallback: undefined },
+ { serviceTier: undefined, fallback: undefined },
+ ] as const)(
+ "sends service tier $serviceTier with fallback $fallback on the OpenAI record",
+ ({ serviceTier, fallback }) => {
+ const env = buildServer({ serviceTier }).configureEnvironment({
+ originProduct: "signal_report",
+ aiStage: "scout",
+ });
- expect(env.openaiCustomHeaders?.["X-PostHog-Service-Tier"]).toBe("flex");
- expect(env.anthropicCustomHeaders).not.toContain("X-PostHog-Service-Tier");
- });
+ expect(env.openaiCustomHeaders?.["X-PostHog-Service-Tier"]).toBe(
+ serviceTier,
+ );
+ expect(env.openaiCustomHeaders?.["X-PostHog-Flex-Fallback"]).toBe(
+ fallback,
+ );
+ expect(env.anthropicCustomHeaders).not.toContain(
+ "X-PostHog-Service-Tier",
+ );
+ expect(env.anthropicCustomHeaders).not.toContain(
+ "X-PostHog-Flex-Fallback",
+ );
+ },
+ );
// The header outranks `traceparent`, so giving it to Claude would replace the
// per-turn ids its CLI mints with one id for the whole run.
diff --git a/products/desktop/packages/agent/src/server/gateway-env.ts b/products/desktop/packages/agent/src/server/gateway-env.ts
index 026ac95dbb2d..40b619903c7a 100644
--- a/products/desktop/packages/agent/src/server/gateway-env.ts
+++ b/products/desktop/packages/agent/src/server/gateway-env.ts
@@ -144,6 +144,9 @@ export function buildGatewayEnv(
// so it rides the OpenAI record; the Claude path has no tier concept.
if (serviceTier) {
openaiCustomHeaders["X-PostHog-Service-Tier"] = serviceTier;
+ if (serviceTier === "flex") {
+ openaiCustomHeaders["X-PostHog-Flex-Fallback"] = "standard";
+ }
}
// Codex sends no trace header, so the gateway stamps a fresh id per
// request and a run's generations each land in a trace of one. Codex-only:
diff --git a/products/desktop/packages/agent/src/server/types.ts b/products/desktop/packages/agent/src/server/types.ts
index 6351a454081c..98b0d15abc64 100644
--- a/products/desktop/packages/agent/src/server/types.ts
+++ b/products/desktop/packages/agent/src/server/types.ts
@@ -71,6 +71,8 @@ export interface AgentServerConfig {
* a model its catalogue does not advertise the tier for, and as the
* `X-PostHog-Service-Tier` header on every request to the Go gateway, which
* writes it into the body itself.
+ * Flex also permits the gateway's standard-tier fallback when capacity is
+ * unavailable, so a flex run can incur standard-tier charges.
*/
serviceTier?: ServiceTier;
contextWindow?: "200k" | "1m";