Skip to content

feat: conectar Langfuse al get_llm() central via LangChain CallbackHandler - #2

Merged
Jbelieve merged 3 commits into
believe/self-hostfrom
feat/langfuse-callback-handler
Jul 30, 2026
Merged

Jbelieve merged 3 commits into
believe/self-hostfrom
feat/langfuse-callback-handler

Conversation

@Jbelieve

Copy link
Copy Markdown
Owner

Resumen

  • Believe está instrumentando Langfuse (self-hosted, langfuse.believe-global.com) en todo el stack que consume LLM.
  • omi-believe ya centraliza todos los clientes LangChain en get_llm() (backend/utils/llm/clients.py), con un usage_tracker.py propio + LangSmith parcial — pero 0% iba a Langfuse.
  • Este PR suma el CallbackHandler oficial de Langfuse (langfuse.langchain.CallbackHandler, paquete langfuse 4.14.1) a la misma lista de callbacks que ya se pasa a cada cliente LangChain (OpenAI, Gemini, OpenRouter, BYOK, llm_mini), sin duplicar el mecanismo de tracking existente.

Cambios

  • backend/requirements.txt: agrega langfuse==4.14.1.
  • backend/utils/llm/clients.py:
    • _init_langfuse_callback(): crea el CallbackHandler de Langfuse solo si LANGFUSE_HOST, LANGFUSE_PUBLIC_KEY y LANGFUSE_SECRET_KEY están seteados en el entorno. Si falta alguno, o si la inicialización falla por cualquier motivo, devuelve None sin romper el arranque (solo loguea).
    • _llm_callbacks(): helper que devuelve [_usage_callback] + el callback de Langfuse cuando está disponible — reemplaza los 5 lugares que hardcodeaban [_usage_callback] directamente (_create_byok_client, _get_or_create_openai_llm, _get_or_create_openrouter_llm, _get_or_create_gemini_llm, llm_mini).

No-op garantizado

Si las 3 env vars de Langfuse no están seteadas (dev local, tests, CI), el comportamiento es idéntico al actual: solo _usage_callback.

Test plan

  • python3 -m py_compile backend/utils/llm/clients.py — compila sin errores.
  • Verificado que sin LANGFUSE_HOST/LANGFUSE_PUBLIC_KEY/LANGFUSE_SECRET_KEY en el entorno, _init_langfuse_callback() devuelve None (no-op).
  • Deploy a Contabo con las 3 env vars de Langfuse seteadas (ya en Infisical) y confirmar traces en langfuse.believe-global.com.

🤖 Generated with Claude Code

Jbelieve and others added 3 commits July 9, 2026 12:20
…a semántica

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…ciones

Los resúmenes que fluyen al Company Brain de Believe (summary=overview,
decision_summary=title en believe_brain.py) deben salir SIEMPRE en español,
sin importar el idioma detectado de la conversación ni la preferencia del
usuario. Algunos salían en inglés (ej. "Radio Segment Promotes Public Notices").

Se fuerza response_language='Spanish' en get_transcript_structure y
get_reprocess_transcript_structure (conversation_processing.py) y en
get_message_structure (external_integrations.py). summarize_experience_text
no tenía instrucción de idioma; se le agrega respuesta en español.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…angChain

Believe está instrumentando Langfuse (langfuse.believe-global.com) en todo
el stack que consume LLM. omi-believe ya centraliza todos los clientes
LangChain en get_llm()/clients.py con un usage_tracker propio, así que el
menor esfuerzo es sumar el CallbackHandler oficial de Langfuse a la misma
lista de callbacks que ya se pasa a cada cliente (OpenAI, Gemini,
OpenRouter, BYOK, llm_mini) en vez de instrumentar cada call site.

No-op si LANGFUSE_HOST/LANGFUSE_PUBLIC_KEY/LANGFUSE_SECRET_KEY no están en
el entorno, y cualquier fallo al inicializar el handler se loguea pero no
rompe el arranque.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@Jbelieve
Jbelieve marked this pull request as ready for review July 30, 2026 02:33
@Jbelieve
Jbelieve merged commit 38c76fa into believe/self-host Jul 30, 2026
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