Skip to content

Cost tracking

Record Cloudflare neurons per call with the WorkersAiUsageReported event or the raw response.

Updated

Cloudflare bills Workers AI in neurons, its own usage unit, not in tokens. Every model call returns a neuron figure, in usage.neurons or a cf-ai-neurons header, so a cost model built on token counts gives the wrong number.

Laravel AI’s usage objects (Usage, TextUsage) and Meta have no field for neurons, so the package gives you two other ways to read them: an event and the raw response.

Listen for the event

The package dispatches Meirdick\WorkersAi\Events\WorkersAiUsageReported once per model call, for chat, embeddings, Clef, Clairic, images, speech, transcription and reranking. A tool loop with two steps makes two model calls, so it dispatches two events.

use Illuminate\Support\Facades\Event;
use Meirdick\WorkersAi\Events\WorkersAiUsageReported;

Event::listen(WorkersAiUsageReported::class, function (WorkersAiUsageReported $event) {
    Spend::record($event->model, $event->neurons);
});

The event has these public properties:

Property Type Description
provider string The provider name from your config.
model string The model name sent to Cloudflare. On /compat it has the workers-ai/ prefix.
neurons float The neuron count for this call.
promptTokens int Uncached prompt tokens on chat, on both laravel/ai lines. The reported input count on run API calls.
completionTokens int Completion tokens.
reasoningTokens int Reasoning tokens.

When the reply has no neuron figure, or the figure is 0, the package dispatches nothing.

Streamed requests

Since 1.0.1, the event fires for streamed requests too. It fires once per model call, after the last chunk, with the total for the call. Cloudflare sends usage.neurons on every stream chunk: early chunks hold a per-chunk figure and the last chunk holds the total. So the package uses the last figure rather than adding up the chunks. A streamed tool loop dispatches one event per step.

$response->raw isn’t set on a stream, so use the event there.

Read the raw response

You can also read the figure from the raw HTTP response, which is on the Laravel AI response and on each step of a tool loop:

$response = agent('You are a helpful assistant.')->prompt('...', provider: 'workers-ai');

$neurons = $response->raw?->json('usage.neurons');
$latency = $response->raw?->transferStats?->getTransferTime();

This needs a laravel/ai version where StepResponse has withRawResponse(). On laravel/ai 0.9, raw is null, so use the event there.

Token counts by laravel/ai line

The usage object on the response differs between the laravel/ai 0.9 to 0.11 line and the 1.x line. The event is the same on both.

// laravel/ai 0.9 to 0.11: promptTokens excludes cached tokens
$response->usage->promptTokens;
$response->usage->completionTokens;
$response->usage->cacheReadInputTokens;

// laravel/ai 1.x: inputTokens includes cached tokens
$response->usage->inputTokens;
$response->usage->outputTokens;
$response->usage->cacheReadInputTokens; // null when absent

See AI Gateway for a measured example.

Calls on the run API

Clef, Clairic, images, speech, transcription and reranking use Cloudflare’s /ai/run API. They dispatch the same event, with provider, model and neurons. promptTokens and completionTokens hold whatever counts the model reports, as Cloudflare sends them. Speech and binary images report no tokens, so those fields are 0.

These calls have no $response->raw, so use the event. Where the model reports token counts, they’re on the response:

$response->usage->inputTokens;   // laravel/ai 1.x
$response->usage->promptTokens;  // laravel/ai 0.9 to 0.11

Cloudflare prices Clef and Clairic per input token, with no charge for output. See the pricing page below.

Order of magnitude

To give you a sense of scale, here are figures measured on 2026-09-09 on one account. Reasoning costs neurons: on @cf/openai/gpt-oss-120b, one two-sentence prompt cost 9.6 neurons at reasoning_effort: low and 31.4 at high. A 16-token completion on @cf/meta/llama-3.3-70b-instruct-fp8-fast cost 4.48 neurons.

Pricing

For the price per neuron and the free allowance, see the Cloudflare Workers AI pricing page.

Type to search…

↑↓ navigate↵ selectEsc close