: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
chore(agent): add timing instrumentation for prompt dispatch and model output (#91550)
Georgiy Tarasov48763c9d4241
diff --git a/products/desktop/packages/agent/src/adapters/claude/claude-agent.ts b/products/desktop/packages/agent/src/adapters/claude/claude-agent.tsindex dbb58a1095d3..7b577b2776b5 100644--- a/products/desktop/packages/agent/src/adapters/claude/claude-agent.ts+++ b/products/desktop/packages/agent/src/adapters/claude/claude-agent.ts@@ -41,6 +41,7 @@ import {type Options,type Query,query,+ type SDKMessage,type SDKUserMessage,type SlashCommand,} from "@anthropic-ai/claude-agent-sdk";@@ -694,9 +695,50 @@ export class ClaudeAcpAgent extends BaseAcpAgent {}const input = head.pendingInput;head.pendingInput = undefined;+ head.dispatchedAt = performance.now();session.input.push(input);}+ /** Time the window between handing a prompt to the SDK and the SDK's first+ * emission for it. Nothing crosses the wire during that window, so without+ * this line a slow first turn cannot be attributed after the fact: the log+ * names both how long the wait was and which message ended it, which+ * separates pre-model setup (a `commands_changed` skill rescan, for+ * example) from the model call itself. */+ private timeFirstSdkMessage(+ session: Session,+ sessionId: string,+ message: SDKMessage,+ ): void {+ const turn =+ session.activeTurn ?? session.turnQueue.find((t) => !t.settled);+ if (turn?.dispatchedAt === undefined || turn.firstMessageTimed) {+ return;+ }+ turn.firstMessageTimed = true;+ this.logger.debug("First SDK message after prompt dispatch", {+ sessionId,+ waitMs: Math.max(0, Math.round(performance.now() - turn.dispatchedAt)),+ messageType: message.type,+ messageSubtype: (message as { subtype?: string }).subtype,+ });+ }++ /** Time to the first assistant message, which is the first output a person+ * sees. Reported once per turn so a slow turn splits into the wait before+ * the model answered and the tool work after it. */+ private timeFirstModelOutput(session: Session, sessionId: string): void {+ const turn = session.activeTurn;+ if (turn?.dispatchedAt === undefined || turn.firstOutputTimed) {+ return;+ }+ turn.firstOutputTimed = true;+ this.logger.debug("First model output after prompt dispatch", {+ sessionId,+ waitMs: Math.max(0, Math.round(performance.now() - turn.dispatchedAt)),+ });+ }+private ensureConsumer(sessionId: string): void {const session = this.session;if (session.consumer) {@@ -993,6 +1035,8 @@ export class ClaudeAcpAgent extends BaseAcpAgent {return;}+ this.timeFirstSdkMessage(session, sessionId, message);+if (session.emitRawSDKMessages &&shouldEmitRawMessage(session.emitRawSDKMessages, message)@@ -1465,6 +1509,7 @@ export class ClaudeAcpAgent extends BaseAcpAgent {}if (message.type === "assistant") {+ this.timeFirstModelOutput(session, sessionId);// Subagent output is a separate model context, so it is no// evidence the steer reached this turn's model.if (session.activeTurn && message.parent_tool_use_id === null) {diff --git a/products/desktop/packages/agent/src/adapters/claude/types.ts b/products/desktop/packages/agent/src/adapters/claude/types.tsindex 64f9a3d79467..f6ff491ec617 100644--- a/products/desktop/packages/agent/src/adapters/claude/types.ts+++ b/products/desktop/packages/agent/src/adapters/claude/types.ts@@ -62,6 +62,15 @@ export type Turn = {/** Invoked once at activation, matching the pre-consumer broadcast timing. */broadcast: () => Promise<void>;pendingInput?: SDKUserMessage;+ /** `performance.now()` at the moment the prompt entered the SDK input+ * stream. The SDK emits nothing while it prepares a turn, so this is the+ * only anchor for measuring that silent window. */+ dispatchedAt?: number;+ /** Set once the first SDK message for this turn has been timed, so the+ * measurement is reported once instead of on every message. */+ firstMessageTimed?: boolean;+ /** Set once the first assistant message for this turn has been timed. */+ firstOutputTimed?: boolean;settled: boolean;resolve: (response: PromptResponse) => void;reject: (error: unknown) => void;