# Langfuse Python SDK — Trace LLM Apps > Langfuse Python SDK adds tracing and observability to any LLM app via decorators or low-level calls, so you can track latency, cost, and prompts. ## Install Copy the content below into your project: ## Quick Use ```bash pip install langfuse ``` Then follow the SDK docs (v4 rewrite released March 2026) for your framework integration. ## Intro Langfuse Python SDK adds tracing and observability to any LLM app via decorators or low-level calls, so you can track latency, cost, and prompts. - **Best for:** Python LLM apps that need reliable tracing across prompts, tools, and providers - **Works with:** Python; decorators or low-level events; works with any LLM/provider (per README) - **Setup time:** 5–20 minutes ## Practical Notes - Per README: SDK v4 rewrite shipped in March 2026 (check the v4 migration guide before upgrading). - Start with one endpoint/function, then expand tracing to tool calls and background jobs. - Log only what you can keep: scrub secrets and PII in prompts/responses before shipping traces. ## Main How to use it without over-instrumenting: 1. **Pick one “golden path” flow** (a user question → tool calls → final answer). 2. Add tracing at the boundaries: request in, model call out, tool call out, response back. 3. Record **inputs/outputs + timings** first. Only add extra metadata (user IDs, tags, datasets) after the baseline works. 4. Create a simple “regression dashboard”: slowest traces, highest error rate, and largest prompt payloads. The fastest win is spotting which step burns tokens (retrieval, tool results, or prompt templates) and then trimming that step only. ### FAQ **Q: Do I need a specific model/provider?** A: No—README says it works with any LLM or framework; focus on consistent trace context instead of vendor-specific fields. **Q: Should I log full prompts?** A: Only if allowed. Prefer redaction + sampling for sensitive environments; keep enough context to reproduce failures. **Q: What breaks during upgrades?** A: SDK major rewrites can change event shapes. Follow the v4 migration guide before upgrading production services. ## Source & Thanks > Source: https://github.com/langfuse/langfuse-python > License: MIT > GitHub stars: 399 · forks: 266 --- ## 快速使用 ```bash pip install langfuse ``` 随后按官方 SDK 文档接入(README 提示 v4 于 2026 年 3 月重写发布)。 ## 简介 Langfuse Python SDK 用 decorators 或底层事件把你的 LLM 应用接入可观测性:记录 traces、延迟、token 成本、prompt/response,上线后也能快速定位回归。 - **适合谁:** 需要把 prompt、工具与多供应商调用统一纳入追踪的 Python LLM 应用 - **可搭配:** Python;支持 decorators 或底层事件;可配任意 LLM/provider(见 README) - **准备时间:** 5–20 分钟 ## 实战建议 - README 提示:SDK 在 2026 年 3 月发布 v4 重写版本,升级前先看迁移指南。 - 先从一个关键入口(chat/tool router)开始埋点,再扩到工具调用与后台任务。 - 只记录你能长期保留的内容:把 secrets/PII 在入库前做脱敏或过滤。 ## 主要内容 避免“过度埋点”的做法: 1. **先选一条黄金路径**(用户问题 → 工具调用 → 最终回答)。 2. 只在边界处埋点:请求进入、模型调用、工具调用、响应返回。 3. 先记录 **输入/输出 + 耗时**,基础稳定后再逐步加用户标签、数据集等元信息。 4. 做一个最小回归看板:最慢 traces、最高错误率、最大 prompt 体积。 最快收益通常来自定位哪一步最“吃 token”(检索、工具结果、prompt 模板),然后只优化那一步。 ### FAQ **必须绑定某个模型/供应商吗?** 答:不需要。README 表示可配任意 LLM/框架;更重要的是把 trace context 做一致。 **要不要记录完整 prompt?** 答:看合规。建议对敏感字段做脱敏 + 抽样;保留能复现问题的最小上下文即可。 **升级最容易踩什么坑?** 答:大版本重写可能改事件结构;上线前按 v4 迁移指南做对照验证。 ## 来源与感谢 > Source: https://github.com/langfuse/langfuse-python > License: MIT > GitHub stars: 399 · forks: 266 --- Source: https://tokrepo.com/en/workflows/langfuse-python-sdk-trace-llm-apps Author: Agent Toolkit