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

# Trace OpenAI-compatible providers

> Trace LLM calls from any OpenAI-compatible provider to LangSmith.

Many LLM providers accept requests in the same format as the OpenAI API. To trace calls from these providers to LangSmith, construct an OpenAI client pointed at the provider's base URL, then wrap it with [`wrap_openai`](https://reference.langchain.com/python/langsmith/wrappers/_openai/wrap_openai) / [`wrapOpenAI`](https://reference.langchain.com/javascript/modules/langsmith.html).

Use `wrap_openai` / `wrapOpenAI` for direct API calls. Use [`@traceable`](https://reference.langchain.com/python/langsmith/run_helpers/traceable) when you need to trace application logic around the call or set metadata per invocation.

|                | `wrap_openai` / `wrapOpenAI`                                      | `@traceable` / `traceable`                                                                                            |
| -------------- | ----------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------- |
| Token tracking | Automatic                                                         | Requires `run_type="llm"`                                                                                             |
| Run type       | LLM (set automatically)                                           | Chain by default                                                                                                      |
| Traces         | The API call                                                      | The function wrapping it                                                                                              |
| Metadata       | Client-level only (Python); client-level or per-call (TypeScript) | Per-call via [`langsmith_extra`](https://reference.langchain.com/python/langsmith/run_helpers/SupportsLangsmithExtra) |

<Note>To trace OpenAI directly, refer to [Trace OpenAI applications](/langsmith/trace-openai).</Note>

## Setup

<CodeGroup>
  ```bash Python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  pip install langsmith openai
  ```

  ```bash npm theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  npm install langsmith openai
  ```
</CodeGroup>

```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
export LANGSMITH_API_KEY=<your-api-key>
export LANGSMITH_TRACING=true
```

## Trace API calls

<CodeGroup>
  ```python Python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  import os

  import openai
  from langsmith import wrappers

  client = wrappers.wrap_openai(
      openai.OpenAI(
          base_url="https://<provider-base-url>/v1",
          api_key=os.environ["PROVIDER_API_KEY"],
      )
  )

  completion = client.chat.completions.create(
      model="<provider-model-name>",
      messages=[{"role": "user", "content": "Hello!"}],
  )
  print(completion.choices[0].message.content)
  ```

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

  const client = wrapOpenAI(
    new OpenAI({
      baseURL: "https://<provider-base-url>/v1",
      apiKey: process.env.PROVIDER_API_KEY!,
    })
  );

  const completion = await client.chat.completions.create({
    model: "<provider-model-name>",
    messages: [{ role: "user", content: "Hello!" }],
  });
  console.log(completion.choices[0].message.content);
  ```
</CodeGroup>

## Add metadata

<Tabs>
  <Tab title="Python">
    Pass `tracing_extra` when wrapping the client. The metadata applies to all calls made with that client.

    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    import os

    import openai

    from langsmith import wrappers

    client = wrappers.wrap_openai(
        openai.OpenAI(
            base_url="https://<provider-base-url>/v1",
            api_key=os.environ["PROVIDER_API_KEY"],
        ),
        tracing_extra={"metadata": {"environment": "production"}},
    )
    ```
  </Tab>

  <Tab title="TypeScript">
    Pass options as the second argument to `wrapOpenAI` for client-level metadata, or pass [`langsmithExtra`](https://reference.langchain.com/javascript/modules/langsmith.html) per call.

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

    const client = wrapOpenAI(
      new OpenAI({
        baseURL: "https://<provider-base-url>/v1",
        apiKey: process.env.PROVIDER_API_KEY!,
      }),
      { metadata: { environment: "production" } }
    );

    // Per-call metadata
    const completion = await client.chat.completions.create(
      {
        model: "<provider-model-name>",
        messages: [{ role: "user", content: "Hello!" }],
      },
      { langsmithExtra: { metadata: { request_id: "abc123" } } }
    );
    ```
  </Tab>
</Tabs>

## Related guides

Some providers have dedicated setup guides that use `@traceable` or a native callback. These approaches trace at the function level rather than wrapping the client directly, or integrate with the provider's own SDK and routing layer.

* [DeepSeek](/langsmith/trace-deepseek): OpenAI-compatible API; guide uses `@traceable` with custom provider metadata
* [LiteLLM](/langsmith/trace-litellm): proxy that exposes an OpenAI-compatible endpoint; guide covers `@traceable` and LiteLLM's built-in LangSmith callback

***

<div className="source-links">
  <Callout icon="terminal-2">
    [Connect these docs](/use-these-docs) to Claude, VSCode, and more via MCP for real-time answers.
  </Callout>

  <Callout icon="edit">
    [Edit this page on GitHub](https://github.com/langchain-ai/docs/edit/main/src/langsmith/trace-with-openai-compatible.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).
  </Callout>
</div>
