tracing

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Add LangWatch Tracing to Your Code

为你的代码添加LangWatch追踪能力

Determine Scope

确定范围

If the user's request is general ("instrument my code", "add tracing", "set up observability"):
  • Read the full codebase to understand the agent's architecture
  • Study git history to understand what changed and why — focus on agent behavior changes, prompt tweaks, bug fixes. Read commit messages for context.
  • Add comprehensive tracing across all LLM call sites
If the user's request is specific ("add tracing to the payment function", "trace this endpoint"):
  • Focus on the specific function or module
  • Add tracing only where requested
  • Verify the instrumentation works in context
如果用户的需求是通用型的(「为我的代码插桩」、「添加追踪」、「搭建可观测体系」):
  • 通读完整代码库,理解Agent的架构
  • 研究git提交历史,了解变更内容和原因——重点关注Agent行为变更、prompt调整、bug修复。阅读提交信息获取上下文。
  • 为所有LLM调用位点添加全量追踪
如果用户的需求是特定型的(「为支付函数添加追踪」、「追踪这个接口」):
  • 聚焦于指定的函数或模块
  • 仅在需求指定的位置添加追踪
  • 验证插桩在当前上下文下可正常运行

Detect Context

识别上下文

This skill is code-only — there is no platform path for tracing. If the user has no codebase, explain that tracing requires code instrumentation and point them to the LangWatch docs.
本技能仅针对代码场景——不存在平台侧的追踪接入路径。如果用户没有代码库,请说明追踪需要代码插桩,并引导他们查阅LangWatch文档。

Step 1: Set up the LangWatch MCP

步骤1:搭建LangWatch MCP

First, install the LangWatch MCP server so you have access to framework-specific documentation:
See MCP Setup for installation instructions.
If MCP installation fails, see docs fallback to fetch docs directly via URLs.
首先安装LangWatch MCP服务器,这样你就能获取框架专属的文档:
安装说明请查看MCP搭建指南
如果MCP安装失败,请查看文档兜底方案直接通过URL获取文档。

Step 2: Get the API Key

步骤2:获取API Key

See API Key Setup.
Add the API key to the project's
.env
file:
LANGWATCH_API_KEY=your-key-here
查看API Key配置指南
将API Key添加到项目的
.env
文件中:
LANGWATCH_API_KEY=your-key-here

Step 3: Read the Integration Docs

步骤3:阅读集成文档

Use the LangWatch MCP to fetch the correct integration guide for this project:
  • Call
    fetch_langwatch_docs
    with no arguments to see the docs index
  • Find the integration guide matching the project's framework (OpenAI, LangGraph, Vercel AI, Agno, Mastra, etc.)
  • Read the specific integration page for step-by-step instructions
CRITICAL: Do NOT guess how to instrument. Read the actual documentation for the specific framework. Different frameworks have different instrumentation patterns.
使用LangWatch MCP拉取适配本项目的集成指南:
  • 不带参数调用
    fetch_langwatch_docs
    查看文档索引
  • 找到与项目框架(OpenAI、LangGraph、Vercel AI、Agno、Mastra等)匹配的集成指南
  • 阅读对应的集成页面获取分步操作说明
注意:请勿猜测插桩方式。务必阅读对应框架的官方文档,不同框架的插桩模式存在差异。

Step 4: Install the LangWatch SDK

步骤4:安装LangWatch SDK

For Python:
bash
pip install langwatch
对于Python:
bash
pip install langwatch

or: uv add langwatch

or: uv add langwatch


For TypeScript:
```bash
npm install langwatch

对于TypeScript:
```bash
npm install langwatch

or: pnpm add langwatch

or: pnpm add langwatch

undefined
undefined

Step 5: Add Instrumentation

步骤5:添加插桩代码

Follow the integration guide you read in Step 3. The general pattern is:
Python:
python
import langwatch
langwatch.setup()

@langwatch.trace()
def my_function():
    # your existing code
    pass
TypeScript:
typescript
import { LangWatch } from "langwatch";
const langwatch = new LangWatch();
IMPORTANT: The exact pattern depends on the framework. Always follow the docs, not these examples.
遵循你在步骤3中阅读的集成指南操作,通用模式如下:
Python:
python
import langwatch
langwatch.setup()

@langwatch.trace()
def my_function():
    # your existing code
    pass
TypeScript:
typescript
import { LangWatch } from "langwatch";
const langwatch = new LangWatch();
重要提示:具体模式取决于所使用的框架,请始终遵循文档说明,而非上述示例。

Step 6: Verify

步骤6:验证效果

Run the application and check that traces appear in your LangWatch dashboard at https://app.langwatch.ai
运行应用,检查追踪数据是否出现在你的LangWatch控制台https://app.langwatch.ai中

Common Mistakes

常见错误

  • Do NOT invent instrumentation patterns — always read the docs for the specific framework
  • Do NOT skip the
    langwatch.setup()
    call in Python
  • Do NOT forget to add LANGWATCH_API_KEY to .env
  • Do NOT use
    platform_
    MCP tools — this skill is about adding code, not creating platform resources
  • 请勿自创插桩模式——请始终阅读对应框架的官方文档
  • 请勿省略Python中的
    langwatch.setup()
    调用
  • 请勿忘记将LANGWATCH_API_KEY添加到.env文件中
  • 请勿使用
    platform_
    开头的MCP工具——本技能专注于添加代码,而非创建平台资源