PHP code example of tokentifyai / usagemeter

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

    

tokentifyai / usagemeter example snippets




use UsageMeter\Tokentify;

 USAGEMETER_BUCKET from argument
Tokentify::init('your_bucket_name', ['account_id', 'user_id']);

// USAGEMETER_API_KEY and USAGEMETER_BUCKET both from .env
Tokentify::init(getenv('USAGEMETER_BUCKET') ?: 'your_bucket_name', ['account_id', 'user_id']);

// Production (skip health checks after first successful setup)
Tokentify::init(getenv('USAGEMETER_BUCKET') ?: 'your_bucket_name', ['account_id', 'user_id'], [
    'verify_connection' => false,
    'verify_api_key' => false,
]);

// Not supported — keep the API key in .env only:
// Tokentify::init(['api_key' => 'secret', 'bucket' => 'x', 'tracking_fields' => ['account_id', 'user_id']]);

Meter::trackTool([
    'trace_id' => 'tr_abc',
    'span_id' => 'sp_1',
    'tool_name' => 'get_weather',
    'tool_input' => ['city' => 'Paris'],
    'tool_output' => ['temp_c' => 22],
    'status' => 'success',
    'duration_ms' => 42,
]);

$response = curl_exec($ch);
$status = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);

$usage = Meter::trackFromResponse(
    [
        'provider' => 'openai',
        'model' => 'gpt-4o',
        'http_status' => $status,
        'status' => $status >= 200 && $status < 300 ? 'success' : 'error',
    ],
    is_string($response) ? $response : '',
);

Meter::track([
    'provider' => 'openai',
    'model' => 'gpt-4o',
    'response_body' => $response,
]);

// ❌ drops cache_read_tokens
Meter::track([
    'provider' => 'openai',
    'model' => 'gpt-4o',
    'input_tokens' => $usage['prompt_tokens'],
    'output_tokens' => $usage['completion_tokens'],
]);



use UsageMeter\Tokentify;
use UsageMeter\Meter;

ucket_name', ['account_id', 'user_id']);
// loads .env when phpdotenv is installed; reads USAGEMETER_API_KEY and USAGEMETER_BUCKET

Meter::tag([
    'account_id' => '00000000-0000-4000-8000-000000000001',
    'user_id' => '00000000-0000-4000-8000-000000000002',
]);

$ch = curl_init('https://api.openai.com/v1/chat/completions');
curl_setopt_array($ch, [
    CURLOPT_POST => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => [
        'Authorization: Bearer YOUR_OPENAI_KEY',
        'Content-Type: application/json',
    ],
    CURLOPT_POSTFIELDS => json_encode([
        'model' => 'gpt-4o',
        'messages' => [['role' => 'user', 'content' => 'hi']],
    ]),
]);
$response = curl_exec($ch);
$status = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

Meter::trackFromResponse(
    [
        'provider' => 'openai',
        'model' => 'gpt-4o',
        'http_status' => $status,
        'status' => $status >= 200 && $status < 300 ? 'success' : 'error',
    ],
    is_string($response) ? $response : '',
);

Meter::flush();
bash
php app.php
bash
php -r "
er\Tokentify;
Tokentify::init(getenv('USAGEMETER_BUCKET') ?: 'your_bucket_name', ['account_id', 'user_id'], [
    'verify_api_key' => false,
    'load_env_file' => false,
]);
echo \"ok\n\";
"