Skip to content

feat(agent): forward ai_stage as a gateway trace property#2737

Merged
joshsny merged 1 commit into
mainfrom
posthog-code/signals-agentic-trace-ai-stage
Jun 17, 2026
Merged

feat(agent): forward ai_stage as a gateway trace property#2737
joshsny merged 1 commit into
mainfrom
posthog-code/signals-agentic-trace-ai-stage

Conversation

@joshsny

@joshsny joshsny commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

Problem

The signals agentic research runs (repo selection + report research) execute inside the sandbox agent, whose LLM calls route through the gateway tagged ai_product=signals. They carried signal_report_id (for the research run) but no ai_stage, so the spend couldn't be broken down by pipeline stage the way the rest of the signals pipeline can.

Changes

configureEnvironment now reads ai_stage from the task run state (getTaskRunStateString(preTaskRun, "ai_stage")) and includes it in the buildGatewayPropertyHeaders bag, so it's forwarded as an x-posthog-property-ai_stage header on the agent's Anthropic calls. The gateway lifts it onto the $ai_generation event.

The producing side — setting TaskRun.state["ai_stage"] to "research" / "repo_selection" and extending signal_report_id to the repo-selection run — is in the companion PostHog/posthog PR (#64367).

How did you test this?

I'm an agent (PostHog Slack app). I ran locally:

  • vitest run on agent-server.configure-environment.test.ts — 16 passed, including a new assertion that ai_stage is forwarded and one that it's omitted when absent.
  • tsc --noEmit typecheck on @posthog/agent — clean.
  • biome check on the changed files — clean.

Automatic notifications

  • Publish to changelog?
  • Alert Sales and Marketing teams?

Read `ai_stage` from the task run state and forward it as an
`x-posthog-property-ai_stage` header alongside the existing trace properties,
so the gateway lifts it onto the agent's $ai_generation events.

The signals pipeline sets this on TaskRun.state ("research" / "repo_selection")
in PostHog/posthog so its agentic research spend can be broken down by stage.

Generated-By: PostHog Code
Task-Id: 44afda91-f6e6-416a-95fd-a99ddaeea413
@joshsny joshsny marked this pull request as ready for review June 17, 2026 18:07
@github-actions

Copy link
Copy Markdown

React Doctor found no issues in the changed files. 🎉

Reviewed by React Doctor for commit 7eb5cf2.

@greptile-apps

greptile-apps Bot commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

Reviews (1): Last reviewed commit: "feat(agent): forward ai_stage as a gatew..." | Re-trigger Greptile

@joshsny joshsny merged commit 229dd75 into main Jun 17, 2026
23 of 24 checks passed
@joshsny joshsny deleted the posthog-code/signals-agentic-trace-ai-stage branch June 17, 2026 19:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants