Skip to content

feat(langfuse): add Langfuse tracing module for ai::Client agent calls#42

Merged
iskakaushik merged 2 commits intomainfrom
feat/langfuse-tracing
May 1, 2026
Merged

feat(langfuse): add Langfuse tracing module for ai::Client agent calls#42
iskakaushik merged 2 commits intomainfrom
feat/langfuse-tracing

Conversation

@iskakaushik
Copy link
Copy Markdown
Collaborator

Summary

New optional `ai::langfuse` component that wraps `ai::Client::generate_text` calls and emits Langfuse traces for the LLM round-trip and each tool invocation.

Trace shape

  • 1 trace per logical operation (caller sets input / output / metadata / tags)
  • 1 `generation` observation (model, modelParameters, input messages, output text, aggregated usage, finish_reason / step count in metadata)
  • 1 `span` per tool call, parented to the generation, with tool args as input and result (or error) as output

Public API (`include/ai/langfuse.h`)
```cpp
ai::langfuse::Tracer tracer({.host = "https://us.cloud.langfuse.com\",
.public_key = ..., .secret_key = ...});
auto trace = tracer.start_trace("sql-generation");
trace->set_input(prompt);
auto result = ai::langfuse::generate_text(client, std::move(options), *trace);
trace->set_output(result.text);
trace->end(); // synchronous batched flush
```

The module hooks into the existing `on_tool_call_start` / `on_tool_call_finish` callbacks on `GenerateOptions` and chains any user-installed callbacks. `Trace::instrument` mints the parent generation observation; `Trace::finish_generation` (called by the convenience wrapper) finalizes it after `generate_text` returns. `Trace::end` POSTs the batch to `/api/public/ingestion` via the already-vendored httplib + Basic auth.

Layout

  • `include/ai/langfuse.h` – public surface (Config, Tracer, Trace, free `generate_text`)
  • `src/langfuse/tracer.cpp` – HTTP / batching / event-shape implementation
  • `examples/langfuse_tracing.cpp` – end-to-end demo using OpenAI with two tools across multiple steps
  • `.env.local.example` – credentials template (`.env.local` itself is gitignored)
  • `CMakeLists.txt` – new `ai-sdk-cpp-langfuse` target (alias `ai::langfuse`), `AI_SDK_HAS_LANGFUSE=1`, in `ai::sdk` umbrella and install/export set

Scope cuts (intentional, MVP)

  • Synchronous flush on `Trace::end()`; no background thread yet.
  • `on_step_finish` is not used to emit per-step generation events; intermediate step text/usage roll up into the single parent generation. Easy to add later.

Test plan

  • `make ai-sdk-cpp-langfuse` clean
  • `make langfuse_tracing` example links and runs
  • End-to-end: trace ingested at `us.cloud.langfuse.com` (HTTP 207 with all events accepted), retrieved via `GET /api/public/traces/`; structure verified — 1 generation + N tool spans with proper input/output and parent-child links
  • No effect on existing `ai-sdk-cpp-core` / `-openai` / `-anthropic` builds

Adds an opt-in ai::langfuse module that wraps generate_text calls and
emits one trace per logical operation with:
- one Generation observation (model, modelParameters, input messages,
  output text, aggregated usage, finish_reason / step count metadata),
- one Span per tool invocation, parented to the generation, with the
  tool's args and result (or error) recorded as input/output.

The module hooks into the existing on_tool_call_start /
on_tool_call_finish callbacks in GenerateOptions so user-installed
callbacks are preserved (chained). After generate_text returns, the
caller invokes Trace::end() to flush the accumulated batch to
POST /api/public/ingestion via the vendored httplib + Basic auth.

Layout
- include/ai/langfuse.h: public surface (Config, Tracer, Trace,
  generate_text helper).
- src/langfuse/tracer.cpp: HTTP / batching / event-shape implementation.
- examples/langfuse_tracing.cpp: end-to-end demo using OpenAI with
  lookup_user / get_weather tools across multiple steps.
- .env.local.example: credentials template (the populated .env.local
  is gitignored).
- CMakeLists.txt: new ai-sdk-cpp-langfuse target (alias ai::langfuse),
  AI_SDK_HAS_LANGFUSE=1, included in ai::sdk and the install/export
  set. Existing targets unchanged.

Verified end-to-end: example produced trace with the expected nested
generation+tool observations against us.cloud.langfuse.com.
@iskakaushik iskakaushik merged commit cb86685 into main May 1, 2026
3 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant