github / github/copilot-cli

Cost data availability in the GitHub Copilot hooks

オープン
#3,686 コメント 0 件 リアクション 8 件 担当者 0 名 GitHub で見る
area:plugins
主要言語
Shell
スター
11.2k
フォーク
1.9k
平均マージ
14時間 16分
マージ済み PR(30日)
6

説明

### Describe the feature or problem you'd like to solve

We want to automatically track AI credit consumption at the Copilot CLI and IDE extension session/hook level and report it to an external system (e.g., Jira ticket custom field) for cost visibility and project budgeting purposes. Currently this information is just visible on the UI level or in the terminal, but it'd be rather convenient to have this information persisted as a part of the information of the session that is visible for the webhook.

### Proposed solution

### Option A: Include usage metrics in hook input (Preferred)

Add an optional `usage` field to the hook input payload for lifecycle hooks (`Stop`, `SubagentStop`, and ideally a new `SessionEnd` or `PostResponse` hook type):

```json
{
"sessionId": "0857b672-...",
"timestamp": 1780574797652,
"cwd": "/path/to/project",
"usage": {
"totalPremiumRequestCost": 2.5,
"totalNanoAiu": 2500000000,
"totalUserRequests": 8,
"totalApiDurationMs": 45000,
"tokenDetails": {
"input": 45000,
"output": 4461,
"cacheRead": 12000,
"cacheWrite": 3000,
"reasoning": 800
},
"modelMetrics": {
"claude-opus-4.6": {
"requests": { "userInitiated": 8, "premiumCost": 2.5 },
"usage": {
"inputTokens": 45000,
"outputTokens": 4461,
"cacheReadTokens": 12000,
"reasoningTokens": 800
}
}
},
"codeChanges": {
"linesAdded": 42,
"linesRemoved": 15,
"filesModified": 3
}
}
}
```

This aligns with the existing `UsageGetMetricsResult` schema already defined internally in `api.schema.json`.

### Option B: Expose environment variables to hook scripts

Provide usage data as environment variables in the hook execution context:

```
COPILOT_SESSION_CREDITS=2.5
COPILOT_SESSION_NANO_AIU=2500000000
COPILOT_SESSION_TOKENS_INPUT=45000
COPILOT_SESSION_TOKENS_OUTPUT=4461
COPILOT_SESSION_TOKENS_CACHE_READ=12000
COPILOT_SESSION_TOKENS_REASONING=800
COPILOT_SESSION_MODEL=claude-opus-4.6
COPILOT_SESSION_DURATION_MS=45000
COPILOT_SESSION_REQUESTS=8
```

### Option C: Expose RPC port/token to hook environment

Allow hooks to call the existing `session.usage.getMetrics` RPC method by providing:

```
COPILOT_RPC_PORT=52341
COPILOT_RPC_AUTH_TOKEN=
```

This is more complex but enables any hook to query any session data on demand.

### Option D: Persist full usage data in `events.jsonl`

Write a `session.usage` or `assistant.turn_usage` event to `events.jsonl` after each API call, including:
- Input tokens
- Output tokens
- Cache read/write tokens
- Reasoning tokens
- Nano-AIU cost for that request
- Cumulative session totals

This would make the local event log self-contained for offline analysis without requiring live RPC access.

## Expected Benefits

- **Zero-touch cost attribution** per work item/ticket
- **Enables team-level AI spend dashboards** without manual data entry
- **Supports governance policies** (budget limits per project/sprint)
- **Unlocks integration ecosystem** — any external system can receive usage data via hooks

### Example prompts or workflows

_No response_

### Additional context

## Use Cases

### 1. Jira Integration via Stop Hook
```
Session ends → Stop hook fires → Hook reads usage from input payload →
Updates Jira ticket custom field with "AI Credits: 2.5, Tokens: 49,461"
```

### 2. Cost Alerting
```
PostToolUse hook checks cumulative credits →
If > threshold, sends Slack/Teams notification
```

### 3. Sprint Reporting
```
Team aggregates usage from all sessions (via events.jsonl or webhook logs) →
Generates per-ticket cost report for sprint retrospective
```

### 4. IDE Extension Integration
```
VS Code extension subscribes to session usage events via Copilot SDK →
Displays running cost in status bar → Exports to project management tool

## Additional Context

- This capability would bring Copilot CLI closer to parity with competitors (e.g., Claude Code CLI stores full cost data in local JSONL files and supports native webhook hooks for session-end reporting)
- The internal `UsageGetMetricsResult` schema is already well-defined — exposing it in hook input is likely a small incremental change
- Enterprise customers are particularly affected as they need cost attribution for chargeback and compliance

コントリビューションガイド

コントリビューションガイドを開く

調査の方向性

Start by reading the existing UsageGetMetricsResult schema in api.schema.json and reviewing the lifecycle hook inputs and events.jsonl references described in the issue. Compare the proposed hook, environment-variable, RPC, and event-log approaches before identifying the affected entry points and tests; done means a chosen design exposes session usage reliably to hooks.

索引モデルが issue の本文から書いたものです。

評価

技術スタック
github
領域
api, cli
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
静か
明瞭さ
おおむね明確
初心者へのやさしさ
38/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。