Skip to content

Cost data availability in the GitHub Copilot hooks #3686

@tbaraukova

Description

@tbaraukova

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):

{
  "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=<one-time-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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:pluginsPlugin system, marketplace, hooks, skills, extensions, and custom agents
    No fields configured for Feature.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions