/observability:session [--refresh] [--host label] [--since date]from observability
Queries Claude Code session history via a DuckDB index over session transcripts. Use for questions about past activity (token usage, topics discussed, sessions by date) instead of grepping JSONL files.
Ships in observability. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
Query Claude Code history from a DuckDB index over the JSONL session files. Report each figure with the named query or SQL that produced it, and any index-health alert or Known Blind Spot that bounds it.
Current session ID: ${CLAUDE_SESSION_ID}
--refresh: run refresh.ts --refresh before querying. Default: the incremental refresh.--host <label>: scope queries through the host param. Default: every host, including local.--since <date>: pass as the after_date param. Default: the full index.The index is ~/.local/share/claude-sessions/session.duckdb (under $XDG_DATA_HOME when set). Use that path directly in every query and agent prompt.
Run refresh.ts before querying. It imports changed files, drops rows for deleted ones, and prints the DB path. Within --max-age (default 300 seconds) of the last refresh it exits without opening the database. Pass --refresh when the user asks for the latest data.
bun ${CLAUDE_SKILL_DIR}/scripts/refresh.ts
Open the index with duckdb -readonly -json -init ${CLAUDE_SKILL_DIR}/resources/limits.sql:
-readonly: a read-write open blocks refreshes and other readers.-init limits.sql: caps memory and threads. DuckDB's per-process defaults let a few concurrent readers freeze the machine.-json: the default box format truncates to 40 rows, and -csv breaks rows on newlines embedded in command and text.Parameterize with SET VARIABLE and read with getvariable('key'). Quote reserved words: SET VARIABLE "limit" = 5.
duckdb -readonly -json -init ${CLAUDE_SKILL_DIR}/resources/limits.sql ~/.local/share/claude-sessions/session.duckdb "SELECT model, SUM(output_tokens) FROM message_usage GROUP BY model"
duckdb -readonly -json -init ${CLAUDE_SKILL_DIR}/resources/limits.sql ~/.local/share/claude-sessions/session.duckdb < ${CLAUDE_SKILL_DIR}/resources/queries/stats.sql
scripts/usage.ts renders one session's token-burn timeline (--session <id>), or the top sessions by estimated cost (--days <n>). Its cost estimate comes from public API rates and comes to about 0.97 of billed spend.
Read-only opens share the lock. A refresh with work to do needs exclusive access. A reader and a writer that collide fail with Could not set lock. Retry after the other side finishes. refresh.ts retries on its own and exits 0 when a concurrent refresh holds the lock.
For a fan-out of agents over the corpus, the orchestrator runs refresh.ts --refresh once up front. Each agent then opens the DB path read-only with -init limits.sql and runs in the shared checkout. references/workflows.md shows param scoping and the breadth-first survey surfaces.
For mining config-change candidates across the whole corpus, follow references/discovery.md.
Run the queries in resources/queries/ by name with SET VARIABLE params before writing SQL from scratch. Every query takes project (a glob on the directory's last path component: myapp* matches the repo and its worktrees) and host.
The most used:
index-health: the index auditing itself. Run it first in any analysis pass, since its alerts cap what the rest can claim.activity: session interaction profile, one row per prompt source, plus interruptions, compactions, API retries, hook friction, permission modes.hooks: per-hook runs, friction rate, and latency corrected for host-wide slowdowns (excess_p95_ms, not p95_ms).hook-blocks: hook overfiring, grouped by normalized reason signature, including the PreToolUse denies hook_events never records.skill-auto-vs-explicit: per skill, model-routed vs chained vs typed. The disable-model-invocation lever.skill-config-vs-observed: installed skills that never fire, the curation instrument (reads disk, needs -init).sandbox-bypass-effective-command: bypassed commands by their real verb, with how many followed a failed sandboxed run, the excludedCommands candidates.repeat-read-waste: repeat Reads split by cause, isolating the true context tax from pagination and fan-out.delegation: whether expensive parents push subagent spawns down to cheaper models.usage-timeline: one session's token burn per time bucket, with estimated cost and a context-size proxy.outcomes: session terminal states (shipped, abandoned-with-edits, handed off, no artifact), the outcome side the activity queries never measure.To find sessions about a topic, run search first. Put the topic's synonyms, command names, and API names in one query, since a session scores on every term it contains:
duckdb -readonly -json -init ${CLAUDE_SKILL_DIR}/resources/limits.sql -cmd "SET VARIABLE query = 'taskpolicy renice setpriority cpu priority'" \
~/.local/share/claude-sessions/session.duckdb < ${CLAUDE_SKILL_DIR}/resources/queries/search.sql
search covers prompts, assistant prose, tool calls, and their output, and matched_surfaces names where terms hit. Use ILIKE only for what it leaves out: Read output, Write and Edit file contents, tool text past 2,000 characters per call, subagent turns, skill bodies, and hook output.
The rest:
already-allowed-still-prompting, bare-refs, catalog-reinjection-thrash-sessions, classifier, cli-health, field-drift, hook-block-then-retry-success, hook-config-vs-observed, hook-self-timing, memory-loads, model-summary, mods, permissions, plan-iterations, plan-sections, plan-sizes, prune-acceptance, sandbox-bypass-scaffold, sandbox-path-deny-recurrence, search, skill-activity, stats, stop-hook-noop-detector, text-export, tool-failure-rates, top-sessions, usage-spikes
references/catalog.md gives each query's purpose and params. Before running a query you have not used, read the YAML header of its resources/queries/<name>.sql for its output, caveats, and param defaults.
plan-sections and skill-config-vs-observed parse files through the markdown/yaml extensions, so add -cmd ".read resources/extensions.sql" after -init. hook-self-timing and hook-config-vs-observed read JSON and need no extension. Each header names its glob param and default.
duckdb -readonly -json -init ${CLAUDE_SKILL_DIR}/resources/limits.sql \
-cmd ".read ${CLAUDE_SKILL_DIR}/resources/extensions.sql" ~/.local/share/claude-sessions/session.duckdb \
< ${CLAUDE_SKILL_DIR}/resources/queries/plan-sections.sql
Each machine is a host: this one is local, and each imported machine has a chosen label. ${CLAUDE_SKILL_DIR}/scripts/hosts.ts lists them. To list, import, re-sync, or remove a machine, follow references/cross-machine.md.
Raise data ownership once, at import. Pass --egress only when the source machine's history may leave this machine. A host imported without it is block_egress: exclude its rows with host != '<label>' (or scope to local) from anything that leaves the machine, such as PR descriptions, Slack, email, web requests, and uploads. hosts.ts prints each host's policy.
Before anything leaves the machine, review imported text for secret prefixes such as sk_live_, xoxb-, ghp_, AKIA, and eyJhbGciOi.
Every table and view carries host. The sessions view adds project_id (host || ':' || project_path). Read references/catalog.md before writing SQL against a surface you have not used.
Columns of the surfaces ad-hoc SQL reads most (DESCRIBE <name> for anything else):
!bun ${CLAUDE_SKILL_DIR}/scripts/schema.ts
For a field not pinned to a column, reach into data with JSON path operators, wrapped in parens before any comparison: (data->>'$.x') = 'y'. references/schema-discovery.md has examples.
No query can surface these. State them when an analysis depends on what they hide. references/blind-spots.md elaborates.
cleanupPeriodDays deletes old sessions, so the corpus floor ratchets forward. ~/.claude/history.jsonl goes further back but isn't ingested.permission_requests.Session logs live at ~/.claude/projects/<encoded-path>/<session-id>.jsonl, where the encoded path replaces / with -. Fetch a row's full JSONL line with sed -n '<source_line>p' <source_file>. source_line is 1-based per file and can trail the physical line in a file with malformed lines, so check the fetched line's uuid or timestamp when exactness matters.
CLAUDE.md (opens in a new tab)README.md (opens in a new tab)fixtures/debug/denials-session.txt (opens in a new tab)fixtures/hook-metrics/guard.jsonl (opens in a new tab)fixtures/mod-events/denials-session/auto-mode.1.0.jsonl (opens in a new tab)fixtures/plans/feature-plan.md (opens in a new tab)fixtures/plans/quick-plan.md (opens in a new tab)fixtures/sessions/-Users-test-health/health.jsonl (opens in a new tab)fixtures/sessions/-Users-test-project-feature/feature.jsonl (opens in a new tab)fixtures/sessions/-Users-test-project/attribution.jsonl (opens in a new tab)fixtures/sessions/-Users-test-project/basic.jsonl (opens in a new tab)fixtures/sessions/-Users-test-project/delegation.jsonl (opens in a new tab)fixtures/sessions/-Users-test-project/denials.jsonl (opens in a new tab)fixtures/sessions/-Users-test-project/hooks.jsonl (opens in a new tab)fixtures/sessions/-Users-test-project/labels.jsonl (opens in a new tab)fixtures/sessions/-Users-test-project/malformed.jsonl (opens in a new tab)fixtures/sessions/-Users-test-project/multiple.jsonl (opens in a new tab)fixtures/sessions/-Users-test-project/no-container.jsonl (opens in a new tab)fixtures/sessions/-Users-test-project/plan-iterations.jsonl (opens in a new tab)fixtures/sessions/-Users-test-project/plan-outcomes.jsonl (opens in a new tab)What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
npx claudepluginhub bendrucker/claude --plugin observabilityOther plugins' skills for the same job