uodev / langfuse-logger
A lightweight PHP logger for Langfuse to track LLM API calls (OpenAI, Claude, Gemini, Grok) with token usage analytics
Installs: 0
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/uodev/langfuse-logger
Requires
- php: >=7.4
- ext-curl: *
- ext-json: *
Requires (Dev)
- phpunit/phpunit: ^9.6
This package is auto-updated.
Last update: 2026-02-05 07:49:22 UTC
README
A lightweight PHP library to log your LLM API calls to Langfuse for observability and analytics.
Supported Providers
| Provider | Class | Models |
|---|---|---|
| OpenAI | OpenAIProvider |
GPT-4, GPT-4o, o1, o3-mini, etc. |
| Anthropic | AnthropicProvider |
Claude 3.5 Sonnet, Claude 3 Opus, etc. |
GoogleProvider |
Gemini 1.5 Pro, Gemini 2.0, etc. | |
| xAI | XAIProvider |
Grok-2, Grok-2 mini, etc. |
Installation
composer require uodev/langfuse-logger
Quick Start
<?php declare(strict_types=1); use Langfuse\LangfuseLogger; use Langfuse\Providers\OpenAIProvider; require 'vendor/autoload.php'; // Initialize the logger $logger = new LangfuseLogger( 'https://cloud.langfuse.com', // Base URL 'pk-lf-...', // Public Key 'sk-lf-...' // Secret Key ); // Optional: Set user and session $logger->setUserId('user-123') ->setSessionId('session-abc'); // Log your API response $logger->log( new OpenAIProvider(), // Provider instance 'gpt-4o', // Model name $messages, // Messages array sent to API $response // Response array from API );
Provider Examples
OpenAI
use Langfuse\Providers\OpenAIProvider; $logger->log( new OpenAIProvider(), 'gpt-4o', $messages, $response );
Anthropic (Claude)
use Langfuse\Providers\AnthropicProvider; $logger->log( new AnthropicProvider(), 'claude-3-5-sonnet-20241022', $messages, $response );
Google (Gemini)
use Langfuse\Providers\GoogleProvider; $logger->log( new GoogleProvider(), 'gemini-1.5-pro', $messages, $response );
xAI (Grok)
use Langfuse\Providers\XAIProvider; $logger->log( new XAIProvider(), 'grok-2', $messages, $response );
Requirements
- PHP >= 7.4
- ext-curl
- ext-json
Features
- Multi-Provider Support: OpenAI, Anthropic, Google, xAI out of the box
- Token Parsing: Automatically parses cached input tokens and reasoning tokens
- Cost Tracking: Reports real input (non-cached) and cached input separately
- Extensible: Implement
ProviderInterfaceto add custom providers - Type Safe: Strict types and interfaces throughout
Configuration
Get your Langfuse credentials from Langfuse Cloud or your self-hosted instance.
| Parameter | Description |
|---|---|
baseUrl |
Langfuse API URL (e.g., https://cloud.langfuse.com) |
publicKey |
Your Langfuse public key (pk-lf-...) |
secretKey |
Your Langfuse secret key (sk-lf-...) |
Custom Provider
Implement the ProviderInterface to add support for other LLM providers:
<?php declare(strict_types=1); use Langfuse\Contracts\ProviderInterface; use Langfuse\DTO\Usage; class MyCustomProvider implements ProviderInterface { public function parseResponse(array $response): Usage { // Extract tokens from your provider's response format return new Usage( $inputTokens, $outputTokens, $totalTokens, $cachedTokens, $reasoningTokens ); } public function formatMessages(array $messages): string { // Format messages to string } public function extractOutput(array $response): string { // Extract output content from response } public function getName(): string { return 'my-custom-provider'; } }
Full Example
See the example.php file for complete working examples.
# Clone the repo git clone https://github.com/uodev/langfuse-logger-php.git cd langfuse-logger-php # Install dependencies composer install # Configure your API keys cp .env.example .env # Edit .env with your keys # Run examples php example.php openai # OpenAI php example.php anthropic # Claude php example.php google # Gemini php example.php xai # Grok
API Reference
LangfuseLogger
setUserId($userId): self
Set the user ID for tracking. Returns self for method chaining.
setSessionId($sessionId): self
Set the session ID for grouping related traces. Returns self for method chaining.
log(ProviderInterface $provider, string $model, array $messages, array $response, array $metadata = []): bool
Log an LLM API call to Langfuse.
| Parameter | Type | Description |
|---|---|---|
$provider |
ProviderInterface | Provider instance (e.g., new OpenAIProvider()) |
$model |
string | Model name (e.g., gpt-4o, claude-3-5-sonnet) |
$messages |
array | The messages array sent to the API |
$response |
array | The decoded response from the API |
$metadata |
array | Optional additional metadata |
Returns true on success, false on failure.
License
MIT License - see the LICENSE file for details.
Development
# Install dependencies composer install # Run tests composer test # Run specific test composer test:filter UsageTest # Run tests with coverage report composer test:coverage
Contributing
Contributions are welcome! Please make sure tests pass before submitting a Pull Request.