What Gets Tracked
LarAgent’s usage tracking system captures:- Prompt tokens - Tokens used in the input/request
- Completion tokens - Tokens used in the output/response
- Total tokens - Combined prompt + completion tokens
- Timestamps - When each response was generated
- Model and provider information - Which model and provider generated the response
- Agent and user context - Which agent and user triggered the response
Configuration
Usage tracking can be configured at multiple levels, with the following priority order:Priority: Agent property > Provider config > Global config
Global Configuration
Enable usage tracking for all agents inconfig/laragent.php:
config/laragent.php
Provider Configuration
Configure usage tracking for specific providers:config/laragent.php
Agent Configuration
Set usage tracking directly in your agent class using properties:app/AiAgents/MyAgent.php
Available Storage Aliases
Available Storage Aliases
Method Override
For complete control, override methods in your agent class:app/AiAgents/CustomTrackingAgent.php
Runtime Configuration
Enable or disable tracking dynamically at runtime:Database Setup
To persist usage data in a database while usingdatabase driver, publish and run the migration:
1
Publish the migration
2
Run the migration
This creates the
laragent_usage table with columns for all tracked metrics.Working with Usage Data
The methods below allow you to access and analyze usage data tracked by LarAgent for any driver, but if you use theEloquentUsageDriver (or "database" alias), you also have direct access to the underlying database model for advanced queries.
Accessing Usage Storage
Getting Usage Records
Available Filter Options
Available Filter Options
Aggregating Usage Statistics
Grouping Usage Data
Getting Usage Identities
Clearing Usage Data
Usage Data Structure
Each usage record contains:Direct Eloquent Model Usage
When using theEloquentUsageDriver (database storage), you have direct access to the LaragentUsage Eloquent model for complex queries and reporting.
Database Schema
Query Scopes
The model includes convenient query scopes for filtering:Aggregation Methods
Advanced Queries
Leverage Laravel’s query builder for complex analytics:- Daily Usage
- Top Users
- Cost Estimation
Real-World Example: Cost Monitoring Dashboard
Here’s a complete example of building a cost monitoring dashboard with usage tracking.1
Enable Global Tracking
Configure tracking in your
config/laragent.php:config/laragent.php
2
Create Analytics Service
app/Services/UsageAnalyticsService.php
3
Create Dashboard Controller
app/Http/Controllers/Admin/UsageController.php
4
Add Usage Alert Job
app/Jobs/CheckUsageThresholds.php
5
Schedule the Alert
app/Console/Kernel.php
Your cost monitoring dashboard is now ready to track usage and alert on thresholds.

