feat(agent): forward ai_stage as a gateway trace property#2737
Merged
Conversation
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
|
React Doctor found no issues in the changed files. 🎉 Reviewed by React Doctor for commit |
Contributor
|
Reviews (1): Last reviewed commit: "feat(agent): forward ai_stage as a gatew..." | Re-trigger Greptile |
andrewm4894
approved these changes
Jun 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 carriedsignal_report_id(for the research run) but noai_stage, so the spend couldn't be broken down by pipeline stage the way the rest of the signals pipeline can.Changes
configureEnvironmentnow readsai_stagefrom the task run state (getTaskRunStateString(preTaskRun, "ai_stage")) and includes it in thebuildGatewayPropertyHeadersbag, so it's forwarded as anx-posthog-property-ai_stageheader on the agent's Anthropic calls. The gateway lifts it onto the$ai_generationevent.The producing side — setting
TaskRun.state["ai_stage"]to"research"/"repo_selection"and extendingsignal_report_idto 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 runonagent-server.configure-environment.test.ts— 16 passed, including a new assertion thatai_stageis forwarded and one that it's omitted when absent.tsc --noEmittypecheck on@posthog/agent— clean.biome checkon the changed files — clean.Automatic notifications