PHP code example of jpsantosbembe / laravel-ai-budget

1. Go to this page and download the library: Download jpsantosbembe/laravel-ai-budget library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jpsantosbembe / laravel-ai-budget example snippets


use Bembe\AiBudget\Models\AiCredential;
use Bembe\AiBudget\Models\AiProfile;

AiCredential::create([
    'key' => 'openrouter',
    'provider' => 'openrouter',
    'credentials' => ['api_key' => config('services.openrouter.key')],
]);

AiProfile::create([
    'key' => 'support_triage',
    'model' => 'openai/gpt-4o-mini',
    'fallback_model' => 'google/gemini-2.5-flash',
    'retries' => 2,
    'max_output_tokens' => 500,
    'max_cost_per_run' => 0.05,
    'system_prompt' => 'You triage customer support messages.',
    'json_schema' => [
        'type' => 'object',
        '

use Bembe\AiBudget\AiRunner;

$result = app(AiRunner::class)->run('support_triage', [
    ['role' => 'user', 'content' => $message->body],
], [
    'subject_type' => $message::class,
    'subject_id' => $message->id,
]);

$result->json;         // ['urgency' => 'high', 'topic' => 'billing']
$result->model;        // which model actually answered
$result->usedFallback; // whether the primary model gave up
$result->costLocal;    // what it really cost

app(AiRunner::class)->estimate('support_triage', approximateInputTokens: 1_200);
// ['tokens' => 1700, 'usd' => 0.00048, 'local' => 0.0024, 'currency' => 'BRL']

// config/ai-budget.php
'currency' => 'BRL',
'fx_rate' => fn () => Cache::remember('usd-brl', 3600, fn () => Fx::usdTo('BRL')),

class WeeklyReview extends AiPipeline
{
    public function key(): string { return 'weekly_review'; }
    public function profileKey(): string { return 'review_master'; }

    public function stages(): array
    {
        return [
            ['name' => 'analyse', 'type' => self::STAGE_MAP],
            ['name' => 'summarise', 'type' => self::STAGE_SINGLE],
        ];
    }

    public function estimateStage(string $stage, array $params): float { /* ... */ }

    public function items(AiPipelineRun $run, string $stage): array
    {
        return Conversation::whereWeek('created_at', $run->params['week'])->pluck('id')->map(strval(...))->all();
    }

    public function handleItem(AiPipelineRun $run, string $stage, string $itemKey): array
    {
        $result = app(AiRunner::class)->run('conversation_analysis', [/* ... */]);

        return ['payload' => $result->json, 'cost' => $result->costLocal];
    }
}

$run = app(AiPipelineRunner::class)->dispatch(new WeeklyReview, ['week' => 38]);
app(AiPipelineRunner::class)->progressSnapshot($run);
app(AiPipelineRunner::class)->retry($run);  // only the items that failed

// config/ai-budget.php
'transports' => [
    'openrouter' => Bembe\AiBudget\Transport\OpenRouterTransport::class,
    'anthropic'  => App\Ai\AnthropicTransport::class,
],
bash
php artisan vendor:publish --tag=ai-budget-config