Using C# to read your GitHub Copilot AI credit and usage status
Posted: (EET/GMT+2)
If your organization is using GitHub Copilot for AI-assisted software development, then at some point, somebody in your organization has to worry about the costs.
GitHub has a new web UI for displaying the current credit consumption, and paid consumption on top of the credits. But, who has the time to navigate to this page periodically? For precisely solving this problem, I wrote a little C# application that automated this work for me. It uses the GitHub API for retrieving the information.
Below are snippets of code that create you a simple console application. My real version pushes the result to an internal dashboard, in the spirit of an "IDP" (Internal Developer Platform).
To get started, you need a Personal Access Token (PAT) from your organization to access the GitHub API. This API provides nice endpoints to retrieve precisely the same information that the GitHub web UI is showing: consumed credits, consumed dollars.
Here's an example in C#, not a full application, so "some assembly required":
using System.Net.Http.Headers;
using System.Text.Json;
using System.Text.Json.Serialization;
// setup
string organization = "MyOrgNameHere";
string token = Environment.GetEnvironmentVariable("GITHUB_TOKEN")
?? throw new InvalidOperationException("Environment variable GITHUB_TOKEN is not set.");
using HttpClient http = new()
{
BaseAddress = new Uri("https://api.github.com")
};
// authentication
http.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
// headers recommended/required by GitHub
http.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.github+json"));
http.DefaultRequestHeaders.UserAgent.ParseAdd("MyOrg-Copilot-Credit-Monitor/1.0");
http.DefaultRequestHeaders.Add("X-GitHub-Api-Version", "2026-03-10");
JsonSerializerOptions jsonOptions = new()
{
PropertyNameCaseInsensitive = true
};
// call the API
AiCreditUsage usage = await GetJsonAsync(http, $"/organizations/{organization}/settings/billing/ai_credit/usage" +
$"?year={now.Year}&month={now.Month}", jsonOptions);
// calculate results
decimal consumedCredits = usage.UsageItems.Sum(x => x.GrossQuantity);
decimal includedOrDiscountedCredits = usage.UsageItems.Sum(x => x.DiscountQuantity);
decimal paidCredits = usage.UsageItems.Sum(x => x.NetQuantity);
decimal paidAmount = usage.UsageItems.Sum(x => x.NetAmount);
const decimal CreditsPerBusinessSeat = 1_900m;
decimal includedPool = copilot.SeatBreakdown.Total * CreditsPerBusinessSeat;
decimal estimatedRemainingIncludedCredits = Math.Max(0, includedPool - includedOrDiscountedCredits);
...
// display the results
Console.WriteLine($"Period: {now:yyyy-MM}");
Console.WriteLine($"Included pool: {includedPool:N2} credits");
Console.WriteLine($"Credits consumed: {consumedCredits:N2}");
Console.WriteLine($"Included/discounted: {includedOrDiscountedCredits:N2}");
Console.WriteLine($"Remaining included: {estimatedRemainingIncludedCredits:N2}");
Console.WriteLine($"Paid credits: {paidCredits:N2}");
Console.WriteLine($"Additional spend: ${paidAmount:N2}");
// model class
public sealed class AiCreditUsageItem
{
public string Product { get; set; } = "";
public string Sku { get; set; } = "";
public string Model { get; set; } = "";
public string UnitType { get; set; } = "";
public decimal PricePerUnit { get; set; }
public decimal GrossQuantity { get; set; }
public decimal GrossAmount { get; set; }
public decimal DiscountQuantity { get; set; }
public decimal DiscountAmount { get; set; }
public decimal NetQuantity { get; set; }
public decimal NetAmount { get; set; }
}
To run this application, first set the PowerShell environment variable to match your PAT like this:
$env:GITHUB_TOKEN="github_pat_ABC123..."
Then, just say dotnet run. The results will then become visible on the console.
Happy hacking!