Agents and a growing set of AI features in Business Central are no longer free to run without limits. They are billed in Copilot Credits (the new AI money💸 in Redmond😅), and when an environment runs out of capacity, AI features simply stop working until an admin adds more.
The System Application ships a small codeunit that sits right on that boundary: codeunit 7785 “Copilot Quota”. For a cloud extension it exposes a single method, CanConsume(). Behind it there’s an internal implementation and a background task that drives the quota notifications on the Copilot & agent capabilities page.
It follows the usual facade pattern of the System Application: a thin public codeunit that delegates every call to an internal implementation codeunit.
| Object | ID | Access | Role |
|---|---|---|---|
| Codeunit “Copilot Quota” | 7785 | Public | Facade you call from your code |
| Codeunit “Copilot Quota Impl.” | 7786 | Internal | Talks to the platform through the ALCopilotFunctions .NET type |
| Codeunit “Copilot Notifications” | – | Internal | Shows the “capacity used up” and “nearly used up” notifications |
| Page “Copilot AI Capabilities” | – | – | Runs the Impl codeunit as a page background task |
CanConsume() is the procedure of the codeunit you can call from a cloud extension:
procedure CanConsume(): Boolean
It asks the platform for the tenant’s quota details and returns whether the tenant is still allowed to consume Copilot capacity. More in details:
- If the platform can’t return quota details, the method returns
false. On-premises or in a Docker sandbox you should not expecttrue, so don’t gate features on it outside SaaS. - It emits telemetry every time (
0000P7Nas a warning,0000P7Oas normal). The scope isExtensionPublisherand the category is the Copilot category. Calling it in a tight loop produces telemetry noise, so call it once per user action.
The Impl codeunit also has an OnRun trigger. It collects three values from the platform and hands them back as a page background task result:
Results.Add('CanConsume', Format(ALCopilotQuotaDetails.CanConsume()));
Results.Add('HasSetupBilling', Format(ALCopilotQuotaDetails.HasSetupBilling()));
Results.Add('QuotaUsedPercentage', Format(ALCopilotQuotaDetails.QuotaUsedPercentage()));
Page.SetBackgroundTaskResult(Results);
This is how the Copilot & agent capabilities page knows whether to warn you, without slowing down page load. The Impl codeunit is internal, so you can’t enqueue it from your own pages.
Two callers reach the platform’s quota service: your extension through the public facade, and the capabilities page through the internal implementation:
Your extension can only ask whether the tenant can consume. The capabilities page skips the facade and runs the Impl codeunit as a background task, then turns its three values into notifications. If billing is set up, the 80% warning is suppressed.
Using CanConsume() in your own extensions.
For a partner, CanConsume() is a pre-flight check. It matters most when your capability runs on the Business Central AI resources (SetManagedResourceAuthorization), because those calls consume the customer’s Copilot Credits. If you bring your own Azure OpenAI resource, Copilot Credits aren’t consumed and the check is less relevant.
Personal suggestion is to wrap the check once, so every feature behaves the same way and you skip it outside SaaS, where CanConsume() returns false. For example:
codeunit 50100 "Copilot Quota Guard"
{
Access = Internal;
procedure CanUseCopilot(): Boolean
var
CopilotQuota: Codeunit "Copilot Quota";
EnvironmentInformation: Codeunit "Environment Information";
begin
if not EnvironmentInformation.IsSaaSInfrastructure() then
exit(true); // own AOAI subscription in dev/test, no Copilot Credits
exit(CopilotQuota.CanConsume());
end;
procedure EnsureCanUseCopilot()
var
QuotaUsedUpErr: Label 'Your organization has used up its Copilot Credits capacity, so this AI feature is unavailable. Ask your administrator to add capacity and try again.';
begin
if not CanUseCopilot() then
Error(QuotaUsedUpErr);
end;
}
Perform the check before you build the prompt and call the model. The user gets a clear message instead of a generic failure from the Azure OpenAI call:
systemaction(Generate)
{
trigger OnAction()
var
QuotaGuard: Codeunit "Copilot Quota Guard";
begin
QuotaGuard.EnsureCanUseCopilot();
GenerateProposal(); // your AzureOpenAI.GenerateChatCompletion logic
end;
}
In unattended processing (Job Queue), check once before the loop, not per record. If quota is gone, log it and exit. The job queue entry ends without an error, the documents stay Pending, and the next scheduled run picks them up. An error on every run would only fill the job queue log and hide real failures:
trigger OnRun()
var
QuotaGuard: Codeunit "Copilot Quota Guard";
PendingDoc: Record "My AI Pending Document";
begin
if not QuotaGuard.CanUseCopilot() then begin
Session.LogMessage('MYAPP002', 'Copilot Credits capacity is used up. AI processing skipped until the next run.',
Verbosity::Warning, DataClassification::SystemMetadata, TelemetryScope::ExtensionPublisher, 'Category', 'MyAI');
exit;
end;
PendingDoc.SetRange(Status, PendingDoc.Status::Pending);
if PendingDoc.FindSet() then
repeat
ProcessWithAI(PendingDoc);
until PendingDoc.Next() = 0;
end;
Beyond quota: tracking AI consumption.
CanConsume() tells you whether you can call the model. To know who pays and how much you consume, System.AI has three more public tools.
Since version 27.0, you can declare who bills a capability when you register it. Enum 7786 “Copilot Billing Type” has three usable values:
| Value | Meaning |
|---|---|
Not Billed | The capability is free for the customer |
Microsoft Billed | Microsoft bills it, in Copilot Credits |
Custom Billed | You, the publisher, bill it |
A fourth value, Undefined, is for internal use only. The old RegisterCapability and ModifyCapability overloads without a billing type are obsolete: they store the billing type as Undefined and disappear once CLEAN27 code is removed.
CopilotCapability.RegisterCapability(
Enum::"Copilot Capability"::"Draft a Job",
Enum::"Copilot Availability"::"Generally Available",
Enum::"Copilot Billing Type"::"Microsoft Billed",
LearnMoreUrlTxt);
The value is stored in the “Copilot Settings” table and shown in the Billing Type column on the capabilities page. It’s also added as a BillingType dimension to the capability’s telemetry. To change it in a later version, call ModifyCapability from an upgrade codeunit.
Regarding tokens, codeunit 7759 “AOAI Token” gives you two different views of token usage:
| Method | When | What it measures |
|---|---|---|
GetGPT4TokenCount, GetGPT35TokenCount, GetAdaTokenCount, GetDavinciTokenCount | Before the call | A local estimate of the input, using the cl100k_base or p50k_base tokenizer |
GetTotalServerSessionTokensConsumed | After the call | Actual tokens consumed in the session, as counted by the platform. The total tokens used is aggregated for all the models. |
On the chat messages side, AOAIChatMessages.GetHistoryTokenCount() and SetHistoryLength() keep the context you send under control.
Regarding GetTotalServerSessionTokensConsumed, remember that:
- It’s cumulative for the server session, across all models. There’s no split by prompt and completion, capability or app.
- Every session has its own counter (a web client session, a background session, a job queue task, a page background task).
Because the counter is cumulative, read it before and after your call and log the difference:
var
AOAIToken: Codeunit "AOAI Token";
TokensBefore, TokensUsed: Integer;
Dims: Dictionary of [Text, Text];
begin
TokensBefore := AOAIToken.GetTotalServerSessionTokensConsumed();
AzureOpenAI.GenerateChatCompletion(ChatMessages, Params, Response);
TokensUsed := AOAIToken.GetTotalServerSessionTokensConsumed() - TokensBefore;
Dims.Add('TokensUsed', Format(TokensUsed));
Session.LogMessage('MYAPP001', 'AI call completed', Verbosity::Normal,
DataClassification::SystemMetadata, TelemetryScope::ExtensionPublisher, Dims);
end;
The actual delta minus the estimated prompt tokens gives a rough figure for the output tokens. That’s enough to set a token budget per operation.
If your capability is Custom Billed, this delta is also the starting point for your own metering. Store it per company and capability in a table you control, not only in telemetry, so your invoices don’t depend on telemetry sampling or retention.
What’s left in telemetry.
Two signals give you the consumption picture in Application Insights:
| Signal | Emitted by | What it carries |
|---|---|---|
0000LT4 | “Azure OpenAI Impl”, on every chat, completion or embeddings call | Estimated metaprompt, prompt and total token counts |
RT0055 “AI Consumption Recorded” | The platform | Copilot Credits consumed, with copilotCredits, agentTaskId, actions and featureName |
RT0055 appears in environment telemetry for every feature with consumption-based billing. In extension telemetry it only covers agents that are published to the environment as part of your app.
traces
| where timestamp > ago(30d)
| where customDimensions.eventId == 'RT0055'
| project timestamp,
featureName = tostring(customDimensions.featureName),
actions = tostring(customDimensions.actions),
agentTaskId = tostring(customDimensions.agentTaskId),
copilotCredits = todouble(customDimensions.copilotCredits)
| summarize credits = sum(copilotCredits) by featureName, bin(timestamp, 1d)
Conclusion
“Copilot Quota” codeunit combined with the billing type, the token counters in “AOAI Token” and the consumption telemetry, it gives you what you need to keep AI costs visible. It also lets you build features that tell the user what’s going on when capacity runs out, instead of failing with a cryptic error.
- Before the call: declare the right billing type, check
CanConsume()and estimate the prompt with “AOAI Token”. - During the call: read
GetTotalServerSessionTokensConsumed()before and after, and log the delta. - After the call: follow
0000LT4andRT0055in Application Insights to see what each feature really costs.
With this in place, your AI features tell users clearly when capacity runs out. You can now also answer the question every customer will soon ask: how much is Copilot costing us, and for what? 😅

