Pi Coding Agent AI Observability installation
Contents
- 1
Prerequisites
RequiredPi is an open-source coding agent that runs in your terminal. The
@posthog/piextension captures LLM generations, tool executions, and conversation traces as$ai_generation,$ai_span, and$ai_traceevents and sends them to PostHog.You need:
- Pi coding agent installed. The extension requires Node.js 22 or later.
- A PostHog account with a project token.
- 2
Install the extension
RequiredInstall the PostHog extension globally:
Or install it for the current project:
- 3
Configure PostHog
RequiredSet environment variables with your PostHog project token and host. You can find these in your PostHog project settings.
Then start Pi as normal:
The extension initializes and captures events for each LLM call, tool execution, and completed agent run.
Tip: Add these variables to your shell profile, such as
~/.zshrcor~/.bashrc, so they persist across sessions. - 4
Configuration options
OptionalConfigure the extension with environment variables or a
~/.pi/agent/posthog.jsonconfig file. Environment variables take precedence over config file values.Variable Default Description POSTHOG_API_KEY(required) Your PostHog project token POSTHOG_HOSThttps://us.i.posthog.comPostHog ingestion host POSTHOG_PRIVACY_MODEfalseWhen true, LLM input and output content is not sent to PostHog. Token counts, costs, latency, and model metadata are still captured.POSTHOG_ENABLEDtrueSet to falseto disable the extensionPOSTHOG_TRACE_GROUPINGmessagemessage: one trace per user prompt.session: group all generations in a session into one trace.POSTHOG_SESSION_WINDOW_MINUTES60Minutes of inactivity before starting a new session window POSTHOG_PROJECT_NAMECurrent directory name Project name included in all events POSTHOG_AGENT_NAMEAgent name Agent name. Defaults to the project name and detects subagent names when available. POSTHOG_TAGS(none) Custom tags added to all events in key1:val1,key2:val2formatPOSTHOG_MAX_ATTRIBUTE_LENGTH12000Maximum length for serialized tool input and output attributes Trace grouping modes
message(default): Each user prompt creates a new trace. Multiple LLM turns within one prompt, including tool-use loops, are grouped under the same trace.session: All generations within a session window are grouped into one trace. A new trace starts afterPOSTHOG_SESSION_WINDOW_MINUTESof inactivity.
Privacy mode
When
POSTHOG_PRIVACY_MODE=true, all LLM input and output content, user prompts, tool inputs, and tool outputs are redacted. Token counts, costs, latency, and model metadata are still captured.Even with privacy mode off, sensitive keys in tool inputs and outputs, such as
api_key,token,secret,password, andauthorization, are redacted.What gets captured
The extension captures three types of events:
$ai_generation: Each LLM call, including model, provider, token usage, cost, latency, and input and output messages in OpenAI chat format.$ai_span: Each tool execution (read, write, edit, bash, and others), including tool name, input parameters, output result, and duration. Learn more about spans.$ai_trace: Completed agent runs with aggregated token totals and latency. Learn more about traces.
- 5
Next steps
RecommendedNow that you're capturing AI conversations, continue with the resources below to learn what else AI Observability enables within the PostHog platform.
Resource Description Basics Learn the basics of how LLM calls become events in PostHog. Generations Read about the $ai_generationevent and its properties.Traces Explore the trace hierarchy and how to use it to debug LLM calls. Spans Review spans and their role in representing individual operations. Anaylze LLM performance Learn how to create dashboards to analyze LLM performance.

