Skip to main content
A plugin can use the AI services the installation already has configured, instead of holding its own API keys. Aikeedo resolves a service for the model, and the billing service handles credits.

The flow

Reserving before the call stops a workspace from starting more work than its balance allows, then you release the estimate and consume the real cost once you know it.

Check access first

isModelGranted() reflects the workspace’s plan. assertCanConsume() throws Ai\Domain\Exceptions\InsufficientCreditsException or Billing\Domain\Exceptions\UsageLimitException, which the exception middleware turns into a 403.

Resolve a service

The factory returns the first registered service that implements the interface and supports the model, and throws when nothing matches.

Bill the usage

Most service responses carry the cost the provider service already calculated, such as TextCompletionResponse::$cost, so you rarely compute it yourself.
Always pass the Model. The billing service uses it to decide whether credits apply at all: when the workspace brings its own provider key, nothing is charged.

Cost calculation

Use the calculator when you bill something the core doesn’t price for you. CostCalculator::estimate($model) returns the model’s configured multiplier as a rough pre-flight number, and CostCalculator::calculate($amount, $model, $opt) converts real usage into credits using the installation’s configured rates. The optional bitmask selects a rate variant, such as CostCalculator::INPUT, CostCalculator::OUTPUT, CostCalculator::IMAGE, CostCalculator::QUALITY_HD and the SIZE_* constants.

Streaming chat

MessageServiceInterface::generateMessage() returns a generator of stream parts. Fold them as they arrive, and take the cost from the usage part:
To forward that stream to a browser, see Streaming responses.

Which models exist

The model registry holds every service and model the installation knows about, including admin customizations:
Let administrators pick a model in your settings page rather than hardcoding one, and fall back to a related global setting, such as option.embeddings.model, where one exists.

Adding your own provider

Everything above consumes services the core registered. To add a provider, implement a capability interface and register it with the factory. See AI providers.