Core: Official Langfuse observability extension for the Pi Coding Agent. Maps each user prompt to a single, complete trace combining prompt workflow, LLM generation, tool usage, metrics, and health scores grouped by Pi session.
π Quick Start
- Install:
npm install pi-langfuse(Requires Node.js >= 22). - Load: Start Pi with
pi --ext pi-langfuse. - Setup: Run
/langfuse-setupin the Pi CLI/TUI to configure keys interactively.
π§ Configuration
- Local Config: Interactive setup persists API keys to
~/.pi/agent/pi-langfuse/config.json. This saved config always takes precedence over environment variables. - Environment Variables: Use
PI_LANGFUSE_PUBLIC_KEY,PI_LANGFUSE_SECRET_KEY, andPI_LANGFUSE_HOSTwhen theconfig.jsonis missing. - Privacy Presets: Set
PI_LANGFUSE_PRIVACY_PRESETtometadata-only,prompts-only,conversations, orfull-debug(default) to control payload granularity. - Fine-Grained Capture: Override presets with binary flags like
PI_LANGFUSE_CAPTURE_TOOL_IO=falseorPI_LANGFUSE_CAPTURE_SYSTEM_PROMPT=false. - Git Identity: Customize trace git tags via a whitelist in
.pi-langfuse.metadata.json(keys:repo_identity,repo_name,remote_origin).
π¦ Dependencies
- Core SDKs:
@langfuse/client,@langfuse/tracing,@langfuse/otel(v5.3+). - OpenTelemetry:
@opentelemetry/sdk-node,@opentelemetry/sdk-trace-base. - Peer Target:
@earendil-works/pi-coding-agent(intercepting Piβs pluginExtensionAPI).
π‘ Best Practices & Patterns
- Session Isolation (AsyncLocalStorage): Uses Nodeβs
AsyncLocalStorageto scopecurrentSessionIdand prevent active observations, state, and metrics from leaking across overlapping Pi sessions. - 1:1 Trace Modeling: Maps exactly one user prompt to one
pi-agenttrace. Sub-events rigorously map to specific Langfuse entities (1 provider request = 1generation, 1 tool call = 1tool). - Event-Driven Architecture: Binds directly to Piβs lifecycle events (
turn_start,before_provider_request,tool_call,agent_end). Execution loops strictly throughwithSessionwrappers to maintain atomic context. - Automatic Redaction: Extension universally masks tokens, API keys (e.g., AWS, GitHub, NPM), passwords, cookies, and local absolute directory paths before uploading anything to Langfuse.
- Safe Metadata Uploads: Extracts local Git context (commit hash, branch, repo name) but explicitly ignores unrecognized overrides to avoid exfiltrating sensitive credentialed remotes.
π¨ Gotchas / Anti-patterns
- Overriding via Env: Relying on environment variables will fail if a persistent
~/.pi/agent/pi-langfuse/config.jsonexists. The JSON config always wins. - Global Mutable Tracing: Using standard global variables for trace IDs creates severe collision leaks during multi-session Pi tasks. Always use the
withSessionAsyncLocalStorage boundary. - Orphaned Observations: Failing to close spans on
session_shutdownor hard interrupts leaves dangling spans in Langfuse. Always intercept exit signals and invokecloseDanglingObservations()andshutdownRuntime(). - Unsafe Tool Payloads: Do not bypass the
applyCapturePolicyredaction layer. Raw local paths and internal tokens must never be sent in observation metadata. - Multiple Package Managers: Repo exclusively uses
npm(package-lock.json). Do not commityarn.lockorpnpm-lock.yaml.
π Research / References
- NPM: pi-langfuse
- Repository: github.com/gooyoung/pi-langfuse
- Upstream Architecture: Documents local setup in
DEVELOPMENT.mddetailing the agent trace tree and event flow integration.