feat(langfuse): add Langfuse tracing module for ai::Client agent calls#42
Merged
iskakaushik merged 2 commits intomainfrom May 1, 2026
Merged
feat(langfuse): add Langfuse tracing module for ai::Client agent calls#42iskakaushik merged 2 commits intomainfrom
iskakaushik merged 2 commits intomainfrom
Conversation
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.
4 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
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
Scope cuts (intentional, MVP)
Test plan