PHP code example of boriskwemo / llm-ledger

1. Go to this page and download the library: Download boriskwemo/llm-ledger 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/ */

    

boriskwemo / llm-ledger example snippets


$llm = Llm::fromYaml('llm.yaml');

$result = $llm->chat('deepseek-chat', 'Explain quantum computing in one sentence.');

echo $result->text();              // the answer
echo $result->usage->totalTokens;  // 42
echo $result->usage->reasoningTokens; // hidden thinking tokens, exposed
echo $result->cost->total;         // 0.000123  (USD)


LlmCostTracker\Client\Llm;

$llm = Llm::fromYaml('llm.yaml');

$result = $llm->chat('gpt-5.2', 'Hello!', [
    'temperature' => 0.4,
    'tags' => ['demo'],
    'user' => 'alice',
]);

echo $result->text();
printf("cost: $%.6f\n", $result->cost->total);

// Lifetime aggregates
$totals = $llm->tracker()->totals();
printf("%d requests, $%.4f total\n", $totals->requests, $totals->cost);

$result = $llm->chat('anthropic:claude-sonnet-4-6', [
    ['role' => 'system', 'content' => 'You are concise.'],
    ['role' => 'user',   'content' => 'What is 17 × 23?'],
]);

// Or multiple content parts (vision)
$result = $llm->chat('qwen-vl-max', [[
    'role' => 'user',
    'content' => [
        ['type' => 'text', 'text' => 'What is in this image?'],
        ['type' => 'image_url', 'image_url' => ['url' => 'https://example.com/img.png']],
    ],
]]);

$registry = $llm->models();
$registry->active();          // non-deprecated models
$registry->deprecated();      // models marked deprecated
$registry->get('gpt-4o');     // full ModelInfo (incl. ->deprecation)
$registry->search('deepseek');

$result = $llm->chat('deepseek-reasoner', 'Prove that sqrt(2) is irrational.');

$result->usage->reasoningTokens;    // thinking/reasoning tokens (provider-reported)
$result->usage->cachedPromptTokens; // prompt-cache reads
$result->usage->cacheCreationTokens;// Anthropic cache writes
$result->reasoningText();           // the chain-of-thought text (when exposed)
$result->finishReason;              // stop | length | tool_calls | ...
$result->toolCalls;                 // tool/function calls

// Strings, or [custom_id => messages] pairs.
$job = $llm->batch('gpt-5.2', [
    'Summarize article A',
    ['custom_id' => 'b-1', 'messages' => 'Summarize article B'],
    ['custom_id' => 'b-2', 'messages' => [['role' => 'user', 'content' => '…']]],
], [
    'completion_window' => '24h',   // OpenAI
    'temperature' => 0.2,
    'timeout' => 900,               // max seconds to wait (default 600)
    'tags' => ['nightly-job'],
]);

$job->status;      // completed | failed | in_progress | ...
$job->succeeded(); // number of successful items
$job->items;       // per-item [custom_id, content, usage, cost, reasoning, error]

   return [
       // ...
       LlmCostTracker\Symfony\LlmCostTrackerBundle::class => ['all' => true],
   ];
   

   public function __construct(private \LlmCostTracker\Client\Llm $llm) {}

   $answer = $this->llm->complete('mistral-large-latest', 'Hello!');
   

use Llm;

$answer = Llm::complete('qwen3-max', 'Hello!');
$result = Llm::chat('openai:gpt-5.2', 'Hi', ['tags' => ['web']]);
bash
php artisan vendor:publish --provider="LlmCostTracker\Laravel\LlmCostTrackerServiceProvider"
php artisan migrate