A pi extension that automatically adds session_id to OpenRouter API requests, enabling you to track and group conversations in the OpenRouter console.
When using pi with OpenRouter as your LLM provider, each request is typically treated as an isolated interaction. OpenRouter supports a session_id field in the request body that groups related requests together in their console/dashboard.
This extension automatically:
- Captures your pi session name (set via
/name <name>) and combines it with a short unique identifier for human-readable grouping - Falls back to the raw session file identifier when no name is set
- Injects the
session_idfield into every OpenRouter API request - Re-resolves the session name on every request, so renaming mid-session takes effect immediately
- Allows you to view conversation threads in the OpenRouter console
| Scenario | Example session_id |
|---|---|
Named session (/name Refactor auth) |
refactor-auth-019dbbc7 |
| Unnamed session | 2026-05-06T12-00-00-000Z_019dbbc7-... (filename) |
Ephemeral (--no-session) |
ephemeral-1234567890-abc123 |
- Conversation Tracking: View your pi sessions as grouped conversations in OpenRouter's dashboard
- Cost Analysis: Better understand token usage and costs per session rather than per-request
- Debugging: Easily trace a series of related API calls in the OpenRouter console
- Session Continuity: Maintain logical grouping even across multiple model calls within one pi session
- pi installed (`npm install -g @earendil-works/pi-coding-agent)
- An OpenRouter API key
pi install npm:pi-openrouter-sessionTo install a specific version:
pi install npm:[email protected]pi install git:github.com/odonnell-anthony/pi-openrouter-sessionTo install a specific version:
pi install git:github.com/odonnell-anthony/[email protected]git clone [email protected]:odonnell-anthony/pi-openrouter-session.git
pi install /path/to/pi-openrouter-sessionTo share the extension with your team via project settings:
pi install -l npm:pi-openrouter-sessionThis writes to .pi/settings.json instead of your global settings.
export OPENROUTER_API_KEY="sk-or-v1-..."Or add it to ~/.pi/agent/auth.json:
{
"openrouter": {
"type": "api_key",
"key": "sk-or-v1-..."
}
}pi --provider openrouter --model "openrouter/anthropic/claude-sonnet-4"Or select OpenRouter interactively:
- Start
pi - Press
Ctrl+Lto open the model selector - Choose an OpenRouter model (e.g.,
openrouter/anthropic/claude-sonnet-4)
When you start pi, you should see:
[openrouter-session] Ready, base session ID: 2026-05-06T12-00-00-000Z_019dbbc7-...
- Visit OpenRouter Console
- Navigate to the activity/sessions section
- Your pi conversations will be grouped by session ID
The extension uses pi's extension API to:
-
Establish base ID (
session_startevent):- Extracts the full filename (without
.jsonl) as the stable base ID - Pi session filenames have the format
<ISO-timestamp>_<uuid>(e.g.2026-05-06T12-00-00-000Z_019dbbc7-c8c5-748c-8710-3fdf6fefc083) - Falls back to generating a random ID for ephemeral sessions (
--no-session)
- Extracts the full filename (without
-
Intercept API Requests (
before_provider_requestevent):- Detects OpenRouter requests by checking:
- Model string contains
openrouter/ - Current model's provider is
openrouter
- Model string contains
- Resolves the session name via
pi.getSessionName()on every request so/namechanges are picked up immediately - If named: splits the base ID on
_, takes the UUID half, strips hyphens, and uses the first 8 chars as a short suffix — e.g.just-testing-019dbbc7 - If unnamed: uses the full raw base ID
- Adds
session_idto the request payload body
- Detects OpenRouter requests by checking:
-
Lifecycle Management:
- Base ID is stable for the entire pi session
- New session / fork / clone → new base ID, new
session_id - Renaming via
/namemid-session → next request picks up the new name automatically
User sends prompt
↓
pi builds OpenRouter API request
↓
Extension intercepts (before_provider_request)
↓
Adds session_id to payload
↓
Request sent to OpenRouter with session tracking
Currently, the extension works automatically with no configuration required.
-
Check if it's installed:
pi list
Should show
npm:pi-openrouter-session(orgit:github.com/odonnell-anthony/pi-openrouter-sessionif installed from GitHub) -
Reload extensions in pi:
/reload -
Check the logs for:
[openrouter-session] Ready, base session ID: ...And on the first OpenRouter request:
[openrouter-session] Using session_id: ...
-
Verify you're using an OpenRouter model:
/modelShould show
openrouter/... -
Check that
OPENROUTER_API_KEYis set correctly -
Enable debug logging and check the request payload
If you have other extensions modifying OpenRouter requests, ensure they don't remove the session_id. The before_provider_request handlers run in extension load order.
pi-openrouter-session/
├── package.json # Pi package manifest
├── README.md # This file
└── extensions/
└── openrouter-session.ts # The extension code
Use pi's /name command to give your sessions meaningful names:
/name Refactor auth module
Once named, subsequent OpenRouter requests will use refactor-auth-module-<id> as the session_id, making it easy to find your sessions in the OpenRouter console.
You can rename at any point in the session — the next API request will use the updated name.
- Ephemeral Sessions: Sessions started with
--no-sessionget a random ID that can't be recovered - Session File Dependent: The base ID is derived from the pi session file name. If you delete/rename the file, the ID changes
- OpenRouter Only: This only works with the OpenRouter provider
- Name length: Session name slugs are capped at 50 characters to keep IDs readable
MIT
- pi - The minimal terminal coding harness
- OpenRouter - Unified API for LLM access
- pi-mono - Pi's source code and documentation
- Open an issue on GitHub
- Check pi's documentation
- Join the pi Discord community
