JSON Event Stream
Outputs all session events as JSON lines to stdout. Useful for integrating pi into other tools or custom UIs.
Event Types
Events are defined in AgentSessionEvent, which extends AgentEvent with session-specific events:
Base events from AgentEvent:
Event Lifecycle
A typical session produces events in this order:
AssistantMessageEvent
The message_update event carries an assistantMessageEvent field with fine-grained streaming data:
Output Format
Each line is a JSON object. The first line is the session header:
Followed by events:
Examples
Extract final text from response
Stream text deltas in real time
Capture tool execution results
Multiple prompts
You can send additional prompts with the -m / --message flag:
Programmatic usage with Node.js
Notes
- Stderr is used for logging and diagnostics; redirect with
2>/dev/nullfor clean JSON output - Each JSON line is self-contained and can be parsed independently
- The
messageandpartialfields in events contain the full message state at that point in time - The session header is always the first line, making it easy to identify the session
- Events are emitted in real time as they occur, enabling streaming UIs