> ## Documentation Index
> Fetch the complete documentation index at: https://cndoc-langchain.site/llms.txt
> Use this file to discover all available pages before exploring further.

# 追踪OpenAI应用

Python/TypeScript 中的 `wrap_openai`/`wrapOpenAI` 方法允许您包装 OpenAI 客户端，以便自动记录跟踪信息——无需装饰器或函数包装！使用该包装器可确保消息（包括工具调用和多模态内容块）在 LangSmith 中得到良好渲染。另请注意，该包装器可与 `@traceable` 装饰器或 `traceable` 函数无缝协作，您可以在同一应用程序中同时使用两者。

<Note>
  即使使用 `wrap_openai` 或 `wrapOpenAI`，也必须将 `LANGSMITH_TRACING` 环境变量设置为 `'true'`，跟踪信息才会记录到 LangSmith。这允许您在不更改代码的情况下切换跟踪功能的开启和关闭。

  此外，您需要将 `LANGSMITH_API_KEY` 环境变量设置为您的 API 密钥（更多信息请参阅[设置](/)）。

  如果您的 LangSmith API 密钥关联了多个工作区，请设置 `LANGSMITH_WORKSPACE_ID` 环境变量以指定要使用的工作区。

  默认情况下，跟踪信息将记录到名为 `default` 的项目中。要将跟踪信息记录到其他项目，请参阅[将跟踪记录到特定项目](/langsmith/log-traces-to-project)。
</Note>

<CodeGroup>
  ```python Python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  import openai
  from langsmith import traceable
  from langsmith.wrappers import wrap_openai

  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(name="Chat Pipeline")
  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", messages=messages
    )
    return chat_completion.choices[0].message.content

  chat_pipeline("Can you summarize this morning's meetings?")
  ```

  ```typescript TypeScript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  import OpenAI from "openai";
  import { traceable } from "langsmith/traceable";
  import { wrapOpenAI } from "langsmith/wrappers";

  const client = wrapOpenAI(new OpenAI());

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

  const chatPipeline = traceable(async (question: string) => {
    const context = await myTool(question);
    const 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: `Question: ${question} Context: ${context}` },
    ];
    const chatCompletion = await client.chat.completions.create({
        model: "gpt-5.4",
        messages: messages,
    });
    return chatCompletion.choices[0].message.content;
  }, { name: "Chat Pipeline" });

  await chatPipeline("Can you summarize this morning's meetings?");
  ```
</CodeGroup>

***

<div className="source-links">
  <Callout icon="terminal-2">
    [通过MCP连接这些文档](/use-these-docs)到Claude、VSCode等，以获取实时答案。
  </Callout>

  <Callout icon="edit">
    [在GitHub上编辑此页面](https://github.com/langchain-ai/docs/edit/main/src/langsmith/trace-openai.mdx)或[提交问题](https://github.com/langchain-ai/docs/issues/new/choose)。
  </Callout>
</div>
