Browse Source

feat(agent): integrate Langfuse observability

Add OpenTelemetry-based Langfuse tracing to the AI agent. Every
generateText call now emits spans with tool calls, token usage, and
metadata (userId, conversationId) to Langfuse for monitoring.
pull/6459/head
jpwilson 1 month ago
parent
commit
23338c3955
  1. 10
      apps/api/src/app/agent/agent.service.ts
  2. 16
      apps/api/src/main.ts

10
apps/api/src/app/agent/agent.service.ts

@ -544,7 +544,15 @@ If you detect any inconsistencies in the data, flag them clearly to the user.`,
} }
}) })
}, },
maxSteps: 5 maxSteps: 5,
experimental_telemetry: {
isEnabled: true,
functionId: 'ghostfolio-agent',
metadata: {
userId,
conversationId: convId
}
}
}); });
// --- Verification Layer --- // --- Verification Layer ---

16
apps/api/src/main.ts

@ -20,6 +20,22 @@ import helmet from 'helmet';
import { AppModule } from './app/app.module'; import { AppModule } from './app/app.module';
import { environment } from './environments/environment'; import { environment } from './environments/environment';
// Initialize Langfuse OpenTelemetry tracing (if configured)
if (process.env.LANGFUSE_SECRET_KEY) {
try {
const { NodeSDK } = require('@opentelemetry/sdk-node');
const { LangfuseSpanProcessor } = require('@langfuse/otel');
const sdk = new NodeSDK({
spanProcessors: [new LangfuseSpanProcessor()]
});
sdk.start();
console.log('Langfuse tracing initialized');
} catch (error) {
console.warn('Langfuse tracing not available:', error.message);
}
}
async function bootstrap() { async function bootstrap() {
const configApp = await NestFactory.create(AppModule); const configApp = await NestFactory.create(AppModule);
const configService = configApp.get<ConfigService>(ConfigService); const configService = configApp.get<ConfigService>(ConfigService);

Loading…
Cancel
Save