Skip to content
LangSmith 文档版本 跟随上游 源快照 3b2137c2 更新于 2026-10-18

在不设置环境变量的情况下追踪

以下环境变量可用于配置是否启用追踪、API 端点、API key 以及追踪项目:

  • LANGSMITH_TRACING
  • LANGSMITH_API_KEY
  • LANGSMITH_ENDPOINT
  • LANGSMITH_PROJECT

如果你需要在自定义配置下追踪运行、所处的环境不支持常规环境变量(例如 Cloudflare Workers),或者不希望依赖环境变量,LangSmith 允许你以编程方式配置追踪。

WARNING

在 Python SDK 的 0.1.95 版本中,with trace 会遵循 LANGSMITH_TRACING 环境变量。详情请参阅发行说明。要在不设置环境变量的情况下禁用或启用追踪,请使用 with tracing_context 上下文管理器,如下例所示。

  • Python:在 Python 中推荐使用 tracing_context 上下文管理器。它既适用于用 traceable 标注的代码,也适用于 trace 上下文管理器内的代码。
  • TypeScript:你可以将 client 和 tracingEnabled 标志一起传给 traceable 装饰器。
python
import openai
from langsmith import Client, tracing_context, traceable
from langsmith.wrappers import wrap_openai

langsmith_client = Client(
  api_key="YOUR_LANGSMITH_API_KEY",  # This can be retrieved from a secrets manager
  api_url="https://api.smith.langchain.com",  # Update appropriately for self-hosted installations or regional SaaS
  workspace_id="YOUR_WORKSPACE_ID", # Must be specified for API keys scoped to multiple workspaces
)

client = wrap_openai(openai.Client())

@traceable(run_type="tool", name="Retrieve Context")
def my_tool(question: str) -> str:
  return "During this morning's meeting, we solved all world conflict."

@traceable
def chat_pipeline(question: str):
  context = my_tool(question)
  messages = [
      { "role": "system", "content": "You are a helpful assistant. Please respond to the user's request only based on the given context." },
      { "role": "user", "content": f"Question: {question}\nContext: {context}"}
  ]
  chat_completion = client.chat.completions.create(
      model="gpt-5.4-mini", messages=messages
  )
  return chat_completion.choices[0].message.content

# Can set to False to disable tracing here without changing code structure
with tracing_context(enabled=True):
  # Use langsmith_extra to pass in a custom client
  chat_pipeline("Can you summarize this morning's meetings?", langsmith_extra={"client": langsmith_client})
typescript
import { Client } from "langsmith";
import { traceable } from "langsmith/traceable";
import { wrapOpenAI } from "langsmith/wrappers";
import { OpenAI } from "openai";

const client = new Client({
    apiKey: "YOUR_API_KEY",  // This can be retrieved from a secrets manager
    apiUrl: "https://api.smith.langchain.com",  // Update appropriately for self-hosted installations or regional SaaS
});

const openai = wrapOpenAI(new OpenAI());

const tool = traceable((question: string) => {
    return "During this morning's meeting, we solved all world conflict.";
}, { name: "Retrieve Context", runType: "tool" });

const pipeline = traceable(
    async (question: string) => {
        const context = await tool(question);

        const completion = await openai.chat.completions.create({
            model: "gpt-5.4-mini",
            messages: [
                { role: "system" as const, content: "You are a helpful assistant. Please respond to the user's request only based on the given context." },
                { role: "user" as const, content: `Question: ${question}\nContext: ${context}`}
            ]
        });

        return completion.choices[0].message.content;
    },
    { name: "Chat", client, tracingEnabled: true }
);

await pipeline("Can you summarize this morning's meetings?");

如果你更喜欢视频教程,可以观看 Introduction to LangSmith 课程中的其他追踪方式视频。

相关

如果你需要根据运行时条件(例如客户要求、数据敏感性或合规策略)动态启用或禁用追踪,请参阅条件追踪中的示例。


BytesRouter 是社区翻译站点,所有译文仅供参考,一切以官方英文原文为准。