* feat(skill-hub): add Skill Hub catalog, publish flow, and lite materialize pipeline Introduce Skill Hub for browsing, importing, publishing, and installing skills, with lite instance package materialization and runtime sync support. Co-authored-by: Cursor <cursoragent@cursor.com> * fix(skill-hub): remove token-governance hooks from Skill Hub PR Strip validateManagedRuntimeEnvironmentOverrides, network lock policy sync, and egress proxy audit wiring that belong to the upcoming token-usage work, so the Skill Hub branch compiles independently. Co-authored-by: Cursor <cursoragent@cursor.com> * fix(skill-hub): update migration number in materialize docs Co-authored-by: Cursor <cursoragent@cursor.com> * fix(skill-hub): make RuntimeAgentClient test stub and hub tests compile-safe Add ResyncInstanceSkills to the runtime pool handler fake client, and harden skill hub payload helpers/tests against nil storage/instance repos so go test passes. Co-authored-by: Cursor <cursoragent@cursor.com> * feat: add Skill Hub hardening with session usage tracking and egress governance Unify Skill Hub runtime sync improvements with session-token observability, egress network policy, and admin/instance usage reporting for reopenable PR. Co-authored-by: Cursor <cursoragent@cursor.com> * fix(skill-hub): repair CI tests and nested skill install * fix(ci): restore release deployment configuration --------- Co-authored-by: heshengran <heshengran@ieisystem.com> Co-authored-by: Cursor <cursoragent@cursor.com>
6.8 KiB
Session Token Usage
Instance-level reporting for LLM calls routed through the platform AI Gateway.
Data flow
- Runtime or user calls
POST /api/v1/gateway/llm/chat/completions. - AI Gateway persists:
model_invocations(tokens, model, status,instance_id,session_id)cost_records(estimated cost per trace)audit_events(includinggateway.session.fallbackwhen session key is missing)chat_sessions(optional title from first user message)
- Instance detail UI calls:
GET /api/v1/instances/:id/session-usageGET /api/v1/instances/:id/session-usage/detail?session_id=...
- Admin overview UI calls:
GET /api/v1/admin/session-usage/overview
Session ID rules
| Source | Stored session_id |
|---|---|
Header x-openclaw-session-key: main on OpenClaw |
agent:openclaw:main |
| Header on Hermes | agent:hermes:{key} |
| Missing stable key (user JWT or non-managed callers) | sess_trc_{traceId} (fallback) |
| Missing key on instance gateway token for OpenClaw/Hermes | agent:{type}:main (managed default) |
Display keys are derived via FormatOpenClawSessionKey (e.g. main).
API
List session usage
GET /api/v1/instances/:id/session-usage?page=1&limit=20&search=main&since=2026-07-01T00:00:00Z
Query parameters:
since/until: optional RFC3339 timestamps (untilmust be aftersince); filter on invocationcreated_at(cost aggregates join non-blocked invocations on the same window)search: filters session rows by session id/key/title (summary totals ignore search)
Response highlights:
summary: totals across all sessions on the instancecompliance: fallback session count and recent fallback audit eventsitems: paginated per-session rows
Session detail
GET /api/v1/instances/:id/session-usage/detail?session_id=agent:openclaw:main&since=2026-07-01T00:00:00Z
Accepts the same optional since / until bounds as the list endpoint. Detail rows, model breakdown, and recent traces respect the time window and exclude blocked invocations.
Returns model breakdown (tokens + cost) and recent traces for one session.
Admin cross-instance overview
GET /api/v1/admin/session-usage/overview?page=1&limit=20&search=openclaw&since=2026-07-01T00:00:00Z
Returns managed OpenClaw/Hermes running instances sorted by total tokens, with per-instance summary and global totals.
UI features
- Time range presets: all time, 24h, 7d, 30d (instance panel and admin overview)
- Auto refresh: optional 15s polling
- CSV export: instance panel exports all filtered session rows; admin page exports instance summary rows
Limits
- Gateway only: direct external LLM calls that bypass the platform gateway are not included.
- Blocked invocations are excluded from token aggregates.
- Supported instance types in UI:
openclaw,hermes.
Database indexes
Migration 038_add_session_usage_indexes.sql adds:
cost_records(instance_id)cost_records(session_id)model_invocations(instance_id, session_id, created_at)
Local verification
- Apply migrations (including
038). - Open an OpenClaw or Hermes instance detail page (Lite or Pro).
- Send a gateway chat completion with
x-openclaw-session-key: main. - Refresh the Session Token Usage panel and confirm token totals increase.
- Open Admin → AI Gateway → Session Usage for the cross-instance overview.
Optional E2E:
cd e2e
npx playwright test tests/instances/session-token-tracking.spec.ts
Requires a running stack, configured gateway models, and DB access for fixtures/dbClient.ts.
E2E coverage
| Spec | Scope |
|---|---|
session-token-tracking.spec.ts |
Instance session usage API, gateway aggregation, fallback compliance, instance gateway token |
session-usage-admin.spec.ts |
Admin overview API, since query validation, non-admin 403 |
Run all session usage specs:
cd e2e
npx playwright test tests/instances/session-token-tracking.spec.ts tests/instances/session-usage-admin.spec.ts
Pre-commit checklist
When preparing the standalone session-usage PR:
- Branch:
feat/session-token-usage(from Skill Hub baseline) - Include migration
038_add_session_usage_indexes.sql - Exclude unrelated WIP: egress policy, local deployment yaml, debug
_*.jsonartifacts - Suggested commit split:
feat(session-usage): add session usage APIs, indexes, and admin overviewfeat(session-usage): add instance/admin UI with filters, refresh, and CSV exporttest(session-usage): add handler, service, repository, and e2e coveragedocs(session-usage): add session token usage guide
- Verify locally:
go test ./internal/services/... ./internal/handlers/... ./internal/repository/... -run SessionUsage- Playwright P1 specs above (P0 gateway aggregation may skip when upstream LLM unavailable)
Phase 9 staging file list
Include (session usage only):
Backend
backend/cmd/server/main.go(session-usage routes only — review diff before staging)backend/internal/db/migrations/038_add_session_usage_indexes.sqlbackend/internal/db/migrations_test.go(038 test)backend/internal/handlers/session_usage_query.gobackend/internal/handlers/session_usage_query_test.gobackend/internal/handlers/instance_handler.gobackend/internal/handlers/instance_handler_test.gobackend/internal/handlers/ai_observability_handler.gobackend/internal/handlers/ai_observability_handler_test.gobackend/internal/repository/session_usage_filter.gobackend/internal/repository/session_usage_filter_test.gobackend/internal/repository/model_invocation_repository.gobackend/internal/repository/cost_record_repository.gobackend/internal/services/ai_observability_service.gobackend/internal/services/ai_observability_session_usage_test.go
Frontend
frontend/src/components/InstanceSessionUsagePanel.tsxfrontend/src/pages/admin/SessionUsageOverviewPage.tsxfrontend/src/pages/instances/InstanceDetailPage.tsxfrontend/src/components/AdminLayout.tsxfrontend/src/pages/admin/AIGatewayPage.tsxfrontend/src/router/index.tsxfrontend/src/services/instanceService.tsfrontend/src/services/adminService.tsfrontend/src/types/instance.tsfrontend/src/utils/sessionUsageExport.tsfrontend/src/lib/i18n.ts
E2E & docs
e2e/fixtures/apiClient.tse2e/fixtures/dbClient.tse2e/tests/instances/session-token-tracking.spec.tse2e/tests/instances/session-usage-admin.spec.tsdocs/session-token-usage.md
Exclude (do not stage for session-usage PR):
backend/internal/egresspolicy/**backend/internal/handlers/egress_proxy_handler*.godeployments/k8s/**/instance-egress-networkpolicy.yamldeployments/scripts/**e2e/tests/instances/llm-governance.spec.ts(unless bundled intentionally)- Root
_*.json,pr138.patch, debug artifacts