Skip to content

PHP

The PHP SDK ctxflow/otel reads the same CTXFLOW_* variables as the Python SDK, puts the conversation and the user on every span, and exports the spans over OTLP/HTTP, batched, when the process ends (one request is one process). PHP 8.2 or later, Composer, ext-mbstring, Guzzle 7.15.2+ or 8.0.1+ (Composer installs it; the LLPhant instrumentation needs Guzzle 7).

Install it from Packagist (it installs ctxflow/semconv, the conventions, too), and set the exporter:

Terminal window
composer require ctxflow/otel:0.2.2
export OTEL_SERVICE_NAME=my-agent
export OTEL_EXPORTER_OTLP_ENDPOINT=https://app.ctxflow.sh # or your collector, http://<collector-host>:4318
export OTEL_EXPORTER_OTLP_HEADERS="Authorization=Bearer%20<your key>" # not needed with a collector

Test the key with the curl check of Verify your setup.

No code. Set OTEL_PHP_AUTOLOAD_ENABLED=true in the environment of the PHP process (with php-fpm: env[...] in the pool, or clear_env = no).

One line. Save as agent.php:

<?php
require __DIR__ . '/vendor/autoload.php';
use Ctxflow\Otel\Ctxflow;
use OpenTelemetry\API\Globals;
use function Ctxflow\Otel\conversation;
Ctxflow::init();
$tracer = Globals::tracerProvider()->getTracer('my-agent');
conversation(function () use ($tracer): void {
$span = $tracer->spanBuilder('chat fake-model')->startSpan();
$span->setAttribute('gen_ai.operation.name', 'chat');
$span->setAttribute('gen_ai.prompt', 'Hi, who are you?');
$span->end();
}, 'conv-1', userId: 'u1', turn: 0);
// no shutdown call: the spans are flushed when the script ends

conversation($block, $conversationId, $userId, $sessionId, $turn) follows the rules of the Python helper: non-empty, trimmed values of at most 256 characters, turn from 0 up. A bad value throws InvalidArgumentException. Invalid CTXFLOW_* values are reported when the SDK starts.

In a long-running worker (Horizon, Octane) spans are not flushed per request: they leave with the batch timing or when the worker exits.

Wrap the calls of one agent turn in agentTurn($block, $agent) inside conversation(): it makes an invoke_agent <agent> span, and ctxflow makes the turn from it. Without it, each LLM call is its own turn, with no agent. Neuron AI makes its own agent span, so it does not need agentTurn().

use function Ctxflow\Otel\agentTurn;
use function Ctxflow\Otel\conversation;
conversation(fn() => agentTurn(function (): void {
// your LLM calls
}, 'support-bot'), 'conv-1', userId: 'u1', turn: 0);

For openai-php/client 0.19.2 or later (composer require openai-php/client), build the client through ctxflow. Every chat completion and Responses API call then gets a model span with model, tokens, messages and errors:

use Ctxflow\Otel\Instrumentation\OpenAI\OpenAiInstrumentation;
$client = OpenAiInstrumentation::factory()->withApiKey(getenv('OPENAI_API_KEY'))->make();

factory() returns the library’s own factory, so you can chain withBaseUri() and the rest. For a factory you build yourself, use OpenAiInstrumentation::instrument($factory). Streamed calls (createStreamed()) work as before and are not traced.

For Laravel Prism (prism-php/prism 0.103.x):

Terminal window
composer require ctxflow/otel-prism:0.2.2

Call Ctxflow::init() once (for example in AppServiceProvider::boot()). Laravel registers the rest. Every Prism::text() and Prism::structured() call, with any Prism provider, gets one model span per LLM call. Streams, embeddings, images and audio are not traced. Prism::fake() makes no spans.

For LLPhant (theodo-group/llphant 1.10.11 or later) with its OpenAI chat, build the chat from a ctxflow config and wrap its tools. Each request and each tool call gets a span:

use Ctxflow\Otel\Instrumentation\LLPhant\LlphantInstrumentation;
use LLPhant\Chat\OpenAIChat;
$chat = new OpenAIChat(LlphantInstrumentation::openAiConfig(model: 'gpt-4o-mini')); // key and URL from OPENAI_API_KEY, OPENAI_BASE_URL
$chat->addTool(LlphantInstrumentation::tool($functionInfo));

Only the OpenAI chat is covered.

For Neuron AI 4.x (composer require neuron-core/neuron-ai), call NeuronInstrumentation::instrument() once on each agent. Every run then gets an agent span, a model span per model call and a tool span per tool call:

use Ctxflow\Otel\Instrumentation\Neuron\NeuronInstrumentation;
use NeuronAI\Chat\Messages\UserMessage;
$agent = NeuronInstrumentation::instrument(SupportAgent::make()->setThreadId($threadId), 'support-bot');
conversation(fn() => $agent->chat(new UserMessage($question)), 'conv-1', userId: 'u1', turn: 0);

These helpers send the text of messages and tool calls as the app wrote it. If a library hook cannot be set (an unsupported version), one warning goes to the OpenTelemetry log and the app runs without spans.

Variables: Configuration. Retrieval spans: Retrieval and source identity.