vizra-ai/ai-tokens
Composer 安装命令:
composer require vizra-ai/ai-tokens
包简介
Estimate AI API costs before making expensive calls
关键字:
README 文档
README
A lightweight PHP package for AI token cost management. Estimate costs before API calls, calculate actual costs from token usage, and track expenses across OpenAI, Claude, Gemini, and other popular AI models.
Installation
composer require vizra-ai/ai-tokens
Usage
1. Count Tokens & Estimate Cost from Text
Estimate tokens and cost for a text message:
use VizraAi\AiTokens\TokenCounter; $result = TokenCounter::count('Hello, how are you?', 'gpt-4o'); // Returns: ['input_tokens' => 5, 'estimated_cost' => 0.0000125]
2. Calculate Actual Cost from Token Usage
Calculate costs when you know the exact token counts (e.g., after an API call):
use VizraAi\AiTokens\TokenCounter; $result = TokenCounter::calculateActualCost( model: 'gpt-4o', inputTokens: 500, outputTokens: 600 ); // Returns: ['total_cost' => 0.00725, 'input_cost' => 0.00125, 'output_cost' => 0.006]
3. Estimate Cost Before API Calls
Plan costs before making API calls:
use VizraAi\AiTokens\TokenCounter; $result = TokenCounter::estimateCost([ 'model' => 'claude-3.5-sonnet', 'input_tokens' => 2000, 'expected_output_tokens' => 1500 // optional ]); // Returns: ['total_cost' => 0.0285, 'input_cost' => 0.006, 'output_cost' => 0.0225]
Supported Models
The package supports 100+ models across major AI providers:
OpenAI
- GPT-4o series (
gpt-4o,gpt-4o-mini,gpt-4o-realtime) - GPT-4 series (
gpt-4,gpt-4-turbo) - GPT-3.5 (
gpt-3.5-turbo) - ChatGPT models (
chatgpt-4o-latest,o1,o3series)
Claude (Anthropic)
- Claude 3.5 Sonnet (
claude-3-5-sonnet-20241022) - Claude 3 series (Opus, Sonnet, Haiku)
- Gemini 2.0 Flash (
gemini-2.0-flash-exp,gemini-2.0-flash-thinking-exp) - Gemini 1.5 Pro & Flash series
Others
- DeepSeek (
deepseek-v3,deepseek-r1,deepseek-r1-lite) - Mistral models
- Llama 3.1 & 3.2 series
- Grok models
- ...and many more
Utility Methods
// Get all supported models $models = TokenCounter::getSupportedModels(); // Get model pricing information $info = TokenCounter::getModelInfo('gpt-4o'); // Returns: ['input_price_per_million' => 2.5, 'output_price_per_million' => 10.0, 'chars_per_token' => 4.0, 'max_tokens' => 128000] // Get last pricing update date $date = TokenCounter::getLastUpdated(); // Returns: '2025-01-04'
Exception Handling
use VizraAi\AiTokens\TokenCounter; use VizraAi\AiTokens\Exceptions\TooManyTokensException; use InvalidArgumentException; try { $result = TokenCounter::count($longText, 'gpt-3.5-turbo'); } catch (TooManyTokensException $e) { // Text exceeds model's token limit echo $e->getMessage(); } catch (InvalidArgumentException $e) { // Unsupported model name echo $e->getMessage(); }
Configuration
The package automatically fetches the latest pricing data from the Vizra AI API. You can configure this behavior:
use VizraAi\AiTokens\Config\Pricing; // Disable remote pricing (use local data only) Pricing::configure([ 'use_remote_pricing' => false ]); // Custom API endpoint Pricing::configure([ 'api_endpoint' => 'https://your-api.com/pricing', 'cache_duration' => 7200, // 2 hours ]); // Clear pricing cache Pricing::clearCache();
Environment Variables
You can also configure via environment variables:
# Disable remote pricing fetching AI_TOKENS_USE_REMOTE=false # Custom API endpoint AI_TOKENS_API_ENDPOINT=https://your-api.com/pricing # Cache duration in seconds (default: 3600) AI_TOKENS_CACHE_DURATION=7200 # API timeout in seconds (default: 5) AI_TOKENS_API_TIMEOUT=10
Pricing Updates
Pricing data is automatically fetched from the Vizra AI Pricing API which is updated daily. The package includes local fallback data as a backup.
- Remote Updates: Fetched automatically (cached for 1 hour)
- Local Fallback: Used if API is unavailable
- Zero Maintenance: No need to update the package for pricing changes
Last local pricing update: 2025-01-04
License
MIT
vizra-ai/ai-tokens 适用场景与选型建议
vizra-ai/ai-tokens 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.69k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2025 年 09 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「api」 「pricing」 「laravel」 「cost」 「ai」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 vizra-ai/ai-tokens 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 vizra-ai/ai-tokens 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 vizra-ai/ai-tokens 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A PSR-7 compatible library for making CRUD API endpoints
Manage acquisition, selling & renting prices for products and services in your Laravel Application
A package provide pricing plans for Laravel.
This extension allows to set if a product is minimalPrice and can show its price by scope and customer group.
A package provide pricing plans for Laravel.
Alfabank REST API integration
统计信息
- 总下载量: 1.69k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-09-11