1. Go to this page and download the library: Download mtyrtov/langfuse-php 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/ */
mtyrtov / langfuse-php example snippets
use Langfuse\LangfuseClient;
use Langfuse\LangfuseProfiler;
your-public-key',
'your-secret-key',
'https://your-langfuse-host.com' # Optional, defaults to http://127.0.0.1:3000
);
$profiler = new LangfuseProfiler($client);
# Create a trace
$trace = $profiler->trace('user-query')
->setSessionId() // Automatically generates a session ID
->setUserId('user-123');
# Set input and output
$trace->setInput('Hello, how are you?');
$trace->setOutput("I'm fine, thank you!");
# Create a span within a trace
$classificationSpan = $trace->span('classification');
# End the span when the operation is complete
$classificationSpan->end();
# Create a generation within a span
$generation = $classificationSpan->generation('model-call')
->withModel('gpt-4.1-mini')
->withModelParameters(['temperature' => 0])
->setInput(['role' => 'user', 'content' => 'Hello there'])
->setOutput('AI response here');
$result = $profiler->flush();
class LangfuseProvider extends ServiceProvider
{
public function register(): void
{
$this->app->singleton(LangfuseClient::class, function ($app) {
return new LangfuseClient(
# Your variables from .env
config('services.langfuse.public_key'),
config('services.langfuse.secret_key'),
config('services.langfuse.base_uri')
);
});
$this->app->singleton(LangfuseProfiler::class, function ($app) {
return new LangfuseProfiler($app->make(LangfuseClient::class));
});
}
public function boot(): void
{
//
}
}