Skip to content

[awf] Calculate and expose AI Credits alongside effective tokens in the API proxy #4356

Description

@lpcox

Summary

The API proxy currently tracks effective tokens using configurable per-model multipliers (unitless weights). This proposal adds a parallel AI Credits calculation using GitHub's published per-token pricing, giving workflows and operators a direct dollar-cost metric (1 AI credit = $0.01 USD).

Background

GitHub Copilot plans include AI Credits allowances. When usage exceeds allowances, billing is based on per-token rates published at:

The API proxy already intercepts every request/response and extracts token counts from usage metadata. Adding credits calculation is a natural extension.

Proposed Pricing Table (per 1M tokens)

Model Input Cached Input Cache Write Output
OpenAI
GPT-5 mini $0.25 $0.025 $2.00
GPT-5.2 / GPT-5.2-Codex / GPT-5.3-Codex $1.75 $0.175 $14.00
GPT-5.4 $2.50 $0.25 $15.00
GPT-5.4 mini $0.75 $0.075 $4.50
GPT-5.4 nano $0.20 $0.02 $1.25
GPT-5.5 $5.00 $0.50 $30.00
Anthropic
Claude Haiku 4.5 $1.00 $0.10 $1.25 $5.00
Claude Sonnet 4/4.5/4.6 $3.00 $0.30 $3.75 $15.00
Claude Opus 4.5/4.6/4.7/4.8 $5.00 $0.50 $6.25 $25.00
Google
Gemini 2.5 Pro $1.25 $0.125 $10.00
Gemini 3 Flash $0.50 $0.05 $3.00
Gemini 3.1 Pro $2.00 $0.20 $12.00
Gemini 3.5 Flash $1.50 $0.15 $9.00
Other
MAI-Code-1-Flash $0.75 $0.075 $4.50
Raptor mini $0.25 $0.025 $2.00

Proposed Implementation

1. Pricing data file: containers/api-proxy/ai-credits-pricing.js

// Per-model pricing in dollars per 1M tokens
// Source: https://docs.github.com/en/copilot/reference/copilot-billing/models-and-pricing
module.exports = {
  'gpt-5-mini':        { input: 0.25,  cachedInput: 0.025,  cacheWrite: null, output: 2.00 },
  'gpt-5.2':           { input: 1.75,  cachedInput: 0.175,  cacheWrite: null, output: 14.00 },
  'gpt-5.2-codex':     { input: 1.75,  cachedInput: 0.175,  cacheWrite: null, output: 14.00 },
  'gpt-5.3-codex':     { input: 1.75,  cachedInput: 0.175,  cacheWrite: null, output: 14.00 },
  'gpt-5.4':           { input: 2.50,  cachedInput: 0.25,   cacheWrite: null, output: 15.00 },
  'gpt-5.4-mini':      { input: 0.75,  cachedInput: 0.075,  cacheWrite: null, output: 4.50 },
  'gpt-5.4-nano':      { input: 0.20,  cachedInput: 0.02,   cacheWrite: null, output: 1.25 },
  'gpt-5.5':           { input: 5.00,  cachedInput: 0.50,   cacheWrite: null, output: 30.00 },
  'claude-haiku-4-5':  { input: 1.00,  cachedInput: 0.10,   cacheWrite: 1.25, output: 5.00 },
  'claude-sonnet-4':   { input: 3.00,  cachedInput: 0.30,   cacheWrite: 3.75, output: 15.00 },
  'claude-sonnet-4-5': { input: 3.00,  cachedInput: 0.30,   cacheWrite: 3.75, output: 15.00 },
  'claude-sonnet-4-6': { input: 3.00,  cachedInput: 0.30,   cacheWrite: 3.75, output: 15.00 },
  'claude-opus-4-5':   { input: 5.00,  cachedInput: 0.50,   cacheWrite: 6.25, output: 25.00 },
  'claude-opus-4-6':   { input: 5.00,  cachedInput: 0.50,   cacheWrite: 6.25, output: 25.00 },
  'claude-opus-4-7':   { input: 5.00,  cachedInput: 0.50,   cacheWrite: 6.25, output: 25.00 },
  'claude-opus-4-8':   { input: 5.00,  cachedInput: 0.50,   cacheWrite: 6.25, output: 25.00 },
  'gemini-2.5-pro':    { input: 1.25,  cachedInput: 0.125,  cacheWrite: null, output: 10.00 },
  'gemini-3-flash':    { input: 0.50,  cachedInput: 0.05,   cacheWrite: null, output: 3.00 },
  'gemini-3.1-pro':    { input: 2.00,  cachedInput: 0.20,   cacheWrite: null, output: 12.00 },
  'gemini-3.5-flash':  { input: 1.50,  cachedInput: 0.15,   cacheWrite: null, output: 9.00 },
  'mai-code-1-flash':  { input: 0.75,  cachedInput: 0.075,  cacheWrite: null, output: 4.50 },
  'raptor-mini':       { input: 0.25,  cachedInput: 0.025,  cacheWrite: null, output: 2.00 },
};

2. Credits calculation module: containers/api-proxy/ai-credits-calculator.js

Parallels the effective-token-guard pattern:

  • On each response, look up the model's pricing
  • Calculate: credits = (inputTokens × inputRate + cachedTokens × cachedRate + cacheWriteTokens × writeRate + outputTokens × outputRate) / 1_000_000 / 0.01
  • The division by $0.01 converts dollars to AI credits (1 credit = $0.01)
  • Accumulate a running total per session

For unknown models, log a warning and skip credits calculation (don't block the request).

3. Expose via management endpoint

Add ai_credits to the /management response alongside effective_tokens:

{
  "effective_tokens": { ... },
  "ai_credits": {
    "total": 4.23,
    "by_model": {
      "claude-sonnet-4-6": { "input_credits": 1.5, "output_credits": 2.73, "total": 4.23 }
    }
  }
}

4. Expose via environment/logs

  • Log ai_credits_this_response alongside existing effectiveTokensThisResponse in the per-request log
  • Optionally expose AWF_AI_CREDITS_USED env var (or file) for the agent to read

5. Optional: credits-based budget guard

Similar to --max-effective-tokens, add --max-ai-credits <N> to halt a run when it exceeds N credits.

Design Considerations

  1. Pricing updates: The pricing table is static at build time. When GitHub publishes new rates, bump the file. Consider a future --ai-credits-pricing-file override for custom/BYOK pricing.

  2. Model name resolution: The proxy already resolves model aliases (e.g. COPILOT_MODEL). Credits lookup should use the resolved model name.

  3. Cache write tokens: Anthropic reports cache_creation_input_tokens separately from input_tokens. The proxy already extracts this for token tracking — reuse it for credits.

  4. No blocking by default: Credits calculation is purely observational unless --max-ai-credits is set.

  5. Relationship to effective tokens: These are complementary, not replacements. Effective tokens are unitless weights for relative cost control; AI credits are absolute dollar-denominated costs.

References

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions