agentsclimarketplace

Langfuse laravel ai

Skill douinc/agent-skills/skills/langfuse-laravel-ai

Use when adding Langfuse tracing/observability to a Laravel app that uses the laravel/ai SDK (Laravel\Ai\ namespace, Agent classes, ->prompt()/->stream(), tools, conversations). Activate when the user wants to trace LLM/agent calls, token usage, or tool invocations to Langfuse in a Laravel project, pairs Langfuse with laravel/ai, or asks why traces aren't showing up. Laravel/PHP-specific: laravel/ai has no OpenTelemetry support and there is no first-party PHP Langfuse SDK, so the integration path is non-obvious.From its SKILL.md

Install
npx -y skills add douinc/agent-skills --skill langfuse-laravel-ai

Assembled from the repository path, not quoted from the project. Check it against their README if it does not work.

4 things to look at

  • reads credentialsReads from 2 credential sources: `LANGFUSE_PUBLIC_KEY` and 1 more.
  • 1 stars1 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.
  • runs commandsInstructs the agent to run 5 commands, including `composer require axyr/laravel-langfuse` and 4 more.
  • fetches URLsInstructs the agent to fetch 1 URL, including $LANGFUSE_BASE_URL/api/public/traces?limit=1.

SKILL.md

6.7 KB, ~2.1k tokens by cl100k_base, as published. Nobody here has run it

Langfuse × laravel/ai

laravel/ai SDK(Laravel\Ai\)를 쓰는 Laravel 앱의 LLM/에이전트 호출을 Langfuse로 트레이싱한다.

핵심 전제 (먼저 알아야 할 것)

  • laravel/ai는 OpenTelemetry를 지원하지 않고, 공식 PHP Langfuse SDK도 없다. 직접 OTLP/인제스션 클라이언트를 짜기 전에 axyr/laravel-langfuse를 먼저 채택한다 — laravel/ai를 zero-code 자동계측하고 Langfuse 인제스션 API로 큐 배칭 전송한다.
  • 패키지 비교(채택 전 근거):
패키지laravel/ai 자동계측전송주의
axyr/laravel-langfuseOLangfuse 네이티브 인제스션require가 illuminate만 → laravel/ai 버전 비강제(충돌 없음)
tracefast/laravel-ai-observabilityOOTLP 경유laravel/ai ^0.7강제 → 낮은 버전과 충돌 위험
dropsolid/ · dij-digital/ · janzaba/X (수동 trace API만)Langfuse결국 이벤트 후킹은 직접 해야 함

설치 & 설정

  1. composer require axyr/laravel-langfuse
    • post-install 훅이 package.json을 무관하게 건드리면 git checkout package.json으로 원복.
  2. php artisan vendor:publish --tag=langfuse-config
  3. .env (env 키가 표준이라 기존 LANGFUSE_* 값을 그대로 재사용):
    LANGFUSE_PUBLIC_KEY=pk-lf-...
    LANGFUSE_SECRET_KEY=sk-lf-...
    LANGFUSE_BASE_URL=https://cloud.langfuse.com   # self-hosted면 그 URL
    LANGFUSE_LARAVEL_AI_ENABLED=true               # ← 자동계측 ON (필수)
    LANGFUSE_QUEUE=default                          # 큐 worker로 비동기 전송
    
  4. 등록 확인: php artisan config:clear && php artisan event:list | grep LaravelAiSubscriber → laravel/ai 6개 이벤트에 붙어야 정상.

자동계측이 잡는 것

Axyr\Langfuse\LaravelAi\LaravelAiSubscriber가 후킹한다:

laravel/ai 이벤트Langfuse 관측치
PromptingAgent / StreamingAgenttrace 생성 + 시작 시각
AgentPrompted / AgentStreamedgeneration (model, input, output, token usage)
InvokingTool / ToolInvokedtool span (arguments, result)

baseline(model·토큰·input/output·observation 타입·span 계층)은 코드 변경 없이 채워진다.

핵심 패턴 — trace 컨텍스트 보강

자동계측만으로는 session_id·user_id·도메인 메타데이터가 없다. subscriber의 resolveTrace()이미 설정된 currentTrace()가 있으면 그걸 재사용한다. 따라서 에이전트 호출 직전에 trace를 미리 열어두면 generation·tool span이 그 아래로 nest된다.

use Axyr\Langfuse\Dto\TraceBody;
use Axyr\Langfuse\LangfuseFacade as Langfuse;

// 에이전트를 prompt()/stream() 하기 전에:
Langfuse::setCurrentTrace(Langfuse::trace(new TraceBody(
    name: 'questionnaire-builder',
    userId: (string) $user->id,
    sessionId: $conversationId,           // 멀티턴 대화 그룹핑
    metadata: ['questionnaire_id' => $q->id, 'team_id' => $team->id],
    tags: ['questionnaire-builder'],
)));

$response = (new MyAgent(...))
    ->continue($conversationId, as: $user)
    ->stream($prompt);

같은 요청 내 LLM 호출이 여러 개여도 한 trace로 묶인다. 큐 잡 등 비-HTTP 컨텍스트에서도 동일하게 호출 전에 setCurrentTrace를 호출하면 된다.

순서가 중요하다. setCurrentTrace에이전트 호출보다 먼저 해야 한다. 빠뜨리면 resolveTrace()laravel-ai-<AgentClass> 이름의 기본 trace를 자동 생성하는데, 여기엔 session_id·user_id·도메인 메타데이터가 없다(=보강 실패). 요청당 한 번, 여러 에이전트를 쓰면 첫 호출 전에 한 번 열어두면 그 아래로 전부 nest된다.

테스트 (Pest)

$langfuse = Langfuse::fake();                     // RecordingEventBatcher
MyAgent::fake(['응답'])->preventStrayPrompts();    // 실제 provider 호출 차단

// ... 엔드포인트/액션 실행 ...

$langfuse->assertTraceCreated('questionnaire-builder');
$body = collect($langfuse->events())
    ->first(fn ($e) => $e->type->value === 'trace-create')
    ->body->toArray();
expect($body['sessionId'])->toBe($conversationId)
    ->and($body['userId'])->toBe((string) $user->id);

중요: laravel/ai의 FakeTextGateway는 이벤트를 dispatch하지 않는다 → 테스트에서 자동계측 generation/tool span은 검증 불가. 검증 가능한 것은 setCurrentTrace로 직접 연 trace다(LangfuseTrace 생성자가 즉시 enqueue하므로 assertTraceCreated가 통과). 자동계측 동작 자체는 패키지 책임이므로 우리 보강 코드만 테스트한다.

함정 (자주 막히는 지점)

  • 실제 trace가 대시보드에 뜨려면 실제 provider 호출 + 큐 worker가 떠 있어야 한다(LANGFUSE_QUEUE 사용 시 queue:work/queue:listen). 로컬은 composer run dev가 worker를 포함하는 경우가 많다. trace가 안 보이면 먼저 worker부터 확인.
  • LANGFUSE_QUEUE를 비워두면(null) 동기 전송 — 큐 worker 없이도 가게 되지만 요청 종료 시 HTTP 호출이 붙어 약간 지연된다. worker 인프라가 없는 환경이면 이 모드가 더 안전하다(설정한 큐를 worker가 처리하지 않으면 trace가 영영 전송되지 않으니 주의).
  • 인증/연결 핑(읽기 전용):
    AUTH=$(printf '%s:%s' "$LANGFUSE_PUBLIC_KEY" "$LANGFUSE_SECRET_KEY" | base64)
    curl -s -o /dev/null -w '%{http_code}\n' -H "Authorization: Basic $AUTH" \
      "$LANGFUSE_BASE_URL/api/public/traces?limit=1"   # 200 이면 OK
    
  • usage 매핑은 prompt/completion 토큰만 — cache/reasoning 토큰은 누락되어 비용이 약간 부정확할 수 있다.
  • LANGFUSE_ENABLED=false면 no-op(NullTrace)로 graceful 비활성 — 코드 변경 없이 끌 수 있다(로컬/CI).
  • PII가 프롬프트·메타데이터에 들어가는 도메인이면 trace input/metadata에서 마스킹·제외한다.
  • Langfuse v3(self-hosted)는 비동기 인제스션이라 trace가 몇 초 지연될 수 있다.

What ships with it

Read from the repository

Just SKILL.md. No reference files, no scripts.

Gives 0 of the 12 instructions most context ai engineering skills give in ~2.1k tokens

Counted across 1,328 of the 2,349 authors here whose files we hold, read 2026-09-06

  • Dispatch a fresh subagent for each taskin 76 of 1328, across 59 files
  • Perform spec compliance review before code quality reviewin 44 of 1328, across 34 files
  • Dispatch a final code reviewer after all tasksin 38 of 1328, across 26 files
  • Answer subagent questions before allowing implementationin 36 of 1328, across 26 files
  • Use the least powerful model capable of the taskin 33 of 1328, across 26 files
  • Create a TodoWrite list for all tasksin 32 of 1328, across 22 files
  • Perform a task review after each implementationin 31 of 1328, across 24 files
  • Extract all tasks and context from the planin 29 of 1328, across 20 files
  • Provide full task text to subagentsin 28 of 1328, across 20 files
  • Use git worktrees for isolated workspacesin 25 of 1328, across 20 files
  • Specify the model explicitly when dispatching a subagentin 23 of 1328, across 18 files
  • Execute all tasks from the plan without stoppingin 21 of 1328, across 16 files

Said here and by no other author read

  • Install axyr/laravel-langfuse for zero-code instrumentation
  • Set LANGFUSE_LARAVEL_AI_ENABLED to true in environment
  • Run artisan vendor:publish for langfuse configuration
  • Verify event registration using artisan event:list
  • Set current trace before calling agents
  • Use setCurrentTrace to nest generation and tool spans

Grouped from the skills themselves: near-identical wordings counted once, and counted by distinct author, so one author publishing three of these counts once. Length counted with cl100k_base; the agent that loads this file may tokenize it differently.

Keep looking

Skills are one crate of 325,949. Ordering is by how many stacks a row turns up in, so the top of any crate is what has actually been picked rather than what has the most stars.