# pi-session-timer

A Pi coding agent extension that tracks **per-run** stats: how long Pi spent
working and how many tokens it consumed, live in a widget below the input
editor.

```
⏱ run 03:12 · 48.5K tok        (while working)
⏱ last run 03:12 · 48.5K tok   (after it finishes)
```

## Why per-run?

Pi's built-in status line already shows session-level active/total tokens.
This widget answers a different question: *what did this one message cost?*
Each time you send a message, the counters reset, tick live while Pi works,
and freeze at the final values when the run completes.

- **run** — wall time of the current agent run
- **tok** — net tokens for the run (input + cacheRead + cacheWrite + output),
  summed from the assistant message `usage` fields

Reset is armed by your message and applied when the next agent run actually
starts, so auto-retries and auto-compaction within one answer don't wipe the
counters.

## Install

From npm (published as `pi-session-timer`):

```
pi install pi-session-timer
```

Or from GitHub:

```
pi install github:stanleytejakusuma/pi-session-timer
```

Or clone into your extension directory:

```
git clone https://github.com/stanleytejakusuma/pi-session-timer \
  ~/.pi/agent/git/github.com/stanleytejakusuma/pi-session-timer
```

Then `/reload` in Pi.

## How it works

One self-contained TypeScript file, zero dependencies. It subscribes to
Pi's lifecycle events:

| Event | Role |
|---|---|
| `session_start` | reset counters |
| `message_start` (role=user) | arm the reset for the next run |
| `agent_start` / `agent_end` | track running time (depth counter handles subagents/retries) |
| `message_end` (role=assistant) | accumulate token usage |
| `session_shutdown` | stop the 1s refresh timer |

The widget is rendered via `ctx.ui.setWidget` with `placement: "belowEditor"`.

## License

MIT
