Skip to content

Custom Providers ​

Spectra has built-in support for several AI providers. To add another one, create a provider class and one or more handler classes, then register the provider in the config. Spectra then detects and tracks requests to the new provider the same way it does for the built-in ones.

How Provider Detection Works ​

When Spectra intercepts an outgoing HTTP request, it matches it against the configured providers in two steps:

  1. Host matching. The request URL's hostname is compared against each provider's getHosts() list. Host patterns support {placeholder} syntax for variable segments, such as {resource}.openai.azure.com.
  2. Endpoint matching. The URL path goes through the provider's normalizeEndpoint() method, then is checked against each handler's endpoints() list to confirm the request targets a trackable API endpoint.

Once a request matches, the provider's handler extracts normalized metrics from the response.

TIP

Spectra only tracks usage endpoints: requests that consume tokens, generate media, or produce other billable output. Other endpoints, such as listing models (/v1/models), health checks, or configuration lookups, are not tracked, even on a recognized provider host.

Architecture Overview ​

Each provider has two levels:

  • Provider class. Extends Provider and defines the provider's hosts, name, and handler list. The provider only coordinates; its handlers do all the metric extraction.
  • Handler classes. Each handler implements the Handler interface and owns the extraction logic for one endpoint type (chat completions, embeddings, images, etc.).
MyProvider (extends Provider)
├── ChatHandler     → /v1/chat/completions
├── EmbeddingHandler → /v1/embeddings
└── ImageHandler    → /v1/images/generations

A provider with a single endpoint type needs only one handler. If the provider has several API shapes, create a handler for each endpoint type.

Creating a Provider ​

This example builds a provider for Mistral AI. Mistral is already built in, but its API is simple and easy to follow, so it makes a good example.

Step 1: Create the Handler ​

Handlers implement the Handler interface and hold all the extraction logic for one endpoint type. A handler defines the model type, the endpoints it matches, and methods that extract the metrics, the model name, and the response content.

The MatchesEndpoints trait provides the matchesEndpoint() method the Handler interface requires. It does an exact match against the handler's endpoints() array, which is enough for most providers. If your endpoints have dynamic segments (for example, /v1/models/{model}:generateContent), override matchesEndpoint() with your own regex instead.

php
<?php

namespace App\Spectra\Handlers;

use Spectra\Concerns\MatchesEndpoints;
use Spectra\Contracts\ExtractsModelFromResponse;
use Spectra\Contracts\Handler;
use Spectra\Data\Metrics;
use Spectra\Data\TokenMetrics;
use Spectra\Enums\ModelType;

class MistralChatHandler implements Handler, ExtractsModelFromResponse
{
    use MatchesEndpoints;

    public function modelType(): ModelType
    {
        return ModelType::Text;
    }

    public function endpoints(): array
    {
        return ['/v1/chat/completions'];
    }

    /**
     * Extract token usage metrics from the provider's response.
     * The returned Metrics DTO determines how cost is calculated.
     * promptTokens is the full prompt, completionTokens the full output.
     */
    public function extractMetrics(
        array $requestData,
        array $responseData
    ): Metrics {
        $usage = $responseData['usage'] ?? [];

        return new Metrics(
            tokens: new TokenMetrics(
                promptTokens: $usage['prompt_tokens'] ?? 0,
                completionTokens: $usage['completion_tokens'] ?? 0,
            ),
        );
    }

    /**
     * Extract the model identifier from the response.
     * This is used for pricing lookup and dashboard display.
     */
    public function extractModelFromResponse(array $response): ?string
    {
        return $response['model'] ?? null;
    }

    /**
     * Extract the generated text content from the response.
     * This is stored in the database for inspection in the dashboard.
     */
    public function extractResponse(array $response): ?string
    {
        return $response['choices'][0]['message']['content'] ?? null;
    }
}

Step 2: Create the Provider Class ​

The provider class ties everything together. It defines the provider's name, the hosts it recognizes, and the handlers to use:

php
<?php

namespace App\Spectra;

use App\Spectra\Handlers\MistralChatHandler;
use Spectra\Providers\Provider;

class MistralProvider extends Provider
{
    public function getProvider(): string
    {
        return 'mistral';
    }

    public function getHosts(): array
    {
        return ['api.mistral.ai'];
    }

    public function handlers(): array
    {
        return [
            app(MistralChatHandler::class),
        ];
    }
}

The Provider base class requires three abstract methods:

MethodPurpose
getProvider()Returns the provider's identifier (for example, 'mistral'). Used as the stored provider value in the database.
getHosts()Returns hostnames to match against. Supports {placeholder} patterns for variable segments.
handlers()Returns an array of Handler instances. The provider routes each request to the matching handler.

Override normalizeEndpoint() when the same API is served under a different path layout. The returned path is used for endpoint matching, handler resolution, and the stored endpoint value. The built-in OpenAI provider uses it to map Azure OpenAI paths such as /openai/deployments/{deployment}/chat/completions to /v1/chat/completions:

php
public function normalizeEndpoint(string $path): string
{
    return preg_replace('#^/api/#', '/v1/', $path);
}

Step 3: Register the Provider ​

Add an entry to the providers array in config/spectra.php:

php
'providers' => [
    // Keep existing built-in providers.
    // ...

    // Your custom provider
    'mistral' => ['class' => App\Spectra\MistralProvider::class, 'name' => 'Mistral'],
],

Spectra now tracks every HTTP request to api.mistral.ai that targets the /v1/chat/completions endpoint.

Multi-Endpoint Providers ​

When a provider has several API shapes, for example separate endpoints for text, images, and audio, create a handler for each endpoint type and return them all from the provider's handlers() method. The Provider base class routes each request to the handler whose endpoint matches.

php
class MyProvider extends Provider
{
    public function handlers(): array
    {
        return [
            app(ImageHandler::class),     // /v1/images/generations
            app(EmbeddingHandler::class), // /v1/embeddings
            app(ChatHandler::class),      // /v1/chat/completions (catch-all)
        ];
    }
}

INFO

When each handler targets its own endpoints, order does not matter. Spectra checks all handlers and picks the one whose endpoint matches. When several handlers share an endpoint (for example, OpenAI's /v1/responses serves both text and image generation), implement the MatchesResponseShape interface on the specialized handlers so Spectra can tell them apart by the response body. List each specialized handler after the generic handler it specializes: Spectra checks later handlers first and falls back to the first matching handler.

Optional Handler Interfaces ​

Handlers can implement these extra interfaces to extract more metrics or change how a request is handled.

HasFinishReason ​

Each response says why text generation stopped. OpenAI uses finish_reason (stop, length, tool_calls), while Anthropic uses stop_reason (end_turn, max_tokens, stop_sequence). Implement HasFinishReason to read this value, so it shows in the dashboard and you can filter by it:

php
use Spectra\Contracts\HasFinishReason;

class MyChatHandler implements Handler, HasFinishReason
{
    public function extractFinishReason(array $response): ?string
    {
        // OpenAI-compatible format
        return $response['choices'][0]['finish_reason'] ?? null;

        // Anthropic format would be:
        // return $response['stop_reason'] ?? null;
    }
}

MatchesResponseShape ​

Some providers use the same endpoint for different kinds of output. For example, OpenAI's /v1/responses endpoint can return both text completions and generated images. When several handlers match the same endpoint, Spectra calls matchesResponse() on each one to decide which should process the response. The handler that recognizes the response structure wins:

php
use Spectra\Contracts\MatchesResponseShape;

// This handler claims /v1/responses only when the output contains images
class MyImageHandler implements Handler, MatchesResponseShape
{
    public function endpoints(): array
    {
        return ['/v1/responses'];
    }

    public function matchesResponse(array $data): bool
    {
        // Check if the response contains image generation output
        foreach ($data['output'] ?? [] as $item) {
            if (($item['type'] ?? null) === 'image_generation_call') {
                return true;
            }
        }

        return false;
    }
}

// This handler claims /v1/responses for regular text completions
class MyTextHandler implements Handler, MatchesResponseShape
{
    public function endpoints(): array
    {
        return ['/v1/chat/completions', '/v1/responses'];
    }

    public function matchesResponse(array $data): bool
    {
        return ($data['object'] ?? null) === 'response'
            || ($data['object'] ?? null) === 'chat.completion';
    }
}

ExtractsModelFromResponse ​

Most AI providers include the model name in the JSON response body. Implement ExtractsModelFromResponse so Spectra can read the model identifier for pricing lookup and dashboard display. This is the most common way to get the model, so implement it on any handler whose response body has a model field:

php
use Spectra\Contracts\ExtractsModelFromResponse;

class MyChatHandler implements Handler, ExtractsModelFromResponse
{
    public function extractModelFromResponse(array $response): ?string
    {
        return $response['model'] ?? null;
    }
}

INFO

extractModelFromResponse() is not part of the base Handler interface. You opt in by implementing ExtractsModelFromResponse. If your handler does not implement this interface, Spectra falls back to ExtractsModelFromRequest (below) or the model field in the request body.

ExtractsModelFromRequest ​

Some providers put the model name in the URL path instead of the response body (for example, Google's /v1/models/{model}:generateContent or fal.ai's /fal-ai/flux/dev). Implement ExtractsModelFromRequest to read the model from the request data or the endpoint path. Handlers that implement this interface usually don't implement ExtractsModelFromResponse, but you can combine both when the model appears in the request URL and in the response body:

php
use Spectra\Contracts\ExtractsModelFromRequest;

class MyHandler implements Handler, ExtractsModelFromRequest
{
    public function extractModelFromRequest(
        array $requestData,
        string $endpoint
    ): ?string {
        // Extract from URL path: /v1/models/gemini-pro:generateContent
        if (preg_match('#/models/([^/:]+)#', $endpoint, $matches)) {
            return $matches[1];
        }

        return null;
    }
}

SkipsResponse ​

Some APIs make you poll for a result that isn't ready yet. Video generation, for example, returns an unfinished job (queued, in_progress) until processing finishes. Implement SkipsResponse and return true for these unfinished states, so Spectra does not record the polls. Only the finished response is tracked:

php
use Spectra\Contracts\SkipsResponse;

class MyVideoHandler implements Handler, SkipsResponse
{
    public function shouldSkipResponse(array $responseData): bool
    {
        // Only track finished video generations
        return ($responseData['status'] ?? null) !== 'completed';
    }
}

If the finished job is fetched more than once, Spectra does not count it twice. Writes with the same provider response id update the existing row, and only the change is added to the daily stats and budgets. Make sure your handler's response id is the job id.

HasProviderLatency ​

For an async job, the HTTP call that fetched the result only measures a status poll. Implement HasProviderLatency on the handler to return the real generation time the provider reports, in milliseconds. Spectra stores it as the request's latency. Return null when the response doesn't say:

php
use Spectra\Contracts\HasProviderLatency;

class MyVideoHandler implements Handler, HasProviderLatency
{
    public function extractProviderLatency(array $responseData): ?int
    {
        // The provider reports the generation time in seconds
        $seconds = $responseData['metrics']['predict_time'] ?? null;

        return is_numeric($seconds) ? (int) round($seconds * 1000) : null;
    }
}

HasExpiration ​

Generated media URLs (images, videos) are often temporary and expire after a set period. Implement HasExpiration so Spectra records when the generated content stops being available:

php
use Illuminate\Support\Carbon;
use Spectra\Contracts\HasExpiration;

class MyHandler implements Handler, HasExpiration
{
    public function extractExpiresAt(array $responseData): ?Carbon
    {
        return isset($responseData['expires_at'])
            ? Carbon::createFromTimestamp($responseData['expires_at'])
            : null;
    }
}

ReturnsBinaryResponse ​

Some endpoints return binary data instead of JSON (for example, text-to-speech endpoints return raw audio bytes). Implement this marker interface so Spectra knows not to JSON-decode the response body. A binary response can't contain a model name, so these handlers usually implement ExtractsModelFromRequest instead of ExtractsModelFromResponse:

php
use Spectra\Contracts\ExtractsModelFromRequest;
use Spectra\Contracts\ReturnsBinaryResponse;

class MySpeechHandler implements Handler, ExtractsModelFromRequest, ReturnsBinaryResponse
{
    public function extractModelFromRequest(
        array $requestData,
        string $endpoint
    ): ?string {
        return $requestData['model'] ?? null;
    }

    public function extractResponse(array $response): ?string
    {
        return '[audio]';
    }
}

StreamsResponse ​

Implement StreamsResponse to provide a custom StreamHandler for streamed responses. Spectra parses the stream (server-sent events, newline-delimited JSON, or a JSON array) and passes each decoded event to the StreamHandler, which extracts text content, token usage, finish reasons, and the model:

php
use Spectra\Contracts\StreamsResponse;
use Spectra\Support\Tracking\StreamHandler;

class MyChatHandler implements Handler, StreamsResponse
{
    public function streamingHandler(): StreamHandler
    {
        return new MyStreamHandler();
    }
}

The StreamHandler abstract class requires four methods:

php
use Spectra\Support\Tracking\StreamHandler;

class MyStreamHandler extends StreamHandler
{
    public function text(array $data): ?string
    {
        return $data['choices'][0]['delta']['content'] ?? null;
    }

    public function usage(array $data, array $currentUsage): array
    {
        $usage = $data['usage'] ?? null;

        if (! is_array($usage)) {
            return $currentUsage; // This event has no usage
        }

        return array_merge($currentUsage, [
            'prompt_tokens' => $usage['prompt_tokens'] ?? 0,         // full prompt
            'completion_tokens' => $usage['completion_tokens'] ?? 0, // full output
            'cached_tokens' => $usage['prompt_tokens_details']['cached_tokens'] ?? 0,
            'reasoning_tokens' => $usage['completion_tokens_details']['reasoning_tokens'] ?? 0,
        ]);
    }

    public function finishReason(array $data): ?string
    {
        return $data['choices'][0]['finish_reason'] ?? null;
    }

    public function model(array $data): ?array
    {
        return isset($data['model']) ? ['model' => $data['model'], 'id' => $data['id'] ?? null] : null;
    }
}

usage() receives the usage collected so far and returns the updated array. This lets providers that send usage in several events (like Anthropic) add it up. The token keys follow the TokenMetrics rules: prompt_tokens is the full prompt and completion_tokens is the full output. The breakdown keys are:

KeyPart of
cached_tokensprompt_tokens
cache_write_tokensprompt_tokens
cache_write_1h_tokenscache_write_tokens
audio_input_tokensprompt_tokens
image_input_tokensprompt_tokens
reasoning_tokenscompletion_tokens
audio_output_tokenscompletion_tokens
image_output_tokenscompletion_tokens

You may also return these extra keys. Spectra applies them to the request:

KeyTypeMeaning
reported_cost_in_centsfloatThe cost the provider reported, in US cents. It replaces the catalog price.
tool_callsarray<string, int>Billable server-tool usage, keyed like the provider's tool_calls
pricing_tierstringThe tier the provider says it served. Ignored when a tier was set explicitly.
cost_multiplierfloatA price multiplier, for example 1.1 for US-only inference

HasMedia ​

AI providers often return generated images and videos as temporary URLs that expire after a set period. Implement HasMedia to download these files and save them to a Laravel filesystem disk before the URLs expire. $rawBody holds the raw response body for binary responses (for example, TTS audio). The following example shows how OpenAI's image handler stores generated images:

php
use Illuminate\Support\Facades\Http;
use Spectra\Contracts\HasMedia;
use Spectra\Support\MediaPersister;

class MyImageHandler implements Handler, HasMedia
{
    public function storeMedia(
        string $requestId,
        array $responseData,
        ?string $rawBody = null
    ): array {
        $persister = app(MediaPersister::class);
        $stored = [];

        foreach ($responseData['data'] ?? [] as $i => $item) {
            if (isset($item['b64_json'])) {
                // Image returned as base64-encoded data
                $content = base64_decode($item['b64_json']);
                $stored[] = $persister->store($requestId, $i, $content, 'png');
            } elseif (isset($item['url'])) {
                // Image returned as a temporary URL: download before it expires
                $content = Http::withoutAITracking()->get($item['url'])->body();
                $stored[] = $persister->store($requestId, $i, $content, 'png');
            }
        }

        return $stored;
    }
}

ExtractsPricingTierFromRequest / ExtractsPricingTierFromResponse ​

Implement these on the provider class (not the handler) when the provider offers several pricing tiers. They let Spectra detect the active tier from the request or the response. Implement one or both, depending on where the tier shows up. Return a Spectra tier name (standard, batch, flex, priority, fast, ultrafast, hd) or null:

php
use Spectra\Contracts\ExtractsPricingTierFromRequest;
use Spectra\Contracts\ExtractsPricingTierFromResponse;

class MyProvider extends Provider implements ExtractsPricingTierFromRequest, ExtractsPricingTierFromResponse
{
    public function extractPricingTierFromRequest(array $requestData): ?string
    {
        return $requestData['service_tier'] ?? null;
    }

    public function extractPricingTierFromResponse(array $responseData): ?string
    {
        return $responseData['service_tier'] ?? null;
    }
}

The tier from the response wins over the tier from the request, because the provider may serve a request at a different tier than it asked for. A tier set with Spectra::withPricingTier() or the pricing_tier option wins over both.

Spectra reads the default pricing tier from config (spectra.costs.provider_settings.{provider}.default_tier), so providers don't need to define it themselves. To set a default tier for your provider, add it to config/spectra.php:

php
'provider_settings' => [
    'myprovider' => [
        'default_tier' => env('SPECTRA_MYPROVIDER_PRICING_TIER', 'standard'),
    ],
],

ExtractsCostMultiplier ​

Also implemented on the provider class. Use it when the provider charges a surcharge that the response shows, such as Anthropic's 1.1x price for US-only inference (usage.inference_geo). Return the multiplier, or null for none. Spectra multiplies the model cost by it (tool call fees are not multiplied):

php
use Spectra\Contracts\ExtractsCostMultiplier;

class MyProvider extends Provider implements ExtractsCostMultiplier
{
    public function extractCostMultiplier(array $responseData, array $requestData): ?float
    {
        return ($responseData['usage']['region'] ?? null) === 'us' ? 1.1 : null;
    }
}

ExtractsCostMultiplierForModel ​

The same, with the model id. Use it when the surcharge depends on the model, such as fal.ai video models, which cost more or less than their catalog price at another resolution or without audio, by a different amount for each model:

php
use Spectra\Contracts\ExtractsCostMultiplierForModel;

class MyProvider extends Provider implements ExtractsCostMultiplierForModel
{
    public function extractCostMultiplierForModel(string $model, array $responseData, array $requestData): ?float
    {
        return $model === 'my-video-model' && ($requestData['generate_audio'] ?? true) === false ? 0.5 : null;
    }
}

For a polled async job, $requestData is the body of the request that submitted it.

ReportsRequestCost ​

A marker with no methods, for a provider whose handlers return reportedCostInCents (see Metrics DTOs). The cost stored for its requests is the provider's own figure, so recalculating stored costs from the catalog leaves them as recorded:

php
use Spectra\Contracts\ReportsRequestCost;

class MyProvider extends Provider implements ReportsRequestCost
{
    // ...
}

Metrics DTOs ​

Handlers return a Metrics container that holds a typed DTO for each kind of metric. Use the DTOs that match your handler's model type:

php
use Spectra\Data\AudioMetrics;
use Spectra\Data\DocumentMetrics;
use Spectra\Data\ImageMetrics;
use Spectra\Data\Metrics;
use Spectra\Data\SearchMetrics;
use Spectra\Data\TokenMetrics;
use Spectra\Data\VideoMetrics;

// Text / LLM response
new Metrics(tokens: new TokenMetrics(
    promptTokens: 1200,      // full prompt: uncached + cache reads + cache writes
    completionTokens: 300,   // full output, including reasoning
    cachedTokens: 800,       // part of promptTokens
    cacheWriteTokens: 200,   // part of promptTokens
    reasoningTokens: 120,    // part of completionTokens
));

// Image generation
new Metrics(image: new ImageMetrics(count: 4, megapixels: 4.2));

// Audio (TTS or STT)
new Metrics(audio: new AudioMetrics(
    durationSeconds: 30.5,
    inputCharacters: 500,    // For TTS
));

// Video generation
new Metrics(video: new VideoMetrics(
    count: 1,
    durationSeconds: 15.0,
));

// Per-search models (for example, rerank)
new Metrics(search: new SearchMetrics(count: 1));

// Per-page document models (for example, OCR)
new Metrics(document: new DocumentMetrics(pages: 12));

TokenMetrics ​

promptTokens must be the full prompt and completionTokens the full output. Every other field is a part of one of these two totals, so nothing is counted twice. If your provider reports a part outside its totals (for example, Anthropic leaves cache tokens out of input_tokens), add it to the total yourself.

FieldPart ofDescription
promptTokensNone (total)All input tokens: uncached, cache reads, and cache writes
completionTokensNone (total)All output tokens, including reasoning
cachedTokenspromptTokensInput tokens read from the prompt cache
cacheWriteTokenspromptTokensInput tokens written to the prompt cache
cacheWrite1hTokenscacheWriteTokensCache writes with a 1-hour lifetime. The rest bill at the 5-minute rate.
audioInputTokenspromptTokensUncached audio input tokens
imageInputTokenspromptTokensUncached image input tokens
reasoningTokenscompletionTokensOutput tokens used for reasoning or thinking
audioOutputTokenscompletionTokensAudio output tokens
imageOutputTokenscompletionTokensImage output tokens

TokenMetrics::fromArray() builds the object from a snake_case usage array (prompt_tokens, cached_tokens, cache_write_tokens, ...), and toUsageArray() turns it back into one.

Metrics ​

FieldTypeDescription
tokens?TokenMetricsToken usage
image?ImageMetricsImage count, and output megapixels for models billed per megapixel
audio?AudioMetricsAudio duration and input characters
video?VideoMetricsVideo count and duration
search?SearchMetricsBilled search units
document?DocumentMetricsPages, for models billed per page
toolCallsarray<string, int>Billable server-tool usage, keyed like the provider's tool_calls (for example, ['web_search_requests' => 3])
reportedCostInCents?floatThe cost the provider reported for this request, in US cents. When set, it replaces the catalog price.
php
$costInDollars = $responseData['usage']['cost'] ?? null;

return new Metrics(
    tokens: new TokenMetrics(promptTokens: 1200, completionTokens: 300),
    toolCalls: ['web_search_requests' => 3],
    reportedCostInCents: $costInDollars !== null ? $costInDollars * 100 : null,
);

The handler's modelType() decides how the request is classified and shown in the dashboard. The cost formula comes from the model's pricing unit in the catalog:

ModelTypeUsed ForPrimary Metric
TextChat completions, text generation, OCRTokens or pages
EmbeddingVector embeddingsTokens (input only)
ImageImage generationImage count or megapixels
VideoVideo generationVideo count / duration
TtsText-to-speechDuration / characters
SttSpeech-to-textDuration

OpenAI-Compatible Providers ​

Many providers offer an OpenAI-compatible chat/completions endpoint. For these, extend OpenAiCompatibleChatHandler. It already reads the usage, model, finish reason, and response text, and it handles streamed responses too. You only need to list the endpoints:

php
use Spectra\Providers\Concerns\OpenAiCompatibleChatHandler;

class AcmeChatHandler extends OpenAiCompatibleChatHandler
{
    public function endpoints(): array
    {
        return ['/v1/chat/completions'];
    }
}

Override these methods when the provider is different from OpenAI. They are used for normal and streamed responses:

MethodOverride when
reasoningTokensAreSeparate(): bool (protected)The provider reports reasoning tokens outside completion_tokens (like xAI). Return true, and Spectra adds them to the output.
reportedCostInCents(array $usage, array $responseData): ?floatThe response contains the charged cost (like OpenRouter usage.cost). Return it in US cents.
usageFrom(array $data): ?arrayThe usage object is somewhere else in the response or stream event. The default reads usage, then x_groq.usage.
tokensFromUsage(array $usage): TokenMetricsThe usage fields have other names. The default reads prompt_tokens, completion_tokens, prompt_tokens_details.cached_tokens, prompt_tokens_details.cache_write_tokens, prompt_tokens_details.audio_tokens, completion_tokens_details.reasoning_tokens, and completion_tokens_details.audio_tokens.
php
class AcmeChatHandler extends OpenAiCompatibleChatHandler
{
    public function endpoints(): array
    {
        return ['/v1/chat/completions'];
    }

    protected function reasoningTokensAreSeparate(): bool
    {
        return true;
    }

    public function reportedCostInCents(array $usage, array $responseData): ?float
    {
        return isset($usage['cost']) ? (float) $usage['cost'] * 100 : null;
    }
}

Adding Pricing ​

To track costs, add your provider to your pricing file and point SPECTRA_PRICING_PATH at it. A provider the bundled catalog doesn't have is added as is, with its models keyed by the id used in API requests:

json
{
    "providers": {
        "mistral": {
            "currency": "USD",
            "sources": ["https://docs.mistral.ai/inference/pricing"],
            "models": {
                "mistral-large-latest": {
                    "display_name": "Mistral Large",
                    "capabilities": ["text"],
                    "tiers": [{ "tier": "standard", "input_price": 200, "output_price": 600 }]
                },
                "mistral-small-latest": {
                    "display_name": "Mistral Small",
                    "capabilities": ["text"],
                    "tiers": [{ "tier": "standard", "input_price": 10, "output_price": 30 }]
                }
            }
        }
    }
}

sources records where the prices come from, so you can check them again when the provider changes its pricing. See The Pricing File for every field.

TIP

All prices in the pricing catalog are stored in cents. For token-based models, prices are in cents per million tokens (for example, 200 means $2.00 per million tokens). For unit-based models (images, audio, video), prices are in cents per unit.

Testing Custom Providers ​

Write fixture-based tests for each handler to check that it extracts metrics correctly. Cover model name extraction, token or metric extraction, response content extraction, endpoint matching, and edge cases such as missing fields or empty responses:

php
use App\Spectra\Handlers\MistralChatHandler;

it('extracts metrics from Mistral chat response', function () {
    $handler = new MistralChatHandler();

    $request = ['model' => 'mistral-large-latest', 'messages' => [...]];
    $response = [
        'model' => 'mistral-large-latest',
        'choices' => [
            ['message' => ['content' => 'Hello!'], 'finish_reason' => 'stop'],
        ],
        'usage' => [
            'prompt_tokens' => 10,
            'completion_tokens' => 5,
        ],
    ];

    $metrics = $handler->extractMetrics($request, $response);

    expect($metrics->tokens->promptTokens)->toBe(10);
    expect($metrics->tokens->completionTokens)->toBe(5);
    expect($handler->extractModelFromResponse($response))->toBe('mistral-large-latest');
    expect($handler->extractResponse($response))->toBe('Hello!');
});

Released under the MIT License.